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 "MessageReceiverMap.h"
32#include "PlatformProcessIdentifier.h"
33#include "PluginInfoStore.h"
34#include "ProcessLauncher.h"
35#include "ResponsivenessTimer.h"
36#include "WebConnectionToWebProcess.h"
37#include "WebPageProxy.h"
38#include "WebProcessProxyMessages.h"
39#include <WebCore/LinkHash.h>
40#include <memory>
41#include <wtf/Forward.h>
42#include <wtf/HashMap.h>
43#include <wtf/PassRefPtr.h>
44#include <wtf/RefCounted.h>
45
46#if ENABLE(CUSTOM_PROTOCOLS)
47#include "CustomProtocolManagerProxy.h"
48#endif
49
50#if PLATFORM(IOS)
51#include "ProcessThrottler.h"
52#endif
53
54namespace WebCore {
55class URL;
56struct PluginInfo;
57};
58
59namespace WebKit {
60
61class DownloadProxyMap;
62class WebBackForwardListItem;
63class WebContext;
64class WebPageGroup;
65struct WebNavigationDataStore;
66
67class WebProcessProxy : public ChildProcessProxy, ResponsivenessTimer::Client {
68public:
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 PassRefPtr<WebProcessProxy> create(WebContext&);
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    WebContext& context() { return m_context.get(); }
84
85    static WebPageProxy* webPage(uint64_t pageID);
86    PassRefPtr<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
112    void willAcquireUniversalFileReadSandboxExtension() { m_mayHaveUniversalFileReadSandboxExtension = true; }
113    void assumeReadAccessToBaseURL(const String&);
114    bool hasAssumedReadAccessToURL(const WebCore::URL&) const;
115
116    bool checkURLReceivedFromWebProcess(const String&);
117    bool checkURLReceivedFromWebProcess(const WebCore::URL&);
118
119    static bool fullKeyboardAccessEnabled();
120
121    DownloadProxy* createDownloadProxy();
122
123    void pageSuppressibilityChanged(WebPageProxy*);
124    void pagePreferencesChanged(WebPageProxy*);
125
126    void didSaveToPageCache();
127    void releasePageCache();
128
129#if PLATFORM(COCOA)
130    bool allPagesAreProcessSuppressible() const;
131    void updateProcessSuppressionState();
132#endif
133
134    void enableSuddenTermination();
135    void disableSuddenTermination();
136
137    void requestTermination();
138
139    RefPtr<API::Object> apiObjectByConvertingToHandles(API::Object*);
140
141    void windowServerConnectionStateChanged();
142
143    void sendProcessWillSuspend();
144    void processReadyToSuspend();
145    void sendCancelProcessWillSuspend();
146    void didCancelProcessSuspension();
147
148    ProcessThrottler& throttler() { return *m_throttler; }
149
150private:
151    explicit WebProcessProxy(WebContext&);
152
153    // From ChildProcessProxy
154    virtual void getLaunchOptions(ProcessLauncher::LaunchOptions&) override;
155    void platformGetLaunchOptions(ProcessLauncher::LaunchOptions&);
156    virtual void connectionWillOpen(IPC::Connection*) override;
157    virtual void connectionWillClose(IPC::Connection*) override;
158
159    // Called when the web process has crashed or we know that it will terminate soon.
160    // Will potentially cause the WebProcessProxy object to be freed.
161    void disconnect();
162
163    // IPC message handlers.
164    void addBackForwardItem(uint64_t itemID, uint64_t pageID, const PageState&);
165    void didDestroyFrame(uint64_t);
166
167    void shouldTerminate(bool& shouldTerminate);
168
169    // Plugins
170#if ENABLE(NETSCAPE_PLUGIN_API)
171    void getPlugins(bool refresh, Vector<WebCore::PluginInfo>& plugins, Vector<WebCore::PluginInfo>& applicationPlugins);
172#endif // ENABLE(NETSCAPE_PLUGIN_API)
173#if ENABLE(NETSCAPE_PLUGIN_API)
174    void getPluginProcessConnection(uint64_t pluginProcessToken, PassRefPtr<Messages::WebProcessProxy::GetPluginProcessConnection::DelayedReply>);
175#endif
176#if ENABLE(NETWORK_PROCESS)
177    void getNetworkProcessConnection(PassRefPtr<Messages::WebProcessProxy::GetNetworkProcessConnection::DelayedReply>);
178#endif
179#if ENABLE(DATABASE_PROCESS)
180    void getDatabaseProcessConnection(PassRefPtr<Messages::WebProcessProxy::GetDatabaseProcessConnection::DelayedReply>);
181#endif
182
183    // IPC::Connection::Client
184    friend class WebConnectionToWebProcess;
185    virtual void didReceiveMessage(IPC::Connection*, IPC::MessageDecoder&) override;
186    virtual void didReceiveSyncMessage(IPC::Connection*, IPC::MessageDecoder&, std::unique_ptr<IPC::MessageEncoder>&) override;
187    virtual void didClose(IPC::Connection*) override;
188    virtual void didReceiveInvalidMessage(IPC::Connection*, IPC::StringReference messageReceiverName, IPC::StringReference messageName) override;
189
190    // ResponsivenessTimer::Client
191    void didBecomeUnresponsive(ResponsivenessTimer*) override;
192    void interactionOccurredWhileUnresponsive(ResponsivenessTimer*) override;
193    void didBecomeResponsive(ResponsivenessTimer*) override;
194
195    // ProcessLauncher::Client
196    virtual void didFinishLaunching(ProcessLauncher*, IPC::Connection::Identifier) override;
197
198    // History client
199    void didNavigateWithNavigationData(uint64_t pageID, const WebNavigationDataStore&, uint64_t frameID);
200    void didPerformClientRedirect(uint64_t pageID, const String& sourceURLString, const String& destinationURLString, uint64_t frameID);
201    void didPerformServerRedirect(uint64_t pageID, const String& sourceURLString, const String& destinationURLString, uint64_t frameID);
202    void didUpdateHistoryTitle(uint64_t pageID, const String& title, const String& url, uint64_t frameID);
203
204    // Implemented in generated WebProcessProxyMessageReceiver.cpp
205    void didReceiveWebProcessProxyMessage(IPC::Connection*, IPC::MessageDecoder&);
206    void didReceiveSyncWebProcessProxyMessage(IPC::Connection*, IPC::MessageDecoder&, std::unique_ptr<IPC::MessageEncoder>&);
207
208    bool canTerminateChildProcess();
209
210    ResponsivenessTimer m_responsivenessTimer;
211
212    RefPtr<WebConnectionToWebProcess> m_webConnection;
213    Ref<WebContext> m_context;
214
215    bool m_mayHaveUniversalFileReadSandboxExtension; // True if a read extension for "/" was ever granted - we don't track whether WebProcess still has it.
216    HashSet<String> m_localPathsWithAssumedReadAccess;
217
218    WebPageProxyMap m_pageMap;
219    WebFrameProxyMap m_frameMap;
220    WebBackForwardListItemMap m_backForwardListItemMap;
221
222    HashSet<VisitedLinkProvider*> m_visitedLinkProviders;
223    HashSet<WebUserContentControllerProxy*> m_webUserContentControllerProxies;
224
225    std::unique_ptr<DownloadProxyMap> m_downloadProxyMap;
226
227#if ENABLE(CUSTOM_PROTOCOLS)
228    CustomProtocolManagerProxy m_customProtocolManagerProxy;
229#endif
230
231#if PLATFORM(COCOA)
232    HashSet<uint64_t> m_processSuppressiblePages;
233    bool m_processSuppressionEnabled;
234#endif
235
236    int m_numberOfTimesSuddenTerminationWasDisabled;
237    std::unique_ptr<ProcessThrottler> m_throttler;
238};
239
240} // namespace WebKit
241
242#endif // WebProcessProxy_h
243