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