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