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