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