Unreviewed, rolling out r219990.
[WebKit-https.git] / Source / WebKit / WebProcess / WebProcess.h
1 /*
2  * Copyright (C) 2010-2017 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 "CacheModel.h"
29 #include "ChildProcess.h"
30 #include "PluginProcessConnectionManager.h"
31 #include "ResourceCachesToClear.h"
32 #include "SandboxExtension.h"
33 #include "TextCheckerState.h"
34 #include "ViewUpdateDispatcher.h"
35 #include "WebInspectorInterruptDispatcher.h"
36 #include <WebCore/ActivityState.h>
37 #include <WebCore/HysteresisActivity.h>
38 #include <WebCore/SessionID.h>
39 #include <WebCore/Timer.h>
40 #include <wtf/Forward.h>
41 #include <wtf/HashMap.h>
42 #include <wtf/HashSet.h>
43 #include <wtf/text/AtomicString.h>
44 #include <wtf/text/AtomicStringHash.h>
45
46 #if PLATFORM(COCOA)
47 #include <dispatch/dispatch.h>
48 #include <WebCore/MachSendRight.h>
49 #endif
50
51 #if PLATFORM(IOS)
52 #include "WebSQLiteDatabaseTracker.h"
53 #endif
54
55 namespace API {
56 class Object;
57 }
58
59 namespace WebCore {
60 class ApplicationCacheStorage;
61 class CPUMonitor;
62 class CertificateInfo;
63 class PageGroup;
64 class ResourceRequest;
65 class SessionID;
66 class UserGestureToken;
67 struct PluginInfo;
68 struct SecurityOriginData;
69 struct SoupNetworkProxySettings;
70 }
71
72 namespace WebKit {
73
74 class EventDispatcher;
75 class GamepadData;
76 class InjectedBundle;
77 class LibWebRTCNetwork;
78 class NetworkProcessConnection;
79 class ObjCObjectGraph;
80 class UserData;
81 class WaylandCompositorDisplay;
82 class WebAutomationSessionProxy;
83 class WebConnectionToUIProcess;
84 class WebFrame;
85 class WebLoaderStrategy;
86 class WebPage;
87 class WebPageGroupProxy;
88 class WebProcessSupplement;
89 class WebToDatabaseProcessConnection;
90 enum class WebsiteDataType;
91 struct WebPageCreationParameters;
92 struct WebPageGroupData;
93 struct WebPreferencesStore;
94 struct WebProcessCreationParameters;
95 struct WebsiteData;
96 struct WebsiteDataStoreParameters;
97
98 class WebProcess : public ChildProcess {
99 public:
100     static WebProcess& singleton();
101
102     template <typename T>
103     T* supplement()
104     {
105         return static_cast<T*>(m_supplements.get(T::supplementName()));
106     }
107
108     template <typename T>
109     void addSupplement()
110     {
111         m_supplements.add(T::supplementName(), std::make_unique<T>(this));
112     }
113
114     WebConnectionToUIProcess* webConnectionToUIProcess() const { return m_webConnection.get(); }
115
116     WebPage* webPage(uint64_t pageID) const;
117     void createWebPage(uint64_t pageID, WebPageCreationParameters&&);
118     void removeWebPage(uint64_t pageID);
119     WebPage* focusedWebPage() const;
120
121     InjectedBundle* injectedBundle() const { return m_injectedBundle.get(); }
122
123 #if PLATFORM(COCOA)
124     const WebCore::MachSendRight& compositingRenderServerPort() const { return m_compositingRenderServerPort; }
125 #endif
126
127     bool shouldPlugInAutoStartFromOrigin(WebPage&, const String& pageOrigin, const String& pluginOrigin, const String& mimeType);
128     void plugInDidStartFromOrigin(const String& pageOrigin, const String& pluginOrigin, const String& mimeType, WebCore::SessionID);
129     void plugInDidReceiveUserInteraction(const String& pageOrigin, const String& pluginOrigin, const String& mimeType, WebCore::SessionID);
130     void setPluginLoadClientPolicy(uint8_t policy, const String& host, const String& bundleIdentifier, const String& versionString);
131     void resetPluginLoadClientPolicies(const HashMap<String, HashMap<String, HashMap<String, uint8_t>>>&);
132     void clearPluginClientPolicies();
133     void refreshPlugins();
134
135     bool fullKeyboardAccessEnabled() const { return m_fullKeyboardAccessEnabled; }
136
137     WebFrame* webFrame(uint64_t) const;
138     void addWebFrame(uint64_t, WebFrame*);
139     void removeWebFrame(uint64_t);
140
141     WebPageGroupProxy* webPageGroup(WebCore::PageGroup*);
142     WebPageGroupProxy* webPageGroup(uint64_t pageGroupID);
143     WebPageGroupProxy* webPageGroup(const WebPageGroupData&);
144
145     uint64_t userGestureTokenIdentifier(RefPtr<WebCore::UserGestureToken>);
146     void userGestureTokenDestroyed(WebCore::UserGestureToken&);
147     
148     const TextCheckerState& textCheckerState() const { return m_textCheckerState; }
149     void setTextCheckerState(const TextCheckerState&);
150
151     void clearResourceCaches(ResourceCachesToClear = AllResourceCaches);
152     
153 #if ENABLE(NETSCAPE_PLUGIN_API)
154     PluginProcessConnectionManager& pluginProcessConnectionManager();
155 #endif
156
157     EventDispatcher& eventDispatcher() { return *m_eventDispatcher; }
158
159     NetworkProcessConnection& networkConnection();
160     void networkProcessConnectionClosed(NetworkProcessConnection*);
161     WebLoaderStrategy& webLoaderStrategy();
162
163 #if USE(LIBWEBRTC)
164     LibWebRTCNetwork& libWebRTCNetwork();
165 #endif
166
167     void webToDatabaseProcessConnectionClosed(WebToDatabaseProcessConnection*);
168     WebToDatabaseProcessConnection* webToDatabaseProcessConnection();
169
170     void setCacheModel(uint32_t);
171
172     void ensurePrivateBrowsingSession(WebCore::SessionID);
173     void ensureLegacyPrivateBrowsingSessionInNetworkProcess();
174     void addWebsiteDataStore(WebsiteDataStoreParameters&&);
175     void destroySession(WebCore::SessionID);
176
177     void pageDidEnterWindow(uint64_t pageID);
178     void pageWillLeaveWindow(uint64_t pageID);
179
180     void nonVisibleProcessCleanupTimerFired();
181
182 #if PLATFORM(COCOA)
183     RetainPtr<CFDataRef> sourceApplicationAuditData() const;
184     void destroyRenderingResources();
185 #endif
186
187     void updateActivePages();
188     void pageActivityStateDidChange(uint64_t pageID, WebCore::ActivityState::Flags changed);
189
190     void setHiddenPageDOMTimerThrottlingIncreaseLimit(int milliseconds);
191
192     void processWillSuspendImminently(bool& handled);
193     void prepareToSuspend();
194     void cancelPrepareToSuspend();
195     void processDidResume();
196
197 #if PLATFORM(IOS)
198     void resetAllGeolocationPermissions();
199 #endif
200
201 #if PLATFORM(WAYLAND)
202     WaylandCompositorDisplay* waylandCompositorDisplay() const { return m_waylandCompositorDisplay.get(); }
203 #endif
204
205     RefPtr<API::Object> transformHandlesToObjects(API::Object*);
206     static RefPtr<API::Object> transformObjectsToHandles(API::Object*);
207
208 #if PLATFORM(COCOA)
209     RefPtr<ObjCObjectGraph> transformHandlesToObjects(ObjCObjectGraph&);
210     static RefPtr<ObjCObjectGraph> transformObjectsToHandles(ObjCObjectGraph&);
211 #endif
212
213 #if ENABLE(SERVICE_CONTROLS)
214     bool hasImageServices() const { return m_hasImageServices; }
215     bool hasSelectionServices() const { return m_hasSelectionServices; }
216     bool hasRichContentServices() const { return m_hasRichContentServices; }
217 #endif
218
219     WebCore::ApplicationCacheStorage& applicationCacheStorage() { return *m_applicationCacheStorage; }
220
221     void prefetchDNS(const String&);
222
223     WebAutomationSessionProxy* automationSessionProxy() { return m_automationSessionProxy.get(); }
224
225 private:
226     WebProcess();
227     ~WebProcess();
228
229     void initializeWebProcess(WebProcessCreationParameters&&);
230     void platformInitializeWebProcess(WebProcessCreationParameters&&);
231
232 #if USE(OS_STATE)
233     void registerWithStateDumper();
234 #endif
235
236     void markAllLayersVolatile(WTF::Function<void()>&& completionHandler);
237     void cancelMarkAllLayersVolatile();
238     void setAllLayerTreeStatesFrozen(bool);
239     void processSuspensionCleanupTimerFired();
240
241     void clearCachedCredentials();
242
243     void platformTerminate();
244     void registerURLSchemeAsEmptyDocument(const String&);
245     void registerURLSchemeAsSecure(const String&) const;
246     void registerURLSchemeAsBypassingContentSecurityPolicy(const String&) const;
247     void setDomainRelaxationForbiddenForURLScheme(const String&) const;
248     void registerURLSchemeAsLocal(const String&) const;
249     void registerURLSchemeAsNoAccess(const String&) const;
250     void registerURLSchemeAsDisplayIsolated(const String&) const;
251     void registerURLSchemeAsCORSEnabled(const String&) const;
252     void registerURLSchemeAsAlwaysRevalidated(const String&) const;
253     void registerURLSchemeAsCachePartitioned(const String&) const;
254     void setDefaultRequestTimeoutInterval(double);
255     void setAlwaysUsesComplexTextCodePath(bool);
256     void setShouldUseFontSmoothing(bool);
257     void setResourceLoadStatisticsEnabled(bool);
258     void userPreferredLanguagesChanged(const Vector<String>&) const;
259     void fullKeyboardAccessModeChanged(bool fullKeyboardAccessEnabled);
260
261     bool isPlugInAutoStartOriginHash(unsigned plugInOriginHash, WebCore::SessionID);
262     void didAddPlugInAutoStartOriginHash(unsigned plugInOriginHash, double expirationTime, WebCore::SessionID);
263     void resetPlugInAutoStartOriginDefaultHashes(const HashMap<unsigned, double>& hashes);
264     void resetPlugInAutoStartOriginHashes(const HashMap<WebCore::SessionID, HashMap<unsigned, double>>& hashes);
265
266     void platformSetCacheModel(CacheModel);
267
268     void setEnhancedAccessibility(bool);
269     
270     void startMemorySampler(const SandboxExtension::Handle&, const String&, const double);
271     void stopMemorySampler();
272     
273     void getWebCoreStatistics(uint64_t callbackID);
274     void garbageCollectJavaScriptObjects();
275     void setJavaScriptGarbageCollectorTimerEnabled(bool flag);
276
277     void mainThreadPing();
278     void backgroundResponsivenessPing();
279
280 #if ENABLE(GAMEPAD)
281     void setInitialGamepads(const Vector<GamepadData>&);
282     void gamepadConnected(const GamepadData&);
283     void gamepadDisconnected(unsigned index);
284 #endif
285
286 #if USE(SOUP)
287     void setNetworkProxySettings(const WebCore::SoupNetworkProxySettings&);
288 #endif
289
290     void releasePageCache();
291
292     void fetchWebsiteData(WebCore::SessionID, OptionSet<WebsiteDataType>, WebsiteData&);
293     void deleteWebsiteData(WebCore::SessionID, OptionSet<WebsiteDataType>, std::chrono::system_clock::time_point modifiedSince);
294     void deleteWebsiteDataForOrigins(WebCore::SessionID, OptionSet<WebsiteDataType>, const Vector<WebCore::SecurityOriginData>& origins);
295
296     void setMemoryCacheDisabled(bool);
297
298 #if ENABLE(SERVICE_CONTROLS)
299     void setEnabledServices(bool hasImageServices, bool hasSelectionServices, bool hasRichContentServices);
300 #endif
301
302     void handleInjectedBundleMessage(const String& messageName, const UserData& messageBody);
303     void setInjectedBundleParameter(const String& key, const IPC::DataReference&);
304     void setInjectedBundleParameters(const IPC::DataReference&);
305
306     enum class ShouldAcknowledgeWhenReadyToSuspend { No, Yes };
307     void actualPrepareToSuspend(ShouldAcknowledgeWhenReadyToSuspend);
308
309     void ensureAutomationSessionProxy(const String& sessionIdentifier);
310     void destroyAutomationSessionProxy();
311
312     void logDiagnosticMessageForNetworkProcessCrash();
313     bool hasVisibleWebPage() const;
314     void updateCPULimit();
315     enum class CPUMonitorUpdateReason { LimitHasChanged, VisibilityHasChanged };
316     void updateCPUMonitorState(CPUMonitorUpdateReason);
317
318     // ChildProcess
319     void initializeProcess(const ChildProcessInitializationParameters&) override;
320     void initializeProcessName(const ChildProcessInitializationParameters&) override;
321     void initializeSandbox(const ChildProcessInitializationParameters&, SandboxInitializationParameters&) override;
322     void initializeConnection(IPC::Connection*) override;
323     bool shouldTerminate() override;
324     void terminate() override;
325
326 #if USE(APPKIT)
327     void stopRunLoop() override;
328 #endif
329
330     void platformInitializeProcess(const ChildProcessInitializationParameters&);
331
332     // IPC::Connection::Client
333     friend class WebConnectionToUIProcess;
334     void didReceiveMessage(IPC::Connection&, IPC::Decoder&) override;
335     void didReceiveSyncMessage(IPC::Connection&, IPC::Decoder&, std::unique_ptr<IPC::Encoder>&) override;
336     void didClose(IPC::Connection&) override;
337
338     // Implemented in generated WebProcessMessageReceiver.cpp
339     void didReceiveWebProcessMessage(IPC::Connection&, IPC::Decoder&);
340     void didReceiveSyncWebProcessMessage(IPC::Connection&, IPC::Decoder&, std::unique_ptr<IPC::Encoder>&);
341
342     RefPtr<WebConnectionToUIProcess> m_webConnection;
343
344     HashMap<uint64_t, RefPtr<WebPage>> m_pageMap;
345     HashMap<uint64_t, RefPtr<WebPageGroupProxy>> m_pageGroupMap;
346     RefPtr<InjectedBundle> m_injectedBundle;
347
348     RefPtr<EventDispatcher> m_eventDispatcher;
349 #if PLATFORM(IOS)
350     RefPtr<ViewUpdateDispatcher> m_viewUpdateDispatcher;
351 #endif
352     RefPtr<WebInspectorInterruptDispatcher> m_webInspectorInterruptDispatcher;
353
354     HashMap<WebCore::SessionID, HashMap<unsigned, double>> m_plugInAutoStartOriginHashes;
355     HashSet<String> m_plugInAutoStartOrigins;
356
357     bool m_hasSetCacheModel { false };
358     CacheModel m_cacheModel { CacheModelDocumentViewer };
359
360 #if PLATFORM(COCOA)
361     WebCore::MachSendRight m_compositingRenderServerPort;
362 #endif
363
364     bool m_fullKeyboardAccessEnabled { false };
365
366     HashMap<uint64_t, WebFrame*> m_frameMap;
367
368     typedef HashMap<const char*, std::unique_ptr<WebProcessSupplement>, PtrHash<const char*>> WebProcessSupplementMap;
369     WebProcessSupplementMap m_supplements;
370
371     TextCheckerState m_textCheckerState;
372
373     void ensureNetworkProcessConnection();
374     RefPtr<NetworkProcessConnection> m_networkProcessConnection;
375     WebLoaderStrategy& m_webLoaderStrategy;
376
377 #if USE(LIBWEBRTC)
378     std::unique_ptr<LibWebRTCNetwork> m_libWebRTCNetwork;
379 #endif
380
381     HashSet<String> m_dnsPrefetchedHosts;
382     WebCore::HysteresisActivity m_dnsPrefetchHystereris;
383
384     std::unique_ptr<WebAutomationSessionProxy> m_automationSessionProxy;
385
386     void ensureWebToDatabaseProcessConnection();
387     RefPtr<WebToDatabaseProcessConnection> m_webToDatabaseProcessConnection;
388
389 #if ENABLE(NETSCAPE_PLUGIN_API)
390     RefPtr<PluginProcessConnectionManager> m_pluginProcessConnectionManager;
391 #endif
392
393 #if ENABLE(SERVICE_CONTROLS)
394     bool m_hasImageServices { false };
395     bool m_hasSelectionServices { false };
396     bool m_hasRichContentServices { false };
397 #endif
398
399     HashSet<uint64_t> m_pagesInWindows;
400     WebCore::Timer m_nonVisibleProcessCleanupTimer;
401
402     RefPtr<WebCore::ApplicationCacheStorage> m_applicationCacheStorage;
403
404 #if PLATFORM(IOS)
405     WebSQLiteDatabaseTracker m_webSQLiteDatabaseTracker;
406 #endif
407
408     unsigned m_pagesMarkingLayersAsVolatile { 0 };
409     bool m_suppressMemoryPressureHandler { false };
410 #if PLATFORM(MAC)
411     std::unique_ptr<WebCore::CPUMonitor> m_cpuMonitor;
412     std::optional<double> m_cpuLimit;
413 #endif
414
415     HashMap<WebCore::UserGestureToken *, uint64_t> m_userGestureTokens;
416
417 #if PLATFORM(WAYLAND)
418     std::unique_ptr<WaylandCompositorDisplay> m_waylandCompositorDisplay;
419 #endif
420 };
421
422 } // namespace WebKit