Move URL from WebCore to WTF
[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/MessagePortChannelProvider.h>
42 #include <WebCore/MessagePortIdentifier.h>
43 #include <WebCore/Process.h>
44 #include <WebCore/SharedStringHash.h>
45 #include <memory>
46 #include <pal/SessionID.h>
47 #include <wtf/Forward.h>
48 #include <wtf/HashMap.h>
49 #include <wtf/HashSet.h>
50 #include <wtf/RefCounted.h>
51 #include <wtf/RefPtr.h>
52
53 #if PLATFORM(MAC) && ENABLE(WEBPROCESS_WINDOWSERVER_BLOCKING)
54 #include "DisplayLink.h"
55 #endif
56
57 namespace API {
58 class Navigation;
59 class PageConfiguration;
60 }
61
62 namespace WebCore {
63 class ResourceRequest;
64 struct PluginInfo;
65 struct SecurityOriginData;
66 }
67
68 namespace WebKit {
69
70 class NetworkProcessProxy;
71 class ObjCObjectGraph;
72 class PageClient;
73 class UserMediaCaptureManagerProxy;
74 class VisitedLinkStore;
75 class WebBackForwardListItem;
76 class WebFrameProxy;
77 class WebPageGroup;
78 class WebPageProxy;
79 class WebProcessPool;
80 class WebUserContentControllerProxy;
81 class WebsiteDataStore;
82 enum class WebsiteDataType;
83 struct WebNavigationDataStore;
84 struct WebPageCreationParameters;
85 struct WebsiteData;
86
87 #if PLATFORM(IOS_FAMILY)
88 enum ForegroundWebProcessCounterType { };
89 typedef RefCounter<ForegroundWebProcessCounterType> ForegroundWebProcessCounter;
90 typedef ForegroundWebProcessCounter::Token ForegroundWebProcessToken;
91 enum BackgroundWebProcessCounterType { };
92 typedef RefCounter<BackgroundWebProcessCounterType> BackgroundWebProcessCounter;
93 typedef BackgroundWebProcessCounter::Token BackgroundWebProcessToken;
94 #endif
95
96 class WebProcessProxy : public ChildProcessProxy, public ResponsivenessTimer::Client, public ThreadSafeRefCounted<WebProcessProxy>, public CanMakeWeakPtr<WebProcessProxy>, private ProcessThrottlerClient {
97 public:
98     typedef HashMap<uint64_t, RefPtr<WebFrameProxy>> WebFrameProxyMap;
99     typedef HashMap<uint64_t, WebPageProxy*> WebPageProxyMap;
100     typedef HashMap<uint64_t, RefPtr<API::UserInitiatedAction>> UserInitiatedActionMap;
101
102     enum class IsPrewarmed {
103         No,
104         Yes
105     };
106
107     static Ref<WebProcessProxy> create(WebProcessPool&, WebsiteDataStore&, IsPrewarmed);
108     ~WebProcessProxy();
109
110     WebConnection* webConnection() const { return m_webConnection.get(); }
111
112     WebProcessPool& processPool() const { ASSERT(m_processPool); return *m_processPool.get(); }
113
114     // FIXME: WebsiteDataStores should be made per-WebPageProxy throughout WebKit2
115     WebsiteDataStore& websiteDataStore() const { return m_websiteDataStore.get(); }
116
117     static WebProcessProxy* processForIdentifier(WebCore::ProcessIdentifier);
118     static WebPageProxy* webPage(uint64_t pageID);
119     Ref<WebPageProxy> createWebPage(PageClient&, Ref<API::PageConfiguration>&&);
120     void addExistingWebPage(WebPageProxy&, uint64_t pageID);
121     void removeWebPage(WebPageProxy&, uint64_t pageID);
122
123     typename WebPageProxyMap::ValuesConstIteratorRange pages() const { return m_pageMap.values(); }
124     unsigned pageCount() const { return m_pageMap.size(); }
125     unsigned visiblePageCount() const { return m_visiblePageCounter.value(); }
126
127     void activePagesDomainsForTesting(CompletionHandler<void(Vector<String>&&)>&&); // This is what is reported to ActivityMonitor.
128
129     virtual bool isServiceWorkerProcess() const { return false; }
130
131     void addVisitedLinkStore(VisitedLinkStore&);
132     void addWebUserContentControllerProxy(WebUserContentControllerProxy&, WebPageCreationParameters&);
133     void didDestroyVisitedLinkStore(VisitedLinkStore&);
134     void didDestroyWebUserContentControllerProxy(WebUserContentControllerProxy&);
135
136     RefPtr<API::UserInitiatedAction> userInitiatedActivity(uint64_t);
137
138     ResponsivenessTimer& responsivenessTimer() { return m_responsivenessTimer; }
139     bool isResponsive() const;
140
141     WebFrameProxy* webFrame(uint64_t) const;
142     bool canCreateFrame(uint64_t frameID) const;
143     void frameCreated(uint64_t, WebFrameProxy&);
144     void disconnectFramesFromPage(WebPageProxy*); // Including main frame.
145     size_t frameCountInPage(WebPageProxy*) const; // Including main frame.
146
147     VisibleWebPageToken visiblePageToken() const;
148
149     void updateTextCheckerState();
150
151     void willAcquireUniversalFileReadSandboxExtension() { m_mayHaveUniversalFileReadSandboxExtension = true; }
152     void assumeReadAccessToBaseURL(const String&);
153     bool hasAssumedReadAccessToURL(const URL&) const;
154
155     bool checkURLReceivedFromWebProcess(const String&);
156     bool checkURLReceivedFromWebProcess(const URL&);
157
158     static bool fullKeyboardAccessEnabled();
159
160     void didSaveToPageCache();
161     void releasePageCache();
162
163     void fetchWebsiteData(PAL::SessionID, OptionSet<WebsiteDataType>, CompletionHandler<void(WebsiteData)>&&);
164     void deleteWebsiteData(PAL::SessionID, OptionSet<WebsiteDataType>, WallTime modifiedSince, CompletionHandler<void()>&&);
165     void deleteWebsiteDataForOrigins(PAL::SessionID, OptionSet<WebsiteDataType>, const Vector<WebCore::SecurityOriginData>&, CompletionHandler<void()>&&);
166     static void deleteWebsiteDataForTopPrivatelyControlledDomainsInAllPersistentDataStores(OptionSet<WebsiteDataType>, Vector<String>&& topPrivatelyControlledDomains, bool shouldNotifyPages, CompletionHandler<void (const HashSet<String>&)>&&);
167     static void topPrivatelyControlledDomainsWithWebsiteData(OptionSet<WebsiteDataType> dataTypes, bool shouldNotifyPage, CompletionHandler<void(HashSet<String>&&)>&&);
168     static void notifyPageStatisticsAndDataRecordsProcessed();
169     static void notifyPageStatisticsTelemetryFinished(API::Object* messageBody);
170
171     void enableSuddenTermination();
172     void disableSuddenTermination();
173     bool isSuddenTerminationEnabled() { return !m_numberOfTimesSuddenTerminationWasDisabled; }
174
175     void requestTermination(ProcessTerminationReason);
176
177     void stopResponsivenessTimer();
178
179     RefPtr<API::Object> transformHandlesToObjects(API::Object*);
180     static RefPtr<API::Object> transformObjectsToHandles(API::Object*);
181
182 #if PLATFORM(COCOA)
183     RefPtr<ObjCObjectGraph> transformHandlesToObjects(ObjCObjectGraph&);
184     static RefPtr<ObjCObjectGraph> transformObjectsToHandles(ObjCObjectGraph&);
185 #endif
186
187     void windowServerConnectionStateChanged();
188
189     void processReadyToSuspend();
190     void didCancelProcessSuspension();
191
192     void setIsHoldingLockedFiles(bool);
193
194     ProcessThrottler& throttler() { return m_throttler; }
195
196     void isResponsive(WTF::Function<void(bool isWebProcessResponsive)>&&);
197     void didReceiveMainThreadPing();
198     void didReceiveBackgroundResponsivenessPing();
199
200     void memoryPressureStatusChanged(bool isUnderMemoryPressure) { m_isUnderMemoryPressure = isUnderMemoryPressure; }
201     bool isUnderMemoryPressure() const { return m_isUnderMemoryPressure; }
202     void didExceedInactiveMemoryLimitWhileActive();
203
204     void processTerminated();
205
206     void didExceedCPULimit();
207     void didExceedActiveMemoryLimit();
208     void didExceedInactiveMemoryLimit();
209
210     void checkProcessLocalPortForActivity(const WebCore::MessagePortIdentifier&, CompletionHandler<void(WebCore::MessagePortChannelProvider::HasActivity)>&&);
211
212     void didCommitProvisionalLoad() { m_hasCommittedAnyProvisionalLoads = true; }
213     bool hasCommittedAnyProvisionalLoads() const { return m_hasCommittedAnyProvisionalLoads; }
214
215 #if PLATFORM(WATCHOS)
216     void takeBackgroundActivityTokenForFullscreenInput();
217     void releaseBackgroundActivityTokenForFullscreenInput();
218 #endif
219
220     bool isPrewarmed() const { return m_isPrewarmed; }
221     void markIsNoLongerInPrewarmedPool();
222
223 #if PLATFORM(COCOA)
224     Vector<String> mediaMIMETypes();
225     void cacheMediaMIMETypes(const Vector<String>&);
226 #endif
227
228 #if PLATFORM(MAC)
229     void requestHighPerformanceGPU();
230     void releaseHighPerformanceGPU();
231 #endif
232
233 #if PLATFORM(MAC) && ENABLE(WEBPROCESS_WINDOWSERVER_BLOCKING)
234     void startDisplayLink(unsigned observerID, uint32_t displayID);
235     void stopDisplayLink(unsigned observerID, uint32_t displayID);
236 #endif
237
238     // Called when the web process has crashed or we know that it will terminate soon.
239     // Will potentially cause the WebProcessProxy object to be freed.
240     void shutDown();
241     void maybeShutDown();
242
243 protected:
244     static uint64_t generatePageID();
245     WebProcessProxy(WebProcessPool&, WebsiteDataStore&, IsPrewarmed);
246
247     // ChildProcessProxy
248     void getLaunchOptions(ProcessLauncher::LaunchOptions&) override;
249     void platformGetLaunchOptions(ProcessLauncher::LaunchOptions&) override;
250     void connectionWillOpen(IPC::Connection&) override;
251     void processWillShutDown(IPC::Connection&) override;
252
253     // ProcessLauncher::Client
254     void didFinishLaunching(ProcessLauncher*, IPC::Connection::Identifier) override;
255
256 #if PLATFORM(COCOA)
257     void cacheMediaMIMETypesInternal(const Vector<String>&);
258 #endif
259
260     bool isJITEnabled() const final;
261     
262 private:
263     // IPC message handlers.
264     void updateBackForwardItem(const BackForwardListItemState&);
265     void didDestroyFrame(uint64_t);
266     void didDestroyUserGestureToken(uint64_t);
267
268     void shouldTerminate(bool& shouldTerminate);
269
270     void createNewMessagePortChannel(const WebCore::MessagePortIdentifier& port1, const WebCore::MessagePortIdentifier& port2);
271     void entangleLocalPortInThisProcessToRemote(const WebCore::MessagePortIdentifier& local, const WebCore::MessagePortIdentifier& remote);
272     void messagePortDisentangled(const WebCore::MessagePortIdentifier&);
273     void messagePortClosed(const WebCore::MessagePortIdentifier&);
274     void takeAllMessagesForPort(const WebCore::MessagePortIdentifier&, uint64_t messagesCallbackIdentifier);
275     void postMessageToRemote(WebCore::MessageWithMessagePorts&&, const WebCore::MessagePortIdentifier&);
276     void checkRemotePortForActivity(const WebCore::MessagePortIdentifier, uint64_t callbackIdentifier);
277     void didDeliverMessagePortMessages(uint64_t messageBatchIdentifier);
278     void didCheckProcessLocalPortForActivity(uint64_t callbackIdentifier, bool isLocallyReachable);
279
280     // Plugins
281 #if ENABLE(NETSCAPE_PLUGIN_API)
282     void getPlugins(bool refresh, Vector<WebCore::PluginInfo>& plugins, Vector<WebCore::PluginInfo>& applicationPlugins, std::optional<Vector<WebCore::SupportedPluginIdentifier>>&);
283 #endif // ENABLE(NETSCAPE_PLUGIN_API)
284 #if ENABLE(NETSCAPE_PLUGIN_API)
285     void getPluginProcessConnection(uint64_t pluginProcessToken, Messages::WebProcessProxy::GetPluginProcessConnection::DelayedReply&&);
286 #endif
287     void getNetworkProcessConnection(Messages::WebProcessProxy::GetNetworkProcessConnection::DelayedReply&&);
288
289     bool platformIsBeingDebugged() const;
290     bool shouldAllowNonValidInjectedCode() const;
291
292     static const HashSet<String>& platformPathsWithAssumedReadAccess();
293
294     void updateBackgroundResponsivenessTimer();
295
296     void processDidTerminateOrFailedToLaunch();
297
298     // IPC::Connection::Client
299     friend class WebConnectionToWebProcess;
300     void didReceiveMessage(IPC::Connection&, IPC::Decoder&) override;
301     void didReceiveSyncMessage(IPC::Connection&, IPC::Decoder&, std::unique_ptr<IPC::Encoder>&) override;
302     void didClose(IPC::Connection&) override;
303     void didReceiveInvalidMessage(IPC::Connection&, IPC::StringReference messageReceiverName, IPC::StringReference messageName) override;
304
305     // ResponsivenessTimer::Client
306     void didBecomeUnresponsive() override;
307     void didBecomeResponsive() override;
308     void willChangeIsResponsive() override;
309     void didChangeIsResponsive() override;
310     bool mayBecomeUnresponsive() override;
311
312     // ProcessThrottlerClient
313     void sendProcessWillSuspendImminently() override;
314     void sendPrepareToSuspend() override;
315     void sendCancelPrepareToSuspend() override;
316     void sendProcessDidResume() override;
317     void didSetAssertionState(AssertionState) override;
318
319     // Implemented in generated WebProcessProxyMessageReceiver.cpp
320     void didReceiveWebProcessProxyMessage(IPC::Connection&, IPC::Decoder&);
321     void didReceiveSyncWebProcessProxyMessage(IPC::Connection&, IPC::Decoder&, std::unique_ptr<IPC::Encoder>&);
322
323     bool canTerminateChildProcess();
324
325     void didCollectPrewarmInformation(const String& domain, const WebCore::PrewarmInformation&);
326
327     void logDiagnosticMessageForResourceLimitTermination(const String& limitKey);
328
329     enum class IsWeak { No, Yes };
330     template<typename T> class WeakOrStrongPtr {
331     public:
332         WeakOrStrongPtr(T& object, IsWeak isWeak)
333             : m_isWeak(isWeak)
334             , m_weakObject(makeWeakPtr(object))
335         {
336             updateStrongReference();
337         }
338
339         void setIsWeak(IsWeak isWeak)
340         {
341             m_isWeak = isWeak;
342             updateStrongReference();
343         }
344
345         T* get() const { return m_weakObject.get(); }
346         T* operator->() const { return m_weakObject.get(); }
347         T& operator*() const { return *m_weakObject; }
348         explicit operator bool() const { return !!m_weakObject; }
349
350     private:
351         void updateStrongReference()
352         {
353             m_strongObject = m_isWeak == IsWeak::Yes ? nullptr : m_weakObject.get();
354         }
355
356         IsWeak m_isWeak;
357         WeakPtr<T> m_weakObject;
358         RefPtr<T> m_strongObject;
359     };
360
361     ResponsivenessTimer m_responsivenessTimer;
362     BackgroundProcessResponsivenessTimer m_backgroundResponsivenessTimer;
363     
364     RefPtr<WebConnectionToWebProcess> m_webConnection;
365     WeakOrStrongPtr<WebProcessPool> m_processPool; // Pre-warmed processes do not hold a strong reference to their pool.
366
367     bool m_mayHaveUniversalFileReadSandboxExtension; // True if a read extension for "/" was ever granted - we don't track whether WebProcess still has it.
368     HashSet<String> m_localPathsWithAssumedReadAccess;
369
370     WebPageProxyMap m_pageMap;
371     WebFrameProxyMap m_frameMap;
372     UserInitiatedActionMap m_userInitiatedActionMap;
373
374     HashSet<VisitedLinkStore*> m_visitedLinkStores;
375     HashSet<WebUserContentControllerProxy*> m_webUserContentControllerProxies;
376
377     int m_numberOfTimesSuddenTerminationWasDisabled;
378     ProcessThrottler m_throttler;
379     ProcessThrottler::BackgroundActivityToken m_tokenForHoldingLockedFiles;
380 #if PLATFORM(IOS_FAMILY)
381     ForegroundWebProcessToken m_foregroundToken;
382     BackgroundWebProcessToken m_backgroundToken;
383 #endif
384
385     HashMap<String, uint64_t> m_pageURLRetainCountMap;
386
387     enum class NoOrMaybe { No, Maybe } m_isResponsive;
388     Vector<WTF::Function<void(bool webProcessIsResponsive)>> m_isResponsiveCallbacks;
389
390     VisibleWebPageCounter m_visiblePageCounter;
391
392     // FIXME: WebsiteDataStores should be made per-WebPageProxy throughout WebKit2. Get rid of this member.
393     Ref<WebsiteDataStore> m_websiteDataStore;
394
395     bool m_isUnderMemoryPressure { false };
396
397 #if PLATFORM(COCOA) && ENABLE(MEDIA_STREAM)
398     std::unique_ptr<UserMediaCaptureManagerProxy> m_userMediaCaptureManagerProxy;
399 #endif
400
401     HashSet<WebCore::MessagePortIdentifier> m_processEntangledPorts;
402     HashMap<uint64_t, Function<void()>> m_messageBatchDeliveryCompletionHandlers;
403     HashMap<uint64_t, CompletionHandler<void(WebCore::MessagePortChannelProvider::HasActivity)>> m_localPortActivityCompletionHandlers;
404
405     bool m_hasCommittedAnyProvisionalLoads { false };
406     bool m_isPrewarmed;
407
408 #if PLATFORM(WATCHOS)
409     ProcessThrottler::BackgroundActivityToken m_backgroundActivityTokenForFullscreenFormControls;
410 #endif
411
412 #if PLATFORM(MAC) && ENABLE(WEBPROCESS_WINDOWSERVER_BLOCKING)
413     Vector<std::unique_ptr<DisplayLink>> m_displayLinks;
414 #endif
415 };
416
417 } // namespace WebKit