Remove DATABASE_PROCESS build flag.
[WebKit-https.git] / Source / WebKit / UIProcess / WebProcessProxy.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 "APIUserInitiatedAction.h"
29 #include "BackgroundProcessResponsivenessTimer.h"
30 #include "ChildProcessProxy.h"
31 #include "MessageReceiverMap.h"
32 #include "PluginInfoStore.h"
33 #include "ProcessLauncher.h"
34 #include "ProcessTerminationReason.h"
35 #include "ProcessThrottler.h"
36 #include "ProcessThrottlerClient.h"
37 #include "ResponsivenessTimer.h"
38 #include "VisibleWebPageCounter.h"
39 #include "WebConnectionToWebProcess.h"
40 #include "WebProcessProxyMessages.h"
41 #include <WebCore/LinkHash.h>
42 #include <WebCore/SessionID.h>
43 #include <memory>
44 #include <wtf/Forward.h>
45 #include <wtf/HashMap.h>
46 #include <wtf/RefCounted.h>
47 #include <wtf/RefPtr.h>
48
49 namespace API {
50 class PageConfiguration;
51 }
52
53 namespace WebCore {
54 class ResourceRequest;
55 class URL;
56 struct PluginInfo;
57 struct SecurityOriginData;
58 }
59
60 namespace WebKit {
61
62 class NetworkProcessProxy;
63 class ObjCObjectGraph;
64 class PageClient;
65 class UserMediaCaptureManagerProxy;
66 class VisitedLinkStore;
67 class WebBackForwardListItem;
68 class WebFrameProxy;
69 class WebPageGroup;
70 class WebPageProxy;
71 class WebProcessPool;
72 class WebUserContentControllerProxy;
73 class WebsiteDataStore;
74 enum class WebsiteDataType;
75 struct WebNavigationDataStore;
76 struct WebPageCreationParameters;
77 struct WebsiteData;
78
79 class WebProcessProxy : public ChildProcessProxy, public ResponsivenessTimer::Client, private ProcessThrottlerClient {
80 public:
81     typedef HashMap<uint64_t, RefPtr<WebBackForwardListItem>> WebBackForwardListItemMap;
82     typedef HashMap<uint64_t, RefPtr<WebFrameProxy>> WebFrameProxyMap;
83     typedef HashMap<uint64_t, WebPageProxy*> WebPageProxyMap;
84     typedef HashMap<uint64_t, RefPtr<API::UserInitiatedAction>> UserInitiatedActionMap;
85
86     static Ref<WebProcessProxy> create(WebProcessPool&, WebsiteDataStore&);
87     ~WebProcessProxy();
88
89     WebConnection* webConnection() const { return m_webConnection.get(); }
90
91     WebProcessPool& processPool() { return m_processPool; }
92
93     // FIXME: WebsiteDataStores should be made per-WebPageProxy throughout WebKit2
94     WebsiteDataStore& websiteDataStore() const { return m_websiteDataStore.get(); }
95
96     static WebPageProxy* webPage(uint64_t pageID);
97     Ref<WebPageProxy> createWebPage(PageClient&, Ref<API::PageConfiguration>&&);
98     void addExistingWebPage(WebPageProxy&, uint64_t pageID);
99     void removeWebPage(WebPageProxy&, uint64_t pageID);
100
101     WTF::IteratorRange<WebPageProxyMap::const_iterator::Values> pages() const { return m_pageMap.values(); }
102     unsigned pageCount() const { return m_pageMap.size(); }
103     unsigned visiblePageCount() const { return m_visiblePageCounter.value(); }
104
105     void addVisitedLinkStore(VisitedLinkStore&);
106     void addWebUserContentControllerProxy(WebUserContentControllerProxy&, WebPageCreationParameters&);
107     void didDestroyVisitedLinkStore(VisitedLinkStore&);
108     void didDestroyWebUserContentControllerProxy(WebUserContentControllerProxy&);
109
110     WebBackForwardListItem* webBackForwardItem(uint64_t itemID) const;
111     RefPtr<API::UserInitiatedAction> userInitiatedActivity(uint64_t);
112
113     ResponsivenessTimer& responsivenessTimer() { return m_responsivenessTimer; }
114     bool isResponsive() const;
115
116     WebFrameProxy* webFrame(uint64_t) const;
117     bool canCreateFrame(uint64_t frameID) const;
118     void frameCreated(uint64_t, WebFrameProxy*);
119     void disconnectFramesFromPage(WebPageProxy*); // Including main frame.
120     size_t frameCountInPage(WebPageProxy*) const; // Including main frame.
121
122     VisibleWebPageToken visiblePageToken() const;
123
124     void updateTextCheckerState();
125
126     void registerNewWebBackForwardListItem(WebBackForwardListItem*);
127     void removeBackForwardItem(uint64_t);
128
129     void willAcquireUniversalFileReadSandboxExtension() { m_mayHaveUniversalFileReadSandboxExtension = true; }
130     void assumeReadAccessToBaseURL(const String&);
131     bool hasAssumedReadAccessToURL(const WebCore::URL&) const;
132
133     bool checkURLReceivedFromWebProcess(const String&);
134     bool checkURLReceivedFromWebProcess(const WebCore::URL&);
135
136     static bool fullKeyboardAccessEnabled();
137
138     void didSaveToPageCache();
139     void releasePageCache();
140
141     void fetchWebsiteData(WebCore::SessionID, OptionSet<WebsiteDataType>, Function<void(WebsiteData)>&& completionHandler);
142     void deleteWebsiteData(WebCore::SessionID, OptionSet<WebsiteDataType>, std::chrono::system_clock::time_point modifiedSince, Function<void()>&& completionHandler);
143     void deleteWebsiteDataForOrigins(WebCore::SessionID, OptionSet<WebsiteDataType>, const Vector<WebCore::SecurityOriginData>&, Function<void()>&& completionHandler);
144     static void deleteWebsiteDataForTopPrivatelyControlledDomainsInAllPersistentDataStores(OptionSet<WebsiteDataType>, Vector<String>&& topPrivatelyControlledDomains, bool shouldNotifyPages, Function<void (const HashSet<String>&)>&& completionHandler);
145     static void topPrivatelyControlledDomainsWithWebsiteData(OptionSet<WebsiteDataType> dataTypes, bool shouldNotifyPage, Function<void(HashSet<String>&&)>&& completionHandler);
146     static void notifyPageStatisticsAndDataRecordsProcessed();
147     static void notifyPageStatisticsTelemetryFinished(API::Object* messageBody);
148
149     void enableSuddenTermination();
150     void disableSuddenTermination();
151     bool isSuddenTerminationEnabled() { return !m_numberOfTimesSuddenTerminationWasDisabled; }
152
153     void requestTermination(ProcessTerminationReason);
154
155     void stopResponsivenessTimer();
156
157     RefPtr<API::Object> transformHandlesToObjects(API::Object*);
158     static RefPtr<API::Object> transformObjectsToHandles(API::Object*);
159
160 #if PLATFORM(COCOA)
161     RefPtr<ObjCObjectGraph> transformHandlesToObjects(ObjCObjectGraph&);
162     static RefPtr<ObjCObjectGraph> transformObjectsToHandles(ObjCObjectGraph&);
163 #endif
164
165     void windowServerConnectionStateChanged();
166
167     void processReadyToSuspend();
168     void didCancelProcessSuspension();
169
170     void setIsHoldingLockedFiles(bool);
171
172     ProcessThrottler& throttler() { return m_throttler; }
173
174     void reinstateNetworkProcessAssertionState(NetworkProcessProxy&);
175
176     void isResponsive(WTF::Function<void(bool isWebProcessResponsive)>&&);
177     void didReceiveMainThreadPing();
178     void didReceiveBackgroundResponsivenessPing();
179
180     void memoryPressureStatusChanged(bool isUnderMemoryPressure) { m_isUnderMemoryPressure = isUnderMemoryPressure; }
181     bool isUnderMemoryPressure() const { return m_isUnderMemoryPressure; }
182     void didExceedInactiveMemoryLimitWhileActive();
183
184     void processTerminated();
185
186     void didExceedCPULimit();
187     void didExceedActiveMemoryLimit();
188     void didExceedInactiveMemoryLimit();
189
190 private:
191     explicit WebProcessProxy(WebProcessPool&, WebsiteDataStore&);
192
193     // From ChildProcessProxy
194     void getLaunchOptions(ProcessLauncher::LaunchOptions&) override;
195     void connectionWillOpen(IPC::Connection&) override;
196     void processWillShutDown(IPC::Connection&) override;
197
198     // Called when the web process has crashed or we know that it will terminate soon.
199     // Will potentially cause the WebProcessProxy object to be freed.
200     void shutDown();
201
202     // IPC message handlers.
203     void addBackForwardItem(uint64_t itemID, uint64_t pageID, const PageState&);
204     void didDestroyFrame(uint64_t);
205     void didDestroyUserGestureToken(uint64_t);
206
207     void shouldTerminate(bool& shouldTerminate);
208
209     // Plugins
210 #if ENABLE(NETSCAPE_PLUGIN_API)
211     void getPlugins(bool refresh, Vector<WebCore::PluginInfo>& plugins, Vector<WebCore::PluginInfo>& applicationPlugins);
212 #endif // ENABLE(NETSCAPE_PLUGIN_API)
213 #if ENABLE(NETSCAPE_PLUGIN_API)
214     void getPluginProcessConnection(uint64_t pluginProcessToken, Ref<Messages::WebProcessProxy::GetPluginProcessConnection::DelayedReply>&&);
215 #endif
216     void getNetworkProcessConnection(Ref<Messages::WebProcessProxy::GetNetworkProcessConnection::DelayedReply>&&);
217     void getDatabaseProcessConnection(Ref<Messages::WebProcessProxy::GetDatabaseProcessConnection::DelayedReply>&&);
218
219     bool platformIsBeingDebugged() const;
220
221     static const HashSet<String>& platformPathsWithAssumedReadAccess();
222
223     void updateBackgroundResponsivenessTimer();
224
225     // IPC::Connection::Client
226     friend class WebConnectionToWebProcess;
227     void didReceiveMessage(IPC::Connection&, IPC::Decoder&) override;
228     void didReceiveSyncMessage(IPC::Connection&, IPC::Decoder&, std::unique_ptr<IPC::Encoder>&) override;
229     void didClose(IPC::Connection&) override;
230     void didReceiveInvalidMessage(IPC::Connection&, IPC::StringReference messageReceiverName, IPC::StringReference messageName) override;
231
232     // ResponsivenessTimer::Client
233     void didBecomeUnresponsive() override;
234     void didBecomeResponsive() override;
235     void willChangeIsResponsive() override;
236     void didChangeIsResponsive() override;
237     bool mayBecomeUnresponsive() override;
238
239     // ProcessThrottlerClient
240     void sendProcessWillSuspendImminently() override;
241     void sendPrepareToSuspend() override;
242     void sendCancelPrepareToSuspend() override;
243     void sendProcessDidResume() override;
244     void didSetAssertionState(AssertionState) override;
245
246     // ProcessLauncher::Client
247     void didFinishLaunching(ProcessLauncher*, IPC::Connection::Identifier) override;
248
249     // Implemented in generated WebProcessProxyMessageReceiver.cpp
250     void didReceiveWebProcessProxyMessage(IPC::Connection&, IPC::Decoder&);
251     void didReceiveSyncWebProcessProxyMessage(IPC::Connection&, IPC::Decoder&, std::unique_ptr<IPC::Encoder>&);
252
253     bool canTerminateChildProcess();
254
255     void logDiagnosticMessageForResourceLimitTermination(const String& limitKey);
256
257     ResponsivenessTimer m_responsivenessTimer;
258     BackgroundProcessResponsivenessTimer m_backgroundResponsivenessTimer;
259     
260     RefPtr<WebConnectionToWebProcess> m_webConnection;
261     Ref<WebProcessPool> m_processPool;
262
263     bool m_mayHaveUniversalFileReadSandboxExtension; // True if a read extension for "/" was ever granted - we don't track whether WebProcess still has it.
264     HashSet<String> m_localPathsWithAssumedReadAccess;
265
266     WebPageProxyMap m_pageMap;
267     WebFrameProxyMap m_frameMap;
268     WebBackForwardListItemMap m_backForwardListItemMap;
269     UserInitiatedActionMap m_userInitiatedActionMap;
270
271     HashSet<VisitedLinkStore*> m_visitedLinkStores;
272     HashSet<WebUserContentControllerProxy*> m_webUserContentControllerProxies;
273
274     int m_numberOfTimesSuddenTerminationWasDisabled;
275     ProcessThrottler m_throttler;
276     ProcessThrottler::BackgroundActivityToken m_tokenForHoldingLockedFiles;
277 #if PLATFORM(IOS)
278     ProcessThrottler::ForegroundActivityToken m_foregroundTokenForNetworkProcess;
279     ProcessThrottler::BackgroundActivityToken m_backgroundTokenForNetworkProcess;
280 #endif
281
282     HashMap<String, uint64_t> m_pageURLRetainCountMap;
283
284     enum class NoOrMaybe { No, Maybe } m_isResponsive;
285     Vector<WTF::Function<void(bool webProcessIsResponsive)>> m_isResponsiveCallbacks;
286
287     VisibleWebPageCounter m_visiblePageCounter;
288
289     // FIXME: WebsiteDataStores should be made per-WebPageProxy throughout WebKit2. Get rid of this member.
290     Ref<WebsiteDataStore> m_websiteDataStore;
291
292     bool m_isUnderMemoryPressure { false };
293
294 #if PLATFORM(COCOA) && ENABLE(MEDIA_STREAM)
295     std::unique_ptr<UserMediaCaptureManagerProxy> m_userMediaCaptureManagerProxy;
296 #endif
297 };
298
299 } // namespace WebKit