aead25db834abf9cc8923ad13f8e337c6ab06a2f
[WebKit-https.git] / Source / WebKit / 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 "InjectedBundleScriptWorld.h"
32 #include "NetworkSessionCreationParameters.h"
33 #include "NotificationPermissionRequestManager.h"
34 #include "SessionTracker.h"
35 #include "UserData.h"
36 #include "WebConnectionToUIProcess.h"
37 #include "WebCookieManager.h"
38 #include "WebCoreArgumentCoders.h"
39 #include "WebFrame.h"
40 #include "WebFrameNetworkingContext.h"
41 #include "WebPage.h"
42 #include "WebPageGroupProxy.h"
43 #include "WebPreferencesKeys.h"
44 #include "WebPreferencesStore.h"
45 #include "WebProcess.h"
46 #include "WebProcessCreationParameters.h"
47 #include "WebProcessMessages.h"
48 #include "WebProcessPoolMessages.h"
49 #include "WebUserContentController.h"
50 #include "WebsiteDataStoreParameters.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/CommonVM.h>
57 #include <WebCore/FrameLoader.h>
58 #include <WebCore/FrameView.h>
59 #include <WebCore/GCController.h>
60 #include <WebCore/GeolocationClient.h>
61 #include <WebCore/GeolocationController.h>
62 #include <WebCore/GeolocationPosition.h>
63 #include <WebCore/JSDOMConvertBufferSource.h>
64 #include <WebCore/JSDOMExceptionHandling.h>
65 #include <WebCore/JSDOMWindow.h>
66 #include <WebCore/JSNotification.h>
67 #include <WebCore/MainFrame.h>
68 #include <WebCore/Page.h>
69 #include <WebCore/PageGroup.h>
70 #include <WebCore/PrintContext.h>
71 #include <WebCore/ResourceHandle.h>
72 #include <WebCore/RuntimeEnabledFeatures.h>
73 #include <WebCore/ScriptController.h>
74 #include <WebCore/SecurityOrigin.h>
75 #include <WebCore/SecurityPolicy.h>
76 #include <WebCore/Settings.h>
77 #include <WebCore/UserGestureIndicator.h>
78 #include <WebCore/UserScript.h>
79 #include <WebCore/UserStyleSheet.h>
80 #include <pal/SessionID.h>
81
82 #if ENABLE(NOTIFICATIONS)
83 #include "WebNotificationManager.h"
84 #endif
85
86 using namespace WebCore;
87 using namespace JSC;
88
89 namespace WebKit {
90
91 RefPtr<InjectedBundle> InjectedBundle::create(WebProcessCreationParameters& parameters, API::Object* initializationUserData)
92 {
93     auto bundle = adoptRef(*new InjectedBundle(parameters));
94
95     bundle->m_sandboxExtension = SandboxExtension::create(WTFMove(parameters.injectedBundlePathExtensionHandle));
96     if (!bundle->initialize(parameters, initializationUserData))
97         return nullptr;
98
99     return WTFMove(bundle);
100 }
101
102 InjectedBundle::InjectedBundle(const WebProcessCreationParameters& parameters)
103     : m_path(parameters.injectedBundlePath)
104     , m_platformBundle(0)
105     , m_client(std::make_unique<API::InjectedBundle::Client>())
106 {
107 }
108
109 InjectedBundle::~InjectedBundle()
110 {
111 }
112
113 void InjectedBundle::setClient(std::unique_ptr<API::InjectedBundle::Client>&& client)
114 {
115     if (!client)
116         m_client = std::make_unique<API::InjectedBundle::Client>();
117     else
118         m_client = WTFMove(client);
119 }
120
121 void InjectedBundle::postMessage(const String& messageName, API::Object* messageBody)
122 {
123     auto& webProcess = WebProcess::singleton();
124     webProcess.parentProcessConnection()->send(Messages::WebProcessPool::HandleMessage(messageName, UserData(webProcess.transformObjectsToHandles(messageBody))), 0);
125 }
126
127 void InjectedBundle::postSynchronousMessage(const String& messageName, API::Object* messageBody, RefPtr<API::Object>& returnData)
128 {
129     UserData returnUserData;
130
131     auto& webProcess = WebProcess::singleton();
132     if (!webProcess.parentProcessConnection()->sendSync(Messages::WebProcessPool::HandleSynchronousMessage(messageName, UserData(webProcess.transformObjectsToHandles(messageBody))), Messages::WebProcessPool::HandleSynchronousMessage::Reply(returnUserData), 0))
133         returnData = nullptr;
134     else
135         returnData = webProcess.transformHandlesToObjects(returnUserData.object());
136 }
137
138 WebConnection* InjectedBundle::webConnectionToUIProcess() const
139 {
140     return WebProcess::singleton().webConnectionToUIProcess();
141 }
142
143 void InjectedBundle::overrideBoolPreferenceForTestRunner(WebPageGroupProxy* pageGroup, const String& preference, bool enabled)
144 {
145     const HashSet<Page*>& pages = PageGroup::pageGroup(pageGroup->identifier())->pages();
146
147     if (preference == "WebKitTabToLinksPreferenceKey") {
148         WebPreferencesStore::overrideBoolValueForKey(WebPreferencesKey::tabsToLinksKey(), enabled);
149         for (auto* page : pages)
150             WebPage::fromCorePage(page)->setTabToLinksEnabled(enabled);
151     }
152
153     if (preference == "WebKit2AsynchronousPluginInitializationEnabled") {
154         WebPreferencesStore::overrideBoolValueForKey(WebPreferencesKey::asynchronousPluginInitializationEnabledKey(), enabled);
155         for (auto* page : pages)
156             WebPage::fromCorePage(page)->setAsynchronousPluginInitializationEnabled(enabled);
157     }
158
159     if (preference == "WebKit2AsynchronousPluginInitializationEnabledForAllPlugins") {
160         WebPreferencesStore::overrideBoolValueForKey(WebPreferencesKey::asynchronousPluginInitializationEnabledForAllPluginsKey(), enabled);
161         for (auto* page : pages)
162             WebPage::fromCorePage(page)->setAsynchronousPluginInitializationEnabledForAllPlugins(enabled);
163     }
164
165     if (preference == "WebKit2ArtificialPluginInitializationDelayEnabled") {
166         WebPreferencesStore::overrideBoolValueForKey(WebPreferencesKey::artificialPluginInitializationDelayEnabledKey(), enabled);
167         for (auto* page : pages)
168             WebPage::fromCorePage(page)->setArtificialPluginInitializationDelayEnabled(enabled);
169     }
170
171 #if ENABLE(SERVICE_CONTROLS)
172     if (preference == "WebKitImageControlsEnabled") {
173         WebPreferencesStore::overrideBoolValueForKey(WebPreferencesKey::imageControlsEnabledKey(), enabled);
174         for (auto* page : pages)
175             page->settings().setImageControlsEnabled(enabled);
176         return;
177     }
178 #endif
179
180 #if ENABLE(CSS_ANIMATIONS_LEVEL_2)
181     if (preference == "WebKitCSSAnimationTriggersEnabled")
182         RuntimeEnabledFeatures::sharedFeatures().setAnimationTriggersEnabled(enabled);
183 #endif
184
185     if (preference == "WebKitWebAnimationsEnabled")
186         RuntimeEnabledFeatures::sharedFeatures().setWebAnimationsEnabled(enabled);
187
188     if (preference == "WebKitCacheAPIEnabled")
189         RuntimeEnabledFeatures::sharedFeatures().setCacheAPIEnabled(enabled);
190
191 #if ENABLE(STREAMS_API)
192     if (preference == "WebKitReadableByteStreamAPIEnabled")
193         RuntimeEnabledFeatures::sharedFeatures().setReadableByteStreamAPIEnabled(enabled);
194     if (preference == "WebKitWritableStreamAPIEnabled")
195         RuntimeEnabledFeatures::sharedFeatures().setWritableStreamAPIEnabled(enabled);
196 #endif
197
198     if (preference == "WebKitCSSGridLayoutEnabled")
199         RuntimeEnabledFeatures::sharedFeatures().setCSSGridLayoutEnabled(enabled);
200
201     if (preference == "WebKitInteractiveFormValidationEnabled")
202         RuntimeEnabledFeatures::sharedFeatures().setInteractiveFormValidationEnabled(enabled);
203
204 #if ENABLE(WEBGL2)
205     if (preference == "WebKitWebGL2Enabled")
206         RuntimeEnabledFeatures::sharedFeatures().setWebGL2Enabled(enabled);
207 #endif
208
209 #if ENABLE(WEBGPU)
210     if (preference == "WebKitWebGPUEnabled")
211         RuntimeEnabledFeatures::sharedFeatures().setWebGPUEnabled(enabled);
212 #endif
213
214     if (preference == "WebKitModernMediaControlsEnabled")
215         RuntimeEnabledFeatures::sharedFeatures().setModernMediaControlsEnabled(enabled);
216
217 #if ENABLE(ENCRYPTED_MEDIA)
218     if (preference == "WebKitEncryptedMediaAPIEnabled") {
219         WebPreferencesStore::overrideBoolValueForKey(WebPreferencesKey::encryptedMediaAPIEnabledKey(), enabled);
220         RuntimeEnabledFeatures::sharedFeatures().setEncryptedMediaAPIEnabled(enabled);
221     }
222 #endif
223
224 #if ENABLE(MEDIA_STREAM)
225     if (preference == "WebKitMediaDevicesEnabled")
226         RuntimeEnabledFeatures::sharedFeatures().setMediaDevicesEnabled(enabled);
227     if (preference == "WebKitScreenCaptureEnabled")
228         RuntimeEnabledFeatures::sharedFeatures().setScreenCaptureEnabled(enabled);
229 #endif
230
231 #if ENABLE(WEB_RTC)
232     if (preference == "WebKitWebRTCLegacyAPIEnabled")
233         RuntimeEnabledFeatures::sharedFeatures().setWebRTCLegacyAPIEnabled(enabled);
234 #endif
235
236     if (preference == "WebKitIsSecureContextAttributeEnabled") {
237         WebPreferencesStore::overrideBoolValueForKey(WebPreferencesKey::isSecureContextAttributeEnabledKey(), enabled);
238         RuntimeEnabledFeatures::sharedFeatures().setIsSecureContextAttributeEnabled(enabled);
239     }
240
241     // Map the names used in LayoutTests with the names used in WebCore::Settings and WebPreferencesStore.
242 #define FOR_EACH_OVERRIDE_BOOL_PREFERENCE(macro) \
243     macro(WebKitJavaEnabled, JavaEnabled, javaEnabled) \
244     macro(WebKitJavaScriptEnabled, ScriptEnabled, javaScriptEnabled) \
245     macro(WebKitPluginsEnabled, PluginsEnabled, pluginsEnabled) \
246     macro(WebKitUsesPageCachePreferenceKey, UsesPageCache, usesPageCache) \
247     macro(WebKitWebAudioEnabled, WebAudioEnabled, webAudioEnabled) \
248     macro(WebKitWebGLEnabled, WebGLEnabled, webGLEnabled) \
249     macro(WebKitXSSAuditorEnabled, XSSAuditorEnabled, xssAuditorEnabled) \
250     macro(WebKitShouldRespectImageOrientation, ShouldRespectImageOrientation, shouldRespectImageOrientation) \
251     macro(WebKitDisplayImagesKey, LoadsImagesAutomatically, loadsImagesAutomatically) \
252     macro(WebKitVisualViewportEnabled, VisualViewportEnabled, visualViewportEnabled) \
253     macro(WebKitLargeImageAsyncDecodingEnabled, LargeImageAsyncDecodingEnabled, largeImageAsyncDecodingEnabled) \
254     macro(WebKitAnimatedImageAsyncDecodingEnabled, AnimatedImageAsyncDecodingEnabled, animatedImageAsyncDecodingEnabled) \
255     \
256
257 #define OVERRIDE_PREFERENCE_AND_SET_IN_EXISTING_PAGES(TestRunnerName, SettingsName, WebPreferencesName) \
258     if (preference == #TestRunnerName) { \
259         WebPreferencesStore::overrideBoolValueForKey(WebPreferencesKey::WebPreferencesName##Key(), enabled); \
260         for (HashSet<Page*>::iterator iter = pages.begin(); iter != pages.end(); ++iter) \
261             (*iter)->settings().set##SettingsName(enabled); \
262         return; \
263     }
264
265     FOR_EACH_OVERRIDE_BOOL_PREFERENCE(OVERRIDE_PREFERENCE_AND_SET_IN_EXISTING_PAGES)
266
267     OVERRIDE_PREFERENCE_AND_SET_IN_EXISTING_PAGES(WebKitHiddenPageDOMTimerThrottlingEnabled, HiddenPageDOMTimerThrottlingEnabled, hiddenPageDOMTimerThrottlingEnabled)
268
269 #undef OVERRIDE_PREFERENCE_AND_SET_IN_EXISTING_PAGES
270 #undef FOR_EACH_OVERRIDE_BOOL_PREFERENCE
271 }
272
273 void InjectedBundle::setAllowUniversalAccessFromFileURLs(WebPageGroupProxy* pageGroup, bool enabled)
274 {
275     const HashSet<Page*>& pages = PageGroup::pageGroup(pageGroup->identifier())->pages();
276     for (HashSet<Page*>::iterator iter = pages.begin(); iter != pages.end(); ++iter)
277         (*iter)->settings().setAllowUniversalAccessFromFileURLs(enabled);
278 }
279
280 void InjectedBundle::setAllowFileAccessFromFileURLs(WebPageGroupProxy* pageGroup, bool enabled)
281 {
282     const HashSet<Page*>& pages = PageGroup::pageGroup(pageGroup->identifier())->pages();
283     for (HashSet<Page*>::iterator iter = pages.begin(); iter != pages.end(); ++iter)
284         (*iter)->settings().setAllowFileAccessFromFileURLs(enabled);
285 }
286
287 void InjectedBundle::setNeedsStorageAccessFromFileURLsQuirk(WebPageGroupProxy* pageGroup, bool needsQuirk)
288 {
289     const HashSet<Page*>& pages = PageGroup::pageGroup(pageGroup->identifier())->pages();
290     for (auto page : pages)
291         page->settings().setNeedsStorageAccessFromFileURLsQuirk(needsQuirk);
292 }
293
294 void InjectedBundle::setMinimumLogicalFontSize(WebPageGroupProxy* pageGroup, int size)
295 {
296     const HashSet<Page*>& pages = PageGroup::pageGroup(pageGroup->identifier())->pages();
297     for (HashSet<Page*>::iterator iter = pages.begin(); iter != pages.end(); ++iter)
298         (*iter)->settings().setMinimumLogicalFontSize(size);
299 }
300
301 void InjectedBundle::setFrameFlatteningEnabled(WebPageGroupProxy* pageGroup, bool enabled)
302 {
303     const HashSet<Page*>& pages = PageGroup::pageGroup(pageGroup->identifier())->pages();
304     for (HashSet<Page*>::iterator iter = pages.begin(); iter != pages.end(); ++iter)
305         (*iter)->settings().setFrameFlattening(enabled ? FrameFlattening::FullyEnabled : FrameFlattening::Disabled);
306 }
307
308 void InjectedBundle::setAsyncFrameScrollingEnabled(WebPageGroupProxy* pageGroup, bool enabled)
309 {
310     const HashSet<Page*>& pages = PageGroup::pageGroup(pageGroup->identifier())->pages();
311     for (HashSet<Page*>::iterator iter = pages.begin(); iter != pages.end(); ++iter)
312         (*iter)->settings().setAsyncFrameScrollingEnabled(enabled);
313 }
314
315 void InjectedBundle::setJavaScriptCanAccessClipboard(WebPageGroupProxy* pageGroup, bool enabled)
316 {
317     const HashSet<Page*>& pages = PageGroup::pageGroup(pageGroup->identifier())->pages();
318     for (HashSet<Page*>::iterator iter = pages.begin(); iter != pages.end(); ++iter)
319         (*iter)->settings().setJavaScriptCanAccessClipboard(enabled);
320 }
321
322 void InjectedBundle::setPrivateBrowsingEnabled(WebPageGroupProxy* pageGroup, bool enabled)
323 {
324     if (enabled) {
325         WebProcess::singleton().ensureLegacyPrivateBrowsingSessionInNetworkProcess();
326         WebFrameNetworkingContext::ensureWebsiteDataStoreSession({ { }, { }, { }, { }, { }, { }, { PAL::SessionID::legacyPrivateSessionID(), { }, { }, AllowsCellularAccess::Yes }});
327     } else
328         SessionTracker::destroySession(PAL::SessionID::legacyPrivateSessionID());
329
330     const HashSet<Page*>& pages = PageGroup::pageGroup(pageGroup->identifier())->pages();
331     for (HashSet<Page*>::iterator iter = pages.begin(); iter != pages.end(); ++iter)
332         (*iter)->enableLegacyPrivateBrowsing(enabled);
333 }
334
335 void InjectedBundle::setUseDashboardCompatibilityMode(WebPageGroupProxy* pageGroup, bool enabled)
336 {
337 #if ENABLE(DASHBOARD_SUPPORT)
338     for (auto& page : PageGroup::pageGroup(pageGroup->identifier())->pages())
339         page->settings().setUsesDashboardBackwardCompatibilityMode(enabled);
340 #endif
341 }
342
343 void InjectedBundle::setPopupBlockingEnabled(WebPageGroupProxy* pageGroup, bool enabled)
344 {
345     const HashSet<Page*>& pages = PageGroup::pageGroup(pageGroup->identifier())->pages();
346     HashSet<Page*>::const_iterator end = pages.end();
347     for (HashSet<Page*>::const_iterator iter = pages.begin(); iter != end; ++iter)
348         (*iter)->settings().setJavaScriptCanOpenWindowsAutomatically(!enabled);
349 }
350
351 void InjectedBundle::setAuthorAndUserStylesEnabled(WebPageGroupProxy* pageGroup, bool enabled)
352 {
353     const HashSet<Page*>& pages = PageGroup::pageGroup(pageGroup->identifier())->pages();
354     for (HashSet<Page*>::iterator iter = pages.begin(); iter != pages.end(); ++iter)
355         (*iter)->settings().setAuthorAndUserStylesEnabled(enabled);
356 }
357
358 void InjectedBundle::setSpatialNavigationEnabled(WebPageGroupProxy* pageGroup, bool enabled)
359 {
360     const HashSet<Page*>& pages = PageGroup::pageGroup(pageGroup->identifier())->pages();
361     for (HashSet<Page*>::iterator iter = pages.begin(); iter != pages.end(); ++iter)
362         (*iter)->settings().setSpatialNavigationEnabled(enabled);
363 }
364
365 void InjectedBundle::addOriginAccessWhitelistEntry(const String& sourceOrigin, const String& destinationProtocol, const String& destinationHost, bool allowDestinationSubdomains)
366 {
367     SecurityPolicy::addOriginAccessWhitelistEntry(SecurityOrigin::createFromString(sourceOrigin).get(), destinationProtocol, destinationHost, allowDestinationSubdomains);
368 }
369
370 void InjectedBundle::removeOriginAccessWhitelistEntry(const String& sourceOrigin, const String& destinationProtocol, const String& destinationHost, bool allowDestinationSubdomains)
371 {
372     SecurityPolicy::removeOriginAccessWhitelistEntry(SecurityOrigin::createFromString(sourceOrigin).get(), destinationProtocol, destinationHost, allowDestinationSubdomains);
373 }
374
375 void InjectedBundle::resetOriginAccessWhitelists()
376 {
377     SecurityPolicy::resetOriginAccessWhitelists();
378 }
379
380 void InjectedBundle::setAsynchronousSpellCheckingEnabled(WebPageGroupProxy* pageGroup, bool enabled)
381 {
382     const HashSet<Page*>& pages = PageGroup::pageGroup(pageGroup->identifier())->pages();
383     for (HashSet<Page*>::iterator iter = pages.begin(); iter != pages.end(); ++iter)
384         (*iter)->settings().setAsynchronousSpellCheckingEnabled(enabled);
385 }
386
387 int InjectedBundle::numberOfPages(WebFrame* frame, double pageWidthInPixels, double pageHeightInPixels)
388 {
389     Frame* coreFrame = frame ? frame->coreFrame() : 0;
390     if (!coreFrame)
391         return -1;
392     if (!pageWidthInPixels)
393         pageWidthInPixels = coreFrame->view()->width();
394     if (!pageHeightInPixels)
395         pageHeightInPixels = coreFrame->view()->height();
396
397     return PrintContext::numberOfPages(*coreFrame, FloatSize(pageWidthInPixels, pageHeightInPixels));
398 }
399
400 int InjectedBundle::pageNumberForElementById(WebFrame* frame, const String& id, double pageWidthInPixels, double pageHeightInPixels)
401 {
402     Frame* coreFrame = frame ? frame->coreFrame() : 0;
403     if (!coreFrame)
404         return -1;
405
406     Element* element = coreFrame->document()->getElementById(id);
407     if (!element)
408         return -1;
409
410     if (!pageWidthInPixels)
411         pageWidthInPixels = coreFrame->view()->width();
412     if (!pageHeightInPixels)
413         pageHeightInPixels = coreFrame->view()->height();
414
415     return PrintContext::pageNumberForElement(element, FloatSize(pageWidthInPixels, pageHeightInPixels));
416 }
417
418 String InjectedBundle::pageSizeAndMarginsInPixels(WebFrame* frame, int pageIndex, int width, int height, int marginTop, int marginRight, int marginBottom, int marginLeft)
419 {
420     Frame* coreFrame = frame ? frame->coreFrame() : 0;
421     if (!coreFrame)
422         return String();
423
424     return PrintContext::pageSizeAndMarginsInPixels(coreFrame, pageIndex, width, height, marginTop, marginRight, marginBottom, marginLeft);
425 }
426
427 bool InjectedBundle::isPageBoxVisible(WebFrame* frame, int pageIndex)
428 {
429     Frame* coreFrame = frame ? frame->coreFrame() : 0;
430     if (!coreFrame)
431         return false;
432
433     return PrintContext::isPageBoxVisible(coreFrame, pageIndex);
434 }
435
436 bool InjectedBundle::isProcessingUserGesture()
437 {
438     return UserGestureIndicator::processingUserGesture();
439 }
440
441 void InjectedBundle::addUserScript(WebPageGroupProxy* pageGroup, InjectedBundleScriptWorld* scriptWorld, String&& source, String&& url, API::Array* whitelist, API::Array* blacklist, WebCore::UserScriptInjectionTime injectionTime, WebCore::UserContentInjectedFrames injectedFrames)
442 {
443     // 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.
444     UserScript userScript { WTFMove(source), URL(URL(), url), whitelist ? whitelist->toStringVector() : Vector<String>(), blacklist ? blacklist->toStringVector() : Vector<String>(), injectionTime, injectedFrames };
445
446     pageGroup->userContentController().addUserScript(*scriptWorld, WTFMove(userScript));
447 }
448
449 void InjectedBundle::addUserStyleSheet(WebPageGroupProxy* pageGroup, InjectedBundleScriptWorld* scriptWorld, const String& source, const String& url, API::Array* whitelist, API::Array* blacklist, WebCore::UserContentInjectedFrames injectedFrames)
450 {
451     // 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.
452     UserStyleSheet userStyleSheet{ source, URL(URL(), url), whitelist ? whitelist->toStringVector() : Vector<String>(), blacklist ? blacklist->toStringVector() : Vector<String>(), injectedFrames, UserStyleUserLevel };
453
454     pageGroup->userContentController().addUserStyleSheet(*scriptWorld, WTFMove(userStyleSheet));
455 }
456
457 void InjectedBundle::removeUserScript(WebPageGroupProxy* pageGroup, InjectedBundleScriptWorld* scriptWorld, const String& url)
458 {
459     // 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.
460     pageGroup->userContentController().removeUserScriptWithURL(*scriptWorld, URL(URL(), url));
461 }
462
463 void InjectedBundle::removeUserStyleSheet(WebPageGroupProxy* pageGroup, InjectedBundleScriptWorld* scriptWorld, const String& url)
464 {
465     // 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.
466     pageGroup->userContentController().removeUserStyleSheetWithURL(*scriptWorld, URL(URL(), url));
467 }
468
469 void InjectedBundle::removeUserScripts(WebPageGroupProxy* pageGroup, InjectedBundleScriptWorld* scriptWorld)
470 {
471     pageGroup->userContentController().removeUserScripts(*scriptWorld);
472 }
473
474 void InjectedBundle::removeUserStyleSheets(WebPageGroupProxy* pageGroup, InjectedBundleScriptWorld* scriptWorld)
475 {
476     pageGroup->userContentController().removeUserStyleSheets(*scriptWorld);
477 }
478
479 void InjectedBundle::removeAllUserContent(WebPageGroupProxy* pageGroup)
480 {
481     pageGroup->userContentController().removeAllUserContent();
482 }
483
484 void InjectedBundle::garbageCollectJavaScriptObjects()
485 {
486     GCController::singleton().garbageCollectNow();
487 }
488
489 void InjectedBundle::garbageCollectJavaScriptObjectsOnAlternateThreadForDebugging(bool waitUntilDone)
490 {
491     GCController::singleton().garbageCollectOnAlternateThreadForDebugging(waitUntilDone);
492 }
493
494 size_t InjectedBundle::javaScriptObjectsCount()
495 {
496     JSLockHolder lock(commonVM());
497     return commonVM().heap.objectCount();
498 }
499
500 void InjectedBundle::reportException(JSContextRef context, JSValueRef exception)
501 {
502     if (!context || !exception)
503         return;
504
505     JSC::ExecState* execState = toJS(context);
506     JSLockHolder lock(execState);
507
508     // Make sure the context has a DOMWindow global object, otherwise this context didn't originate from a Page.
509     JSC::JSGlobalObject* globalObject = execState->lexicalGlobalObject();
510     if (!toJSDOMWindow(globalObject->vm(), globalObject))
511         return;
512
513     WebCore::reportException(execState, toJS(execState, exception));
514 }
515
516 void InjectedBundle::didCreatePage(WebPage* page)
517 {
518     m_client->didCreatePage(*this, *page);
519 }
520
521 void InjectedBundle::willDestroyPage(WebPage* page)
522 {
523     m_client->willDestroyPage(*this, *page);
524 }
525
526 void InjectedBundle::didInitializePageGroup(WebPageGroupProxy* pageGroup)
527 {
528     m_client->didInitializePageGroup(*this, *pageGroup);
529 }
530
531 void InjectedBundle::didReceiveMessage(const String& messageName, API::Object* messageBody)
532 {
533     m_client->didReceiveMessage(*this, messageName, messageBody);
534 }
535
536 void InjectedBundle::didReceiveMessageToPage(WebPage* page, const String& messageName, API::Object* messageBody)
537 {
538     m_client->didReceiveMessageToPage(*this, *page, messageName, messageBody);
539 }
540
541 void InjectedBundle::setUserStyleSheetLocation(WebPageGroupProxy* pageGroup, const String& location)
542 {
543     const HashSet<Page*>& pages = PageGroup::pageGroup(pageGroup->identifier())->pages();
544     for (HashSet<Page*>::iterator iter = pages.begin(); iter != pages.end(); ++iter)
545         (*iter)->settings().setUserStyleSheetLocation(URL(URL(), location));
546 }
547
548 void InjectedBundle::setWebNotificationPermission(WebPage* page, const String& originString, bool allowed)
549 {
550 #if ENABLE(NOTIFICATIONS)
551     page->notificationPermissionRequestManager()->setPermissionLevelForTesting(originString, allowed);
552 #else
553     UNUSED_PARAM(page);
554     UNUSED_PARAM(originString);
555     UNUSED_PARAM(allowed);
556 #endif
557 }
558
559 void InjectedBundle::removeAllWebNotificationPermissions(WebPage* page)
560 {
561 #if ENABLE(NOTIFICATIONS)
562     page->notificationPermissionRequestManager()->removeAllPermissionsForTesting();
563 #else
564     UNUSED_PARAM(page);
565 #endif
566 }
567
568 uint64_t InjectedBundle::webNotificationID(JSContextRef jsContext, JSValueRef jsNotification)
569 {
570 #if ENABLE(NOTIFICATIONS)
571     WebCore::Notification* notification = JSNotification::toWrapped(toJS(jsContext)->vm(), toJS(toJS(jsContext), jsNotification));
572     if (!notification)
573         return 0;
574     return WebProcess::singleton().supplement<WebNotificationManager>()->notificationIDForTesting(notification);
575 #else
576     UNUSED_PARAM(jsContext);
577     UNUSED_PARAM(jsNotification);
578     return 0;
579 #endif
580 }
581
582 // FIXME Get rid of this function and move it into WKBundle.cpp.
583 Ref<API::Data> InjectedBundle::createWebDataFromUint8Array(JSContextRef context, JSValueRef data)
584 {
585     JSC::ExecState* execState = toJS(context);
586     JSLockHolder lock(execState);
587     RefPtr<Uint8Array> arrayData = WebCore::toUnsharedUint8Array(execState->vm(), toJS(execState, data));
588     return API::Data::create(static_cast<unsigned char*>(arrayData->baseAddress()), arrayData->byteLength());
589 }
590
591 void InjectedBundle::setTabKeyCyclesThroughElements(WebPage* page, bool enabled)
592 {
593     page->corePage()->setTabKeyCyclesThroughElements(enabled);
594 }
595
596 void InjectedBundle::setCSSAnimationTriggersEnabled(bool enabled)
597 {
598 #if ENABLE(CSS_ANIMATIONS_LEVEL_2)
599     RuntimeEnabledFeatures::sharedFeatures().setAnimationTriggersEnabled(enabled);
600 #else
601     UNUSED_PARAM(enabled);
602 #endif
603 }
604
605 void InjectedBundle::setWebAnimationsEnabled(bool enabled)
606 {
607     RuntimeEnabledFeatures::sharedFeatures().setWebAnimationsEnabled(enabled);
608 }
609
610 } // namespace WebKit