5ab438b711859b9ef83690dff1b78e6f66a70bea
[WebKit-https.git] / Source / WebKit2 / UIProcess / WebProcessProxy.h
1 /*
2  * Copyright (C) 2010, 2011 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 WebProcessProxy_h
27 #define WebProcessProxy_h
28
29 #include "APISession.h"
30 #include "ChildProcessProxy.h"
31 #include "CustomProtocolManagerProxy.h"
32 #include "MessageReceiverMap.h"
33 #include "PlatformProcessIdentifier.h"
34 #include "PluginInfoStore.h"
35 #include "ProcessLauncher.h"
36 #include "ProcessThrottlerClient.h"
37 #include "ResponsivenessTimer.h"
38 #include "WebConnectionToWebProcess.h"
39 #include "WebPageProxy.h"
40 #include "WebProcessProxyMessages.h"
41 #include "WebsiteDataTypes.h"
42 #include <WebCore/LinkHash.h>
43 #include <memory>
44 #include <wtf/Forward.h>
45 #include <wtf/HashMap.h>
46 #include <wtf/PassRefPtr.h>
47 #include <wtf/RefCounted.h>
48
49 #if PLATFORM(IOS)
50 #include "ProcessThrottler.h"
51 #endif
52
53 namespace WebCore {
54 class ResourceRequest;
55 class URL;
56 struct PluginInfo;
57 };
58
59 namespace WebKit {
60
61 class DownloadProxyMap;
62 class NetworkProcessProxy;
63 class WebBackForwardListItem;
64 class WebPageGroup;
65 class WebProcessPool;
66 struct WebNavigationDataStore;
67     
68 class WebProcessProxy : public ChildProcessProxy, ResponsivenessTimer::Client, private ProcessThrottlerClient {
69 public:
70     typedef HashMap<uint64_t, RefPtr<WebBackForwardListItem>> WebBackForwardListItemMap;
71     typedef HashMap<uint64_t, RefPtr<WebFrameProxy>> WebFrameProxyMap;
72     typedef HashMap<uint64_t, WebPageProxy*> WebPageProxyMap;
73
74     static Ref<WebProcessProxy> create(WebProcessPool&);
75     ~WebProcessProxy();
76
77     static WebProcessProxy* fromConnection(IPC::Connection* connection)
78     {
79         return static_cast<WebProcessProxy*>(ChildProcessProxy::fromConnection(connection));
80     }
81
82     WebConnection* webConnection() const { return m_webConnection.get(); }
83
84     WebProcessPool& processPool() { return m_processPool; }
85
86     static WebPageProxy* webPage(uint64_t pageID);
87     Ref<WebPageProxy> createWebPage(PageClient&, Ref<API::PageConfiguration>&&);
88     void addExistingWebPage(WebPageProxy*, uint64_t pageID);
89     void removeWebPage(uint64_t pageID);
90
91     WTF::IteratorRange<WebPageProxyMap::const_iterator::Values> pages() const { return m_pageMap.values(); }
92     unsigned pageCount() const { return m_pageMap.size(); }
93
94     void addVisitedLinkStore(VisitedLinkStore&);
95     void addWebUserContentControllerProxy(WebUserContentControllerProxy&);
96     void didDestroyVisitedLinkStore(VisitedLinkStore&);
97     void didDestroyWebUserContentControllerProxy(WebUserContentControllerProxy&);
98
99     WebBackForwardListItem* webBackForwardItem(uint64_t itemID) const;
100
101     ResponsivenessTimer& responsivenessTimer() { return m_responsivenessTimer; }
102
103     WebFrameProxy* webFrame(uint64_t) const;
104     bool canCreateFrame(uint64_t frameID) const;
105     void frameCreated(uint64_t, WebFrameProxy*);
106     void disconnectFramesFromPage(WebPageProxy*); // Including main frame.
107     size_t frameCountInPage(WebPageProxy*) const; // Including main frame.
108
109     void updateTextCheckerState();
110
111     void registerNewWebBackForwardListItem(WebBackForwardListItem*);
112     void removeBackForwardItem(uint64_t);
113
114     void willAcquireUniversalFileReadSandboxExtension() { m_mayHaveUniversalFileReadSandboxExtension = true; }
115     void assumeReadAccessToBaseURL(const String&);
116     bool hasAssumedReadAccessToURL(const WebCore::URL&) const;
117
118     bool checkURLReceivedFromWebProcess(const String&);
119     bool checkURLReceivedFromWebProcess(const WebCore::URL&);
120
121     static bool fullKeyboardAccessEnabled();
122
123     DownloadProxy* createDownloadProxy(const WebCore::ResourceRequest&);
124
125     void didSaveToPageCache();
126     void releasePageCache();
127
128     void fetchWebsiteData(WebCore::SessionID, WebsiteDataTypes, std::function<void (WebsiteData)> completionHandler);
129     void deleteWebsiteData(WebCore::SessionID, WebsiteDataTypes, std::chrono::system_clock::time_point modifiedSince, std::function<void ()> completionHandler);
130     void deleteWebsiteDataForOrigins(WebCore::SessionID, WebsiteDataTypes, const Vector<RefPtr<WebCore::SecurityOrigin>>& origins, std::function<void ()> completionHandler);
131
132     void enableSuddenTermination();
133     void disableSuddenTermination();
134     bool isSuddenTerminationEnabled() { return !m_numberOfTimesSuddenTerminationWasDisabled; }
135
136     void requestTermination();
137
138     RefPtr<API::Object> transformHandlesToObjects(API::Object*);
139     static RefPtr<API::Object> transformObjectsToHandles(API::Object*);
140
141 #if PLATFORM(COCOA)
142     RefPtr<ObjCObjectGraph> transformHandlesToObjects(ObjCObjectGraph&);
143     static RefPtr<ObjCObjectGraph> transformObjectsToHandles(ObjCObjectGraph&);
144 #endif
145
146     void windowServerConnectionStateChanged();
147
148     void processReadyToSuspend();
149     void didCancelProcessSuspension();
150
151     void setIsHoldingLockedFiles(bool);
152
153     ProcessThrottler& throttler() { return m_throttler; }
154
155     void reinstateNetworkProcessAssertionState(NetworkProcessProxy&);
156
157 private:
158     explicit WebProcessProxy(WebProcessPool&);
159
160     // From ChildProcessProxy
161     virtual void getLaunchOptions(ProcessLauncher::LaunchOptions&) override;
162     void platformGetLaunchOptions(ProcessLauncher::LaunchOptions&);
163     virtual void connectionWillOpen(IPC::Connection&) override;
164     virtual void processWillShutDown(IPC::Connection&) override;
165
166     // Called when the web process has crashed or we know that it will terminate soon.
167     // Will potentially cause the WebProcessProxy object to be freed.
168     void shutDown();
169
170     // IPC message handlers.
171     void addBackForwardItem(uint64_t itemID, uint64_t pageID, const PageState&);
172     void didDestroyFrame(uint64_t);
173     
174     void shouldTerminate(bool& shouldTerminate);
175
176     void didFetchWebsiteData(uint64_t callbackID, const WebsiteData&);
177     void didDeleteWebsiteData(uint64_t callbackID);
178     void didDeleteWebsiteDataForOrigins(uint64_t callbackID);
179
180     // Plugins
181 #if ENABLE(NETSCAPE_PLUGIN_API)
182     void getPlugins(bool refresh, Vector<WebCore::PluginInfo>& plugins, Vector<WebCore::PluginInfo>& applicationPlugins);
183 #endif // ENABLE(NETSCAPE_PLUGIN_API)
184 #if ENABLE(NETSCAPE_PLUGIN_API)
185     void getPluginProcessConnection(uint64_t pluginProcessToken, PassRefPtr<Messages::WebProcessProxy::GetPluginProcessConnection::DelayedReply>);
186 #endif
187     void getNetworkProcessConnection(PassRefPtr<Messages::WebProcessProxy::GetNetworkProcessConnection::DelayedReply>);
188 #if ENABLE(DATABASE_PROCESS)
189     void getDatabaseProcessConnection(PassRefPtr<Messages::WebProcessProxy::GetDatabaseProcessConnection::DelayedReply>);
190 #endif
191
192     void retainIconForPageURL(const String& pageURL);
193     void releaseIconForPageURL(const String& pageURL);
194     void releaseRemainingIconsForPageURLs();
195
196     // IPC::Connection::Client
197     friend class WebConnectionToWebProcess;
198     virtual void didReceiveMessage(IPC::Connection&, IPC::MessageDecoder&) override;
199     virtual void didReceiveSyncMessage(IPC::Connection&, IPC::MessageDecoder&, std::unique_ptr<IPC::MessageEncoder>&) override;
200     virtual void didClose(IPC::Connection&) override;
201     virtual void didReceiveInvalidMessage(IPC::Connection&, IPC::StringReference messageReceiverName, IPC::StringReference messageName) override;
202     virtual IPC::ProcessType localProcessType() override { return IPC::ProcessType::UI; }
203     virtual IPC::ProcessType remoteProcessType() override { return IPC::ProcessType::Web; }
204
205     // ResponsivenessTimer::Client
206     void didBecomeUnresponsive() override;
207     void didBecomeResponsive() override;
208     virtual void willChangeIsResponsive() override;
209     virtual void didChangeIsResponsive() override;
210
211     // ProcessThrottlerClient
212     void sendProcessWillSuspendImminently() override;
213     void sendPrepareToSuspend() override;
214     void sendCancelPrepareToSuspend() override;
215     void sendProcessDidResume() override;
216     void didSetAssertionState(AssertionState) override;
217
218     // ProcessLauncher::Client
219     virtual void didFinishLaunching(ProcessLauncher*, IPC::Connection::Identifier) override;
220
221     // Implemented in generated WebProcessProxyMessageReceiver.cpp
222     void didReceiveWebProcessProxyMessage(IPC::Connection&, IPC::MessageDecoder&);
223     void didReceiveSyncWebProcessProxyMessage(IPC::Connection&, IPC::MessageDecoder&, std::unique_ptr<IPC::MessageEncoder>&);
224
225     bool canTerminateChildProcess();
226
227     ResponsivenessTimer m_responsivenessTimer;
228     
229     RefPtr<WebConnectionToWebProcess> m_webConnection;
230     Ref<WebProcessPool> m_processPool;
231
232     bool m_mayHaveUniversalFileReadSandboxExtension; // True if a read extension for "/" was ever granted - we don't track whether WebProcess still has it.
233     HashSet<String> m_localPathsWithAssumedReadAccess;
234
235     WebPageProxyMap m_pageMap;
236     WebFrameProxyMap m_frameMap;
237     WebBackForwardListItemMap m_backForwardListItemMap;
238
239     HashSet<VisitedLinkStore*> m_visitedLinkStores;
240     HashSet<WebUserContentControllerProxy*> m_webUserContentControllerProxies;
241
242     std::unique_ptr<DownloadProxyMap> m_downloadProxyMap;
243     CustomProtocolManagerProxy m_customProtocolManagerProxy;
244
245     HashMap<uint64_t, std::function<void (WebsiteData)>> m_pendingFetchWebsiteDataCallbacks;
246     HashMap<uint64_t, std::function<void ()>> m_pendingDeleteWebsiteDataCallbacks;
247     HashMap<uint64_t, std::function<void ()>> m_pendingDeleteWebsiteDataForOriginsCallbacks;
248
249     int m_numberOfTimesSuddenTerminationWasDisabled;
250     ProcessThrottler m_throttler;
251     ProcessThrottler::BackgroundActivityToken m_tokenForHoldingLockedFiles;
252 #if PLATFORM(IOS)
253     ProcessThrottler::ForegroundActivityToken m_foregroundTokenForNetworkProcess;
254     ProcessThrottler::BackgroundActivityToken m_backgroundTokenForNetworkProcess;
255 #endif
256
257     HashMap<String, uint64_t> m_pageURLRetainCountMap;
258 };
259
260 } // namespace WebKit
261
262 #endif // WebProcessProxy_h