Add long running gmail memory benchmark for background tab.
[chromium-blink-merge.git] / content / browser / frame_host / render_frame_proxy_host.cc
blob5406cc86cd4c19128835945ceaaa1cb4e71750f3
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 if (render_view_host)
63 frame_tree_node_->frame_tree()->AddRenderViewHostRef(render_view_host_);
65 bool is_proxy_to_parent = !frame_tree_node_->IsMainFrame() &&
66 frame_tree_node_->parent()
67 ->render_manager()
68 ->current_frame_host()
69 ->GetSiteInstance() == site_instance;
71 // If this is a proxy to parent frame or this proxy is for the inner
72 // WebContents's FrameTreeNode in outer WebContents's SiteInstance, then we
73 // need a CrossProcessFrameConnector.
74 if (is_proxy_to_parent ||
75 frame_tree_node_->render_manager()->ForInnerDelegate()) {
76 // The RenderFrameHost navigating cross-process is destroyed and a proxy for
77 // it is created in the parent's process. CrossProcessFrameConnector
78 // initialization only needs to happen on an initial cross-process
79 // navigation, when the RenderFrameHost leaves the same process as its
80 // parent. The same CrossProcessFrameConnector is used for subsequent cross-
81 // process navigations, but it will be destroyed if the frame is
82 // navigated back to the same SiteInstance as its parent.
83 cross_process_frame_connector_.reset(new CrossProcessFrameConnector(this));
87 RenderFrameProxyHost::~RenderFrameProxyHost() {
88 if (GetProcess()->HasConnection()) {
89 // TODO(nasko): For now, don't send this IPC for top-level frames, as
90 // the top-level RenderFrame will delete the RenderFrameProxy.
91 // This can be removed once we don't have a swapped out state on
92 // RenderFrame. See https://crbug.com/357747
93 if (!frame_tree_node_->IsMainFrame())
94 Send(new FrameMsg_DeleteProxy(routing_id_));
97 if (render_view_host_)
98 frame_tree_node_->frame_tree()->ReleaseRenderViewHostRef(render_view_host_);
99 GetProcess()->RemoveRoute(routing_id_);
100 g_routing_id_frame_proxy_map.Get().erase(
101 RenderFrameProxyHostID(GetProcess()->GetID(), routing_id_));
104 void RenderFrameProxyHost::SetChildRWHView(RenderWidgetHostView* view) {
105 cross_process_frame_connector_->set_view(
106 static_cast<RenderWidgetHostViewChildFrame*>(view));
109 RenderViewHostImpl* RenderFrameProxyHost::GetRenderViewHost() {
110 return frame_tree_node_->frame_tree()->GetRenderViewHost(
111 site_instance_.get());
114 RenderWidgetHostView* RenderFrameProxyHost::GetRenderWidgetHostView() {
115 return frame_tree_node_->parent()->render_manager()
116 ->GetRenderWidgetHostView();
119 void RenderFrameProxyHost::TakeFrameHostOwnership(
120 scoped_ptr<RenderFrameHostImpl> render_frame_host) {
121 CHECK(render_frame_host_ == nullptr);
122 render_frame_host_ = render_frame_host.Pass();
123 render_frame_host_->set_render_frame_proxy_host(this);
126 scoped_ptr<RenderFrameHostImpl> RenderFrameProxyHost::PassFrameHostOwnership() {
127 render_frame_host_->set_render_frame_proxy_host(NULL);
128 return render_frame_host_.Pass();
131 bool RenderFrameProxyHost::Send(IPC::Message *msg) {
132 return GetProcess()->Send(msg);
135 bool RenderFrameProxyHost::OnMessageReceived(const IPC::Message& msg) {
136 if (cross_process_frame_connector_.get() &&
137 cross_process_frame_connector_->OnMessageReceived(msg))
138 return true;
140 bool handled = true;
141 IPC_BEGIN_MESSAGE_MAP(RenderFrameProxyHost, msg)
142 IPC_MESSAGE_HANDLER(FrameHostMsg_Detach, OnDetach)
143 IPC_MESSAGE_HANDLER(FrameHostMsg_OpenURL, OnOpenURL)
144 IPC_MESSAGE_HANDLER(FrameHostMsg_RouteMessageEvent, OnRouteMessageEvent)
145 IPC_MESSAGE_UNHANDLED(handled = false)
146 IPC_END_MESSAGE_MAP()
147 return handled;
150 bool RenderFrameProxyHost::InitRenderFrameProxy() {
151 DCHECK(!render_frame_proxy_created_);
153 // It is possible to reach this when the process is dead (in particular, when
154 // creating proxies from CreateProxiesForChildFrame). In that case, don't
155 // create the proxy. The process shouldn't be resurrected just to create
156 // RenderFrameProxies; it should be restored only if it needs to host a
157 // RenderFrame. When that happens, the process will be reinitialized, and
158 // all necessary proxies, including any of the ones we skipped here, will be
159 // created by CreateProxiesForSiteInstance. See https://crbug.com/476846
160 if (!GetProcess()->HasConnection())
161 return false;
163 int parent_routing_id = MSG_ROUTING_NONE;
164 if (frame_tree_node_->parent()) {
165 // It is safe to use GetRenderFrameProxyHost to get the parent proxy, since
166 // new child frames always start out as local frames, so a new proxy should
167 // never have a RenderFrameHost as a parent.
168 RenderFrameProxyHost* parent_proxy =
169 frame_tree_node_->parent()->render_manager()->GetRenderFrameProxyHost(
170 site_instance_.get());
171 CHECK(parent_proxy);
173 // Proxies that aren't live in the parent node should not be initialized
174 // here, since there is no valid parent RenderFrameProxy on the renderer
175 // side. This can happen when adding a new child frame after an opener
176 // process crashed and was reloaded. See https://crbug.com/501152.
177 if (!parent_proxy->is_render_frame_proxy_live())
178 return false;
180 parent_routing_id = parent_proxy->GetRoutingID();
181 CHECK_NE(parent_routing_id, MSG_ROUTING_NONE);
184 Send(new FrameMsg_NewFrameProxy(routing_id_,
185 parent_routing_id,
186 frame_tree_node_->frame_tree()
187 ->GetRenderViewHost(site_instance_.get())
188 ->GetRoutingID(),
189 frame_tree_node_
190 ->current_replication_state()));
192 render_frame_proxy_created_ = true;
193 return true;
196 void RenderFrameProxyHost::DisownOpener() {
197 Send(new FrameMsg_DisownOpener(GetRoutingID()));
200 void RenderFrameProxyHost::OnDetach() {
201 if (frame_tree_node_->render_manager()->ForInnerDelegate()) {
202 frame_tree_node_->render_manager()->RemoveOuterDelegateFrame();
203 return;
206 // This message should only be received for subframes. Note that we can't
207 // restrict it to just the current SiteInstances of the ancestors of this
208 // frame, because another frame in the tree may be able to detach this frame
209 // by navigating its parent.
210 if (frame_tree_node_->IsMainFrame()) {
211 bad_message::ReceivedBadMessage(GetProcess(), bad_message::RFPH_DETACH);
212 return;
214 frame_tree_node_->frame_tree()->RemoveFrame(frame_tree_node_);
217 void RenderFrameProxyHost::OnOpenURL(
218 const FrameHostMsg_OpenURL_Params& params) {
219 // TODO(creis): Verify that we are in the same BrowsingInstance as the current
220 // RenderFrameHost. See NavigatorImpl::RequestOpenURL.
221 frame_tree_node_->current_frame_host()->OpenURL(params, site_instance_.get());
224 void RenderFrameProxyHost::OnRouteMessageEvent(
225 const FrameMsg_PostMessage_Params& params) {
226 RenderFrameHostImpl* target_rfh = frame_tree_node()->current_frame_host();
228 // Only deliver the message if the request came from a RenderFrameHost in the
229 // same BrowsingInstance or if this WebContents is dedicated to a browser
230 // plugin guest.
232 // TODO(alexmos, lazyboy): The check for browser plugin guest currently
233 // requires going through the delegate. It should be refactored and
234 // performed here once OOPIF support in <webview> is further along.
235 SiteInstance* target_site_instance = target_rfh->GetSiteInstance();
236 if (!target_site_instance->IsRelatedSiteInstance(GetSiteInstance()) &&
237 !target_rfh->delegate()->ShouldRouteMessageEvent(target_rfh,
238 GetSiteInstance()))
239 return;
241 FrameMsg_PostMessage_Params new_params(params);
243 // If there is a source_routing_id, translate it to the routing ID of the
244 // equivalent RenderFrameProxyHost in the target process.
245 if (new_params.source_routing_id != MSG_ROUTING_NONE) {
246 RenderFrameHostImpl* source_rfh = RenderFrameHostImpl::FromID(
247 GetProcess()->GetID(), new_params.source_routing_id);
248 if (!source_rfh) {
249 new_params.source_routing_id = MSG_ROUTING_NONE;
250 } else {
251 // Ensure that we have a swapped-out RVH and proxy for the source frame
252 // in the target SiteInstance. If it doesn't exist, create it on demand
253 // and also create its opener chain, since that will also be accessible
254 // to the target page.
255 target_rfh->delegate()->EnsureOpenerProxiesExist(source_rfh);
257 // If the message source is a cross-process subframe, its proxy will only
258 // be created in --site-per-process mode. If the proxy wasn't created,
259 // set the source routing ID to MSG_ROUTING_NONE (see
260 // https://crbug.com/485520 for discussion on why this is ok).
261 RenderFrameProxyHost* source_proxy_in_target_site_instance =
262 source_rfh->frame_tree_node()
263 ->render_manager()
264 ->GetRenderFrameProxyHost(target_site_instance);
265 if (source_proxy_in_target_site_instance) {
266 new_params.source_routing_id =
267 source_proxy_in_target_site_instance->GetRoutingID();
268 } else {
269 new_params.source_routing_id = MSG_ROUTING_NONE;
274 if (!params.message_ports.empty()) {
275 // Updating the message port information has to be done in the IO thread;
276 // MessagePortMessageFilter::RouteMessageEventWithMessagePorts will send
277 // FrameMsg_PostMessageEvent after it's done. Note that a trivial solution
278 // would've been to post a task on the IO thread to do the IO-thread-bound
279 // work, and make that post a task back to WebContentsImpl in the UI
280 // thread. But we cannot do that, since there's nothing to guarantee that
281 // WebContentsImpl stays alive during the round trip.
282 scoped_refptr<MessagePortMessageFilter> message_port_message_filter(
283 static_cast<RenderProcessHostImpl*>(target_rfh->GetProcess())
284 ->message_port_message_filter());
285 BrowserThread::PostTask(
286 BrowserThread::IO, FROM_HERE,
287 base::Bind(&MessagePortMessageFilter::RouteMessageEventWithMessagePorts,
288 message_port_message_filter, target_rfh->GetRoutingID(),
289 new_params));
290 } else {
291 target_rfh->Send(
292 new FrameMsg_PostMessageEvent(target_rfh->GetRoutingID(), new_params));
296 } // namespace content