ded7adbaff191e9893bba81f65e5682dc0895dfc
[WebKit-https.git] / Source / WebKit2 / WebProcess / InjectedBundle / InjectedBundle.cpp
1 /*
2  * Copyright (C) 2010, 2013 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 "APIArray.h"
30 #include "APIData.h"
31 #include "Arguments.h"
32 #include "InjectedBundleScriptWorld.h"
33 #include "NotificationPermissionRequestManager.h"
34 #include "SessionTracker.h"
35 #include "UserData.h"
36 #include "WKAPICast.h"
37 #include "WKBundleAPICast.h"
38 #include "WebConnectionToUIProcess.h"
39 #include "WebCookieManager.h"
40 #include "WebCoreArgumentCoders.h"
41 #include "WebDatabaseManager.h"
42 #include "WebFrame.h"
43 #include "WebFrameNetworkingContext.h"
44 #include "WebPage.h"
45 #include "WebPageGroupProxy.h"
46 #include "WebPreferencesKeys.h"
47 #include "WebPreferencesStore.h"
48 #include "WebProcess.h"
49 #include "WebProcessCreationParameters.h"
50 #include "WebProcessPoolMessages.h"
51 #include <JavaScriptCore/APICast.h>
52 #include <JavaScriptCore/Exception.h>
53 #include <JavaScriptCore/JSLock.h>
54 #include <WebCore/ApplicationCache.h>
55 #include <WebCore/ApplicationCacheStorage.h>
56 #include <WebCore/FrameLoader.h>
57 #include <WebCore/FrameView.h>
58 #include <WebCore/GCController.h>
59 #include <WebCore/GeolocationClient.h>
60 #include <WebCore/GeolocationController.h>
61 #include <WebCore/GeolocationPosition.h>
62 #include <WebCore/JSDOMWindow.h>
63 #include <WebCore/JSNotification.h>
64 #include <WebCore/MainFrame.h>
65 #include <WebCore/Page.h>
66 #include <WebCore/PageGroup.h>
67 #include <WebCore/PrintContext.h>
68 #include <WebCore/ResourceHandle.h>
69 #include <WebCore/ResourceLoadScheduler.h>
70 #include <WebCore/ScriptController.h>
71 #include <WebCore/SecurityOrigin.h>
72 #include <WebCore/SecurityPolicy.h>
73 #include <WebCore/SessionID.h>
74 #include <WebCore/Settings.h>
75 #include <WebCore/UserContentController.h>
76 #include <WebCore/UserGestureIndicator.h>
77
78 #if ENABLE(CSS_REGIONS) || ENABLE(CSS_COMPOSITING)
79 #include <WebCore/RuntimeEnabledFeatures.h>
80 #endif
81
82 #if ENABLE(NOTIFICATIONS) || ENABLE(LEGACY_NOTIFICATIONS)
83 #include "WebNotificationManager.h"
84 #endif
85
86 using namespace WebCore;
87 using namespace JSC;
88
89 namespace WebKit {
90
91 PassRefPtr<InjectedBundle> InjectedBundle::create(const WebProcessCreationParameters& parameters, API::Object* initializationUserData)
92 {
93     RefPtr<InjectedBundle> bundle = adoptRef(new InjectedBundle(parameters));
94
95     bundle->m_sandboxExtension = SandboxExtension::create(parameters.injectedBundlePathExtensionHandle);
96     if (!bundle->initialize(parameters, initializationUserData))
97         return nullptr;
98
99     return bundle.release();
100 }
101
102 InjectedBundle::InjectedBundle(const WebProcessCreationParameters& parameters)
103     : m_path(parameters.injectedBundlePath)
104     , m_platformBundle(0)
105 {
106 }
107
108 InjectedBundle::~InjectedBundle()
109 {
110 }
111
112 void InjectedBundle::initializeClient(const WKBundleClientBase* client)
113 {
114     m_client.initialize(client);
115 }
116
117 void InjectedBundle::postMessage(const String& messageName, API::Object* messageBody)
118 {
119     auto& webProcess = WebProcess::singleton();
120     webProcess.parentProcessConnection()->send(Messages::WebProcessPool::HandleMessage(messageName, UserData(webProcess.transformObjectsToHandles(messageBody))), 0);
121 }
122
123 void InjectedBundle::postSynchronousMessage(const String& messageName, API::Object* messageBody, RefPtr<API::Object>& returnData)
124 {
125     UserData returnUserData;
126
127     auto& webProcess = WebProcess::singleton();
128     if (!webProcess.parentProcessConnection()->sendSync(Messages::WebProcessPool::HandleSynchronousMessage(messageName, UserData(webProcess.transformObjectsToHandles(messageBody))), Messages::WebProcessPool::HandleSynchronousMessage::Reply(returnUserData), 0))
129         returnData = nullptr;
130     else
131         returnData = webProcess.transformHandlesToObjects(returnUserData.object());
132 }
133
134 WebConnection* InjectedBundle::webConnectionToUIProcess() const
135 {
136     return WebProcess::singleton().webConnectionToUIProcess();
137 }
138
139 void InjectedBundle::overrideBoolPreferenceForTestRunner(WebPageGroupProxy* pageGroup, const String& preference, bool enabled)
140 {
141     const HashSet<Page*>& pages = PageGroup::pageGroup(pageGroup->identifier())->pages();
142
143     if (preference == "WebKitTabToLinksPreferenceKey") {
144         WebPreferencesStore::overrideBoolValueForKey(WebPreferencesKey::tabsToLinksKey(), enabled);
145         for (auto* page : pages)
146             WebPage::fromCorePage(page)->setTabToLinksEnabled(enabled);
147     }
148
149     if (preference == "WebKit2AsynchronousPluginInitializationEnabled") {
150         WebPreferencesStore::overrideBoolValueForKey(WebPreferencesKey::asynchronousPluginInitializationEnabledKey(), enabled);
151         for (auto* page : pages)
152             WebPage::fromCorePage(page)->setAsynchronousPluginInitializationEnabled(enabled);
153     }
154
155     if (preference == "WebKit2AsynchronousPluginInitializationEnabledForAllPlugins") {
156         WebPreferencesStore::overrideBoolValueForKey(WebPreferencesKey::asynchronousPluginInitializationEnabledForAllPluginsKey(), enabled);
157         for (auto* page : pages)
158             WebPage::fromCorePage(page)->setAsynchronousPluginInitializationEnabledForAllPlugins(enabled);
159     }
160
161     if (preference == "WebKit2ArtificialPluginInitializationDelayEnabled") {
162         WebPreferencesStore::overrideBoolValueForKey(WebPreferencesKey::artificialPluginInitializationDelayEnabledKey(), enabled);
163         for (auto* page : pages)
164             WebPage::fromCorePage(page)->setArtificialPluginInitializationDelayEnabled(enabled);
165     }
166
167 #if ENABLE(SERVICE_CONTROLS)
168     if (preference == "WebKitImageControlsEnabled") {
169         WebPreferencesStore::overrideBoolValueForKey(WebPreferencesKey::imageControlsEnabledKey(), enabled);
170         for (auto* page : pages)
171             page->settings().setImageControlsEnabled(enabled);
172         return;
173     }
174 #endif
175
176 #if ENABLE(CSS_ANIMATIONS_LEVEL_2)
177     if (preference == "WebKitCSSAnimationTriggersEnabled")
178         RuntimeEnabledFeatures::sharedFeatures().setAnimationTriggersEnabled(enabled);
179 #endif
180
181 #if ENABLE(CSS_REGIONS)
182     if (preference == "WebKitCSSRegionsEnabled")
183         RuntimeEnabledFeatures::sharedFeatures().setCSSRegionsEnabled(enabled);
184 #endif
185
186 #if ENABLE(CSS_COMPOSITING)
187     if (preference == "WebKitCSSCompositingEnabled")
188         RuntimeEnabledFeatures::sharedFeatures().setCSSCompositingEnabled(enabled);
189 #endif
190
191     // Map the names used in LayoutTests with the names used in WebCore::Settings and WebPreferencesStore.
192 #define FOR_EACH_OVERRIDE_BOOL_PREFERENCE(macro) \
193     macro(WebKitAcceleratedCompositingEnabled, AcceleratedCompositingEnabled, acceleratedCompositingEnabled) \
194     macro(WebKitCanvasUsesAcceleratedDrawing, CanvasUsesAcceleratedDrawing, canvasUsesAcceleratedDrawing) \
195     macro(WebKitFrameFlatteningEnabled, FrameFlatteningEnabled, frameFlatteningEnabled) \
196     macro(WebKitJavaEnabled, JavaEnabled, javaEnabled) \
197     macro(WebKitJavaScriptEnabled, ScriptEnabled, javaScriptEnabled) \
198     macro(WebKitLoadSiteIconsKey, LoadsSiteIconsIgnoringImageLoadingSetting, loadsSiteIconsIgnoringImageLoadingPreference) \
199     macro(WebKitOfflineWebApplicationCacheEnabled, OfflineWebApplicationCacheEnabled, offlineWebApplicationCacheEnabled) \
200     macro(WebKitPageCacheSupportsPluginsPreferenceKey, PageCacheSupportsPlugins, pageCacheSupportsPlugins) \
201     macro(WebKitPluginsEnabled, PluginsEnabled, pluginsEnabled) \
202     macro(WebKitUsesPageCachePreferenceKey, UsesPageCache, usesPageCache) \
203     macro(WebKitWebAudioEnabled, WebAudioEnabled, webAudioEnabled) \
204     macro(WebKitWebGLEnabled, WebGLEnabled, webGLEnabled) \
205     macro(WebKitXSSAuditorEnabled, XSSAuditorEnabled, xssAuditorEnabled) \
206     macro(WebKitShouldRespectImageOrientation, ShouldRespectImageOrientation, shouldRespectImageOrientation) \
207     macro(WebKitEnableCaretBrowsing, CaretBrowsingEnabled, caretBrowsingEnabled) \
208     macro(WebKitDisplayImagesKey, LoadsImagesAutomatically, loadsImagesAutomatically) \
209     macro(WebKitMediaStreamEnabled, MediaStreamEnabled, mediaStreamEnabled) \
210     macro(WebKitHTTPEquivEnabled, HttpEquivEnabled, httpEquivEnabled)
211
212 #define OVERRIDE_PREFERENCE_AND_SET_IN_EXISTING_PAGES(TestRunnerName, SettingsName, WebPreferencesName) \
213     if (preference == #TestRunnerName) { \
214         WebPreferencesStore::overrideBoolValueForKey(WebPreferencesKey::WebPreferencesName##Key(), enabled); \
215         for (HashSet<Page*>::iterator iter = pages.begin(); iter != pages.end(); ++iter) \
216             (*iter)->settings().set##SettingsName(enabled); \
217         return; \
218     }
219
220     FOR_EACH_OVERRIDE_BOOL_PREFERENCE(OVERRIDE_PREFERENCE_AND_SET_IN_EXISTING_PAGES)
221
222 #if ENABLE(HIDDEN_PAGE_DOM_TIMER_THROTTLING)
223     OVERRIDE_PREFERENCE_AND_SET_IN_EXISTING_PAGES(WebKitHiddenPageDOMTimerThrottlingEnabled, HiddenPageDOMTimerThrottlingEnabled, hiddenPageDOMTimerThrottlingEnabled)
224 #endif
225
226 #undef OVERRIDE_PREFERENCE_AND_SET_IN_EXISTING_PAGES
227 #undef FOR_EACH_OVERRIDE_BOOL_PREFERENCE
228 }
229
230 void InjectedBundle::overrideXSSAuditorEnabledForTestRunner(WebPageGroupProxy* pageGroup, bool enabled)
231 {
232     // Override the preference for all future pages.
233     WebPreferencesStore::overrideBoolValueForKey(WebPreferencesKey::xssAuditorEnabledKey(), enabled);
234
235     // Change the setting for existing ones.
236     const HashSet<Page*>& pages = PageGroup::pageGroup(pageGroup->identifier())->pages();
237     for (HashSet<Page*>::iterator iter = pages.begin(); iter != pages.end(); ++iter)
238         (*iter)->settings().setXSSAuditorEnabled(enabled);
239 }
240
241 void InjectedBundle::setAllowUniversalAccessFromFileURLs(WebPageGroupProxy* pageGroup, bool enabled)
242 {
243     const HashSet<Page*>& pages = PageGroup::pageGroup(pageGroup->identifier())->pages();
244     for (HashSet<Page*>::iterator iter = pages.begin(); iter != pages.end(); ++iter)
245         (*iter)->settings().setAllowUniversalAccessFromFileURLs(enabled);
246 }
247
248 void InjectedBundle::setAllowFileAccessFromFileURLs(WebPageGroupProxy* pageGroup, bool enabled)
249 {
250     const HashSet<Page*>& pages = PageGroup::pageGroup(pageGroup->identifier())->pages();
251     for (HashSet<Page*>::iterator iter = pages.begin(); iter != pages.end(); ++iter)
252         (*iter)->settings().setAllowFileAccessFromFileURLs(enabled);
253 }
254
255 void InjectedBundle::setMinimumLogicalFontSize(WebPageGroupProxy* pageGroup, int size)
256 {
257     const HashSet<Page*>& pages = PageGroup::pageGroup(pageGroup->identifier())->pages();
258     for (HashSet<Page*>::iterator iter = pages.begin(); iter != pages.end(); ++iter)
259         (*iter)->settings().setMinimumLogicalFontSize(size);
260 }
261
262 void InjectedBundle::setFrameFlatteningEnabled(WebPageGroupProxy* pageGroup, bool enabled)
263 {
264     const HashSet<Page*>& pages = PageGroup::pageGroup(pageGroup->identifier())->pages();
265     for (HashSet<Page*>::iterator iter = pages.begin(); iter != pages.end(); ++iter)
266         (*iter)->settings().setFrameFlatteningEnabled(enabled);
267 }
268
269 void InjectedBundle::setPluginsEnabled(WebPageGroupProxy* pageGroup, bool enabled)
270 {
271     const HashSet<Page*>& pages = PageGroup::pageGroup(pageGroup->identifier())->pages();
272     for (HashSet<Page*>::iterator iter = pages.begin(); iter != pages.end(); ++iter)
273         (*iter)->settings().setPluginsEnabled(enabled);
274 }
275
276 void InjectedBundle::setJavaScriptCanAccessClipboard(WebPageGroupProxy* pageGroup, bool enabled)
277 {
278     const HashSet<Page*>& pages = PageGroup::pageGroup(pageGroup->identifier())->pages();
279     for (HashSet<Page*>::iterator iter = pages.begin(); iter != pages.end(); ++iter)
280         (*iter)->settings().setJavaScriptCanAccessClipboard(enabled);
281 }
282
283 void InjectedBundle::setPrivateBrowsingEnabled(WebPageGroupProxy* pageGroup, bool enabled)
284 {
285     // FIXME (NetworkProcess): This test-only function doesn't work with NetworkProcess, <https://bugs.webkit.org/show_bug.cgi?id=115274>.
286 #if PLATFORM(COCOA) || USE(CFNETWORK) || USE(SOUP)
287     if (enabled)
288         WebFrameNetworkingContext::ensurePrivateBrowsingSession(SessionID::legacyPrivateSessionID());
289     else
290         SessionTracker::destroySession(SessionID::legacyPrivateSessionID());
291 #endif
292     const HashSet<Page*>& pages = PageGroup::pageGroup(pageGroup->identifier())->pages();
293     for (HashSet<Page*>::iterator iter = pages.begin(); iter != pages.end(); ++iter)
294         (*iter)->enableLegacyPrivateBrowsing(enabled);
295 }
296
297 void InjectedBundle::setPopupBlockingEnabled(WebPageGroupProxy* pageGroup, bool enabled)
298 {
299     const HashSet<Page*>& pages = PageGroup::pageGroup(pageGroup->identifier())->pages();
300     HashSet<Page*>::const_iterator end = pages.end();
301     for (HashSet<Page*>::const_iterator iter = pages.begin(); iter != end; ++iter)
302         (*iter)->settings().setJavaScriptCanOpenWindowsAutomatically(!enabled);
303 }
304
305 void InjectedBundle::setAuthorAndUserStylesEnabled(WebPageGroupProxy* pageGroup, bool enabled)
306 {
307     const HashSet<Page*>& pages = PageGroup::pageGroup(pageGroup->identifier())->pages();
308     for (HashSet<Page*>::iterator iter = pages.begin(); iter != pages.end(); ++iter)
309         (*iter)->settings().setAuthorAndUserStylesEnabled(enabled);
310 }
311
312 void InjectedBundle::setSpatialNavigationEnabled(WebPageGroupProxy* pageGroup, bool enabled)
313 {
314     const HashSet<Page*>& pages = PageGroup::pageGroup(pageGroup->identifier())->pages();
315     for (HashSet<Page*>::iterator iter = pages.begin(); iter != pages.end(); ++iter)
316         (*iter)->settings().setSpatialNavigationEnabled(enabled);
317 }
318
319 void InjectedBundle::addOriginAccessWhitelistEntry(const String& sourceOrigin, const String& destinationProtocol, const String& destinationHost, bool allowDestinationSubdomains)
320 {
321     SecurityPolicy::addOriginAccessWhitelistEntry(SecurityOrigin::createFromString(sourceOrigin).get(), destinationProtocol, destinationHost, allowDestinationSubdomains);
322 }
323
324 void InjectedBundle::removeOriginAccessWhitelistEntry(const String& sourceOrigin, const String& destinationProtocol, const String& destinationHost, bool allowDestinationSubdomains)
325 {
326     SecurityPolicy::removeOriginAccessWhitelistEntry(SecurityOrigin::createFromString(sourceOrigin).get(), destinationProtocol, destinationHost, allowDestinationSubdomains);
327 }
328
329 void InjectedBundle::resetOriginAccessWhitelists()
330 {
331     SecurityPolicy::resetOriginAccessWhitelists();
332 }
333
334 void InjectedBundle::setAsynchronousSpellCheckingEnabled(WebPageGroupProxy* pageGroup, bool enabled)
335 {
336     const HashSet<Page*>& pages = PageGroup::pageGroup(pageGroup->identifier())->pages();
337     for (HashSet<Page*>::iterator iter = pages.begin(); iter != pages.end(); ++iter)
338         (*iter)->settings().setAsynchronousSpellCheckingEnabled(enabled);
339 }
340
341 void InjectedBundle::clearAllDatabases()
342 {
343     WebProcess::singleton().supplement<WebDatabaseManager>()->deleteAllDatabases();
344 }
345
346 void InjectedBundle::setDatabaseQuota(uint64_t quota)
347 {
348     // Historically, we've used the following (somewhat non-sensical) string
349     // for the databaseIdentifier of local files.
350     WebProcess::singleton().supplement<WebDatabaseManager>()->setQuotaForOrigin("file__0", quota);
351 }
352
353 int InjectedBundle::numberOfPages(WebFrame* frame, double pageWidthInPixels, double pageHeightInPixels)
354 {
355     Frame* coreFrame = frame ? frame->coreFrame() : 0;
356     if (!coreFrame)
357         return -1;
358     if (!pageWidthInPixels)
359         pageWidthInPixels = coreFrame->view()->width();
360     if (!pageHeightInPixels)
361         pageHeightInPixels = coreFrame->view()->height();
362
363     return PrintContext::numberOfPages(*coreFrame, FloatSize(pageWidthInPixels, pageHeightInPixels));
364 }
365
366 int InjectedBundle::pageNumberForElementById(WebFrame* frame, const String& id, double pageWidthInPixels, double pageHeightInPixels)
367 {
368     Frame* coreFrame = frame ? frame->coreFrame() : 0;
369     if (!coreFrame)
370         return -1;
371
372     Element* element = coreFrame->document()->getElementById(id);
373     if (!element)
374         return -1;
375
376     if (!pageWidthInPixels)
377         pageWidthInPixels = coreFrame->view()->width();
378     if (!pageHeightInPixels)
379         pageHeightInPixels = coreFrame->view()->height();
380
381     return PrintContext::pageNumberForElement(element, FloatSize(pageWidthInPixels, pageHeightInPixels));
382 }
383
384 String InjectedBundle::pageSizeAndMarginsInPixels(WebFrame* frame, int pageIndex, int width, int height, int marginTop, int marginRight, int marginBottom, int marginLeft)
385 {
386     Frame* coreFrame = frame ? frame->coreFrame() : 0;
387     if (!coreFrame)
388         return String();
389
390     return PrintContext::pageSizeAndMarginsInPixels(coreFrame, pageIndex, width, height, marginTop, marginRight, marginBottom, marginLeft);
391 }
392
393 bool InjectedBundle::isPageBoxVisible(WebFrame* frame, int pageIndex)
394 {
395     Frame* coreFrame = frame ? frame->coreFrame() : 0;
396     if (!coreFrame)
397         return false;
398
399     return PrintContext::isPageBoxVisible(coreFrame, pageIndex);
400 }
401
402 bool InjectedBundle::isProcessingUserGesture()
403 {
404     return ScriptController::processingUserGesture();
405 }
406
407 void InjectedBundle::addUserScript(WebPageGroupProxy* pageGroup, InjectedBundleScriptWorld* scriptWorld, const String& source, const String& url, API::Array* whitelist, API::Array* blacklist, WebCore::UserScriptInjectionTime injectionTime, WebCore::UserContentInjectedFrames injectedFrames)
408 {
409     // url is not from URL::string(), i.e. it has not already been parsed by URL, so we have to use the relative URL constructor for URL instead of the ParsedURLStringTag version.
410     auto userScript = std::make_unique<UserScript>(source, URL(URL(), url), whitelist ? whitelist->toStringVector() : Vector<String>(), blacklist ? blacklist->toStringVector() : Vector<String>(), injectionTime, injectedFrames);
411
412     pageGroup->userContentController().addUserScript(scriptWorld->coreWorld(), WTF::move(userScript));
413 }
414
415 void InjectedBundle::addUserStyleSheet(WebPageGroupProxy* pageGroup, InjectedBundleScriptWorld* scriptWorld, const String& source, const String& url, API::Array* whitelist, API::Array* blacklist, WebCore::UserContentInjectedFrames injectedFrames)
416 {
417     // url is not from URL::string(), i.e. it has not already been parsed by URL, so we have to use the relative URL constructor for URL instead of the ParsedURLStringTag version.
418     auto userStyleSheet = std::make_unique<UserStyleSheet>(source, URL(URL(), url), whitelist ? whitelist->toStringVector() : Vector<String>(), blacklist ? blacklist->toStringVector() : Vector<String>(), injectedFrames, UserStyleUserLevel);
419
420     pageGroup->userContentController().addUserStyleSheet(scriptWorld->coreWorld(), WTF::move(userStyleSheet), InjectInExistingDocuments);
421 }
422
423 void InjectedBundle::removeUserScript(WebPageGroupProxy* pageGroup, InjectedBundleScriptWorld* scriptWorld, const String& url)
424 {
425     // url is not from URL::string(), i.e. it has not already been parsed by URL, so we have to use the relative URL constructor for URL instead of the ParsedURLStringTag version.
426     pageGroup->userContentController().removeUserScript(scriptWorld->coreWorld(), URL(URL(), url));
427 }
428
429 void InjectedBundle::removeUserStyleSheet(WebPageGroupProxy* pageGroup, InjectedBundleScriptWorld* scriptWorld, const String& url)
430 {
431     // url is not from URL::string(), i.e. it has not already been parsed by URL, so we have to use the relative URL constructor for URL instead of the ParsedURLStringTag version.
432     pageGroup->userContentController().removeUserStyleSheet(scriptWorld->coreWorld(), URL(URL(), url));
433 }
434
435 void InjectedBundle::removeUserScripts(WebPageGroupProxy* pageGroup, InjectedBundleScriptWorld* scriptWorld)
436 {
437     pageGroup->userContentController().removeUserScripts(scriptWorld->coreWorld());
438 }
439
440 void InjectedBundle::removeUserStyleSheets(WebPageGroupProxy* pageGroup, InjectedBundleScriptWorld* scriptWorld)
441 {
442     pageGroup->userContentController().removeUserStyleSheets(scriptWorld->coreWorld());
443 }
444
445 void InjectedBundle::removeAllUserContent(WebPageGroupProxy* pageGroup)
446 {
447     pageGroup->userContentController().removeAllUserContent();
448 }
449
450 void InjectedBundle::garbageCollectJavaScriptObjects()
451 {
452     GCController::singleton().garbageCollectNow();
453 }
454
455 void InjectedBundle::garbageCollectJavaScriptObjectsOnAlternateThreadForDebugging(bool waitUntilDone)
456 {
457     GCController::singleton().garbageCollectOnAlternateThreadForDebugging(waitUntilDone);
458 }
459
460 size_t InjectedBundle::javaScriptObjectsCount()
461 {
462     JSLockHolder lock(JSDOMWindow::commonVM());
463     return JSDOMWindow::commonVM().heap.objectCount();
464 }
465
466 void InjectedBundle::reportException(JSContextRef context, JSValueRef exception)
467 {
468     if (!context || !exception)
469         return;
470
471     JSC::ExecState* execState = toJS(context);
472     JSLockHolder lock(execState);
473
474     // Make sure the context has a DOMWindow global object, otherwise this context didn't originate from a Page.
475     if (!toJSDOMWindow(execState->lexicalGlobalObject()))
476         return;
477
478     WebCore::reportException(execState, toJS(execState, exception));
479 }
480
481 void InjectedBundle::didCreatePage(WebPage* page)
482 {
483     m_client.didCreatePage(this, page);
484 }
485
486 void InjectedBundle::willDestroyPage(WebPage* page)
487 {
488     m_client.willDestroyPage(this, page);
489 }
490
491 void InjectedBundle::didInitializePageGroup(WebPageGroupProxy* pageGroup)
492 {
493     m_client.didInitializePageGroup(this, pageGroup);
494 }
495
496 void InjectedBundle::didReceiveMessage(const String& messageName, API::Object* messageBody)
497 {
498     m_client.didReceiveMessage(this, messageName, messageBody);
499 }
500
501 void InjectedBundle::didReceiveMessageToPage(WebPage* page, const String& messageName, API::Object* messageBody)
502 {
503     m_client.didReceiveMessageToPage(this, page, messageName, messageBody);
504 }
505
506 void InjectedBundle::setUserStyleSheetLocation(WebPageGroupProxy* pageGroup, const String& location)
507 {
508     const HashSet<Page*>& pages = PageGroup::pageGroup(pageGroup->identifier())->pages();
509     for (HashSet<Page*>::iterator iter = pages.begin(); iter != pages.end(); ++iter)
510         (*iter)->settings().setUserStyleSheetLocation(URL(URL(), location));
511 }
512
513 void InjectedBundle::setWebNotificationPermission(WebPage* page, const String& originString, bool allowed)
514 {
515 #if ENABLE(NOTIFICATIONS) || ENABLE(LEGACY_NOTIFICATIONS)
516     page->notificationPermissionRequestManager()->setPermissionLevelForTesting(originString, allowed);
517 #else
518     UNUSED_PARAM(page);
519     UNUSED_PARAM(originString);
520     UNUSED_PARAM(allowed);
521 #endif
522 }
523
524 void InjectedBundle::removeAllWebNotificationPermissions(WebPage* page)
525 {
526 #if ENABLE(NOTIFICATIONS) || ENABLE(LEGACY_NOTIFICATIONS)
527     page->notificationPermissionRequestManager()->removeAllPermissionsForTesting();
528 #else
529     UNUSED_PARAM(page);
530 #endif
531 }
532
533 uint64_t InjectedBundle::webNotificationID(JSContextRef jsContext, JSValueRef jsNotification)
534 {
535 #if ENABLE(NOTIFICATIONS) || ENABLE(LEGACY_NOTIFICATIONS)
536     WebCore::Notification* notification = JSNotification::toWrapped(toJS(toJS(jsContext), jsNotification));
537     if (!notification)
538         return 0;
539     return WebProcess::singleton().supplement<WebNotificationManager>()->notificationIDForTesting(notification);
540 #else
541     UNUSED_PARAM(jsContext);
542     UNUSED_PARAM(jsNotification);
543     return 0;
544 #endif
545 }
546
547 // FIXME Get rid of this function and move it into WKBundle.cpp.
548 PassRefPtr<API::Data> InjectedBundle::createWebDataFromUint8Array(JSContextRef context, JSValueRef data)
549 {
550     JSC::ExecState* execState = toJS(context);
551     RefPtr<Uint8Array> arrayData = WebCore::toUint8Array(toJS(execState, data));
552     return API::Data::create(static_cast<unsigned char*>(arrayData->baseAddress()), arrayData->byteLength());
553 }
554
555 void InjectedBundle::setTabKeyCyclesThroughElements(WebPage* page, bool enabled)
556 {
557     page->corePage()->setTabKeyCyclesThroughElements(enabled);
558 }
559
560 void InjectedBundle::setCSSAnimationTriggersEnabled(bool enabled)
561 {
562 #if ENABLE(CSS_ANIMATIONS_LEVEL_2)
563     RuntimeEnabledFeatures::sharedFeatures().setAnimationTriggersEnabled(enabled);
564 #else
565     UNUSED_PARAM(enabled);
566 #endif
567 }
568
569 void InjectedBundle::setCSSRegionsEnabled(bool enabled)
570 {
571 #if ENABLE(CSS_REGIONS)
572     RuntimeEnabledFeatures::sharedFeatures().setCSSRegionsEnabled(enabled);
573 #else
574     UNUSED_PARAM(enabled);
575 #endif
576 }
577
578 void InjectedBundle::setCSSCompositingEnabled(bool enabled)
579 {
580 #if ENABLE(CSS_COMPOSITING)
581     RuntimeEnabledFeatures::sharedFeatures().setCSSCompositingEnabled(enabled);
582 #else
583     UNUSED_PARAM(enabled);
584 #endif
585 }
586
587 } // namespace WebKit