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