OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2012 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/browser_plugin/browser_plugin_host.h" | |
6 | |
7 #include "content/browser/browser_plugin/browser_plugin_host_helper.h" | |
8 #include "content/browser/renderer_host/render_view_host_impl.h" | |
9 #include "content/browser/web_contents/web_contents_impl.h" | |
10 #include "content/common/browser_plugin_messages.h" | |
11 #include "content/public/browser/notification_details.h" | |
12 #include "content/public/browser/notification_source.h" | |
13 #include "content/public/browser/notification_types.h" | |
14 #include "content/public/browser/render_process_host.h" | |
15 #include "content/public/browser/render_widget_host_view.h" | |
16 #include "content/public/browser/site_instance.h" | |
17 #include "content/public/browser/web_contents_view.h" | |
18 #include "ppapi/proxy/ppapi_messages.h" | |
19 | |
20 namespace content { | |
21 | |
22 BrowserPluginHost::BrowserPluginHost( | |
23 WebContentsImpl* web_contents) | |
24 : WebContentsObserver(web_contents), | |
25 embedder_render_process_host_(NULL), | |
26 instance_id_(0), | |
27 pending_render_view_host_(NULL) { | |
28 // Listen to visibility changes so that an embedder hides its guests | |
29 // as well. | |
30 registrar_.Add(this, | |
31 NOTIFICATION_WEB_CONTENTS_VISIBILITY_CHANGED, | |
32 Source<WebContents>(web_contents)); | |
33 // Construct plumbing helpers when a new RenderViewHost is created for | |
34 // this BrowserPluginHost's WebContentsImpl. | |
35 registrar_.Add(this, | |
36 NOTIFICATION_RENDER_VIEW_HOST_CREATED_FOR_TAB, | |
37 Source<WebContents>(web_contents)); | |
38 } | |
39 | |
40 BrowserPluginHost::~BrowserPluginHost() { | |
41 } | |
42 | |
43 BrowserPluginHost* BrowserPluginHost::GetGuestByContainerID(int container_id) { | |
44 ContainerInstanceMap::const_iterator it = | |
45 guests_by_container_id_.find(container_id); | |
46 if (it != guests_by_container_id_.end()) | |
47 return it->second; | |
48 return NULL; | |
49 } | |
50 | |
51 void BrowserPluginHost::RegisterContainerInstance( | |
52 int container_id, | |
53 BrowserPluginHost* observer) { | |
54 DCHECK(guests_by_container_id_.find(container_id) == | |
55 guests_by_container_id_.end()); | |
56 guests_by_container_id_[container_id] = observer; | |
57 } | |
58 | |
59 void BrowserPluginHost::OnPendingNavigation(RenderViewHost* dest_rvh) { | |
60 if (web_contents()->GetRenderViewHost() != dest_rvh) { | |
61 pending_render_view_host_ = dest_rvh; | |
62 } | |
63 } | |
64 | |
65 bool BrowserPluginHost::OnMessageReceived(const IPC::Message& message) { | |
66 bool handled = true; | |
67 IPC_BEGIN_MESSAGE_MAP(BrowserPluginHost, message) | |
68 IPC_MESSAGE_HANDLER(BrowserPluginHostMsg_NavigateFromGuest, | |
69 OnNavigateFromGuest) | |
70 IPC_MESSAGE_HANDLER(BrowserPluginHostMsg_MapInstance, | |
71 OnMapInstance) | |
72 IPC_MESSAGE_UNHANDLED(handled = false) | |
73 IPC_END_MESSAGE_MAP() | |
74 return handled; | |
75 } | |
76 | |
77 void BrowserPluginHost::NavigateGuestFromEmbedder( | |
78 RenderViewHost* render_view_host, | |
79 int container_instance_id, | |
80 long long frame_id, | |
81 const std::string& src, | |
82 const gfx::Size& size) { | |
83 BrowserPluginHost* guest_observer = | |
84 GetGuestByContainerID(container_instance_id); | |
85 WebContentsImpl* guest_web_contents = | |
86 guest_observer ? | |
87 static_cast<WebContentsImpl*>(guest_observer->web_contents()): NULL; | |
88 if (!guest_observer) { | |
89 guest_web_contents = | |
90 static_cast<WebContentsImpl*>( | |
91 WebContents::Create( | |
92 web_contents()->GetBrowserContext(), | |
93 render_view_host->GetSiteInstance(), | |
94 MSG_ROUTING_NONE, | |
95 NULL, // base WebContents | |
96 NULL // session storage namespace | |
97 )); | |
98 guest_observer = | |
99 guest_web_contents->browser_plugin_host(); | |
100 guest_observer->set_embedder_render_process_host( | |
101 render_view_host->GetProcess()); | |
102 guest_observer->set_instance_id(container_instance_id); | |
103 guest_observer->set_initial_size(size); | |
104 RegisterContainerInstance(container_instance_id, guest_observer); | |
105 AddGuest(guest_web_contents, frame_id); | |
106 } | |
107 | |
108 GURL url(src); | |
109 guest_observer->web_contents()->SetDelegate(guest_observer); | |
110 guest_observer->web_contents()->GetController().LoadURL( | |
111 url, | |
112 Referrer(), | |
113 PAGE_TRANSITION_AUTO_SUBFRAME, | |
114 std::string()); | |
115 } | |
116 | |
117 void BrowserPluginHost::OnNavigateFromGuest( | |
118 PP_Instance instance, | |
119 const std::string& src) { | |
120 DCHECK(embedder_render_process_host()); | |
121 GURL url(src); | |
122 web_contents()->GetController().LoadURL( | |
123 url, | |
124 Referrer(), | |
125 PAGE_TRANSITION_AUTO_SUBFRAME, | |
126 std::string()); | |
127 } | |
128 | |
129 // TODO(fsamuel): This handler is all kinds of bad and could be racy. | |
130 // Between the time we set the pending_render_view_host and use it here, | |
131 // the pending_render_view_host may no longer be valid and this message | |
132 // may not go to the right place. | |
133 // See https://code.google.com/p/chromium/issues/detail?id=128976. | |
134 // The correct solution is probably to send | |
135 // "BrowserPluginMsg_CompleteNavigation" over the pepper channel to the guest | |
136 // and then have the guest send the browser "BrowserPluginHostMsg_ResizeGuest" | |
137 // to resize appropriately. | |
138 void BrowserPluginHost::OnMapInstance(int container_instance_id, | |
139 PP_Instance instance) { | |
140 BrowserPluginHost* guest_observer = | |
141 GetGuestByContainerID(container_instance_id); | |
142 WebContentsImpl* guest_web_contents = | |
143 static_cast<WebContentsImpl*>(guest_observer->web_contents()); | |
144 RenderViewHost* rvh = guest_observer->pending_render_view_host() ? | |
145 guest_observer->pending_render_view_host() : | |
146 guest_web_contents->GetRenderViewHost(); | |
147 | |
148 guest_web_contents->GetView()->SizeContents(guest_observer->initial_size()); | |
149 | |
150 rvh->Send(new BrowserPluginMsg_CompleteNavigation( | |
151 rvh->GetRoutingID(), | |
152 instance)); | |
153 } | |
154 | |
155 void BrowserPluginHost::ConnectEmbedderToChannel( | |
156 RenderViewHost* render_view_host, | |
157 const IPC::ChannelHandle& channel_handle) { | |
158 DCHECK(embedder_render_process_host()); | |
159 // Tell the BrowserPlugin in the embedder that we're done and that it can | |
160 // begin using the guest renderer. | |
161 embedder_render_process_host()->Send( | |
162 new BrowserPluginMsg_LoadGuest( | |
163 instance_id(), | |
164 render_view_host->GetProcess()-> | |
165 GetID(), | |
166 channel_handle)); | |
167 } | |
168 | |
169 void BrowserPluginHost::AddGuest(WebContentsImpl* guest, int64 frame_id) { | |
170 guests_[guest] = frame_id; | |
171 } | |
172 | |
173 void BrowserPluginHost::RemoveGuest(WebContentsImpl* guest) { | |
174 guests_.erase(guest); | |
175 } | |
176 | |
177 void BrowserPluginHost::DestroyGuests() { | |
178 for (GuestMap::const_iterator it = guests_.begin(); | |
179 it != guests_.end(); ++it) { | |
180 WebContentsImpl* web_contents = it->first; | |
181 delete web_contents; | |
182 } | |
183 guests_.clear(); | |
184 guests_by_container_id_.clear(); | |
185 } | |
186 | |
187 void BrowserPluginHost::DidCommitProvisionalLoadForFrame( | |
188 int64 frame_id, | |
189 bool is_main_frame, | |
190 const GURL& url, | |
191 PageTransition transition_type, | |
192 RenderViewHost* render_view_host) { | |
193 // Clean-up guests that lie in the frame that we're navigating. | |
194 typedef std::set<WebContentsImpl*> GuestSet; | |
195 GuestSet guests_to_delete; | |
196 for (GuestMap::const_iterator it = guests_.begin(); | |
197 it != guests_.end(); ++it) { | |
198 WebContentsImpl* web_contents = it->first; | |
199 if (it->second == frame_id) { | |
200 guests_to_delete.insert(web_contents); | |
201 } | |
202 } | |
203 for (GuestSet::const_iterator it = guests_to_delete.begin(); | |
204 it != guests_to_delete.end(); ++it) { | |
205 delete *it; | |
206 guests_.erase(*it); | |
207 } | |
208 } | |
209 | |
210 void BrowserPluginHost::RenderViewDeleted(RenderViewHost* render_view_host) { | |
211 // TODO(fsamuel): For some reason ToT hangs when killing a guest, this wasn't | |
212 // the case earlier. Presumably, when a guest is killed/crashes, one of | |
213 // RenderViewDeleted, RenderViewGone or WebContentsDestroyed will get called. | |
214 // At that point, we should remove reference to this BrowserPluginHost | |
215 // from its embedder, in addition to destroying its guests, to ensure | |
216 // that we do not attempt a double delete. | |
217 DestroyGuests(); | |
218 } | |
219 | |
220 void BrowserPluginHost::RenderViewGone(base::TerminationStatus status) { | |
221 DestroyGuests(); | |
222 } | |
223 | |
224 void BrowserPluginHost::WebContentsDestroyed(WebContents* web_contents) { | |
225 DestroyGuests(); | |
226 } | |
227 | |
228 void BrowserPluginHost::Observe( | |
229 int type, | |
230 const NotificationSource& source, | |
231 const NotificationDetails& details) { | |
232 switch (type) { | |
233 case NOTIFICATION_RENDER_VIEW_HOST_CREATED_FOR_TAB: { | |
234 RenderViewHost* render_view_host = | |
235 Details<RenderViewHost>(details).ptr(); | |
236 new BrowserPluginHostHelper(this, render_view_host); | |
Charlie Reis
2012/05/21 18:20:44
Who owns this, and when will it be deleted?
jam
2012/05/21 18:30:24
RenderViewHostObservers get deleted automatically
Fady Samuel
2012/05/21 19:22:17
Added a comment.
| |
237 break; | |
238 } | |
239 case NOTIFICATION_WEB_CONTENTS_VISIBILITY_CHANGED: { | |
240 bool visible = *Details<bool>(details).ptr(); | |
241 // If the embedder is hidden we need to hide the guests as well. | |
242 for (GuestMap::const_iterator it = guests_.begin(); | |
243 it != guests_.end(); ++it) { | |
244 WebContentsImpl* web_contents = it->first; | |
245 if (visible) | |
246 web_contents->ShowContents(); | |
247 else | |
248 web_contents->HideContents(); | |
249 } | |
250 break; | |
251 } | |
252 default: | |
253 NOTREACHED() << "Unexpected notification type: " << type; | |
254 } | |
255 } | |
256 | |
257 } // namespace content | |
OLD | NEW |