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