614340bfbbffa020941a75217212547b98b155e5
[WebKit-https.git] / Source / WebKit2 / WebProcess / WebProcess.h
1 /*
2  * Copyright (C) 2010 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 #ifndef WebProcess_h
27 #define WebProcess_h
28
29 #include "CacheModel.h"
30 #include "ChildProcess.h"
31 #include "DownloadManager.h"
32 #include "DrawingArea.h"
33 #include "PluginProcessConnectionManager.h"
34 #include "ResourceCachesToClear.h"
35 #include "SandboxExtension.h"
36 #include "SharedMemory.h"
37 #include "TextCheckerState.h"
38 #include "ViewUpdateDispatcher.h"
39 #include "VisitedLinkTable.h"
40 #include <WebCore/HysteresisActivity.h>
41 #include <WebCore/SessionID.h>
42 #include <WebCore/Timer.h>
43 #include <wtf/Forward.h>
44 #include <wtf/HashMap.h>
45 #include <wtf/HashSet.h>
46 #include <wtf/NeverDestroyed.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 CertificateInfo;
65 class PageGroup;
66 class ResourceRequest;
67 class SessionID;
68 struct PluginInfo;
69 struct SecurityOriginData;
70 }
71
72 namespace WebKit {
73
74 class DownloadManager;
75 class EventDispatcher;
76 class InjectedBundle;
77 class NetworkProcessConnection;
78 class ObjCObjectGraph;
79 class UserData;
80 class WebConnectionToUIProcess;
81 class WebFrame;
82 class WebIconDatabaseProxy;
83 class WebPage;
84 class WebPageGroupProxy;
85 class WebProcessSupplement;
86 class WebResourceLoadScheduler;
87 struct WebPageCreationParameters;
88 struct WebPageGroupData;
89 struct WebPreferencesStore;
90 struct WebProcessCreationParameters;
91
92 #if ENABLE(DATABASE_PROCESS)
93 class WebToDatabaseProcessConnection;
94 #endif
95
96 class WebProcess : public ChildProcess, private DownloadManager::Client {
97     friend class NeverDestroyed<DownloadManager>;
98 public:
99     static WebProcess& singleton();
100
101     template <typename T>
102     T* supplement()
103     {
104         return static_cast<T*>(m_supplements.get(T::supplementName()));
105     }
106
107     template <typename T>
108     void addSupplement()
109     {
110         m_supplements.add(T::supplementName(), std::make_unique<T>(this));
111     }
112
113     WebConnectionToUIProcess* webConnectionToUIProcess() const { return m_webConnection.get(); }
114
115     WebPage* webPage(uint64_t pageID) const;
116     void createWebPage(uint64_t pageID, const WebPageCreationParameters&);
117     void removeWebPage(uint64_t pageID);
118     WebPage* focusedWebPage() const;
119
120     InjectedBundle* injectedBundle() const { return m_injectedBundle.get(); }
121
122 #if PLATFORM(COCOA)
123     const WebCore::MachSendRight& compositingRenderServerPort() const { return m_compositingRenderServerPort; }
124 #endif
125
126     bool shouldPlugInAutoStartFromOrigin(WebPage&, const String& pageOrigin, const String& pluginOrigin, const String& mimeType);
127     void plugInDidStartFromOrigin(const String& pageOrigin, const String& pluginOrigin, const String& mimeType, WebCore::SessionID);
128     void plugInDidReceiveUserInteraction(const String& pageOrigin, const String& pluginOrigin, const String& mimeType, WebCore::SessionID);
129     void setPluginLoadClientPolicy(uint8_t policy, const String& host, const String& bundleIdentifier, const String& versionString);
130     void clearPluginClientPolicies();
131
132     bool fullKeyboardAccessEnabled() const { return m_fullKeyboardAccessEnabled; }
133
134     WebFrame* webFrame(uint64_t) const;
135     void addWebFrame(uint64_t, WebFrame*);
136     void removeWebFrame(uint64_t);
137
138     WebPageGroupProxy* webPageGroup(WebCore::PageGroup*);
139     WebPageGroupProxy* webPageGroup(uint64_t pageGroupID);
140     WebPageGroupProxy* webPageGroup(const WebPageGroupData&);
141
142 #if PLATFORM(COCOA)
143     pid_t presenterApplicationPid() const { return m_presenterApplicationPid; }
144 #endif
145     
146     const TextCheckerState& textCheckerState() const { return m_textCheckerState; }
147     DownloadManager& downloadManager();
148
149     void clearResourceCaches(ResourceCachesToClear = AllResourceCaches);
150     
151 #if ENABLE(NETSCAPE_PLUGIN_API)
152     PluginProcessConnectionManager& pluginProcessConnectionManager();
153 #endif
154
155     EventDispatcher& eventDispatcher() { return *m_eventDispatcher; }
156
157     bool usesNetworkProcess() const;
158
159     NetworkProcessConnection* networkConnection();
160     void networkProcessConnectionClosed(NetworkProcessConnection*);
161     WebResourceLoadScheduler& webResourceLoadScheduler();
162
163 #if ENABLE(DATABASE_PROCESS)
164     void webToDatabaseProcessConnectionClosed(WebToDatabaseProcessConnection*);
165     WebToDatabaseProcessConnection* webToDatabaseProcessConnection();
166 #endif
167
168     void setCacheModel(uint32_t);
169
170     void ensurePrivateBrowsingSession(WebCore::SessionID);
171     void destroyPrivateBrowsingSession(WebCore::SessionID);
172
173     void pageDidEnterWindow(uint64_t pageID);
174     void pageWillLeaveWindow(uint64_t pageID);
175
176     void nonVisibleProcessCleanupTimerFired();
177
178 #if PLATFORM(COCOA)
179     void destroyRenderingResources();
180 #endif
181
182     void updateActivePages();
183
184 #if USE(SOUP)
185     void allowSpecificHTTPSCertificateForHost(const WebCore::CertificateInfo&, const String& host);
186 #endif
187
188     void processWillSuspendImminently(bool& handled);
189     void prepareToSuspend();
190     void cancelPrepareToSuspend();
191     bool markAllLayersVolatileIfPossible();
192     void setAllLayerTreeStatesFrozen(bool);
193     void processSuspensionCleanupTimerFired();
194     void processDidResume();
195
196 #if PLATFORM(IOS)
197     void resetAllGeolocationPermissions();
198 #endif
199
200     RefPtr<API::Object> transformHandlesToObjects(API::Object*);
201     static RefPtr<API::Object> transformObjectsToHandles(API::Object*);
202
203 #if PLATFORM(COCOA)
204     RefPtr<ObjCObjectGraph> transformHandlesToObjects(ObjCObjectGraph&);
205     static RefPtr<ObjCObjectGraph> transformObjectsToHandles(ObjCObjectGraph&);
206 #endif
207
208 #if ENABLE(SERVICE_CONTROLS)
209     bool hasImageServices() const { return m_hasImageServices; }
210     bool hasSelectionServices() const { return m_hasSelectionServices; }
211     bool hasRichContentServices() const { return m_hasRichContentServices; }
212 #endif
213
214     void prefetchDNS(const String&);
215
216 private:
217     WebProcess();
218     ~WebProcess();
219
220     // DownloadManager::Client.
221     virtual void didCreateDownload() override;
222     virtual void didDestroyDownload() override;
223     virtual IPC::Connection* downloadProxyConnection() override;
224     virtual AuthenticationManager& downloadsAuthenticationManager() override;
225
226     void initializeWebProcess(WebProcessCreationParameters&&);
227     void platformInitializeWebProcess(WebProcessCreationParameters&&);
228
229     void platformTerminate();
230     void registerURLSchemeAsEmptyDocument(const String&);
231     void registerURLSchemeAsSecure(const String&) const;
232     void registerURLSchemeAsBypassingContentSecurityPolicy(const String&) const;
233     void setDomainRelaxationForbiddenForURLScheme(const String&) const;
234     void registerURLSchemeAsLocal(const String&) const;
235     void registerURLSchemeAsNoAccess(const String&) const;
236     void registerURLSchemeAsDisplayIsolated(const String&) const;
237     void registerURLSchemeAsCORSEnabled(const String&) const;
238 #if ENABLE(CACHE_PARTITIONING)
239     void registerURLSchemeAsCachePartitioned(const String&) const;
240 #endif
241     void setDefaultRequestTimeoutInterval(double);
242     void setAlwaysUsesComplexTextCodePath(bool);
243     void setShouldUseFontSmoothing(bool);
244     void userPreferredLanguagesChanged(const Vector<String>&) const;
245     void fullKeyboardAccessModeChanged(bool fullKeyboardAccessEnabled);
246
247     bool isPlugInAutoStartOriginHash(unsigned plugInOriginHash, WebCore::SessionID);
248     void didAddPlugInAutoStartOriginHash(unsigned plugInOriginHash, double expirationTime, WebCore::SessionID);
249     void resetPlugInAutoStartOriginDefaultHashes(const HashMap<unsigned, double>& hashes);
250     void resetPlugInAutoStartOriginHashes(const HashMap<WebCore::SessionID, HashMap<unsigned, double>>& hashes);
251
252     void platformSetCacheModel(CacheModel);
253     void platformClearResourceCaches(ResourceCachesToClear);
254     void clearApplicationCache();
255
256     void setEnhancedAccessibility(bool);
257     
258     void startMemorySampler(const SandboxExtension::Handle&, const String&, const double);
259     void stopMemorySampler();
260
261     void downloadRequest(WebCore::SessionID, uint64_t downloadID, uint64_t initiatingPageID, const WebCore::ResourceRequest&);
262     void resumeDownload(uint64_t downloadID, const IPC::DataReference& resumeData, const String& path, const SandboxExtension::Handle&);
263     void cancelDownload(uint64_t downloadID);
264
265     void setTextCheckerState(const TextCheckerState&);
266     
267     void getWebCoreStatistics(uint64_t callbackID);
268     void garbageCollectJavaScriptObjects();
269     void setJavaScriptGarbageCollectorTimerEnabled(bool flag);
270
271     void mainThreadPing();
272
273     void releasePageCache();
274
275     void fetchWebsiteData(WebCore::SessionID, uint64_t websiteDataTypes, uint64_t callbackID);
276     void deleteWebsiteData(WebCore::SessionID, uint64_t websiteDataTypes, std::chrono::system_clock::time_point modifiedSince, uint64_t callbackID);
277     void deleteWebsiteDataForOrigins(WebCore::SessionID, uint64_t websiteDataTypes, const Vector<WebCore::SecurityOriginData>& origins, uint64_t callbackID);
278
279 #if USE(SOUP)
280     void setIgnoreTLSErrors(bool);
281 #endif
282
283     void setMemoryCacheDisabled(bool);
284
285 #if ENABLE(SERVICE_CONTROLS)
286     void setEnabledServices(bool hasImageServices, bool hasSelectionServices, bool hasRichContentServices);
287 #endif
288
289     void handleInjectedBundleMessage(const String& messageName, const UserData& messageBody);
290     void setInjectedBundleParameter(const String& key, const IPC::DataReference&);
291     void setInjectedBundleParameters(const IPC::DataReference&);
292
293     enum class ShouldAcknowledgeWhenReadyToSuspend { No, Yes };
294     void actualPrepareToSuspend(ShouldAcknowledgeWhenReadyToSuspend);
295
296     // ChildProcess
297     virtual void initializeProcess(const ChildProcessInitializationParameters&) override;
298     virtual void initializeProcessName(const ChildProcessInitializationParameters&) override;
299     virtual void initializeSandbox(const ChildProcessInitializationParameters&, SandboxInitializationParameters&) override;
300     virtual void initializeConnection(IPC::Connection*) override;
301     virtual bool shouldTerminate() override;
302     virtual void terminate() override;
303
304 #if USE(APPKIT)
305     virtual void stopRunLoop() override;
306 #endif
307
308     void platformInitializeProcess(const ChildProcessInitializationParameters&);
309
310     // IPC::Connection::Client
311     friend class WebConnectionToUIProcess;
312     virtual void didReceiveMessage(IPC::Connection&, IPC::MessageDecoder&) override;
313     virtual void didReceiveSyncMessage(IPC::Connection&, IPC::MessageDecoder&, std::unique_ptr<IPC::MessageEncoder>&) override;
314     virtual void didClose(IPC::Connection&) override;
315     virtual void didReceiveInvalidMessage(IPC::Connection&, IPC::StringReference messageReceiverName, IPC::StringReference messageName) override;
316     virtual IPC::ProcessType localProcessType() override { return IPC::ProcessType::Web; }
317     virtual IPC::ProcessType remoteProcessType() override { return IPC::ProcessType::UI; }
318
319     // Implemented in generated WebProcessMessageReceiver.cpp
320     void didReceiveWebProcessMessage(IPC::Connection&, IPC::MessageDecoder&);
321     void didReceiveSyncWebProcessMessage(IPC::Connection&, IPC::MessageDecoder&, std::unique_ptr<IPC::MessageEncoder>&);
322
323     RefPtr<WebConnectionToUIProcess> m_webConnection;
324
325     HashMap<uint64_t, RefPtr<WebPage>> m_pageMap;
326     HashMap<uint64_t, RefPtr<WebPageGroupProxy>> m_pageGroupMap;
327     RefPtr<InjectedBundle> m_injectedBundle;
328
329     RefPtr<EventDispatcher> m_eventDispatcher;
330 #if PLATFORM(IOS)
331     RefPtr<ViewUpdateDispatcher> m_viewUpdateDispatcher;
332 #endif
333     WebCore::Timer m_processSuspensionCleanupTimer;
334
335     bool m_inDidClose;
336
337     HashMap<WebCore::SessionID, HashMap<unsigned, double>> m_plugInAutoStartOriginHashes;
338     HashSet<String> m_plugInAutoStartOrigins;
339
340     bool m_hasSetCacheModel;
341     CacheModel m_cacheModel;
342
343 #if PLATFORM(COCOA)
344     WebCore::MachSendRight m_compositingRenderServerPort;
345     pid_t m_presenterApplicationPid;
346 #endif
347
348     bool m_fullKeyboardAccessEnabled;
349
350     HashMap<uint64_t, WebFrame*> m_frameMap;
351
352     typedef HashMap<const char*, std::unique_ptr<WebProcessSupplement>, PtrHash<const char*>> WebProcessSupplementMap;
353     WebProcessSupplementMap m_supplements;
354
355     TextCheckerState m_textCheckerState;
356
357     WebIconDatabaseProxy* m_iconDatabaseProxy;
358
359     void ensureNetworkProcessConnection();
360     RefPtr<NetworkProcessConnection> m_networkProcessConnection;
361     bool m_usesNetworkProcess;
362     WebResourceLoadScheduler* m_webResourceLoadScheduler;
363     HashSet<String> m_dnsPrefetchedHosts;
364     WebCore::HysteresisActivity m_dnsPrefetchHystereris;
365
366 #if ENABLE(DATABASE_PROCESS)
367     void ensureWebToDatabaseProcessConnection();
368     RefPtr<WebToDatabaseProcessConnection> m_webToDatabaseProcessConnection;
369 #endif
370
371 #if ENABLE(NETSCAPE_PLUGIN_API)
372     RefPtr<PluginProcessConnectionManager> m_pluginProcessConnectionManager;
373 #endif
374
375 #if ENABLE(SERVICE_CONTROLS)
376     bool m_hasImageServices;
377     bool m_hasSelectionServices;
378     bool m_hasRichContentServices;
379 #endif
380
381     HashSet<uint64_t> m_pagesInWindows;
382     WebCore::Timer m_nonVisibleProcessCleanupTimer;
383
384 #if PLATFORM(IOS)
385     WebSQLiteDatabaseTracker m_webSQLiteDatabaseTracker;
386 #endif
387
388     ShouldAcknowledgeWhenReadyToSuspend m_shouldAcknowledgeWhenReadyToSuspend;
389 };
390
391 } // namespace WebKit
392
393 #endif // WebProcess_h