Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / content / browser / frame_host / render_frame_proxy_host.cc
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.
4
5 #include "content/browser/frame_host/render_frame_proxy_host.h"
6
7 #include "base/lazy_instance.h"
8 #include "content/browser/frame_host/cross_process_frame_connector.h"
9 #include "content/browser/frame_host/frame_tree.h"
10 #include "content/browser/frame_host/frame_tree_node.h"
11 #include "content/browser/frame_host/render_frame_host_impl.h"
12 #include "content/browser/frame_host/render_widget_host_view_child_frame.h"
13 #include "content/browser/renderer_host/render_view_host_impl.h"
14 #include "content/browser/renderer_host/render_widget_host_view_base.h"
15 #include "content/browser/site_instance_impl.h"
16 #include "content/common/frame_messages.h"
17 #include "content/public/browser/browser_thread.h"
18 #include "ipc/ipc_message.h"
19
20 namespace content {
21
22 namespace {
23
24 // The (process id, routing id) pair that identifies one RenderFrameProxy.
25 typedef std::pair<int32, int32> RenderFrameProxyHostID;
26 typedef base::hash_map<RenderFrameProxyHostID, RenderFrameProxyHost*>
27     RoutingIDFrameProxyMap;
28 base::LazyInstance<RoutingIDFrameProxyMap> g_routing_id_frame_proxy_map =
29   LAZY_INSTANCE_INITIALIZER;
30
31 }
32
33 // static
34 RenderFrameProxyHost* RenderFrameProxyHost::FromID(int process_id,
35                                                    int routing_id) {
36   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
37   RoutingIDFrameProxyMap* frames = g_routing_id_frame_proxy_map.Pointer();
38   RoutingIDFrameProxyMap::iterator it = frames->find(
39       RenderFrameProxyHostID(process_id, routing_id));
40   return it == frames->end() ? NULL : it->second;
41 }
42
43 RenderFrameProxyHost::RenderFrameProxyHost(SiteInstance* site_instance,
44                                            FrameTreeNode* frame_tree_node)
45     : routing_id_(site_instance->GetProcess()->GetNextRoutingID()),
46       site_instance_(site_instance),
47       frame_tree_node_(frame_tree_node) {
48   GetProcess()->AddRoute(routing_id_, this);
49   CHECK(g_routing_id_frame_proxy_map.Get().insert(
50       std::make_pair(
51           RenderFrameProxyHostID(GetProcess()->GetID(), routing_id_),
52           this)).second);
53
54   if (!frame_tree_node_->IsMainFrame() &&
55       frame_tree_node_->parent()
56               ->render_manager()
57               ->current_frame_host()
58               ->GetSiteInstance() == site_instance) {
59     // The RenderFrameHost navigating cross-process is destroyed and a proxy for
60     // it is created in the parent's process. CrossProcessFrameConnector
61     // initialization only needs to happen on an initial cross-process
62     // navigation, when the RenderFrameHost leaves the same process as its
63     // parent. The same CrossProcessFrameConnector is used for subsequent cross-
64     // process navigations, but it will be destroyed if the frame is
65     // navigated back to the same SiteInstance as its parent.
66     cross_process_frame_connector_.reset(new CrossProcessFrameConnector(this));
67   }
68 }
69
70 RenderFrameProxyHost::~RenderFrameProxyHost() {
71   if (GetProcess()->HasConnection()) {
72     // TODO(nasko): For now, don't send this IPC for top-level frames, as
73     // the top-level RenderFrame will delete the RenderFrameProxy.
74     // This can be removed once we don't have a swapped out state on
75     // RenderFrame. See https://crbug.com/357747
76     if (!frame_tree_node_->IsMainFrame())
77       Send(new FrameMsg_DeleteProxy(routing_id_));
78   }
79
80   GetProcess()->RemoveRoute(routing_id_);
81   g_routing_id_frame_proxy_map.Get().erase(
82       RenderFrameProxyHostID(GetProcess()->GetID(), routing_id_));
83 }
84
85 void RenderFrameProxyHost::SetChildRWHView(RenderWidgetHostView* view) {
86   cross_process_frame_connector_->set_view(
87       static_cast<RenderWidgetHostViewChildFrame*>(view));
88 }
89
90 RenderViewHostImpl* RenderFrameProxyHost::GetRenderViewHost() {
91   return frame_tree_node_->frame_tree()->GetRenderViewHost(
92       site_instance_.get());
93 }
94
95 void RenderFrameProxyHost::TakeFrameHostOwnership(
96     scoped_ptr<RenderFrameHostImpl> render_frame_host) {
97   render_frame_host_ = render_frame_host.Pass();
98   render_frame_host_->set_render_frame_proxy_host(this);
99 }
100
101 scoped_ptr<RenderFrameHostImpl> RenderFrameProxyHost::PassFrameHostOwnership() {
102   render_frame_host_->set_render_frame_proxy_host(NULL);
103   return render_frame_host_.Pass();
104 }
105
106 bool RenderFrameProxyHost::Send(IPC::Message *msg) {
107   // TODO(nasko): For now, RenderFrameHost uses this object to send IPC messages
108   // while swapped out. This can be removed once we don't have a swapped out
109   // state on RenderFrameHosts. See https://crbug.com/357747.
110
111   // Don't reset the routing ID for control messages.  See
112   // https://crbug.com/423538
113   if (msg->routing_id() != MSG_ROUTING_CONTROL)
114     msg->set_routing_id(routing_id_);
115   return GetProcess()->Send(msg);
116 }
117
118 bool RenderFrameProxyHost::OnMessageReceived(const IPC::Message& msg) {
119   if (cross_process_frame_connector_.get() &&
120       cross_process_frame_connector_->OnMessageReceived(msg))
121     return true;
122
123   // TODO(nasko): This can be removed once we don't have a swapped out state on
124   // RenderFrameHosts. See https://crbug.com/357747.
125   if (render_frame_host_.get())
126     return render_frame_host_->OnMessageReceived(msg);
127
128   bool handled = true;
129   IPC_BEGIN_MESSAGE_MAP(RenderFrameProxyHost, msg)
130     IPC_MESSAGE_HANDLER(FrameHostMsg_OpenURL, OnOpenURL)
131     IPC_MESSAGE_UNHANDLED(handled = false)
132   IPC_END_MESSAGE_MAP()
133   return handled;
134 }
135
136 bool RenderFrameProxyHost::InitRenderFrameProxy() {
137   // The process may (if we're sharing a process with another host that already
138   // initialized it) or may not (we have our own process or the old process
139   // crashed) have been initialized. Calling Init multiple times will be
140   // ignored, so this is safe.
141   if (!site_instance_->GetProcess()->Init())
142     return false;
143
144   DCHECK(GetProcess()->HasConnection());
145
146   int parent_routing_id = MSG_ROUTING_NONE;
147   if (frame_tree_node_->parent()) {
148     parent_routing_id = frame_tree_node_->parent()
149                             ->render_manager()
150                             ->GetRoutingIdForSiteInstance(site_instance_.get());
151     CHECK_NE(parent_routing_id, MSG_ROUTING_NONE);
152   }
153
154   Send(new FrameMsg_NewFrameProxy(routing_id_,
155                                   parent_routing_id,
156                                   frame_tree_node_->frame_tree()
157                                       ->GetRenderViewHost(site_instance_.get())
158                                       ->GetRoutingID()));
159
160   return true;
161 }
162
163 void RenderFrameProxyHost::DisownOpener() {
164   Send(new FrameMsg_DisownOpener(GetRoutingID()));
165 }
166
167 void RenderFrameProxyHost::OnOpenURL(
168     const FrameHostMsg_OpenURL_Params& params) {
169   frame_tree_node_->current_frame_host()->OpenURL(params);
170 }
171
172 }  // namespace content