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