[refactor] More post-NSS WebCrypto cleanups (utility functions).
[chromium-blink-merge.git] / content / browser / frame_host / render_frame_proxy_host.cc
blob2ef3d264d614746cf3d1e7566044741685922488
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "content/browser/frame_host/render_frame_proxy_host.h"
7 #include "base/lazy_instance.h"
8 #include "content/browser/bad_message.h"
9 #include "content/browser/frame_host/cross_process_frame_connector.h"
10 #include "content/browser/frame_host/frame_tree.h"
11 #include "content/browser/frame_host/frame_tree_node.h"
12 #include "content/browser/frame_host/render_frame_host_delegate.h"
13 #include "content/browser/frame_host/render_frame_host_impl.h"
14 #include "content/browser/frame_host/render_widget_host_view_child_frame.h"
15 #include "content/browser/message_port_message_filter.h"
16 #include "content/browser/renderer_host/render_view_host_impl.h"
17 #include "content/browser/renderer_host/render_widget_host_view_base.h"
18 #include "content/browser/site_instance_impl.h"
19 #include "content/common/frame_messages.h"
20 #include "content/public/browser/browser_thread.h"
21 #include "ipc/ipc_message.h"
23 namespace content {
25 namespace {
27 // The (process id, routing id) pair that identifies one RenderFrameProxy.
28 typedef std::pair<int32, int32> RenderFrameProxyHostID;
29 typedef base::hash_map<RenderFrameProxyHostID, RenderFrameProxyHost*>
30 RoutingIDFrameProxyMap;
31 base::LazyInstance<RoutingIDFrameProxyMap> g_routing_id_frame_proxy_map =
32 LAZY_INSTANCE_INITIALIZER;
36 // static
37 RenderFrameProxyHost* RenderFrameProxyHost::FromID(int process_id,
38 int routing_id) {
39 DCHECK_CURRENTLY_ON(BrowserThread::UI);
40 RoutingIDFrameProxyMap* frames = g_routing_id_frame_proxy_map.Pointer();
41 RoutingIDFrameProxyMap::iterator it = frames->find(
42 RenderFrameProxyHostID(process_id, routing_id));
43 return it == frames->end() ? NULL : it->second;
46 RenderFrameProxyHost::RenderFrameProxyHost(SiteInstance* site_instance,
47 RenderViewHostImpl* render_view_host,
48 FrameTreeNode* frame_tree_node)
49 : routing_id_(site_instance->GetProcess()->GetNextRoutingID()),
50 site_instance_(site_instance),
51 process_(site_instance->GetProcess()),
52 frame_tree_node_(frame_tree_node),
53 render_frame_proxy_created_(false),
54 render_view_host_(render_view_host) {
55 GetProcess()->AddRoute(routing_id_, this);
56 CHECK(g_routing_id_frame_proxy_map.Get().insert(
57 std::make_pair(
58 RenderFrameProxyHostID(GetProcess()->GetID(), routing_id_),
59 this)).second);
60 CHECK_IMPLIES(!render_view_host,
61 frame_tree_node_->render_manager()->ForInnerDelegate() &&
62 frame_tree_node_->IsMainFrame());
63 if (render_view_host)
64 frame_tree_node_->frame_tree()->AddRenderViewHostRef(render_view_host_);
66 bool is_proxy_to_parent = !frame_tree_node_->IsMainFrame() &&
67 frame_tree_node_->parent()
68 ->render_manager()
69 ->current_frame_host()
70 ->GetSiteInstance() == site_instance;
71 bool is_proxy_to_outer_delegate =
72 frame_tree_node_->IsMainFrame() &&
73 frame_tree_node_->render_manager()->ForInnerDelegate();
75 // If this is a proxy to parent frame or this proxy is for the inner
76 // WebContents's FrameTreeNode in outer WebContents's SiteInstance, then we
77 // need a CrossProcessFrameConnector.
78 if (is_proxy_to_parent || is_proxy_to_outer_delegate) {
79 // The RenderFrameHost navigating cross-process is destroyed and a proxy for
80 // it is created in the parent's process. CrossProcessFrameConnector
81 // initialization only needs to happen on an initial cross-process
82 // navigation, when the RenderFrameHost leaves the same process as its
83 // parent. The same CrossProcessFrameConnector is used for subsequent cross-
84 // process navigations, but it will be destroyed if the frame is
85 // navigated back to the same SiteInstance as its parent.
86 cross_process_frame_connector_.reset(new CrossProcessFrameConnector(this));
90 RenderFrameProxyHost::~RenderFrameProxyHost() {
91 if (GetProcess()->HasConnection()) {
92 // TODO(nasko): For now, don't send this IPC for top-level frames, as
93 // the top-level RenderFrame will delete the RenderFrameProxy.
94 // This can be removed once we don't have a swapped out state on
95 // RenderFrame. See https://crbug.com/357747
96 if (!frame_tree_node_->IsMainFrame())
97 Send(new FrameMsg_DeleteProxy(routing_id_));
100 if (render_view_host_)
101 frame_tree_node_->frame_tree()->ReleaseRenderViewHostRef(render_view_host_);
102 GetProcess()->RemoveRoute(routing_id_);
103 g_routing_id_frame_proxy_map.Get().erase(
104 RenderFrameProxyHostID(GetProcess()->GetID(), routing_id_));
107 void RenderFrameProxyHost::SetChildRWHView(RenderWidgetHostView* view) {
108 cross_process_frame_connector_->set_view(
109 static_cast<RenderWidgetHostViewChildFrame*>(view));
112 RenderViewHostImpl* RenderFrameProxyHost::GetRenderViewHost() {
113 return frame_tree_node_->frame_tree()->GetRenderViewHost(
114 site_instance_.get());
117 RenderWidgetHostView* RenderFrameProxyHost::GetRenderWidgetHostView() {
118 return frame_tree_node_->parent()->render_manager()
119 ->GetRenderWidgetHostView();
122 void RenderFrameProxyHost::TakeFrameHostOwnership(
123 scoped_ptr<RenderFrameHostImpl> render_frame_host) {
124 CHECK(render_frame_host_ == nullptr);
125 render_frame_host_ = render_frame_host.Pass();
126 render_frame_host_->set_render_frame_proxy_host(this);
129 scoped_ptr<RenderFrameHostImpl> RenderFrameProxyHost::PassFrameHostOwnership() {
130 render_frame_host_->set_render_frame_proxy_host(NULL);
131 return render_frame_host_.Pass();
134 bool RenderFrameProxyHost::Send(IPC::Message *msg) {
135 return GetProcess()->Send(msg);
138 bool RenderFrameProxyHost::OnMessageReceived(const IPC::Message& msg) {
139 if (cross_process_frame_connector_.get() &&
140 cross_process_frame_connector_->OnMessageReceived(msg))
141 return true;
143 bool handled = true;
144 IPC_BEGIN_MESSAGE_MAP(RenderFrameProxyHost, msg)
145 IPC_MESSAGE_HANDLER(FrameHostMsg_Detach, OnDetach)
146 IPC_MESSAGE_HANDLER(FrameHostMsg_OpenURL, OnOpenURL)
147 IPC_MESSAGE_HANDLER(FrameHostMsg_RouteMessageEvent, OnRouteMessageEvent)
148 IPC_MESSAGE_HANDLER(FrameHostMsg_DidChangeOpener, OnDidChangeOpener)
149 IPC_MESSAGE_UNHANDLED(handled = false)
150 IPC_END_MESSAGE_MAP()
151 return handled;
154 bool RenderFrameProxyHost::InitRenderFrameProxy() {
155 DCHECK(!render_frame_proxy_created_);
157 // It is possible to reach this when the process is dead (in particular, when
158 // creating proxies from CreateProxiesForChildFrame). In that case, don't
159 // create the proxy. The process shouldn't be resurrected just to create
160 // RenderFrameProxies; it should be restored only if it needs to host a
161 // RenderFrame. When that happens, the process will be reinitialized, and
162 // all necessary proxies, including any of the ones we skipped here, will be
163 // created by CreateProxiesForSiteInstance. See https://crbug.com/476846
164 if (!GetProcess()->HasConnection())
165 return false;
167 int parent_routing_id = MSG_ROUTING_NONE;
168 if (frame_tree_node_->parent()) {
169 // It is safe to use GetRenderFrameProxyHost to get the parent proxy, since
170 // new child frames always start out as local frames, so a new proxy should
171 // never have a RenderFrameHost as a parent.
172 RenderFrameProxyHost* parent_proxy =
173 frame_tree_node_->parent()->render_manager()->GetRenderFrameProxyHost(
174 site_instance_.get());
175 CHECK(parent_proxy);
177 // Proxies that aren't live in the parent node should not be initialized
178 // here, since there is no valid parent RenderFrameProxy on the renderer
179 // side. This can happen when adding a new child frame after an opener
180 // process crashed and was reloaded. See https://crbug.com/501152.
181 if (!parent_proxy->is_render_frame_proxy_live())
182 return false;
184 parent_routing_id = parent_proxy->GetRoutingID();
185 CHECK_NE(parent_routing_id, MSG_ROUTING_NONE);
188 int opener_routing_id = MSG_ROUTING_NONE;
189 if (frame_tree_node_->opener()) {
190 opener_routing_id = frame_tree_node_->render_manager()->GetOpenerRoutingID(
191 site_instance_.get());
194 Send(new FrameMsg_NewFrameProxy(routing_id_,
195 frame_tree_node_->frame_tree()
196 ->GetRenderViewHost(site_instance_.get())
197 ->GetRoutingID(),
198 opener_routing_id,
199 parent_routing_id,
200 frame_tree_node_
201 ->current_replication_state()));
203 render_frame_proxy_created_ = true;
204 return true;
207 void RenderFrameProxyHost::UpdateOpener() {
208 // Another frame in this proxy's SiteInstance may reach the new opener by
209 // first reaching this proxy and then referencing its window.opener. Ensure
210 // the new opener's proxy exists in this case.
211 if (frame_tree_node_->opener()) {
212 frame_tree_node_->opener()->render_manager()->CreateOpenerProxies(
213 GetSiteInstance(), frame_tree_node_);
216 int opener_routing_id =
217 frame_tree_node_->render_manager()->GetOpenerRoutingID(GetSiteInstance());
218 Send(new FrameMsg_UpdateOpener(GetRoutingID(), opener_routing_id));
221 void RenderFrameProxyHost::OnDetach() {
222 if (frame_tree_node_->render_manager()->ForInnerDelegate()) {
223 // Only main frame proxy can detach for inner WebContents.
224 DCHECK(frame_tree_node_->IsMainFrame());
225 frame_tree_node_->render_manager()->RemoveOuterDelegateFrame();
226 return;
229 // This message should only be received for subframes. Note that we can't
230 // restrict it to just the current SiteInstances of the ancestors of this
231 // frame, because another frame in the tree may be able to detach this frame
232 // by navigating its parent.
233 if (frame_tree_node_->IsMainFrame()) {
234 bad_message::ReceivedBadMessage(GetProcess(), bad_message::RFPH_DETACH);
235 return;
237 frame_tree_node_->frame_tree()->RemoveFrame(frame_tree_node_);
240 void RenderFrameProxyHost::OnOpenURL(
241 const FrameHostMsg_OpenURL_Params& params) {
242 // TODO(creis): Verify that we are in the same BrowsingInstance as the current
243 // RenderFrameHost. See NavigatorImpl::RequestOpenURL.
244 frame_tree_node_->current_frame_host()->OpenURL(params, site_instance_.get());
247 void RenderFrameProxyHost::OnRouteMessageEvent(
248 const FrameMsg_PostMessage_Params& params) {
249 RenderFrameHostImpl* target_rfh = frame_tree_node()->current_frame_host();
251 // Only deliver the message if the request came from a RenderFrameHost in the
252 // same BrowsingInstance or if this WebContents is dedicated to a browser
253 // plugin guest.
255 // TODO(alexmos, lazyboy): The check for browser plugin guest currently
256 // requires going through the delegate. It should be refactored and
257 // performed here once OOPIF support in <webview> is further along.
258 SiteInstance* target_site_instance = target_rfh->GetSiteInstance();
259 if (!target_site_instance->IsRelatedSiteInstance(GetSiteInstance()) &&
260 !target_rfh->delegate()->ShouldRouteMessageEvent(target_rfh,
261 GetSiteInstance()))
262 return;
264 FrameMsg_PostMessage_Params new_params(params);
266 // If there is a source_routing_id, translate it to the routing ID of the
267 // equivalent RenderFrameProxyHost in the target process.
268 if (new_params.source_routing_id != MSG_ROUTING_NONE) {
269 RenderFrameHostImpl* source_rfh = RenderFrameHostImpl::FromID(
270 GetProcess()->GetID(), new_params.source_routing_id);
271 if (!source_rfh) {
272 new_params.source_routing_id = MSG_ROUTING_NONE;
273 } else {
274 // Ensure that we have a swapped-out RVH and proxy for the source frame
275 // in the target SiteInstance. If it doesn't exist, create it on demand
276 // and also create its opener chain, since that will also be accessible
277 // to the target page.
278 target_rfh->delegate()->EnsureOpenerProxiesExist(source_rfh);
280 // If the message source is a cross-process subframe, its proxy will only
281 // be created in --site-per-process mode. If the proxy wasn't created,
282 // set the source routing ID to MSG_ROUTING_NONE (see
283 // https://crbug.com/485520 for discussion on why this is ok).
284 RenderFrameProxyHost* source_proxy_in_target_site_instance =
285 source_rfh->frame_tree_node()
286 ->render_manager()
287 ->GetRenderFrameProxyHost(target_site_instance);
288 if (source_proxy_in_target_site_instance) {
289 new_params.source_routing_id =
290 source_proxy_in_target_site_instance->GetRoutingID();
291 } else {
292 new_params.source_routing_id = MSG_ROUTING_NONE;
297 if (!params.message_ports.empty()) {
298 // Updating the message port information has to be done in the IO thread;
299 // MessagePortMessageFilter::RouteMessageEventWithMessagePorts will send
300 // FrameMsg_PostMessageEvent after it's done. Note that a trivial solution
301 // would've been to post a task on the IO thread to do the IO-thread-bound
302 // work, and make that post a task back to WebContentsImpl in the UI
303 // thread. But we cannot do that, since there's nothing to guarantee that
304 // WebContentsImpl stays alive during the round trip.
305 scoped_refptr<MessagePortMessageFilter> message_port_message_filter(
306 static_cast<RenderProcessHostImpl*>(target_rfh->GetProcess())
307 ->message_port_message_filter());
308 BrowserThread::PostTask(
309 BrowserThread::IO, FROM_HERE,
310 base::Bind(&MessagePortMessageFilter::RouteMessageEventWithMessagePorts,
311 message_port_message_filter, target_rfh->GetRoutingID(),
312 new_params));
313 } else {
314 target_rfh->Send(
315 new FrameMsg_PostMessageEvent(target_rfh->GetRoutingID(), new_params));
319 void RenderFrameProxyHost::OnDidChangeOpener(int32 opener_routing_id) {
320 frame_tree_node_->render_manager()->DidChangeOpener(opener_routing_id,
321 GetSiteInstance());
324 } // namespace content