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