[EFL] Expose JavaScript binding interface through ewk_extension
[WebKit-https.git] / Source / WebKit2 / WebProcess / InjectedBundle / API / efl / ewk_page.cpp
1 /*
2  * Copyright (C) 2015 Ryuan Choi <ryuan.choi@gmail.com>.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #include "config.h"
27 #include "ewk_page.h"
28
29 #include "WKBundle.h"
30 #include "WKBundleAPICast.h"
31 #include "WKBundleFrame.h"
32 #include "WebFrame.h"
33 #include "WebPage.h"
34 #include "ewk_page_private.h"
35
36 using namespace WebKit;
37
38 static inline EwkPage* toEwkPage(const void* clientInfo)
39 {
40     return const_cast<EwkPage*>(static_cast<const EwkPage*>(clientInfo));
41 }
42
43 EwkPage::EwkPage(WebPage* page)
44     : m_page(page)
45 {
46     WKBundlePageLoaderClientV7 loaderClient = {
47         {
48             7, // version
49             this, // clientInfo
50         },
51         0, // didStartProvisionalLoadForFrame,
52         0, // didReceiveServerRedirectForProvisionalLoadForFrame,
53         0, // didFailProvisionalLoadWithErrorForFrame
54         0, // didCommitLoadForFrame
55         didFinishDocumentLoadForFrame,
56         0, // didFinishLoadForFrame
57         0, // didFailLoadWithErrorForFrame
58         0, // didSameDocumentNavigationForFrame,
59         0, // didReceiveTitleForFrame
60         0, // didFirstLayoutForFrame
61         0, // didFirstVisuallyNonEmptyLayoutForFrame
62         0, // didRemoveFrameFromHierarchy
63         0, // didDisplayInsecureContentForFrame
64         0, // didRunInsecureContentForFrame
65         0, // didClearWindowObjectForFrame,
66         0, // didCancelClientRedirectForFrame
67         0, // willPerformClientRedirectForFrame
68         0, // didHandleOnloadEventsForFrame
69         0, // didLayoutForFrame
70         0, // didNewFirstVisuallyNonEmptyLayout
71         0, // didDetectXSSForFrame
72         0, // shouldGoToBackForwardListItem
73         0, // globalObjectIsAvailableForFrame
74         0, // willDisconnectDOMWindowExtensionFromGlobalObject
75         0, // didReconnectDOMWindowExtensionToGlobalObject
76         0, // willDestroyGlobalObjectForDOMWindowExtension
77         0, // didFinishProgress
78         0, // shouldForceUniversalAccessFromLocalURL
79         0, // didReceiveIntentForFrame_unavailable
80         0, // registerIntentServiceForFrame_unavailable
81         0, // didLayout
82         0, // featuresUsedInPage
83         0, // willLoadURLRequest
84         0, // willLoadDataRequest
85         0 // willDestroyFrame
86     };
87     WKBundlePageSetPageLoaderClient(toAPI(page), &loaderClient.base);
88 }
89
90 void EwkPage::append(const Ewk_Page_Client* client)
91 {
92     m_clients.append(client);
93 }
94
95 void EwkPage::remove(const Ewk_Page_Client* client)
96 {
97     m_clients.remove(m_clients.find(client));
98 }
99
100 void EwkPage::didFinishDocumentLoadForFrame(WKBundlePageRef, WKBundleFrameRef frame, WKTypeRef*, const void* clientInfo)
101 {
102     if (!WKBundleFrameIsMainFrame(frame))
103         return;
104
105     EwkPage* self = toEwkPage(clientInfo);
106     for (auto& it : self->m_clients) {
107         if (it->load_finished)
108             it->load_finished(self, it->data);
109     }
110 }
111
112 JSGlobalContextRef ewk_page_js_global_context_get(const Ewk_Page* ewkPage)
113 {
114     EINA_SAFETY_ON_NULL_RETURN_VAL(ewkPage, nullptr);
115
116     WebFrame* coreFrame = ewkPage->page()->mainWebFrame();
117     if (!coreFrame)
118         return nullptr;
119
120     return coreFrame->jsContext();
121 }
122
123 void ewk_page_client_register(Ewk_Page* ewkPage, const Ewk_Page_Client* client)
124 {
125     EINA_SAFETY_ON_NULL_RETURN(ewkPage);
126     EINA_SAFETY_ON_NULL_RETURN(client);
127
128     ewkPage->append(client);
129 }
130
131 void ewk_page_client_unregister(Ewk_Page* ewkPage, const Ewk_Page_Client* client)
132 {
133     EINA_SAFETY_ON_NULL_RETURN(ewkPage);
134     EINA_SAFETY_ON_NULL_RETURN(client);
135
136     ewkPage->remove(client);
137 }