WebProcessProxy.h revision 2daae5fd11344eaa88a0d92b0f6d65f8d2255c00
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 "Connection.h"
30#include "PlatformProcessIdentifier.h"
31#include "PluginInfoStore.h"
32#include "ProcessLauncher.h"
33#include "ProcessModel.h"
34#include "ResponsivenessTimer.h"
35#include "ThreadLauncher.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
44namespace WebCore {
45    class KURL;
46};
47
48namespace WebKit {
49
50class WebBackForwardListItem;
51class WebContext;
52class WebPageGroup;
53struct WebNavigationDataStore;
54
55class WebProcessProxy : public RefCounted<WebProcessProxy>, CoreIPC::Connection::Client, ResponsivenessTimer::Client, ProcessLauncher::Client, ThreadLauncher::Client {
56public:
57    typedef HashMap<uint64_t, RefPtr<WebFrameProxy> > WebFrameProxyMap;
58    typedef HashMap<uint64_t, RefPtr<WebBackForwardListItem> > WebBackForwardListItemMap;
59
60    static PassRefPtr<WebProcessProxy> create(PassRefPtr<WebContext>);
61    ~WebProcessProxy();
62
63    void terminate();
64
65    template<typename T> bool send(const T& message, uint64_t destinationID, unsigned messageSendFlags = 0);
66    template<typename U> bool sendSync(const U& message, const typename U::Reply& reply, uint64_t destinationID, double timeout = 1);
67
68    CoreIPC::Connection* connection() const
69    {
70        ASSERT(m_connection);
71
72        return m_connection.get();
73    }
74
75    WebContext* context() const { return m_context.get(); }
76
77    PlatformProcessIdentifier processIdentifier() const { return m_processLauncher->processIdentifier(); }
78
79    WebPageProxy* webPage(uint64_t pageID) const;
80    PassRefPtr<WebPageProxy> createWebPage(PageClient*, WebContext*, WebPageGroup*);
81    void addExistingWebPage(WebPageProxy*, uint64_t pageID);
82    void removeWebPage(uint64_t pageID);
83
84    WebBackForwardListItem* webBackForwardItem(uint64_t itemID) const;
85
86    ResponsivenessTimer* responsivenessTimer() { return &m_responsivenessTimer; }
87
88    bool isValid() const { return m_connection; }
89    bool isLaunching() const;
90    bool canSendMessage() const { return isValid() || isLaunching(); }
91
92    WebFrameProxy* webFrame(uint64_t) const;
93    bool canCreateFrame(uint64_t frameID) const;
94    void frameCreated(uint64_t, WebFrameProxy*);
95    void disconnectFramesFromPage(WebPageProxy*); // Including main frame.
96    size_t frameCountInPage(WebPageProxy*) const; // Including main frame.
97
98    void updateTextCheckerState();
99
100    void registerNewWebBackForwardListItem(WebBackForwardListItem*);
101
102    // FIXME: This variant of send is deprecated. All clients should move to an overload that take a message type.
103    template<typename E, typename T> bool deprecatedSend(E messageID, uint64_t destinationID, const T& arguments);
104
105private:
106    explicit WebProcessProxy(PassRefPtr<WebContext>);
107
108    // Initializes the process or thread launcher which will begin launching the process.
109    void connect();
110
111    // Called when the web process has crashed or we know that it will terminate soon.
112    // Will potentially cause the WebProcessProxy object to be freed.
113    void disconnect();
114
115    bool sendMessage(CoreIPC::MessageID, PassOwnPtr<CoreIPC::ArgumentEncoder>, unsigned messageSendFlags);
116
117    // CoreIPC message handlers.
118    void addBackForwardItem(uint64_t itemID, const String& originalURLString, const String& urlString, const String& title, const CoreIPC::DataReference& backForwardData);
119    void didDestroyFrame(uint64_t);
120
121    void shouldTerminate(bool& shouldTerminate);
122
123#if ENABLE(PLUGIN_PROCESS)
124    void getPluginProcessConnection(const String& pluginPath, PassRefPtr<Messages::WebProcessProxy::GetPluginProcessConnection::DelayedReply>);
125    void pluginSyncMessageSendTimedOut(const String& pluginPath);
126#endif
127
128    // CoreIPC::Connection::Client
129    virtual void didReceiveMessage(CoreIPC::Connection*, CoreIPC::MessageID, CoreIPC::ArgumentDecoder*);
130    virtual CoreIPC::SyncReplyMode didReceiveSyncMessage(CoreIPC::Connection*, CoreIPC::MessageID, CoreIPC::ArgumentDecoder*, CoreIPC::ArgumentEncoder*);
131    virtual void didClose(CoreIPC::Connection*);
132    virtual void didReceiveInvalidMessage(CoreIPC::Connection*, CoreIPC::MessageID);
133    virtual void syncMessageSendTimedOut(CoreIPC::Connection*);
134
135#if PLATFORM(WIN)
136    Vector<HWND> windowsToReceiveSentMessagesWhileWaitingForSyncReply();
137#endif
138
139    // ResponsivenessTimer::Client
140    void didBecomeUnresponsive(ResponsivenessTimer*);
141    void didBecomeResponsive(ResponsivenessTimer*);
142
143    // ProcessLauncher::Client
144    virtual void didFinishLaunching(ProcessLauncher*, CoreIPC::Connection::Identifier);
145
146    // ThreadLauncher::Client
147    virtual void didFinishLaunching(ThreadLauncher*, CoreIPC::Connection::Identifier);
148
149    void didFinishLaunching(CoreIPC::Connection::Identifier);
150
151    // Implemented in generated WebProcessProxyMessageReceiver.cpp
152    void didReceiveWebProcessProxyMessage(CoreIPC::Connection*, CoreIPC::MessageID, CoreIPC::ArgumentDecoder*);
153    CoreIPC::SyncReplyMode didReceiveSyncWebProcessProxyMessage(CoreIPC::Connection*, CoreIPC::MessageID, CoreIPC::ArgumentDecoder* arguments, CoreIPC::ArgumentEncoder* reply);
154
155    ResponsivenessTimer m_responsivenessTimer;
156    RefPtr<CoreIPC::Connection> m_connection;
157
158    Vector<std::pair<CoreIPC::Connection::OutgoingMessage, unsigned> > m_pendingMessages;
159    RefPtr<ProcessLauncher> m_processLauncher;
160    RefPtr<ThreadLauncher> m_threadLauncher;
161
162    RefPtr<WebContext> m_context;
163
164    HashMap<uint64_t, WebPageProxy*> m_pageMap;
165    WebFrameProxyMap m_frameMap;
166    WebBackForwardListItemMap m_backForwardListItemMap;
167};
168
169template<typename E, typename T>
170bool WebProcessProxy::deprecatedSend(E messageID, uint64_t destinationID, const T& arguments)
171{
172    OwnPtr<CoreIPC::ArgumentEncoder> argumentEncoder = CoreIPC::ArgumentEncoder::create(destinationID);
173    argumentEncoder->encode(arguments);
174
175    return sendMessage(CoreIPC::MessageID(messageID), argumentEncoder.release(), 0);
176}
177
178template<typename T>
179bool WebProcessProxy::send(const T& message, uint64_t destinationID, unsigned messageSendFlags)
180{
181    OwnPtr<CoreIPC::ArgumentEncoder> argumentEncoder = CoreIPC::ArgumentEncoder::create(destinationID);
182    argumentEncoder->encode(message);
183
184    return sendMessage(CoreIPC::MessageID(T::messageID), argumentEncoder.release(), messageSendFlags);
185}
186
187template<typename U>
188bool WebProcessProxy::sendSync(const U& message, const typename U::Reply& reply, uint64_t destinationID, double timeout)
189{
190    return m_connection->sendSync(message, reply, destinationID, timeout);
191}
192
193} // namespace WebKit
194
195#endif // WebProcessProxy_h
196