WebProcess should be terminated if invalid frameIDs are
[WebKit-https.git] / WebKit2 / UIProcess / WebProcessProxy.h
1 /*
2  * Copyright (C) 2010 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 <WebCore/LinkHash.h>
38 #include <wtf/Forward.h>
39 #include <wtf/HashMap.h>
40 #include <wtf/PassRefPtr.h>
41 #include <wtf/RefCounted.h>
42
43 namespace WebCore {
44     class KURL;
45 };
46
47 namespace WebKit {
48
49 class WebBackForwardListItem;
50 class WebContext;
51 class WebPageGroup;
52 struct WebNavigationDataStore;
53
54 class WebProcessProxy : public RefCounted<WebProcessProxy>, CoreIPC::Connection::Client, ResponsivenessTimer::Client, ProcessLauncher::Client, ThreadLauncher::Client {
55 public:
56     typedef HashMap<uint64_t, RefPtr<WebPageProxy> > WebPageProxyMap;
57     typedef WebPageProxyMap::const_iterator::Values pages_const_iterator;
58     typedef HashMap<uint64_t, RefPtr<WebFrameProxy> > WebFrameProxyMap;
59     typedef HashMap<uint64_t, RefPtr<WebBackForwardListItem> > WebBackForwardListItemMap;
60
61     static PassRefPtr<WebProcessProxy> create(WebContext*);
62     ~WebProcessProxy();
63
64     void terminate();
65
66     template<typename E, typename T> bool send(E messageID, uint64_t destinationID, const T& arguments);
67     template<typename T> bool send(const T& message, uint64_t destinationID);
68     template<typename U> bool sendSync(const U& message, const typename U::Reply& reply, uint64_t destinationID, double timeout = 1);
69     
70     CoreIPC::Connection* connection() const
71     { 
72         ASSERT(m_connection);
73         
74         return m_connection.get(); 
75     }
76
77     WebContext* context() const { return m_context; }
78
79     PlatformProcessIdentifier processIdentifier() const { return m_processLauncher->processIdentifier(); }
80
81     WebPageProxy* webPage(uint64_t pageID) const;
82     WebPageProxy* createWebPage(WebContext*, WebPageGroup*);
83     void addExistingWebPage(WebPageProxy*, uint64_t pageID);
84     void removeWebPage(uint64_t pageID);
85
86     pages_const_iterator pages_begin();
87     pages_const_iterator pages_end();
88     size_t numberOfPages();
89
90     WebBackForwardListItem* webBackForwardItem(uint64_t itemID) const;
91
92     ResponsivenessTimer* responsivenessTimer() { return &m_responsivenessTimer; }
93
94     bool isValid() const { return m_connection; }
95     bool isLaunching() const;
96     bool canSendMessage() const { return isValid() || isLaunching(); }
97
98     WebFrameProxy* webFrame(uint64_t) const;
99     bool canCreateFrame(uint64_t frameID) const;
100     void frameCreated(uint64_t, WebFrameProxy*);
101     void didDestroyFrame(uint64_t);
102     void disconnectFramesFromPage(WebPageProxy*); // Including main frame.
103     size_t frameCountInPage(WebPageProxy*) const; // Including main frame.
104
105 private:
106     explicit WebProcessProxy(WebContext*);
107
108     void connect();
109
110     bool sendMessage(CoreIPC::MessageID, PassOwnPtr<CoreIPC::ArgumentEncoder>);
111
112     void addBackForwardItem(uint64_t itemID, const String& originalURLString, const String& urlString, const String& title);
113
114 #if ENABLE(PLUGIN_PROCESS)
115     void getPluginProcessConnection(const String& pluginPath, CoreIPC::ArgumentEncoder* reply);
116 #endif
117
118     // CoreIPC::Connection::Client
119     void didReceiveMessage(CoreIPC::Connection*, CoreIPC::MessageID, CoreIPC::ArgumentDecoder*);
120     CoreIPC::SyncReplyMode didReceiveSyncMessage(CoreIPC::Connection*, CoreIPC::MessageID, CoreIPC::ArgumentDecoder*, CoreIPC::ArgumentEncoder*);
121     void didClose(CoreIPC::Connection*);
122     void didReceiveInvalidMessage(CoreIPC::Connection*, CoreIPC::MessageID);
123
124     // ResponsivenessTimer::Client
125     void didBecomeUnresponsive(ResponsivenessTimer*);
126     void didBecomeResponsive(ResponsivenessTimer*);
127
128     // ProcessLauncher::Client
129     virtual void didFinishLaunching(ProcessLauncher*, CoreIPC::Connection::Identifier);
130
131     // ThreadLauncher::Client
132     virtual void didFinishLaunching(ThreadLauncher*, CoreIPC::Connection::Identifier);
133
134     void didFinishLaunching(CoreIPC::Connection::Identifier);
135
136     // Implemented in generated WebProcessProxyMessageReceiver.cpp
137     void didReceiveWebProcessProxyMessage(CoreIPC::Connection*, CoreIPC::MessageID, CoreIPC::ArgumentDecoder*);
138
139     ResponsivenessTimer m_responsivenessTimer;
140     RefPtr<CoreIPC::Connection> m_connection;
141
142     Vector<CoreIPC::Connection::OutgoingMessage> m_pendingMessages;
143     RefPtr<ProcessLauncher> m_processLauncher;
144     RefPtr<ThreadLauncher> m_threadLauncher;
145
146     WebContext* m_context;
147
148     WebPageProxyMap m_pageMap;
149     WebFrameProxyMap m_frameMap;
150     WebBackForwardListItemMap m_backForwardListItemMap;
151 };
152
153 template<typename E, typename T>
154 bool WebProcessProxy::send(E messageID, uint64_t destinationID, const T& arguments)
155 {
156     OwnPtr<CoreIPC::ArgumentEncoder> argumentEncoder = CoreIPC::ArgumentEncoder::create(destinationID);
157     argumentEncoder->encode(arguments);
158
159     return sendMessage(CoreIPC::MessageID(messageID), argumentEncoder.release());
160 }
161
162 template<typename T>
163 bool WebProcessProxy::send(const T& message, uint64_t destinationID)
164 {
165     OwnPtr<CoreIPC::ArgumentEncoder> argumentEncoder = CoreIPC::ArgumentEncoder::create(destinationID);
166     argumentEncoder->encode(message);
167
168     return sendMessage(CoreIPC::MessageID(T::messageID), argumentEncoder.release());
169 }
170
171 template<typename U> 
172 bool WebProcessProxy::sendSync(const U& message, const typename U::Reply& reply, uint64_t destinationID, double timeout)
173 {
174     return m_connection->sendSync(message, reply, destinationID, timeout);
175 }
176     
177 } // namespace WebKit
178
179 #endif // WebProcessProxy_h