1 /*
2  * Copyright (C) 2010 Apple Inc. 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 "InjectedBundle.h"
28 
29 #include "Arguments.h"
30 #include "ImmutableArray.h"
31 #include "InjectedBundleMessageKinds.h"
32 #include "InjectedBundleScriptWorld.h"
33 #include "InjectedBundleUserMessageCoders.h"
34 #include "WKAPICast.h"
35 #include "WKBundleAPICast.h"
36 #include "WebContextMessageKinds.h"
37 #include "WebCoreArgumentCoders.h"
38 #include "WebDatabaseManager.h"
39 #include "WebFrame.h"
40 #include "WebPage.h"
41 #include "WebPreferencesStore.h"
42 #include "WebProcess.h"
43 #include <JavaScriptCore/APICast.h>
44 #include <JavaScriptCore/JSLock.h>
45 #include <WebCore/Frame.h>
46 #include <WebCore/FrameView.h>
47 #include <WebCore/GCController.h>
48 #include <WebCore/JSDOMWindow.h>
49 #include <WebCore/Page.h>
50 #include <WebCore/PageGroup.h>
51 #include <WebCore/PrintContext.h>
52 #include <WebCore/SecurityOrigin.h>
53 #include <WebCore/Settings.h>
54 #include <wtf/OwnArrayPtr.h>
55 #include <wtf/PassOwnArrayPtr.h>
56 
57 using namespace WebCore;
58 using namespace JSC;
59 
60 namespace WebKit {
61 
InjectedBundle(const String & path)62 InjectedBundle::InjectedBundle(const String& path)
63     : m_path(path)
64     , m_platformBundle(0)
65 {
66     initializeClient(0);
67 }
68 
~InjectedBundle()69 InjectedBundle::~InjectedBundle()
70 {
71 }
72 
initializeClient(WKBundleClient * client)73 void InjectedBundle::initializeClient(WKBundleClient* client)
74 {
75     m_client.initialize(client);
76 }
77 
postMessage(const String & messageName,APIObject * messageBody)78 void InjectedBundle::postMessage(const String& messageName, APIObject* messageBody)
79 {
80     WebProcess::shared().connection()->deprecatedSend(WebContextLegacyMessage::PostMessage, 0, CoreIPC::In(messageName, InjectedBundleUserMessageEncoder(messageBody)));
81 }
82 
postSynchronousMessage(const String & messageName,APIObject * messageBody,RefPtr<APIObject> & returnData)83 void InjectedBundle::postSynchronousMessage(const String& messageName, APIObject* messageBody, RefPtr<APIObject>& returnData)
84 {
85     RefPtr<APIObject> returnDataTmp;
86     InjectedBundleUserMessageDecoder messageDecoder(returnDataTmp);
87 
88     bool succeeded = WebProcess::shared().connection()->deprecatedSendSync(WebContextLegacyMessage::PostSynchronousMessage, 0, CoreIPC::In(messageName, InjectedBundleUserMessageEncoder(messageBody)), CoreIPC::Out(messageDecoder));
89 
90     if (!succeeded)
91         return;
92 
93     returnData = returnDataTmp;
94 }
95 
setShouldTrackVisitedLinks(bool shouldTrackVisitedLinks)96 void InjectedBundle::setShouldTrackVisitedLinks(bool shouldTrackVisitedLinks)
97 {
98     PageGroup::setShouldTrackVisitedLinks(shouldTrackVisitedLinks);
99 }
100 
removeAllVisitedLinks()101 void InjectedBundle::removeAllVisitedLinks()
102 {
103     PageGroup::removeAllVisitedLinks();
104 }
105 
overrideXSSAuditorEnabledForTestRunner(WebPageGroupProxy * pageGroup,bool enabled)106 void InjectedBundle::overrideXSSAuditorEnabledForTestRunner(WebPageGroupProxy* pageGroup, bool enabled)
107 {
108     // Override the preference for all future pages.
109     WebPreferencesStore::overrideXSSAuditorEnabledForTestRunner(enabled);
110 
111     // Change the setting for existing ones.
112     const HashSet<Page*>& pages = PageGroup::pageGroup(pageGroup->identifier())->pages();
113     for (HashSet<Page*>::iterator iter = pages.begin(); iter != pages.end(); ++iter)
114         (*iter)->settings()->setXSSAuditorEnabled(enabled);
115 }
116 
setAllowUniversalAccessFromFileURLs(WebPageGroupProxy * pageGroup,bool enabled)117 void InjectedBundle::setAllowUniversalAccessFromFileURLs(WebPageGroupProxy* pageGroup, bool enabled)
118 {
119     const HashSet<Page*>& pages = PageGroup::pageGroup(pageGroup->identifier())->pages();
120     for (HashSet<Page*>::iterator iter = pages.begin(); iter != pages.end(); ++iter)
121         (*iter)->settings()->setAllowUniversalAccessFromFileURLs(enabled);
122 }
123 
setAllowFileAccessFromFileURLs(WebPageGroupProxy * pageGroup,bool enabled)124 void InjectedBundle::setAllowFileAccessFromFileURLs(WebPageGroupProxy* pageGroup, bool enabled)
125 {
126     const HashSet<Page*>& pages = PageGroup::pageGroup(pageGroup->identifier())->pages();
127     for (HashSet<Page*>::iterator iter = pages.begin(); iter != pages.end(); ++iter)
128         (*iter)->settings()->setAllowFileAccessFromFileURLs(enabled);
129 }
130 
setFrameFlatteningEnabled(WebPageGroupProxy * pageGroup,bool enabled)131 void InjectedBundle::setFrameFlatteningEnabled(WebPageGroupProxy* pageGroup, bool enabled)
132 {
133     const HashSet<Page*>& pages = PageGroup::pageGroup(pageGroup->identifier())->pages();
134     for (HashSet<Page*>::iterator iter = pages.begin(); iter != pages.end(); ++iter)
135         (*iter)->settings()->setFrameFlatteningEnabled(enabled);
136 }
137 
addOriginAccessWhitelistEntry(const String & sourceOrigin,const String & destinationProtocol,const String & destinationHost,bool allowDestinationSubdomains)138 void InjectedBundle::addOriginAccessWhitelistEntry(const String& sourceOrigin, const String& destinationProtocol, const String& destinationHost, bool allowDestinationSubdomains)
139 {
140     SecurityOrigin::addOriginAccessWhitelistEntry(*SecurityOrigin::createFromString(sourceOrigin), destinationProtocol, destinationHost, allowDestinationSubdomains);
141 }
142 
removeOriginAccessWhitelistEntry(const String & sourceOrigin,const String & destinationProtocol,const String & destinationHost,bool allowDestinationSubdomains)143 void InjectedBundle::removeOriginAccessWhitelistEntry(const String& sourceOrigin, const String& destinationProtocol, const String& destinationHost, bool allowDestinationSubdomains)
144 {
145     SecurityOrigin::removeOriginAccessWhitelistEntry(*SecurityOrigin::createFromString(sourceOrigin), destinationProtocol, destinationHost, allowDestinationSubdomains);
146 }
147 
resetOriginAccessWhitelists()148 void InjectedBundle::resetOriginAccessWhitelists()
149 {
150     SecurityOrigin::resetOriginAccessWhitelists();
151 }
152 
clearAllDatabases()153 void InjectedBundle::clearAllDatabases()
154 {
155     WebDatabaseManager::shared().deleteAllDatabases();
156 }
157 
setDatabaseQuota(uint64_t quota)158 void InjectedBundle::setDatabaseQuota(uint64_t quota)
159 {
160     WebDatabaseManager::shared().setQuotaForOrigin("file:///", quota);
161 }
162 
numberOfPages(WebFrame * frame,double pageWidthInPixels,double pageHeightInPixels)163 int InjectedBundle::numberOfPages(WebFrame* frame, double pageWidthInPixels, double pageHeightInPixels)
164 {
165     Frame* coreFrame = frame ? frame->coreFrame() : 0;
166     if (!coreFrame)
167         return -1;
168     if (!pageWidthInPixels)
169         pageWidthInPixels = coreFrame->view()->width();
170     if (!pageHeightInPixels)
171         pageHeightInPixels = coreFrame->view()->height();
172 
173     return PrintContext::numberOfPages(coreFrame, FloatSize(pageWidthInPixels, pageHeightInPixels));
174 }
175 
pageNumberForElementById(WebFrame * frame,const String & id,double pageWidthInPixels,double pageHeightInPixels)176 int InjectedBundle::pageNumberForElementById(WebFrame* frame, const String& id, double pageWidthInPixels, double pageHeightInPixels)
177 {
178     Frame* coreFrame = frame ? frame->coreFrame() : 0;
179     if (!coreFrame)
180         return -1;
181 
182     Element* element = coreFrame->document()->getElementById(AtomicString(id));
183     if (!element)
184         return -1;
185 
186     if (!pageWidthInPixels)
187         pageWidthInPixels = coreFrame->view()->width();
188     if (!pageHeightInPixels)
189         pageHeightInPixels = coreFrame->view()->height();
190 
191     return PrintContext::pageNumberForElement(element, FloatSize(pageWidthInPixels, pageHeightInPixels));
192 }
193 
pageSizeAndMarginsInPixels(WebFrame * frame,int pageIndex,int width,int height,int marginTop,int marginRight,int marginBottom,int marginLeft)194 String InjectedBundle::pageSizeAndMarginsInPixels(WebFrame* frame, int pageIndex, int width, int height, int marginTop, int marginRight, int marginBottom, int marginLeft)
195 {
196     Frame* coreFrame = frame ? frame->coreFrame() : 0;
197     if (!coreFrame)
198         return String();
199 
200     return PrintContext::pageSizeAndMarginsInPixels(coreFrame, pageIndex, width, height, marginTop, marginRight, marginBottom, marginLeft);
201 }
202 
isPageBoxVisible(WebFrame * frame,int pageIndex)203 bool InjectedBundle::isPageBoxVisible(WebFrame* frame, int pageIndex)
204 {
205     Frame* coreFrame = frame ? frame->coreFrame() : 0;
206     if (!coreFrame)
207         return false;
208 
209     return PrintContext::isPageBoxVisible(coreFrame, pageIndex);
210 }
211 
toStringVector(ImmutableArray * patterns)212 static PassOwnPtr<Vector<String> > toStringVector(ImmutableArray* patterns)
213 {
214     if (!patterns)
215         return nullptr;
216 
217     size_t size =  patterns->size();
218     if (!size)
219         return nullptr;
220 
221     OwnPtr<Vector<String> > patternsVector = adoptPtr(new Vector<String>);
222     patternsVector->reserveInitialCapacity(size);
223     for (size_t i = 0; i < size; ++i) {
224         WebString* entry = patterns->at<WebString>(i);
225         if (entry)
226             patternsVector->uncheckedAppend(entry->string());
227     }
228     return patternsVector.release();
229 }
230 
addUserScript(WebPageGroupProxy * pageGroup,InjectedBundleScriptWorld * scriptWorld,const String & source,const String & url,ImmutableArray * whitelist,ImmutableArray * blacklist,WebCore::UserScriptInjectionTime injectionTime,WebCore::UserContentInjectedFrames injectedFrames)231 void InjectedBundle::addUserScript(WebPageGroupProxy* pageGroup, InjectedBundleScriptWorld* scriptWorld, const String& source, const String& url, ImmutableArray* whitelist, ImmutableArray* blacklist, WebCore::UserScriptInjectionTime injectionTime, WebCore::UserContentInjectedFrames injectedFrames)
232 {
233     // url is not from KURL::string(), i.e. it has not already been parsed by KURL, so we have to use the relative URL constructor for KURL instead of the ParsedURLStringTag version.
234     PageGroup::pageGroup(pageGroup->identifier())->addUserScriptToWorld(scriptWorld->coreWorld(), source, KURL(KURL(), url), toStringVector(whitelist), toStringVector(blacklist), injectionTime, injectedFrames);
235 }
236 
addUserStyleSheet(WebPageGroupProxy * pageGroup,InjectedBundleScriptWorld * scriptWorld,const String & source,const String & url,ImmutableArray * whitelist,ImmutableArray * blacklist,WebCore::UserContentInjectedFrames injectedFrames)237 void InjectedBundle::addUserStyleSheet(WebPageGroupProxy* pageGroup, InjectedBundleScriptWorld* scriptWorld, const String& source, const String& url, ImmutableArray* whitelist, ImmutableArray* blacklist, WebCore::UserContentInjectedFrames injectedFrames)
238 {
239     // url is not from KURL::string(), i.e. it has not already been parsed by KURL, so we have to use the relative URL constructor for KURL instead of the ParsedURLStringTag version.
240     PageGroup::pageGroup(pageGroup->identifier())->addUserStyleSheetToWorld(scriptWorld->coreWorld(), source, KURL(KURL(), url), toStringVector(whitelist), toStringVector(blacklist), injectedFrames);
241 }
242 
removeUserScript(WebPageGroupProxy * pageGroup,InjectedBundleScriptWorld * scriptWorld,const String & url)243 void InjectedBundle::removeUserScript(WebPageGroupProxy* pageGroup, InjectedBundleScriptWorld* scriptWorld, const String& url)
244 {
245     // url is not from KURL::string(), i.e. it has not already been parsed by KURL, so we have to use the relative URL constructor for KURL instead of the ParsedURLStringTag version.
246     PageGroup::pageGroup(pageGroup->identifier())->removeUserScriptFromWorld(scriptWorld->coreWorld(), KURL(KURL(), url));
247 }
248 
removeUserStyleSheet(WebPageGroupProxy * pageGroup,InjectedBundleScriptWorld * scriptWorld,const String & url)249 void InjectedBundle::removeUserStyleSheet(WebPageGroupProxy* pageGroup, InjectedBundleScriptWorld* scriptWorld, const String& url)
250 {
251     // url is not from KURL::string(), i.e. it has not already been parsed by KURL, so we have to use the relative URL constructor for KURL instead of the ParsedURLStringTag version.
252     PageGroup::pageGroup(pageGroup->identifier())->removeUserStyleSheetFromWorld(scriptWorld->coreWorld(), KURL(KURL(), url));
253 }
254 
removeUserScripts(WebPageGroupProxy * pageGroup,InjectedBundleScriptWorld * scriptWorld)255 void InjectedBundle::removeUserScripts(WebPageGroupProxy* pageGroup, InjectedBundleScriptWorld* scriptWorld)
256 {
257     PageGroup::pageGroup(pageGroup->identifier())->removeUserScriptsFromWorld(scriptWorld->coreWorld());
258 }
259 
removeUserStyleSheets(WebPageGroupProxy * pageGroup,InjectedBundleScriptWorld * scriptWorld)260 void InjectedBundle::removeUserStyleSheets(WebPageGroupProxy* pageGroup, InjectedBundleScriptWorld* scriptWorld)
261 {
262     PageGroup::pageGroup(pageGroup->identifier())->removeUserStyleSheetsFromWorld(scriptWorld->coreWorld());
263 }
264 
removeAllUserContent(WebPageGroupProxy * pageGroup)265 void InjectedBundle::removeAllUserContent(WebPageGroupProxy* pageGroup)
266 {
267     PageGroup::pageGroup(pageGroup->identifier())->removeAllUserContent();
268 }
269 
garbageCollectJavaScriptObjects()270 void InjectedBundle::garbageCollectJavaScriptObjects()
271 {
272     gcController().garbageCollectNow();
273 }
274 
garbageCollectJavaScriptObjectsOnAlternateThreadForDebugging(bool waitUntilDone)275 void InjectedBundle::garbageCollectJavaScriptObjectsOnAlternateThreadForDebugging(bool waitUntilDone)
276 {
277     gcController().garbageCollectOnAlternateThreadForDebugging(waitUntilDone);
278 }
279 
javaScriptObjectsCount()280 size_t InjectedBundle::javaScriptObjectsCount()
281 {
282     JSLock lock(SilenceAssertionsOnly);
283     return JSDOMWindow::commonJSGlobalData()->heap.objectCount();
284 }
285 
reportException(JSContextRef context,JSValueRef exception)286 void InjectedBundle::reportException(JSContextRef context, JSValueRef exception)
287 {
288     if (!context || !exception)
289         return;
290 
291     JSLock lock(JSC::SilenceAssertionsOnly);
292     JSC::ExecState* execState = toJS(context);
293 
294     // Make sure the context has a DOMWindow global object, otherwise this context didn't originate from a Page.
295     if (!toJSDOMWindow(execState->lexicalGlobalObject()))
296         return;
297 
298     WebCore::reportException(execState, toJS(execState, exception));
299 }
300 
didCreatePage(WebPage * page)301 void InjectedBundle::didCreatePage(WebPage* page)
302 {
303     m_client.didCreatePage(this, page);
304 }
305 
willDestroyPage(WebPage * page)306 void InjectedBundle::willDestroyPage(WebPage* page)
307 {
308     m_client.willDestroyPage(this, page);
309 }
310 
didInitializePageGroup(WebPageGroupProxy * pageGroup)311 void InjectedBundle::didInitializePageGroup(WebPageGroupProxy* pageGroup)
312 {
313     m_client.didInitializePageGroup(this, pageGroup);
314 }
315 
didReceiveMessage(const String & messageName,APIObject * messageBody)316 void InjectedBundle::didReceiveMessage(const String& messageName, APIObject* messageBody)
317 {
318     m_client.didReceiveMessage(this, messageName, messageBody);
319 }
320 
didReceiveMessage(CoreIPC::Connection * connection,CoreIPC::MessageID messageID,CoreIPC::ArgumentDecoder * arguments)321 void InjectedBundle::didReceiveMessage(CoreIPC::Connection* connection, CoreIPC::MessageID messageID, CoreIPC::ArgumentDecoder* arguments)
322 {
323     switch (messageID.get<InjectedBundleMessage::Kind>()) {
324         case InjectedBundleMessage::PostMessage: {
325             String messageName;
326             RefPtr<APIObject> messageBody;
327             InjectedBundleUserMessageDecoder messageDecoder(messageBody);
328             if (!arguments->decode(CoreIPC::Out(messageName, messageDecoder)))
329                 return;
330 
331             didReceiveMessage(messageName, messageBody.get());
332             return;
333         }
334     }
335 
336     ASSERT_NOT_REACHED();
337 }
338 
339 } // namespace WebKit
340