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 "ChildProcessProxy.h"
30#include "MessageReceiverMap.h"
31#include "PlatformProcessIdentifier.h"
32#include "PluginInfoStore.h"
33#include "ProcessLauncher.h"
34#include "ResponsivenessTimer.h"
35#include "WebConnectionToWebProcess.h"
36#include "WebPageProxy.h"
37#include "WebProcessProxyMessages.h"
38#include <WebCore/LinkHash.h>
39#include <wtf/Forward.h>
40#include <wtf/HashMap.h>
41#include <wtf/PassRefPtr.h>
42#include <wtf/RefCounted.h>
43
44#if ENABLE(CUSTOM_PROTOCOLS)
45#include "CustomProtocolManagerProxy.h"
46#endif
47
48namespace WebCore {
49class KURL;
50struct PluginInfo;
51};
52
53namespace WebKit {
54
55class DownloadProxyMap;
56class WebBackForwardListItem;
57class WebContext;
58class WebPageGroup;
59struct WebNavigationDataStore;
60
61class WebProcessProxy : public ChildProcessProxy, ResponsivenessTimer::Client {
62public:
63    typedef HashMap<uint64_t, RefPtr<WebBackForwardListItem>> WebBackForwardListItemMap;
64    typedef HashMap<uint64_t, RefPtr<WebFrameProxy>> WebFrameProxyMap;
65    typedef HashMap<uint64_t, WebPageProxy*> WebPageProxyMap;
66
67    static PassRefPtr<WebProcessProxy> create(PassRefPtr<WebContext>);
68    ~WebProcessProxy();
69
70    static WebProcessProxy* fromConnection(CoreIPC::Connection* connection)
71    {
72        return static_cast<WebProcessProxy*>(ChildProcessProxy::fromConnection(connection));
73    }
74
75    WebConnection* webConnection() const { return m_webConnection.get(); }
76
77    WebContext* context() const { return m_context.get(); }
78
79    static WebPageProxy* webPage(uint64_t pageID);
80    PassRefPtr<WebPageProxy> createWebPage(PageClient*, WebContext*, WebPageGroup*);
81    void addExistingWebPage(WebPageProxy*, uint64_t pageID);
82    void removeWebPage(uint64_t pageID);
83    Vector<WebPageProxy*> pages() const;
84
85    WebBackForwardListItem* webBackForwardItem(uint64_t itemID) const;
86
87    ResponsivenessTimer* responsivenessTimer() { return &m_responsivenessTimer; }
88
89    WebFrameProxy* webFrame(uint64_t) const;
90    bool canCreateFrame(uint64_t frameID) const;
91    void frameCreated(uint64_t, WebFrameProxy*);
92    void disconnectFramesFromPage(WebPageProxy*); // Including main frame.
93    size_t frameCountInPage(WebPageProxy*) const; // Including main frame.
94
95    void updateTextCheckerState();
96
97    void registerNewWebBackForwardListItem(WebBackForwardListItem*);
98
99    void willAcquireUniversalFileReadSandboxExtension() { m_mayHaveUniversalFileReadSandboxExtension = true; }
100    void assumeReadAccessToBaseURL(const String&);
101    bool hasAssumedReadAccessToURL(const WebCore::KURL&) const;
102
103    bool checkURLReceivedFromWebProcess(const String&);
104    bool checkURLReceivedFromWebProcess(const WebCore::KURL&);
105
106    static bool fullKeyboardAccessEnabled();
107
108    DownloadProxy* createDownloadProxy();
109
110    void pageVisibilityChanged(WebPageProxy*);
111    void pagePreferencesChanged(WebPageProxy*);
112
113    void didSaveToPageCache();
114    void releasePageCache();
115
116#if PLATFORM(MAC)
117    bool allPagesAreProcessSuppressible() const;
118    static bool pageIsProcessSuppressible(WebPageProxy*);
119    void updateProcessSuppressionState();
120#endif
121
122    void enableSuddenTermination();
123    void disableSuddenTermination();
124
125    void requestTermination();
126
127private:
128    explicit WebProcessProxy(PassRefPtr<WebContext>);
129
130    // From ChildProcessProxy
131    virtual void getLaunchOptions(ProcessLauncher::LaunchOptions&) OVERRIDE;
132    void platformGetLaunchOptions(ProcessLauncher::LaunchOptions&);
133    virtual void connectionWillOpen(CoreIPC::Connection*) OVERRIDE;
134    virtual void connectionWillClose(CoreIPC::Connection*) OVERRIDE;
135
136    // Called when the web process has crashed or we know that it will terminate soon.
137    // Will potentially cause the WebProcessProxy object to be freed.
138    void disconnect();
139
140    // CoreIPC message handlers.
141    void addBackForwardItem(uint64_t itemID, const String& originalURLString, const String& urlString, const String& title, const CoreIPC::DataReference& backForwardData);
142    void didDestroyFrame(uint64_t);
143
144    void shouldTerminate(bool& shouldTerminate);
145
146    // Plugins
147#if ENABLE(NETSCAPE_PLUGIN_API)
148    void getPlugins(bool refresh, Vector<WebCore::PluginInfo>& plugins, Vector<WebCore::PluginInfo>& applicationPlugins);
149#endif // ENABLE(NETSCAPE_PLUGIN_API)
150#if ENABLE(PLUGIN_PROCESS)
151    void getPluginProcessConnection(uint64_t pluginProcessToken, PassRefPtr<Messages::WebProcessProxy::GetPluginProcessConnection::DelayedReply>);
152#elif ENABLE(NETSCAPE_PLUGIN_API)
153    void didGetSitesWithPluginData(const Vector<String>& sites, uint64_t callbackID);
154    void didClearPluginSiteData(uint64_t callbackID);
155#endif
156#if ENABLE(NETWORK_PROCESS)
157    void getNetworkProcessConnection(PassRefPtr<Messages::WebProcessProxy::GetNetworkProcessConnection::DelayedReply>);
158#endif
159#if ENABLE(SHARED_WORKER_PROCESS)
160    void getSharedWorkerProcessConnection(const String& url, const String& name, PassRefPtr<Messages::WebProcessProxy::GetSharedWorkerProcessConnection::DelayedReply>);
161#endif
162
163    // CoreIPC::Connection::Client
164    friend class WebConnectionToWebProcess;
165    virtual void didReceiveMessage(CoreIPC::Connection*, CoreIPC::MessageDecoder&) OVERRIDE;
166    virtual void didReceiveSyncMessage(CoreIPC::Connection*, CoreIPC::MessageDecoder&, OwnPtr<CoreIPC::MessageEncoder>&) OVERRIDE;
167    virtual void didClose(CoreIPC::Connection*) OVERRIDE;
168    virtual void didReceiveInvalidMessage(CoreIPC::Connection*, CoreIPC::StringReference messageReceiverName, CoreIPC::StringReference messageName) OVERRIDE;
169
170    // ResponsivenessTimer::Client
171    void didBecomeUnresponsive(ResponsivenessTimer*) OVERRIDE;
172    void interactionOccurredWhileUnresponsive(ResponsivenessTimer*) OVERRIDE;
173    void didBecomeResponsive(ResponsivenessTimer*) OVERRIDE;
174
175    // ProcessLauncher::Client
176    virtual void didFinishLaunching(ProcessLauncher*, CoreIPC::Connection::Identifier) OVERRIDE;
177
178    // History client
179    void didNavigateWithNavigationData(uint64_t pageID, const WebNavigationDataStore&, uint64_t frameID);
180    void didPerformClientRedirect(uint64_t pageID, const String& sourceURLString, const String& destinationURLString, uint64_t frameID);
181    void didPerformServerRedirect(uint64_t pageID, const String& sourceURLString, const String& destinationURLString, uint64_t frameID);
182    void didUpdateHistoryTitle(uint64_t pageID, const String& title, const String& url, uint64_t frameID);
183
184    // Implemented in generated WebProcessProxyMessageReceiver.cpp
185    void didReceiveWebProcessProxyMessage(CoreIPC::Connection*, CoreIPC::MessageDecoder&);
186    void didReceiveSyncWebProcessProxyMessage(CoreIPC::Connection*, CoreIPC::MessageDecoder&, OwnPtr<CoreIPC::MessageEncoder>&);
187
188    bool canTerminateChildProcess();
189
190    ResponsivenessTimer m_responsivenessTimer;
191
192    RefPtr<WebConnectionToWebProcess> m_webConnection;
193    RefPtr<WebContext> m_context;
194
195    bool m_mayHaveUniversalFileReadSandboxExtension; // True if a read extension for "/" was ever granted - we don't track whether WebProcess still has it.
196    HashSet<String> m_localPathsWithAssumedReadAccess;
197
198    WebPageProxyMap m_pageMap;
199    WebFrameProxyMap m_frameMap;
200    WebBackForwardListItemMap m_backForwardListItemMap;
201
202    OwnPtr<DownloadProxyMap> m_downloadProxyMap;
203
204#if ENABLE(CUSTOM_PROTOCOLS)
205    CustomProtocolManagerProxy m_customProtocolManagerProxy;
206#endif
207
208#if PLATFORM(MAC)
209    HashSet<uint64_t> m_processSuppressiblePages;
210    bool m_processSuppressionEnabled;
211#endif
212};
213
214} // namespace WebKit
215
216#endif // WebProcessProxy_h
217