1/*
2 * Copyright (C) 2010-2018 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#pragma once
27
28#include "APIInjectedBundleBundleClient.h"
29#include "APIObject.h"
30#include "SandboxExtension.h"
31#include <JavaScriptCore/JavaScript.h>
32#include <WebCore/UserContentTypes.h>
33#include <WebCore/UserScriptTypes.h>
34#include <wtf/RefPtr.h>
35#include <wtf/RetainPtr.h>
36#include <wtf/text/WTFString.h>
37
38#if USE(GLIB)
39typedef struct _GModule GModule;
40#endif
41
42#if USE(FOUNDATION)
43OBJC_CLASS NSSet;
44OBJC_CLASS NSBundle;
45OBJC_CLASS NSMutableDictionary;
46OBJC_CLASS WKWebProcessBundleParameters;
47#endif
48
49namespace API {
50class Array;
51class Data;
52}
53
54namespace IPC {
55class Decoder;
56class Connection;
57class DataReference;
58}
59
60namespace WebKit {
61
62#if USE(FOUNDATION)
63typedef NSBundle *PlatformBundle;
64#elif USE(GLIB)
65typedef ::GModule* PlatformBundle;
66#else
67typedef void* PlatformBundle;
68#endif
69
70class InjectedBundleScriptWorld;
71class WebCertificateInfo;
72class WebConnection;
73class WebFrame;
74class WebPage;
75class WebPageGroupProxy;
76struct WebProcessCreationParameters;
77
78class InjectedBundle : public API::ObjectImpl<API::Object::Type::Bundle> {
79public:
80 static RefPtr<InjectedBundle> create(WebProcessCreationParameters&, API::Object* initializationUserData);
81
82 ~InjectedBundle();
83
84 bool initialize(const WebProcessCreationParameters&, API::Object* initializationUserData);
85
86 void setBundleParameter(const String&, const IPC::DataReference&);
87 void setBundleParameters(const IPC::DataReference&);
88
89 // API
90 void setClient(std::unique_ptr<API::InjectedBundle::Client>&&);
91 void postMessage(const String&, API::Object*);
92 void postSynchronousMessage(const String&, API::Object*, RefPtr<API::Object>& returnData);
93 void setServiceWorkerProxyCreationCallback(void (*)(uint64_t));
94
95 WebConnection* webConnectionToUIProcess() const;
96
97 // TestRunner only SPI
98 void overrideBoolPreferenceForTestRunner(WebPageGroupProxy*, const String& preference, bool enabled);
99 void setAllowUniversalAccessFromFileURLs(WebPageGroupProxy*, bool);
100 void setAllowFileAccessFromFileURLs(WebPageGroupProxy*, bool);
101 void setNeedsStorageAccessFromFileURLsQuirk(WebPageGroupProxy*, bool);
102 void setMinimumLogicalFontSize(WebPageGroupProxy*, int size);
103 void setFrameFlatteningEnabled(WebPageGroupProxy*, bool);
104 void setAsyncFrameScrollingEnabled(WebPageGroupProxy*, bool);
105 void setPluginsEnabled(WebPageGroupProxy*, bool);
106 void setJavaScriptCanAccessClipboard(WebPageGroupProxy*, bool);
107 void setPrivateBrowsingEnabled(WebPageGroupProxy*, bool);
108 void setPopupBlockingEnabled(WebPageGroupProxy*, bool);
109 void setAuthorAndUserStylesEnabled(WebPageGroupProxy*, bool);
110 void setSpatialNavigationEnabled(WebPageGroupProxy*, bool);
111 void addOriginAccessWhitelistEntry(const String&, const String&, const String&, bool);
112 void removeOriginAccessWhitelistEntry(const String&, const String&, const String&, bool);
113 void resetOriginAccessWhitelists();
114 void setAsynchronousSpellCheckingEnabled(WebPageGroupProxy*, bool);
115 int numberOfPages(WebFrame*, double, double);
116 int pageNumberForElementById(WebFrame*, const String&, double, double);
117 String pageSizeAndMarginsInPixels(WebFrame*, int, int, int, int, int, int, int);
118 bool isPageBoxVisible(WebFrame*, int);
119 void setUserStyleSheetLocation(WebPageGroupProxy*, const String&);
120 void setWebNotificationPermission(WebPage*, const String& originString, bool allowed);
121 void removeAllWebNotificationPermissions(WebPage*);
122 uint64_t webNotificationID(JSContextRef, JSValueRef);
123 Ref<API::Data> createWebDataFromUint8Array(JSContextRef, JSValueRef);
124
125 typedef HashMap<uint64_t, String> DocumentIDToURLMap;
126 DocumentIDToURLMap liveDocumentURLs(WebPageGroupProxy*, bool excludeDocumentsInPageGroupPages);
127
128 // UserContent API
129 void addUserScript(WebPageGroupProxy*, InjectedBundleScriptWorld*, String&& source, String&& url, API::Array* whitelist, API::Array* blacklist, WebCore::UserScriptInjectionTime, WebCore::UserContentInjectedFrames);
130 void addUserStyleSheet(WebPageGroupProxy*, InjectedBundleScriptWorld*, const String& source, const String& url, API::Array* whitelist, API::Array* blacklist, WebCore::UserContentInjectedFrames);
131 void removeUserScript(WebPageGroupProxy*, InjectedBundleScriptWorld*, const String& url);
132 void removeUserStyleSheet(WebPageGroupProxy*, InjectedBundleScriptWorld*, const String& url);
133 void removeUserScripts(WebPageGroupProxy*, InjectedBundleScriptWorld*);
134 void removeUserStyleSheets(WebPageGroupProxy*, InjectedBundleScriptWorld*);
135 void removeAllUserContent(WebPageGroupProxy*);
136
137 // Garbage collection API
138 void garbageCollectJavaScriptObjects();
139 void garbageCollectJavaScriptObjectsOnAlternateThreadForDebugging(bool waitUntilDone);
140 size_t javaScriptObjectsCount();
141
142 // Callback hooks
143 void didCreatePage(WebPage*);
144 void willDestroyPage(WebPage*);
145 void didInitializePageGroup(WebPageGroupProxy*);
146 void didReceiveMessage(const String&, API::Object*);
147 void didReceiveMessageToPage(WebPage*, const String&, API::Object*);
148
149 static void reportException(JSContextRef, JSValueRef exception);
150
151 static bool isProcessingUserGesture();
152
153 void setTabKeyCyclesThroughElements(WebPage*, bool enabled);
154 void setSerialLoadingEnabled(bool);
155 void setWebAnimationsEnabled(bool);
156 void setWebAnimationsCSSIntegrationEnabled(bool);
157 void dispatchPendingLoadRequests();
158
159#if PLATFORM(COCOA)
160 WKWebProcessBundleParameters *bundleParameters();
161
162 void extendClassesForParameterCoder(API::Array& classes);
163 NSSet* classesForCoder();
164#endif
165
166private:
167 explicit InjectedBundle(const WebProcessCreationParameters&);
168
169 String m_path;
170 PlatformBundle m_platformBundle; // This is leaked right now, since we never unload the bundle/module.
171
172 RefPtr<SandboxExtension> m_sandboxExtension;
173
174 std::unique_ptr<API::InjectedBundle::Client> m_client;
175
176#if PLATFORM(COCOA)
177 RetainPtr<WKWebProcessBundleParameters> m_bundleParameters;
178 RetainPtr<NSSet> m_classesForCoder;
179#endif
180};
181
182} // namespace WebKit
183
184