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