Track pages preventing suppression in WebProcessProxy using RefCounter
[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 "ResponsivenessTimer.h"
37 #include "WebConnectionToWebProcess.h"
38 #include "WebPageProxy.h"
39 #include "WebProcessProxyMessages.h"
40 #include <WebCore/LinkHash.h>
41 #include <memory>
42 #include <wtf/Forward.h>
43 #include <wtf/HashMap.h>
44 #include <wtf/PassRefPtr.h>
45 #include <wtf/RefCounted.h>
46 #include <wtf/RefCounter.h>
47
48 #if PLATFORM(IOS)
49 #include "ProcessThrottler.h"
50 #endif
51
52 namespace WebCore {
53 class ResourceRequest;
54 class URL;
55 struct PluginInfo;
56 };
57
58 namespace WebKit {
59
60 class DownloadProxyMap;
61 class WebBackForwardListItem;
62 class WebContext;
63 class WebPageGroup;
64 struct WebNavigationDataStore;
65     
66 class WebProcessProxy : public ChildProcessProxy, ResponsivenessTimer::Client {
67 public:
68     typedef HashMap<uint64_t, RefPtr<WebBackForwardListItem>> WebBackForwardListItemMap;
69     typedef HashMap<uint64_t, RefPtr<WebFrameProxy>> WebFrameProxyMap;
70     typedef HashMap<uint64_t, WebPageProxy*> WebPageProxyMap;
71
72     static PassRefPtr<WebProcessProxy> create(WebContext&);
73     ~WebProcessProxy();
74
75     static WebProcessProxy* fromConnection(IPC::Connection* connection)
76     {
77         return static_cast<WebProcessProxy*>(ChildProcessProxy::fromConnection(connection));
78     }
79
80     WebConnection* webConnection() const { return m_webConnection.get(); }
81
82     WebContext& context() { return m_context; }
83
84     static WebPageProxy* webPage(uint64_t pageID);
85     PassRefPtr<WebPageProxy> createWebPage(PageClient&, const WebPageConfiguration&);
86     void addExistingWebPage(WebPageProxy*, uint64_t pageID);
87     void removeWebPage(uint64_t pageID);
88
89     WTF::IteratorRange<WebPageProxyMap::const_iterator::Values> pages() const { return m_pageMap.values(); }
90     unsigned pageCount() const { return m_pageMap.size(); }
91
92     void addVisitedLinkProvider(VisitedLinkProvider&);
93     void addWebUserContentControllerProxy(WebUserContentControllerProxy&);
94     void didDestroyVisitedLinkProvider(VisitedLinkProvider&);
95     void didDestroyWebUserContentControllerProxy(WebUserContentControllerProxy&);
96
97     WebBackForwardListItem* webBackForwardItem(uint64_t itemID) const;
98
99     ResponsivenessTimer* responsivenessTimer() { return &m_responsivenessTimer; }
100
101     WebFrameProxy* webFrame(uint64_t) const;
102     bool canCreateFrame(uint64_t frameID) const;
103     void frameCreated(uint64_t, WebFrameProxy*);
104     void disconnectFramesFromPage(WebPageProxy*); // Including main frame.
105     size_t frameCountInPage(WebPageProxy*) const; // Including main frame.
106
107     void updateTextCheckerState();
108
109     void registerNewWebBackForwardListItem(WebBackForwardListItem*);
110
111     void willAcquireUniversalFileReadSandboxExtension() { m_mayHaveUniversalFileReadSandboxExtension = true; }
112     void assumeReadAccessToBaseURL(const String&);
113     bool hasAssumedReadAccessToURL(const WebCore::URL&) const;
114
115     bool checkURLReceivedFromWebProcess(const String&);
116     bool checkURLReceivedFromWebProcess(const WebCore::URL&);
117
118     static bool fullKeyboardAccessEnabled();
119
120     DownloadProxy* createDownloadProxy(const WebCore::ResourceRequest&);
121
122 #if PLATFORM(COCOA)
123     PassRefPtr<RefCounter::Count> processSuppressionCounter()
124     {
125         return m_pagesPreventingSuppressionCounter.count();
126     }
127 #endif
128
129     void didSaveToPageCache();
130     void releasePageCache();
131
132 #if PLATFORM(COCOA)
133     bool allPagesAreProcessSuppressible() const;
134     void updateProcessSuppressionState();
135 #endif
136
137     void enableSuddenTermination();
138     void disableSuddenTermination();
139
140     void requestTermination();
141
142     RefPtr<API::Object> apiObjectByConvertingToHandles(API::Object*);
143
144     void windowServerConnectionStateChanged();
145
146     void sendProcessWillSuspend();
147     void processReadyToSuspend();
148     void sendCancelProcessWillSuspend();
149     void didCancelProcessSuspension();
150
151     void setIsHoldingLockedFiles(bool);
152
153     ProcessThrottler& throttler() { return *m_throttler; }
154     
155 private:
156     explicit WebProcessProxy(WebContext&);
157
158     // From ChildProcessProxy
159     virtual void getLaunchOptions(ProcessLauncher::LaunchOptions&) override;
160     void platformGetLaunchOptions(ProcessLauncher::LaunchOptions&);
161     virtual void connectionWillOpen(IPC::Connection*) override;
162     virtual void connectionWillClose(IPC::Connection*) override;
163
164     // Called when the web process has crashed or we know that it will terminate soon.
165     // Will potentially cause the WebProcessProxy object to be freed.
166     void disconnect();
167
168     // IPC message handlers.
169     void addBackForwardItem(uint64_t itemID, uint64_t pageID, const PageState&);
170     void didDestroyFrame(uint64_t);
171     
172     void shouldTerminate(bool& shouldTerminate);
173
174     // Plugins
175 #if ENABLE(NETSCAPE_PLUGIN_API)
176     void getPlugins(bool refresh, Vector<WebCore::PluginInfo>& plugins, Vector<WebCore::PluginInfo>& applicationPlugins);
177 #endif // ENABLE(NETSCAPE_PLUGIN_API)
178 #if ENABLE(NETSCAPE_PLUGIN_API)
179     void getPluginProcessConnection(uint64_t pluginProcessToken, PassRefPtr<Messages::WebProcessProxy::GetPluginProcessConnection::DelayedReply>);
180 #endif
181 #if ENABLE(NETWORK_PROCESS)
182     void getNetworkProcessConnection(PassRefPtr<Messages::WebProcessProxy::GetNetworkProcessConnection::DelayedReply>);
183 #endif
184 #if ENABLE(DATABASE_PROCESS)
185     void getDatabaseProcessConnection(PassRefPtr<Messages::WebProcessProxy::GetDatabaseProcessConnection::DelayedReply>);
186 #endif
187
188     // IPC::Connection::Client
189     friend class WebConnectionToWebProcess;
190     virtual void didReceiveMessage(IPC::Connection*, IPC::MessageDecoder&) override;
191     virtual void didReceiveSyncMessage(IPC::Connection*, IPC::MessageDecoder&, std::unique_ptr<IPC::MessageEncoder>&) override;
192     virtual void didClose(IPC::Connection*) override;
193     virtual void didReceiveInvalidMessage(IPC::Connection*, IPC::StringReference messageReceiverName, IPC::StringReference messageName) override;
194
195     // ResponsivenessTimer::Client
196     void didBecomeUnresponsive(ResponsivenessTimer*) override;
197     void interactionOccurredWhileUnresponsive(ResponsivenessTimer*) override;
198     void didBecomeResponsive(ResponsivenessTimer*) override;
199
200     // ProcessLauncher::Client
201     virtual void didFinishLaunching(ProcessLauncher*, IPC::Connection::Identifier) override;
202
203     // History client
204     void didNavigateWithNavigationData(uint64_t pageID, const WebNavigationDataStore&, uint64_t frameID);
205     void didPerformClientRedirect(uint64_t pageID, const String& sourceURLString, const String& destinationURLString, uint64_t frameID);
206     void didPerformServerRedirect(uint64_t pageID, const String& sourceURLString, const String& destinationURLString, uint64_t frameID);
207     void didUpdateHistoryTitle(uint64_t pageID, const String& title, const String& url, uint64_t frameID);
208
209     // Implemented in generated WebProcessProxyMessageReceiver.cpp
210     void didReceiveWebProcessProxyMessage(IPC::Connection*, IPC::MessageDecoder&);
211     void didReceiveSyncWebProcessProxyMessage(IPC::Connection*, IPC::MessageDecoder&, std::unique_ptr<IPC::MessageEncoder>&);
212
213     bool canTerminateChildProcess();
214
215     ResponsivenessTimer m_responsivenessTimer;
216     
217     RefPtr<WebConnectionToWebProcess> m_webConnection;
218     Ref<WebContext> m_context;
219
220     bool m_mayHaveUniversalFileReadSandboxExtension; // True if a read extension for "/" was ever granted - we don't track whether WebProcess still has it.
221     HashSet<String> m_localPathsWithAssumedReadAccess;
222
223     WebPageProxyMap m_pageMap;
224     WebFrameProxyMap m_frameMap;
225     WebBackForwardListItemMap m_backForwardListItemMap;
226
227     HashSet<VisitedLinkProvider*> m_visitedLinkProviders;
228     HashSet<WebUserContentControllerProxy*> m_webUserContentControllerProxies;
229
230     std::unique_ptr<DownloadProxyMap> m_downloadProxyMap;
231     CustomProtocolManagerProxy m_customProtocolManagerProxy;
232
233 #if PLATFORM(COCOA)
234     RefCounter m_pagesPreventingSuppressionCounter;
235     bool m_processSuppressionEnabled;
236 #endif
237
238     int m_numberOfTimesSuddenTerminationWasDisabled;
239     std::unique_ptr<ProcessThrottler> m_throttler;
240     std::unique_ptr<ProcessThrottler::BackgroundActivityToken> m_tokenForHoldingLockedFiles;
241 };
242
243 } // namespace WebKit
244
245 #endif // WebProcessProxy_h