157535b1134c6ebf1f435c07b74120dd4b16620a
[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(WEB_ANIMATIONS)
182     if (preference == "WebKitWebAnimationsEnabled")
183         RuntimeEnabledFeatures::sharedFeatures().setWebAnimationsEnabled(enabled);
184 #endif
185
186 #if ENABLE(CSS_REGIONS)
187     if (preference == "WebKitCSSRegionsEnabled")
188         RuntimeEnabledFeatures::sharedFeatures().setCSSRegionsEnabled(enabled);
189 #endif
190
191 #if ENABLE(CSS_COMPOSITING)
192     if (preference == "WebKitCSSCompositingEnabled")
193         RuntimeEnabledFeatures::sharedFeatures().setCSSCompositingEnabled(enabled);
194 #endif
195
196     // Map the names used in LayoutTests with the names used in WebCore::Settings and WebPreferencesStore.
197 #define FOR_EACH_OVERRIDE_BOOL_PREFERENCE(macro) \
198     macro(WebKitAcceleratedCompositingEnabled, AcceleratedCompositingEnabled, acceleratedCompositingEnabled) \
199     macro(WebKitCanvasUsesAcceleratedDrawing, CanvasUsesAcceleratedDrawing, canvasUsesAcceleratedDrawing) \
200     macro(WebKitFrameFlatteningEnabled, FrameFlatteningEnabled, frameFlatteningEnabled) \
201     macro(WebKitJavaEnabled, JavaEnabled, javaEnabled) \
202     macro(WebKitJavaScriptEnabled, ScriptEnabled, javaScriptEnabled) \
203     macro(WebKitLoadSiteIconsKey, LoadsSiteIconsIgnoringImageLoadingSetting, loadsSiteIconsIgnoringImageLoadingPreference) \
204     macro(WebKitOfflineWebApplicationCacheEnabled, OfflineWebApplicationCacheEnabled, offlineWebApplicationCacheEnabled) \
205     macro(WebKitPageCacheSupportsPluginsPreferenceKey, PageCacheSupportsPlugins, pageCacheSupportsPlugins) \
206     macro(WebKitPluginsEnabled, PluginsEnabled, pluginsEnabled) \
207     macro(WebKitUsesPageCachePreferenceKey, UsesPageCache, usesPageCache) \
208     macro(WebKitWebAudioEnabled, WebAudioEnabled, webAudioEnabled) \
209     macro(WebKitWebGLEnabled, WebGLEnabled, webGLEnabled) \
210     macro(WebKitXSSAuditorEnabled, XSSAuditorEnabled, xssAuditorEnabled) \
211     macro(WebKitShouldRespectImageOrientation, ShouldRespectImageOrientation, shouldRespectImageOrientation) \
212     macro(WebKitEnableCaretBrowsing, CaretBrowsingEnabled, caretBrowsingEnabled) \
213     macro(WebKitDisplayImagesKey, LoadsImagesAutomatically, loadsImagesAutomatically) \
214     macro(WebKitMediaStreamEnabled, MediaStreamEnabled, mediaStreamEnabled) \
215     macro(WebKitHTTPEquivEnabled, HttpEquivEnabled, httpEquivEnabled)
216
217 #define OVERRIDE_PREFERENCE_AND_SET_IN_EXISTING_PAGES(TestRunnerName, SettingsName, WebPreferencesName) \
218     if (preference == #TestRunnerName) { \
219         WebPreferencesStore::overrideBoolValueForKey(WebPreferencesKey::WebPreferencesName##Key(), enabled); \
220         for (HashSet<Page*>::iterator iter = pages.begin(); iter != pages.end(); ++iter) \
221             (*iter)->settings().set##SettingsName(enabled); \
222         return; \
223     }
224
225     FOR_EACH_OVERRIDE_BOOL_PREFERENCE(OVERRIDE_PREFERENCE_AND_SET_IN_EXISTING_PAGES)
226
227 #if ENABLE(HIDDEN_PAGE_DOM_TIMER_THROTTLING)
228     OVERRIDE_PREFERENCE_AND_SET_IN_EXISTING_PAGES(WebKitHiddenPageDOMTimerThrottlingEnabled, HiddenPageDOMTimerThrottlingEnabled, hiddenPageDOMTimerThrottlingEnabled)
229 #endif
230
231 #undef OVERRIDE_PREFERENCE_AND_SET_IN_EXISTING_PAGES
232 #undef FOR_EACH_OVERRIDE_BOOL_PREFERENCE
233 }
234
235 void InjectedBundle::overrideXSSAuditorEnabledForTestRunner(WebPageGroupProxy* pageGroup, bool enabled)
236 {
237     // Override the preference for all future pages.
238     WebPreferencesStore::overrideBoolValueForKey(WebPreferencesKey::xssAuditorEnabledKey(), enabled);
239
240     // Change the setting for existing ones.
241     const HashSet<Page*>& pages = PageGroup::pageGroup(pageGroup->identifier())->pages();
242     for (HashSet<Page*>::iterator iter = pages.begin(); iter != pages.end(); ++iter)
243         (*iter)->settings().setXSSAuditorEnabled(enabled);
244 }
245
246 void InjectedBundle::setAllowUniversalAccessFromFileURLs(WebPageGroupProxy* pageGroup, bool enabled)
247 {
248     const HashSet<Page*>& pages = PageGroup::pageGroup(pageGroup->identifier())->pages();
249     for (HashSet<Page*>::iterator iter = pages.begin(); iter != pages.end(); ++iter)
250         (*iter)->settings().setAllowUniversalAccessFromFileURLs(enabled);
251 }
252
253 void InjectedBundle::setAllowFileAccessFromFileURLs(WebPageGroupProxy* pageGroup, bool enabled)
254 {
255     const HashSet<Page*>& pages = PageGroup::pageGroup(pageGroup->identifier())->pages();
256     for (HashSet<Page*>::iterator iter = pages.begin(); iter != pages.end(); ++iter)
257         (*iter)->settings().setAllowFileAccessFromFileURLs(enabled);
258 }
259
260 void InjectedBundle::setMinimumLogicalFontSize(WebPageGroupProxy* pageGroup, int size)
261 {
262     const HashSet<Page*>& pages = PageGroup::pageGroup(pageGroup->identifier())->pages();
263     for (HashSet<Page*>::iterator iter = pages.begin(); iter != pages.end(); ++iter)
264         (*iter)->settings().setMinimumLogicalFontSize(size);
265 }
266
267 void InjectedBundle::setFrameFlatteningEnabled(WebPageGroupProxy* pageGroup, bool enabled)
268 {
269     const HashSet<Page*>& pages = PageGroup::pageGroup(pageGroup->identifier())->pages();
270     for (HashSet<Page*>::iterator iter = pages.begin(); iter != pages.end(); ++iter)
271         (*iter)->settings().setFrameFlatteningEnabled(enabled);
272 }
273
274 void InjectedBundle::setPluginsEnabled(WebPageGroupProxy* pageGroup, bool enabled)
275 {
276     const HashSet<Page*>& pages = PageGroup::pageGroup(pageGroup->identifier())->pages();
277     for (HashSet<Page*>::iterator iter = pages.begin(); iter != pages.end(); ++iter)
278         (*iter)->settings().setPluginsEnabled(enabled);
279 }
280
281 void InjectedBundle::setJavaScriptCanAccessClipboard(WebPageGroupProxy* pageGroup, bool enabled)
282 {
283     const HashSet<Page*>& pages = PageGroup::pageGroup(pageGroup->identifier())->pages();
284     for (HashSet<Page*>::iterator iter = pages.begin(); iter != pages.end(); ++iter)
285         (*iter)->settings().setJavaScriptCanAccessClipboard(enabled);
286 }
287
288 void InjectedBundle::setPrivateBrowsingEnabled(WebPageGroupProxy* pageGroup, bool enabled)
289 {
290     // FIXME (NetworkProcess): This test-only function doesn't work with NetworkProcess, <https://bugs.webkit.org/show_bug.cgi?id=115274>.
291 #if PLATFORM(COCOA) || USE(CFNETWORK) || USE(SOUP)
292     if (enabled)
293         WebFrameNetworkingContext::ensurePrivateBrowsingSession(SessionID::legacyPrivateSessionID());
294     else
295         SessionTracker::destroySession(SessionID::legacyPrivateSessionID());
296 #endif
297     const HashSet<Page*>& pages = PageGroup::pageGroup(pageGroup->identifier())->pages();
298     for (HashSet<Page*>::iterator iter = pages.begin(); iter != pages.end(); ++iter)
299         (*iter)->enableLegacyPrivateBrowsing(enabled);
300 }
301
302 void InjectedBundle::setPopupBlockingEnabled(WebPageGroupProxy* pageGroup, bool enabled)
303 {
304     const HashSet<Page*>& pages = PageGroup::pageGroup(pageGroup->identifier())->pages();
305     HashSet<Page*>::const_iterator end = pages.end();
306     for (HashSet<Page*>::const_iterator iter = pages.begin(); iter != end; ++iter)
307         (*iter)->settings().setJavaScriptCanOpenWindowsAutomatically(!enabled);
308 }
309
310 void InjectedBundle::setAuthorAndUserStylesEnabled(WebPageGroupProxy* pageGroup, bool enabled)
311 {
312     const HashSet<Page*>& pages = PageGroup::pageGroup(pageGroup->identifier())->pages();
313     for (HashSet<Page*>::iterator iter = pages.begin(); iter != pages.end(); ++iter)
314         (*iter)->settings().setAuthorAndUserStylesEnabled(enabled);
315 }
316
317 void InjectedBundle::setSpatialNavigationEnabled(WebPageGroupProxy* pageGroup, bool enabled)
318 {
319     const HashSet<Page*>& pages = PageGroup::pageGroup(pageGroup->identifier())->pages();
320     for (HashSet<Page*>::iterator iter = pages.begin(); iter != pages.end(); ++iter)
321         (*iter)->settings().setSpatialNavigationEnabled(enabled);
322 }
323
324 void InjectedBundle::addOriginAccessWhitelistEntry(const String& sourceOrigin, const String& destinationProtocol, const String& destinationHost, bool allowDestinationSubdomains)
325 {
326     SecurityPolicy::addOriginAccessWhitelistEntry(SecurityOrigin::createFromString(sourceOrigin).get(), destinationProtocol, destinationHost, allowDestinationSubdomains);
327 }
328
329 void InjectedBundle::removeOriginAccessWhitelistEntry(const String& sourceOrigin, const String& destinationProtocol, const String& destinationHost, bool allowDestinationSubdomains)
330 {
331     SecurityPolicy::removeOriginAccessWhitelistEntry(SecurityOrigin::createFromString(sourceOrigin).get(), destinationProtocol, destinationHost, allowDestinationSubdomains);
332 }
333
334 void InjectedBundle::resetOriginAccessWhitelists()
335 {
336     SecurityPolicy::resetOriginAccessWhitelists();
337 }
338
339 void InjectedBundle::setAsynchronousSpellCheckingEnabled(WebPageGroupProxy* pageGroup, bool enabled)
340 {
341     const HashSet<Page*>& pages = PageGroup::pageGroup(pageGroup->identifier())->pages();
342     for (HashSet<Page*>::iterator iter = pages.begin(); iter != pages.end(); ++iter)
343         (*iter)->settings().setAsynchronousSpellCheckingEnabled(enabled);
344 }
345
346 void InjectedBundle::clearAllDatabases()
347 {
348     WebProcess::singleton().supplement<WebDatabaseManager>()->deleteAllDatabases();
349 }
350
351 void InjectedBundle::setDatabaseQuota(uint64_t quota)
352 {
353     // Historically, we've used the following (somewhat non-sensical) string
354     // for the databaseIdentifier of local files.
355     WebProcess::singleton().supplement<WebDatabaseManager>()->setQuotaForOrigin("file__0", quota);
356 }
357
358 int InjectedBundle::numberOfPages(WebFrame* frame, double pageWidthInPixels, double pageHeightInPixels)
359 {
360     Frame* coreFrame = frame ? frame->coreFrame() : 0;
361     if (!coreFrame)
362         return -1;
363     if (!pageWidthInPixels)
364         pageWidthInPixels = coreFrame->view()->width();
365     if (!pageHeightInPixels)
366         pageHeightInPixels = coreFrame->view()->height();
367
368     return PrintContext::numberOfPages(*coreFrame, FloatSize(pageWidthInPixels, pageHeightInPixels));
369 }
370
371 int InjectedBundle::pageNumberForElementById(WebFrame* frame, const String& id, double pageWidthInPixels, double pageHeightInPixels)
372 {
373     Frame* coreFrame = frame ? frame->coreFrame() : 0;
374     if (!coreFrame)
375         return -1;
376
377     Element* element = coreFrame->document()->getElementById(id);
378     if (!element)
379         return -1;
380
381     if (!pageWidthInPixels)
382         pageWidthInPixels = coreFrame->view()->width();
383     if (!pageHeightInPixels)
384         pageHeightInPixels = coreFrame->view()->height();
385
386     return PrintContext::pageNumberForElement(element, FloatSize(pageWidthInPixels, pageHeightInPixels));
387 }
388
389 String InjectedBundle::pageSizeAndMarginsInPixels(WebFrame* frame, int pageIndex, int width, int height, int marginTop, int marginRight, int marginBottom, int marginLeft)
390 {
391     Frame* coreFrame = frame ? frame->coreFrame() : 0;
392     if (!coreFrame)
393         return String();
394
395     return PrintContext::pageSizeAndMarginsInPixels(coreFrame, pageIndex, width, height, marginTop, marginRight, marginBottom, marginLeft);
396 }
397
398 bool InjectedBundle::isPageBoxVisible(WebFrame* frame, int pageIndex)
399 {
400     Frame* coreFrame = frame ? frame->coreFrame() : 0;
401     if (!coreFrame)
402         return false;
403
404     return PrintContext::isPageBoxVisible(coreFrame, pageIndex);
405 }
406
407 bool InjectedBundle::isProcessingUserGesture()
408 {
409     return ScriptController::processingUserGesture();
410 }
411
412 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)
413 {
414     // 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.
415     auto userScript = std::make_unique<UserScript>(source, URL(URL(), url), whitelist ? whitelist->toStringVector() : Vector<String>(), blacklist ? blacklist->toStringVector() : Vector<String>(), injectionTime, injectedFrames);
416
417     pageGroup->userContentController().addUserScript(scriptWorld->coreWorld(), WTF::move(userScript));
418 }
419
420 void InjectedBundle::addUserStyleSheet(WebPageGroupProxy* pageGroup, InjectedBundleScriptWorld* scriptWorld, const String& source, const String& url, API::Array* whitelist, API::Array* blacklist, WebCore::UserContentInjectedFrames injectedFrames)
421 {
422     // 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.
423     auto userStyleSheet = std::make_unique<UserStyleSheet>(source, URL(URL(), url), whitelist ? whitelist->toStringVector() : Vector<String>(), blacklist ? blacklist->toStringVector() : Vector<String>(), injectedFrames, UserStyleUserLevel);
424
425     pageGroup->userContentController().addUserStyleSheet(scriptWorld->coreWorld(), WTF::move(userStyleSheet), InjectInExistingDocuments);
426 }
427
428 void InjectedBundle::removeUserScript(WebPageGroupProxy* pageGroup, InjectedBundleScriptWorld* scriptWorld, const String& url)
429 {
430     // 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.
431     pageGroup->userContentController().removeUserScript(scriptWorld->coreWorld(), URL(URL(), url));
432 }
433
434 void InjectedBundle::removeUserStyleSheet(WebPageGroupProxy* pageGroup, InjectedBundleScriptWorld* scriptWorld, const String& url)
435 {
436     // 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.
437     pageGroup->userContentController().removeUserStyleSheet(scriptWorld->coreWorld(), URL(URL(), url));
438 }
439
440 void InjectedBundle::removeUserScripts(WebPageGroupProxy* pageGroup, InjectedBundleScriptWorld* scriptWorld)
441 {
442     pageGroup->userContentController().removeUserScripts(scriptWorld->coreWorld());
443 }
444
445 void InjectedBundle::removeUserStyleSheets(WebPageGroupProxy* pageGroup, InjectedBundleScriptWorld* scriptWorld)
446 {
447     pageGroup->userContentController().removeUserStyleSheets(scriptWorld->coreWorld());
448 }
449
450 void InjectedBundle::removeAllUserContent(WebPageGroupProxy* pageGroup)
451 {
452     pageGroup->userContentController().removeAllUserContent();
453 }
454
455 void InjectedBundle::garbageCollectJavaScriptObjects()
456 {
457     GCController::singleton().garbageCollectNow();
458 }
459
460 void InjectedBundle::garbageCollectJavaScriptObjectsOnAlternateThreadForDebugging(bool waitUntilDone)
461 {
462     GCController::singleton().garbageCollectOnAlternateThreadForDebugging(waitUntilDone);
463 }
464
465 size_t InjectedBundle::javaScriptObjectsCount()
466 {
467     JSLockHolder lock(JSDOMWindow::commonVM());
468     return JSDOMWindow::commonVM().heap.objectCount();
469 }
470
471 void InjectedBundle::reportException(JSContextRef context, JSValueRef exception)
472 {
473     if (!context || !exception)
474         return;
475
476     JSC::ExecState* execState = toJS(context);
477     JSLockHolder lock(execState);
478
479     // Make sure the context has a DOMWindow global object, otherwise this context didn't originate from a Page.
480     if (!toJSDOMWindow(execState->lexicalGlobalObject()))
481         return;
482
483     WebCore::reportException(execState, toJS(execState, exception));
484 }
485
486 void InjectedBundle::didCreatePage(WebPage* page)
487 {
488     m_client.didCreatePage(this, page);
489 }
490
491 void InjectedBundle::willDestroyPage(WebPage* page)
492 {
493     m_client.willDestroyPage(this, page);
494 }
495
496 void InjectedBundle::didInitializePageGroup(WebPageGroupProxy* pageGroup)
497 {
498     m_client.didInitializePageGroup(this, pageGroup);
499 }
500
501 void InjectedBundle::didReceiveMessage(const String& messageName, API::Object* messageBody)
502 {
503     m_client.didReceiveMessage(this, messageName, messageBody);
504 }
505
506 void InjectedBundle::didReceiveMessageToPage(WebPage* page, const String& messageName, API::Object* messageBody)
507 {
508     m_client.didReceiveMessageToPage(this, page, messageName, messageBody);
509 }
510
511 void InjectedBundle::setUserStyleSheetLocation(WebPageGroupProxy* pageGroup, const String& location)
512 {
513     const HashSet<Page*>& pages = PageGroup::pageGroup(pageGroup->identifier())->pages();
514     for (HashSet<Page*>::iterator iter = pages.begin(); iter != pages.end(); ++iter)
515         (*iter)->settings().setUserStyleSheetLocation(URL(URL(), location));
516 }
517
518 void InjectedBundle::setWebNotificationPermission(WebPage* page, const String& originString, bool allowed)
519 {
520 #if ENABLE(NOTIFICATIONS) || ENABLE(LEGACY_NOTIFICATIONS)
521     page->notificationPermissionRequestManager()->setPermissionLevelForTesting(originString, allowed);
522 #else
523     UNUSED_PARAM(page);
524     UNUSED_PARAM(originString);
525     UNUSED_PARAM(allowed);
526 #endif
527 }
528
529 void InjectedBundle::removeAllWebNotificationPermissions(WebPage* page)
530 {
531 #if ENABLE(NOTIFICATIONS) || ENABLE(LEGACY_NOTIFICATIONS)
532     page->notificationPermissionRequestManager()->removeAllPermissionsForTesting();
533 #else
534     UNUSED_PARAM(page);
535 #endif
536 }
537
538 uint64_t InjectedBundle::webNotificationID(JSContextRef jsContext, JSValueRef jsNotification)
539 {
540 #if ENABLE(NOTIFICATIONS) || ENABLE(LEGACY_NOTIFICATIONS)
541     WebCore::Notification* notification = JSNotification::toWrapped(toJS(toJS(jsContext), jsNotification));
542     if (!notification)
543         return 0;
544     return WebProcess::singleton().supplement<WebNotificationManager>()->notificationIDForTesting(notification);
545 #else
546     UNUSED_PARAM(jsContext);
547     UNUSED_PARAM(jsNotification);
548     return 0;
549 #endif
550 }
551
552 // FIXME Get rid of this function and move it into WKBundle.cpp.
553 PassRefPtr<API::Data> InjectedBundle::createWebDataFromUint8Array(JSContextRef context, JSValueRef data)
554 {
555     JSC::ExecState* execState = toJS(context);
556     RefPtr<Uint8Array> arrayData = WebCore::toUint8Array(toJS(execState, data));
557     return API::Data::create(static_cast<unsigned char*>(arrayData->baseAddress()), arrayData->byteLength());
558 }
559
560 void InjectedBundle::setTabKeyCyclesThroughElements(WebPage* page, bool enabled)
561 {
562     page->corePage()->setTabKeyCyclesThroughElements(enabled);
563 }
564
565 void InjectedBundle::setCSSAnimationTriggersEnabled(bool enabled)
566 {
567 #if ENABLE(CSS_ANIMATIONS_LEVEL_2)
568     RuntimeEnabledFeatures::sharedFeatures().setAnimationTriggersEnabled(enabled);
569 #else
570     UNUSED_PARAM(enabled);
571 #endif
572 }
573
574 void InjectedBundle::setWebAnimationsEnabled(bool enabled)
575 {
576 #if ENABLE(WEB_ANIMATIONS)
577     RuntimeEnabledFeatures::sharedFeatures().setWebAnimationsEnabled(enabled);
578 #else
579     UNUSED_PARAM(enabled);
580 #endif
581 }
582
583 void InjectedBundle::setCSSRegionsEnabled(bool enabled)
584 {
585 #if ENABLE(CSS_REGIONS)
586     RuntimeEnabledFeatures::sharedFeatures().setCSSRegionsEnabled(enabled);
587 #else
588     UNUSED_PARAM(enabled);
589 #endif
590 }
591
592 void InjectedBundle::setCSSCompositingEnabled(bool enabled)
593 {
594 #if ENABLE(CSS_COMPOSITING)
595     RuntimeEnabledFeatures::sharedFeatures().setCSSCompositingEnabled(enabled);
596 #else
597     UNUSED_PARAM(enabled);
598 #endif
599 }
600
601 } // namespace WebKit