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