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