2200ca4ee9ed07c3f2e816a6cd6e4aaca0728b14
[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 releasePageCache();
272
273     void fetchWebsiteData(WebCore::SessionID, uint64_t websiteDataTypes, uint64_t callbackID);
274     void deleteWebsiteData(WebCore::SessionID, uint64_t websiteDataTypes, std::chrono::system_clock::time_point modifiedSince, uint64_t callbackID);
275     void deleteWebsiteDataForOrigins(WebCore::SessionID, uint64_t websiteDataTypes, const Vector<WebCore::SecurityOriginData>& origins, uint64_t callbackID);
276
277 #if USE(SOUP)
278     void setIgnoreTLSErrors(bool);
279 #endif
280
281     void setMemoryCacheDisabled(bool);
282
283 #if ENABLE(SERVICE_CONTROLS)
284     void setEnabledServices(bool hasImageServices, bool hasSelectionServices, bool hasRichContentServices);
285 #endif
286
287     void handleInjectedBundleMessage(const String& messageName, const UserData& messageBody);
288     void setInjectedBundleParameter(const String& key, const IPC::DataReference&);
289     void setInjectedBundleParameters(const IPC::DataReference&);
290
291     enum class ShouldAcknowledgeWhenReadyToSuspend { No, Yes };
292     void actualPrepareToSuspend(ShouldAcknowledgeWhenReadyToSuspend);
293
294     // ChildProcess
295     virtual void initializeProcess(const ChildProcessInitializationParameters&) override;
296     virtual void initializeProcessName(const ChildProcessInitializationParameters&) override;
297     virtual void initializeSandbox(const ChildProcessInitializationParameters&, SandboxInitializationParameters&) override;
298     virtual void initializeConnection(IPC::Connection*) override;
299     virtual bool shouldTerminate() override;
300     virtual void terminate() override;
301
302 #if USE(APPKIT)
303     virtual void stopRunLoop() override;
304 #endif
305
306     void platformInitializeProcess(const ChildProcessInitializationParameters&);
307
308     // IPC::Connection::Client
309     friend class WebConnectionToUIProcess;
310     virtual void didReceiveMessage(IPC::Connection&, IPC::MessageDecoder&) override;
311     virtual void didReceiveSyncMessage(IPC::Connection&, IPC::MessageDecoder&, std::unique_ptr<IPC::MessageEncoder>&) override;
312     virtual void didClose(IPC::Connection&) override;
313     virtual void didReceiveInvalidMessage(IPC::Connection&, IPC::StringReference messageReceiverName, IPC::StringReference messageName) override;
314     virtual IPC::ProcessType localProcessType() override { return IPC::ProcessType::Web; }
315     virtual IPC::ProcessType remoteProcessType() override { return IPC::ProcessType::UI; }
316
317     // Implemented in generated WebProcessMessageReceiver.cpp
318     void didReceiveWebProcessMessage(IPC::Connection&, IPC::MessageDecoder&);
319     void didReceiveSyncWebProcessMessage(IPC::Connection&, IPC::MessageDecoder&, std::unique_ptr<IPC::MessageEncoder>&);
320
321     RefPtr<WebConnectionToUIProcess> m_webConnection;
322
323     HashMap<uint64_t, RefPtr<WebPage>> m_pageMap;
324     HashMap<uint64_t, RefPtr<WebPageGroupProxy>> m_pageGroupMap;
325     RefPtr<InjectedBundle> m_injectedBundle;
326
327     RefPtr<EventDispatcher> m_eventDispatcher;
328 #if PLATFORM(IOS)
329     RefPtr<ViewUpdateDispatcher> m_viewUpdateDispatcher;
330 #endif
331     WebCore::Timer m_processSuspensionCleanupTimer;
332
333     bool m_inDidClose;
334
335     HashMap<WebCore::SessionID, HashMap<unsigned, double>> m_plugInAutoStartOriginHashes;
336     HashSet<String> m_plugInAutoStartOrigins;
337
338     bool m_hasSetCacheModel;
339     CacheModel m_cacheModel;
340
341 #if PLATFORM(COCOA)
342     WebCore::MachSendRight m_compositingRenderServerPort;
343     pid_t m_presenterApplicationPid;
344 #endif
345
346     bool m_fullKeyboardAccessEnabled;
347
348     HashMap<uint64_t, WebFrame*> m_frameMap;
349
350     typedef HashMap<const char*, std::unique_ptr<WebProcessSupplement>, PtrHash<const char*>> WebProcessSupplementMap;
351     WebProcessSupplementMap m_supplements;
352
353     TextCheckerState m_textCheckerState;
354
355     WebIconDatabaseProxy* m_iconDatabaseProxy;
356
357     void ensureNetworkProcessConnection();
358     RefPtr<NetworkProcessConnection> m_networkProcessConnection;
359     bool m_usesNetworkProcess;
360     WebResourceLoadScheduler* m_webResourceLoadScheduler;
361     HashSet<String> m_dnsPrefetchedHosts;
362     WebCore::HysteresisActivity m_dnsPrefetchHystereris;
363
364 #if ENABLE(DATABASE_PROCESS)
365     void ensureWebToDatabaseProcessConnection();
366     RefPtr<WebToDatabaseProcessConnection> m_webToDatabaseProcessConnection;
367 #endif
368
369 #if ENABLE(NETSCAPE_PLUGIN_API)
370     RefPtr<PluginProcessConnectionManager> m_pluginProcessConnectionManager;
371 #endif
372
373 #if ENABLE(SERVICE_CONTROLS)
374     bool m_hasImageServices;
375     bool m_hasSelectionServices;
376     bool m_hasRichContentServices;
377 #endif
378
379     HashSet<uint64_t> m_pagesInWindows;
380     WebCore::Timer m_nonVisibleProcessCleanupTimer;
381
382 #if PLATFORM(IOS)
383     WebSQLiteDatabaseTracker m_webSQLiteDatabaseTracker;
384 #endif
385
386     ShouldAcknowledgeWhenReadyToSuspend m_shouldAcknowledgeWhenReadyToSuspend;
387 };
388
389 } // namespace WebKit
390
391 #endif // WebProcess_h