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