Add ICU message format support
[chromium-blink-merge.git] / content / browser / frame_host / render_frame_proxy_host.cc
bloba83e96ba5dd31c9d2b575d146b32498ac1886c69
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_UNHANDLED(handled = false)
149 IPC_END_MESSAGE_MAP()
150 return handled;
153 bool RenderFrameProxyHost::InitRenderFrameProxy() {
154 DCHECK(!render_frame_proxy_created_);
156 // It is possible to reach this when the process is dead (in particular, when
157 // creating proxies from CreateProxiesForChildFrame). In that case, don't
158 // create the proxy. The process shouldn't be resurrected just to create
159 // RenderFrameProxies; it should be restored only if it needs to host a
160 // RenderFrame. When that happens, the process will be reinitialized, and
161 // all necessary proxies, including any of the ones we skipped here, will be
162 // created by CreateProxiesForSiteInstance. See https://crbug.com/476846
163 if (!GetProcess()->HasConnection())
164 return false;
166 int parent_routing_id = MSG_ROUTING_NONE;
167 if (frame_tree_node_->parent()) {
168 // It is safe to use GetRenderFrameProxyHost to get the parent proxy, since
169 // new child frames always start out as local frames, so a new proxy should
170 // never have a RenderFrameHost as a parent.
171 RenderFrameProxyHost* parent_proxy =
172 frame_tree_node_->parent()->render_manager()->GetRenderFrameProxyHost(
173 site_instance_.get());
174 CHECK(parent_proxy);
176 // Proxies that aren't live in the parent node should not be initialized
177 // here, since there is no valid parent RenderFrameProxy on the renderer
178 // side. This can happen when adding a new child frame after an opener
179 // process crashed and was reloaded. See https://crbug.com/501152.
180 if (!parent_proxy->is_render_frame_proxy_live())
181 return false;
183 parent_routing_id = parent_proxy->GetRoutingID();
184 CHECK_NE(parent_routing_id, MSG_ROUTING_NONE);
187 Send(new FrameMsg_NewFrameProxy(routing_id_,
188 parent_routing_id,
189 frame_tree_node_->frame_tree()
190 ->GetRenderViewHost(site_instance_.get())
191 ->GetRoutingID(),
192 frame_tree_node_
193 ->current_replication_state()));
195 render_frame_proxy_created_ = true;
196 return true;
199 void RenderFrameProxyHost::DisownOpener() {
200 Send(new FrameMsg_DisownOpener(GetRoutingID()));
203 void RenderFrameProxyHost::OnDetach() {
204 if (frame_tree_node_->render_manager()->ForInnerDelegate()) {
205 // Only main frame proxy can detach for inner WebContents.
206 DCHECK(frame_tree_node_->IsMainFrame());
207 frame_tree_node_->render_manager()->RemoveOuterDelegateFrame();
208 return;
211 // This message should only be received for subframes. Note that we can't
212 // restrict it to just the current SiteInstances of the ancestors of this
213 // frame, because another frame in the tree may be able to detach this frame
214 // by navigating its parent.
215 if (frame_tree_node_->IsMainFrame()) {
216 bad_message::ReceivedBadMessage(GetProcess(), bad_message::RFPH_DETACH);
217 return;
219 frame_tree_node_->frame_tree()->RemoveFrame(frame_tree_node_);
222 void RenderFrameProxyHost::OnOpenURL(
223 const FrameHostMsg_OpenURL_Params& params) {
224 // TODO(creis): Verify that we are in the same BrowsingInstance as the current
225 // RenderFrameHost. See NavigatorImpl::RequestOpenURL.
226 frame_tree_node_->current_frame_host()->OpenURL(params, site_instance_.get());
229 void RenderFrameProxyHost::OnRouteMessageEvent(
230 const FrameMsg_PostMessage_Params& params) {
231 RenderFrameHostImpl* target_rfh = frame_tree_node()->current_frame_host();
233 // Only deliver the message if the request came from a RenderFrameHost in the
234 // same BrowsingInstance or if this WebContents is dedicated to a browser
235 // plugin guest.
237 // TODO(alexmos, lazyboy): The check for browser plugin guest currently
238 // requires going through the delegate. It should be refactored and
239 // performed here once OOPIF support in <webview> is further along.
240 SiteInstance* target_site_instance = target_rfh->GetSiteInstance();
241 if (!target_site_instance->IsRelatedSiteInstance(GetSiteInstance()) &&
242 !target_rfh->delegate()->ShouldRouteMessageEvent(target_rfh,
243 GetSiteInstance()))
244 return;
246 FrameMsg_PostMessage_Params new_params(params);
248 // If there is a source_routing_id, translate it to the routing ID of the
249 // equivalent RenderFrameProxyHost in the target process.
250 if (new_params.source_routing_id != MSG_ROUTING_NONE) {
251 RenderFrameHostImpl* source_rfh = RenderFrameHostImpl::FromID(
252 GetProcess()->GetID(), new_params.source_routing_id);
253 if (!source_rfh) {
254 new_params.source_routing_id = MSG_ROUTING_NONE;
255 } else {
256 // Ensure that we have a swapped-out RVH and proxy for the source frame
257 // in the target SiteInstance. If it doesn't exist, create it on demand
258 // and also create its opener chain, since that will also be accessible
259 // to the target page.
260 target_rfh->delegate()->EnsureOpenerProxiesExist(source_rfh);
262 // If the message source is a cross-process subframe, its proxy will only
263 // be created in --site-per-process mode. If the proxy wasn't created,
264 // set the source routing ID to MSG_ROUTING_NONE (see
265 // https://crbug.com/485520 for discussion on why this is ok).
266 RenderFrameProxyHost* source_proxy_in_target_site_instance =
267 source_rfh->frame_tree_node()
268 ->render_manager()
269 ->GetRenderFrameProxyHost(target_site_instance);
270 if (source_proxy_in_target_site_instance) {
271 new_params.source_routing_id =
272 source_proxy_in_target_site_instance->GetRoutingID();
273 } else {
274 new_params.source_routing_id = MSG_ROUTING_NONE;
279 if (!params.message_ports.empty()) {
280 // Updating the message port information has to be done in the IO thread;
281 // MessagePortMessageFilter::RouteMessageEventWithMessagePorts will send
282 // FrameMsg_PostMessageEvent after it's done. Note that a trivial solution
283 // would've been to post a task on the IO thread to do the IO-thread-bound
284 // work, and make that post a task back to WebContentsImpl in the UI
285 // thread. But we cannot do that, since there's nothing to guarantee that
286 // WebContentsImpl stays alive during the round trip.
287 scoped_refptr<MessagePortMessageFilter> message_port_message_filter(
288 static_cast<RenderProcessHostImpl*>(target_rfh->GetProcess())
289 ->message_port_message_filter());
290 BrowserThread::PostTask(
291 BrowserThread::IO, FROM_HERE,
292 base::Bind(&MessagePortMessageFilter::RouteMessageEventWithMessagePorts,
293 message_port_message_filter, target_rfh->GetRoutingID(),
294 new_params));
295 } else {
296 target_rfh->Send(
297 new FrameMsg_PostMessageEvent(target_rfh->GetRoutingID(), new_params));
301 } // namespace content