IndexedDB in service workers is using a memory backed store
[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/Timer.h>
38 #include <pal/HysteresisActivity.h>
39 #include <pal/SessionID.h>
40 #include <wtf/Forward.h>
41 #include <wtf/HashMap.h>
42 #include <wtf/HashSet.h>
43 #include <wtf/RefCounter.h>
44 #include <wtf/text/AtomicString.h>
45 #include <wtf/text/AtomicStringHash.h>
46
47 #if PLATFORM(COCOA)
48 #include <dispatch/dispatch.h>
49 #include <WebCore/MachSendRight.h>
50 #endif
51
52 #if PLATFORM(IOS)
53 #include "WebSQLiteDatabaseTracker.h"
54 #endif
55
56 namespace API {
57 class Object;
58 }
59
60 namespace PAL {
61 class SessionID;
62 }
63
64 namespace WebCore {
65 class ApplicationCacheStorage;
66 class CPUMonitor;
67 class CertificateInfo;
68 class PageGroup;
69 class ResourceRequest;
70 class UserGestureToken;
71 struct MessagePortIdentifier;
72 struct MessageWithMessagePorts;
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 class WebToStorageProcessConnection;
101 enum class WebsiteDataType;
102 struct WebPageCreationParameters;
103 struct WebPageGroupData;
104 struct WebPreferencesStore;
105 struct WebProcessCreationParameters;
106 struct WebsiteData;
107 struct WebsiteDataStoreParameters;
108
109 class WebProcess : public ChildProcess {
110 public:
111     static WebProcess& singleton();
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 WebCore::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     WebLoaderStrategy& webLoaderStrategy();
173
174 #if USE(LIBWEBRTC)
175     LibWebRTCNetwork& libWebRTCNetwork();
176 #endif
177
178     void webToStorageProcessConnectionClosed(WebToStorageProcessConnection*);
179     WebToStorageProcessConnection* existingWebToStorageProcessConnection() { return m_webToStorageProcessConnection.get(); }
180     WebToStorageProcessConnection& ensureWebToStorageProcessConnection(PAL::SessionID initialSessionID);
181
182     void setCacheModel(uint32_t);
183
184     void ensureLegacyPrivateBrowsingSessionInNetworkProcess();
185     void addWebsiteDataStore(WebsiteDataStoreParameters&&);
186     void destroySession(PAL::SessionID);
187
188     void pageDidEnterWindow(uint64_t pageID);
189     void pageWillLeaveWindow(uint64_t pageID);
190
191     void nonVisibleProcessCleanupTimerFired();
192
193 #if PLATFORM(COCOA)
194     RetainPtr<CFDataRef> sourceApplicationAuditData() const;
195     void destroyRenderingResources();
196 #endif
197
198     void updateActivePages();
199     void pageActivityStateDidChange(uint64_t pageID, WebCore::ActivityState::Flags changed);
200
201     void setHiddenPageDOMTimerThrottlingIncreaseLimit(int milliseconds);
202
203     void processWillSuspendImminently(bool& handled);
204     void prepareToSuspend();
205     void cancelPrepareToSuspend();
206     void processDidResume();
207
208 #if PLATFORM(IOS)
209     void resetAllGeolocationPermissions();
210 #endif
211
212 #if PLATFORM(WAYLAND)
213     WaylandCompositorDisplay* waylandCompositorDisplay() const { return m_waylandCompositorDisplay.get(); }
214 #endif
215
216     RefPtr<API::Object> transformHandlesToObjects(API::Object*);
217     static RefPtr<API::Object> transformObjectsToHandles(API::Object*);
218
219 #if PLATFORM(COCOA)
220     RefPtr<ObjCObjectGraph> transformHandlesToObjects(ObjCObjectGraph&);
221     static RefPtr<ObjCObjectGraph> transformObjectsToHandles(ObjCObjectGraph&);
222 #endif
223
224 #if ENABLE(SERVICE_CONTROLS)
225     bool hasImageServices() const { return m_hasImageServices; }
226     bool hasSelectionServices() const { return m_hasSelectionServices; }
227     bool hasRichContentServices() const { return m_hasRichContentServices; }
228 #endif
229
230     WebCore::ApplicationCacheStorage& applicationCacheStorage() { return *m_applicationCacheStorage; }
231
232     void prefetchDNS(const String&);
233
234     WebAutomationSessionProxy* automationSessionProxy() { return m_automationSessionProxy.get(); }
235
236     WebCacheStorageProvider& cacheStorageProvider() { return m_cacheStorageProvider.get(); }
237
238 #if PLATFORM(IOS)
239     void accessibilityProcessSuspendedNotification(bool);
240 #endif
241
242 private:
243     WebProcess();
244     ~WebProcess();
245
246     void initializeWebProcess(WebProcessCreationParameters&&);
247     void platformInitializeWebProcess(WebProcessCreationParameters&&);
248
249 #if USE(OS_STATE)
250     void registerWithStateDumper();
251 #endif
252
253     void markAllLayersVolatile(WTF::Function<void()>&& completionHandler);
254     void cancelMarkAllLayersVolatile();
255     void setAllLayerTreeStatesFrozen(bool);
256     void processSuspensionCleanupTimerFired();
257
258     void clearCachedCredentials();
259
260     void platformTerminate();
261     void registerURLSchemeAsEmptyDocument(const String&);
262     void registerURLSchemeAsSecure(const String&) const;
263     void registerURLSchemeAsBypassingContentSecurityPolicy(const String&) const;
264     void setDomainRelaxationForbiddenForURLScheme(const String&) const;
265     void registerURLSchemeAsLocal(const String&) const;
266     void registerURLSchemeAsNoAccess(const String&) const;
267     void registerURLSchemeAsDisplayIsolated(const String&) const;
268     void registerURLSchemeAsCORSEnabled(const String&) const;
269     void registerURLSchemeAsAlwaysRevalidated(const String&) const;
270     void registerURLSchemeAsCachePartitioned(const String&) const;
271     void setDefaultRequestTimeoutInterval(double);
272     void setAlwaysUsesComplexTextCodePath(bool);
273     void setShouldUseFontSmoothing(bool);
274     void setResourceLoadStatisticsEnabled(bool);
275     void clearResourceLoadStatistics();
276     void userPreferredLanguagesChanged(const Vector<String>&) const;
277     void fullKeyboardAccessModeChanged(bool fullKeyboardAccessEnabled);
278
279     bool isPlugInAutoStartOriginHash(unsigned plugInOriginHash, PAL::SessionID);
280     void didAddPlugInAutoStartOriginHash(unsigned plugInOriginHash, double expirationTime, PAL::SessionID);
281     void resetPlugInAutoStartOriginDefaultHashes(const HashMap<unsigned, double>& hashes);
282     void resetPlugInAutoStartOriginHashes(const HashMap<PAL::SessionID, HashMap<unsigned, double>>& hashes);
283
284     void platformSetCacheModel(CacheModel);
285
286     void setEnhancedAccessibility(bool);
287     
288     void startMemorySampler(SandboxExtension::Handle&&, const String&, const double);
289     void stopMemorySampler();
290     
291     void getWebCoreStatistics(uint64_t callbackID);
292     void garbageCollectJavaScriptObjects();
293     void setJavaScriptGarbageCollectorTimerEnabled(bool flag);
294
295     void mainThreadPing();
296     void backgroundResponsivenessPing();
297
298     void syncIPCMessageWhileWaitingForSyncReplyForTesting();
299
300     void didTakeAllMessagesForPort(Vector<WebCore::MessageWithMessagePorts>&& messages, uint64_t messageCallbackIdentifier, uint64_t messageBatchIdentifier);
301     void checkProcessLocalPortForActivity(const WebCore::MessagePortIdentifier&, uint64_t callbackIdentifier);
302     void didCheckRemotePortForActivity(uint64_t callbackIdentifier, bool hasActivity);
303     void messagesAvailableForPort(const WebCore::MessagePortIdentifier&);
304
305 #if ENABLE(GAMEPAD)
306     void setInitialGamepads(const Vector<GamepadData>&);
307     void gamepadConnected(const GamepadData&);
308     void gamepadDisconnected(unsigned index);
309 #endif
310 #if USE(SOUP)
311     void setNetworkProxySettings(const WebCore::SoupNetworkProxySettings&);
312 #endif
313 #if ENABLE(SERVICE_WORKER)
314     void establishWorkerContextConnectionToStorageProcess(uint64_t pageGroupID, uint64_t pageID, const WebPreferencesStore&, PAL::SessionID);
315     void registerServiceWorkerClients(PAL::SessionID);
316 #endif
317
318     void releasePageCache();
319
320     void fetchWebsiteData(PAL::SessionID, OptionSet<WebsiteDataType>, WebsiteData&);
321     void deleteWebsiteData(PAL::SessionID, OptionSet<WebsiteDataType>, WallTime modifiedSince);
322     void deleteWebsiteDataForOrigins(PAL::SessionID, OptionSet<WebsiteDataType>, const Vector<WebCore::SecurityOriginData>& origins);
323
324     void setMemoryCacheDisabled(bool);
325
326 #if ENABLE(SERVICE_CONTROLS)
327     void setEnabledServices(bool hasImageServices, bool hasSelectionServices, bool hasRichContentServices);
328 #endif
329
330     void handleInjectedBundleMessage(const String& messageName, const UserData& messageBody);
331     void setInjectedBundleParameter(const String& key, const IPC::DataReference&);
332     void setInjectedBundleParameters(const IPC::DataReference&);
333
334     enum class ShouldAcknowledgeWhenReadyToSuspend { No, Yes };
335     void actualPrepareToSuspend(ShouldAcknowledgeWhenReadyToSuspend);
336
337     void ensureAutomationSessionProxy(const String& sessionIdentifier);
338     void destroyAutomationSessionProxy();
339
340     void logDiagnosticMessageForNetworkProcessCrash();
341     bool hasVisibleWebPage() const;
342     void updateCPULimit();
343     enum class CPUMonitorUpdateReason { LimitHasChanged, VisibilityHasChanged };
344     void updateCPUMonitorState(CPUMonitorUpdateReason);
345
346     // ChildProcess
347     void initializeProcess(const ChildProcessInitializationParameters&) override;
348     void initializeProcessName(const ChildProcessInitializationParameters&) override;
349     void initializeSandbox(const ChildProcessInitializationParameters&, SandboxInitializationParameters&) override;
350     void initializeConnection(IPC::Connection*) override;
351     bool shouldTerminate() override;
352     void terminate() override;
353
354 #if USE(APPKIT)
355     void stopRunLoop() override;
356 #endif
357
358     void platformInitializeProcess(const ChildProcessInitializationParameters&);
359
360     // IPC::Connection::Client
361     friend class WebConnectionToUIProcess;
362     void didReceiveMessage(IPC::Connection&, IPC::Decoder&) override;
363     void didReceiveSyncMessage(IPC::Connection&, IPC::Decoder&, std::unique_ptr<IPC::Encoder>&) override;
364     void didClose(IPC::Connection&) override;
365
366     // Implemented in generated WebProcessMessageReceiver.cpp
367     void didReceiveWebProcessMessage(IPC::Connection&, IPC::Decoder&);
368     void didReceiveSyncWebProcessMessage(IPC::Connection&, IPC::Decoder&, std::unique_ptr<IPC::Encoder>&);
369
370     RefPtr<WebConnectionToUIProcess> m_webConnection;
371
372     HashMap<uint64_t, RefPtr<WebPage>> m_pageMap;
373     HashMap<uint64_t, RefPtr<WebPageGroupProxy>> m_pageGroupMap;
374     RefPtr<InjectedBundle> m_injectedBundle;
375
376     RefPtr<EventDispatcher> m_eventDispatcher;
377 #if PLATFORM(IOS)
378     RefPtr<ViewUpdateDispatcher> m_viewUpdateDispatcher;
379 #endif
380     RefPtr<WebInspectorInterruptDispatcher> m_webInspectorInterruptDispatcher;
381
382     HashMap<PAL::SessionID, HashMap<unsigned, double>> m_plugInAutoStartOriginHashes;
383     HashSet<String> m_plugInAutoStartOrigins;
384
385     bool m_hasSetCacheModel { false };
386     CacheModel m_cacheModel { CacheModelDocumentViewer };
387
388 #if PLATFORM(COCOA)
389     WebCore::MachSendRight m_compositingRenderServerPort;
390 #endif
391
392     bool m_fullKeyboardAccessEnabled { false };
393
394     HashMap<uint64_t, WebFrame*> m_frameMap;
395
396     typedef HashMap<const char*, std::unique_ptr<WebProcessSupplement>, PtrHash<const char*>> WebProcessSupplementMap;
397     WebProcessSupplementMap m_supplements;
398
399     TextCheckerState m_textCheckerState;
400
401     RefPtr<NetworkProcessConnection> m_networkProcessConnection;
402     WebLoaderStrategy& m_webLoaderStrategy;
403
404     Ref<WebCacheStorageProvider> m_cacheStorageProvider;
405
406 #if USE(LIBWEBRTC)
407     std::unique_ptr<LibWebRTCNetwork> m_libWebRTCNetwork;
408 #endif
409
410     HashSet<String> m_dnsPrefetchedHosts;
411     PAL::HysteresisActivity m_dnsPrefetchHystereris;
412
413     std::unique_ptr<WebAutomationSessionProxy> m_automationSessionProxy;
414
415     RefPtr<WebToStorageProcessConnection> m_webToStorageProcessConnection;
416
417 #if ENABLE(NETSCAPE_PLUGIN_API)
418     RefPtr<PluginProcessConnectionManager> m_pluginProcessConnectionManager;
419 #endif
420
421 #if ENABLE(SERVICE_CONTROLS)
422     bool m_hasImageServices { false };
423     bool m_hasSelectionServices { false };
424     bool m_hasRichContentServices { false };
425 #endif
426
427     HashSet<uint64_t> m_pagesInWindows;
428     WebCore::Timer m_nonVisibleProcessCleanupTimer;
429
430     RefPtr<WebCore::ApplicationCacheStorage> m_applicationCacheStorage;
431
432 #if PLATFORM(IOS)
433     WebSQLiteDatabaseTracker m_webSQLiteDatabaseTracker;
434 #endif
435
436     enum PageMarkingLayersAsVolatileCounterType { };
437     using PageMarkingLayersAsVolatileCounter = RefCounter<PageMarkingLayersAsVolatileCounterType>;
438     std::unique_ptr<PageMarkingLayersAsVolatileCounter> m_pageMarkingLayersAsVolatileCounter;
439
440     bool m_suppressMemoryPressureHandler { false };
441 #if PLATFORM(MAC)
442     std::unique_ptr<WebCore::CPUMonitor> m_cpuMonitor;
443     std::optional<double> m_cpuLimit;
444 #endif
445
446     HashMap<WebCore::UserGestureToken *, uint64_t> m_userGestureTokens;
447
448 #if PLATFORM(WAYLAND)
449     std::unique_ptr<WaylandCompositorDisplay> m_waylandCompositorDisplay;
450 #endif
451 };
452
453 } // namespace WebKit