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