2 * Copyright (C) 2010 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
27 #include "InjectedBundle.h"
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"
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/Settings.h>
53 #include <wtf/OwnArrayPtr.h>
54 #include <wtf/PassOwnArrayPtr.h>
56 using namespace WebCore;
61 InjectedBundle::InjectedBundle(const String& path)
68 InjectedBundle::~InjectedBundle()
72 void InjectedBundle::initializeClient(WKBundleClient* client)
74 m_client.initialize(client);
77 void InjectedBundle::postMessage(const String& messageName, APIObject* messageBody)
79 WebProcess::shared().connection()->deprecatedSend(WebContextLegacyMessage::PostMessage, 0, CoreIPC::In(messageName, InjectedBundleUserMessageEncoder(messageBody)));
82 void InjectedBundle::postSynchronousMessage(const String& messageName, APIObject* messageBody, RefPtr<APIObject>& returnData)
84 RefPtr<APIObject> returnDataTmp;
85 InjectedBundleUserMessageDecoder messageDecoder(returnDataTmp);
87 bool succeeded = WebProcess::shared().connection()->deprecatedSendSync(WebContextLegacyMessage::PostSynchronousMessage, 0, CoreIPC::In(messageName, InjectedBundleUserMessageEncoder(messageBody)), CoreIPC::Out(messageDecoder));
92 returnData = returnDataTmp;
95 void InjectedBundle::setShouldTrackVisitedLinks(bool shouldTrackVisitedLinks)
97 PageGroup::setShouldTrackVisitedLinks(shouldTrackVisitedLinks);
100 void InjectedBundle::removeAllVisitedLinks()
102 PageGroup::removeAllVisitedLinks();
105 void InjectedBundle::overrideXSSAuditorEnabledForTestRunner(WebPageGroupProxy* pageGroup, bool enabled)
107 // Override the preference for all future pages.
108 WebPreferencesStore::overrideXSSAuditorEnabledForTestRunner(enabled);
110 // Change the setting for existing ones.
111 const HashSet<Page*>& pages = PageGroup::pageGroup(pageGroup->identifier())->pages();
112 for (HashSet<Page*>::iterator iter = pages.begin(); iter != pages.end(); ++iter)
113 (*iter)->settings()->setXSSAuditorEnabled(enabled);
116 void InjectedBundle::overrideAllowUniversalAccessFromFileURLsForTestRunner(WebPageGroupProxy* pageGroup, bool enabled)
118 // Override the preference for all future pages.
119 WebPreferencesStore::overrideAllowUniversalAccessFromFileURLsForTestRunner(enabled);
121 // Change the setting for existing ones.
122 const HashSet<Page*>& pages = PageGroup::pageGroup(pageGroup->identifier())->pages();
123 for (HashSet<Page*>::iterator iter = pages.begin(); iter != pages.end(); ++iter)
124 (*iter)->settings()->setAllowUniversalAccessFromFileURLs(enabled);
127 void InjectedBundle::setAllowFileAccessFromFileURLs(WebPageGroupProxy* pageGroup, bool enabled)
129 // Override the preference for all future pages.
130 WebPreferencesStore::overrideAllowFileAccessFromFileURLsForTestRunner(enabled);
132 // Change the setting for existing ones.
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()->setAllowFileAccessFromFileURLs(enabled);
138 void InjectedBundle::clearAllDatabases()
140 WebDatabaseManager::shared().deleteAllDatabases();
143 void InjectedBundle::setDatabaseQuota(uint64_t quota)
145 WebDatabaseManager::shared().setQuotaForOrigin("file:///", quota);
148 int InjectedBundle::numberOfPages(WebFrame* frame, double pageWidthInPixels, double pageHeightInPixels)
150 Frame* coreFrame = frame ? frame->coreFrame() : 0;
153 if (!pageWidthInPixels)
154 pageWidthInPixels = coreFrame->view()->width();
155 if (!pageHeightInPixels)
156 pageHeightInPixels = coreFrame->view()->height();
158 return PrintContext::numberOfPages(coreFrame, FloatSize(pageWidthInPixels, pageHeightInPixels));
161 int InjectedBundle::pageNumberForElementById(WebFrame* frame, const String& id, double pageWidthInPixels, double pageHeightInPixels)
163 Frame* coreFrame = frame ? frame->coreFrame() : 0;
167 Element* element = coreFrame->document()->getElementById(AtomicString(id));
171 if (!pageWidthInPixels)
172 pageWidthInPixels = coreFrame->view()->width();
173 if (!pageHeightInPixels)
174 pageHeightInPixels = coreFrame->view()->height();
176 return PrintContext::pageNumberForElement(element, FloatSize(pageWidthInPixels, pageHeightInPixels));
179 String InjectedBundle::pageSizeAndMarginsInPixels(WebFrame* frame, int pageIndex, int width, int height, int marginTop, int marginRight, int marginBottom, int marginLeft)
181 Frame* coreFrame = frame ? frame->coreFrame() : 0;
185 return PrintContext::pageSizeAndMarginsInPixels(coreFrame, pageIndex, width, height, marginTop, marginRight, marginBottom, marginLeft);
188 static PassOwnPtr<Vector<String> > toStringVector(ImmutableArray* patterns)
193 size_t size = patterns->size();
197 Vector<String>* patternsVector = new Vector<String>;
198 patternsVector->reserveInitialCapacity(size);
199 for (size_t i = 0; i < size; ++i) {
200 WebString* entry = patterns->at<WebString>(i);
202 patternsVector->uncheckedAppend(entry->string());
204 return patternsVector;
207 void InjectedBundle::addUserScript(WebPageGroupProxy* pageGroup, InjectedBundleScriptWorld* scriptWorld, const String& source, const String& url, ImmutableArray* whitelist, ImmutableArray* blacklist, WebCore::UserScriptInjectionTime injectionTime, WebCore::UserContentInjectedFrames injectedFrames)
209 // 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.
210 PageGroup::pageGroup(pageGroup->identifier())->addUserScriptToWorld(scriptWorld->coreWorld(), source, KURL(KURL(), url), toStringVector(whitelist), toStringVector(blacklist), injectionTime, injectedFrames);
213 void InjectedBundle::addUserStyleSheet(WebPageGroupProxy* pageGroup, InjectedBundleScriptWorld* scriptWorld, const String& source, const String& url, ImmutableArray* whitelist, ImmutableArray* blacklist, WebCore::UserContentInjectedFrames injectedFrames)
215 // 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.
216 PageGroup::pageGroup(pageGroup->identifier())->addUserStyleSheetToWorld(scriptWorld->coreWorld(), source, KURL(KURL(), url), toStringVector(whitelist), toStringVector(blacklist), injectedFrames);
219 void InjectedBundle::removeUserScript(WebPageGroupProxy* pageGroup, InjectedBundleScriptWorld* scriptWorld, const String& url)
221 // 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.
222 PageGroup::pageGroup(pageGroup->identifier())->removeUserScriptFromWorld(scriptWorld->coreWorld(), KURL(KURL(), url));
225 void InjectedBundle::removeUserStyleSheet(WebPageGroupProxy* pageGroup, InjectedBundleScriptWorld* scriptWorld, const String& url)
227 // 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.
228 PageGroup::pageGroup(pageGroup->identifier())->removeUserStyleSheetFromWorld(scriptWorld->coreWorld(), KURL(KURL(), url));
231 void InjectedBundle::removeUserScripts(WebPageGroupProxy* pageGroup, InjectedBundleScriptWorld* scriptWorld)
233 PageGroup::pageGroup(pageGroup->identifier())->removeUserScriptsFromWorld(scriptWorld->coreWorld());
236 void InjectedBundle::removeUserStyleSheets(WebPageGroupProxy* pageGroup, InjectedBundleScriptWorld* scriptWorld)
238 PageGroup::pageGroup(pageGroup->identifier())->removeUserStyleSheetsFromWorld(scriptWorld->coreWorld());
241 void InjectedBundle::removeAllUserContent(WebPageGroupProxy* pageGroup)
243 PageGroup::pageGroup(pageGroup->identifier())->removeAllUserContent();
246 void InjectedBundle::garbageCollectJavaScriptObjects()
248 gcController().garbageCollectNow();
251 void InjectedBundle::garbageCollectJavaScriptObjectsOnAlternateThreadForDebugging(bool waitUntilDone)
253 gcController().garbageCollectOnAlternateThreadForDebugging(waitUntilDone);
256 size_t InjectedBundle::javaScriptObjectsCount()
258 JSLock lock(SilenceAssertionsOnly);
259 return JSDOMWindow::commonJSGlobalData()->heap.objectCount();
262 void InjectedBundle::reportException(JSContextRef context, JSValueRef exception)
264 if (!context || !exception)
267 JSLock lock(JSC::SilenceAssertionsOnly);
268 JSC::ExecState* execState = toJS(context);
270 // Make sure the context has a DOMWindow global object, otherwise this context didn't originate from a Page.
271 if (!toJSDOMWindow(execState->lexicalGlobalObject()))
274 WebCore::reportException(execState, toJS(execState, exception));
277 void InjectedBundle::didCreatePage(WebPage* page)
279 m_client.didCreatePage(this, page);
282 void InjectedBundle::willDestroyPage(WebPage* page)
284 m_client.willDestroyPage(this, page);
287 void InjectedBundle::didInitializePageGroup(WebPageGroupProxy* pageGroup)
289 m_client.didInitializePageGroup(this, pageGroup);
292 void InjectedBundle::didReceiveMessage(const String& messageName, APIObject* messageBody)
294 m_client.didReceiveMessage(this, messageName, messageBody);
297 void InjectedBundle::didReceiveMessage(CoreIPC::Connection* connection, CoreIPC::MessageID messageID, CoreIPC::ArgumentDecoder* arguments)
299 switch (messageID.get<InjectedBundleMessage::Kind>()) {
300 case InjectedBundleMessage::PostMessage: {
302 RefPtr<APIObject> messageBody;
303 InjectedBundleUserMessageDecoder messageDecoder(messageBody);
304 if (!arguments->decode(CoreIPC::Out(messageName, messageDecoder)))
307 didReceiveMessage(messageName, messageBody.get());
312 ASSERT_NOT_REACHED();
315 } // namespace WebKit