#include "config.h"
#include "ewk_page.h"
#include "WKBundle.h"
#include "WKBundleAPICast.h"
#include "WKBundleFrame.h"
#include "WebFrame.h"
#include "WebPage.h"
#include "ewk_page_private.h"
using namespace WebKit;
static inline EwkPage* toEwkPage(const void* clientInfo)
{
return const_cast<EwkPage*>(static_cast<const EwkPage*>(clientInfo));
}
EwkPage::EwkPage(WebPage* page)
: m_page(page)
{
WKBundlePageLoaderClientV7 loaderClient = {
{
7, this, },
didStartProvisionalLoadForFrame,
0, 0, 0, didFinishDocumentLoadForFrame,
0, 0, 0, 0, 0, 0, 0, 0, 0, didClearWindowObjectForFrame,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
WKBundlePageSetPageLoaderClient(toAPI(page), &loaderClient.base);
}
void EwkPage::append(const Ewk_Page_Client* client)
{
m_clients.append(client);
}
void EwkPage::remove(const Ewk_Page_Client* client)
{
m_clients.remove(m_clients.find(client));
}
void EwkPage::didStartProvisionalLoadForFrame(WKBundlePageRef, WKBundleFrameRef frame, WKTypeRef*, const void* clientInfo)
{
if (!WKBundleFrameIsMainFrame(frame))
return;
EwkPage* self = toEwkPage(clientInfo);
for (auto& it : self->m_clients) {
if (it->load_started)
it->load_started(self, it->data);
}
}
void EwkPage::didClearWindowObjectForFrame(WKBundlePageRef, WKBundleFrameRef frame, WKBundleScriptWorldRef, const void* clientInfo)
{
if (!WKBundleFrameIsMainFrame(frame))
return;
EwkPage* self = toEwkPage(clientInfo);
for (auto& it : self->m_clients) {
if (it->window_object_cleared)
it->window_object_cleared(self, it->data);
}
}
void EwkPage::didFinishDocumentLoadForFrame(WKBundlePageRef, WKBundleFrameRef frame, WKTypeRef*, const void* clientInfo)
{
if (!WKBundleFrameIsMainFrame(frame))
return;
EwkPage* self = toEwkPage(clientInfo);
for (auto& it : self->m_clients) {
if (it->load_finished)
it->load_finished(self, it->data);
}
}
JSGlobalContextRef ewk_page_js_global_context_get(const Ewk_Page* ewkPage)
{
EINA_SAFETY_ON_NULL_RETURN_VAL(ewkPage, nullptr);
WebFrame* coreFrame = ewkPage->page()->mainWebFrame();
if (!coreFrame)
return nullptr;
return coreFrame->jsContext();
}
void ewk_page_client_register(Ewk_Page* ewkPage, const Ewk_Page_Client* client)
{
EINA_SAFETY_ON_NULL_RETURN(ewkPage);
EINA_SAFETY_ON_NULL_RETURN(client);
ewkPage->append(client);
}
void ewk_page_client_unregister(Ewk_Page* ewkPage, const Ewk_Page_Client* client)
{
EINA_SAFETY_ON_NULL_RETURN(ewkPage);
EINA_SAFETY_ON_NULL_RETURN(client);
ewkPage->remove(client);
}