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_web_contents_observer.h" | |
6 | |
7 #include "base/lazy_instance.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/browser_thread.h" | |
12 #include "content/public/browser/notification_details.h" | |
13 #include "content/public/browser/notification_source.h" | |
14 #include "content/public/browser/notification_types.h" | |
15 #include "content/public/browser/render_process_host.h" | |
16 #include "content/public/browser/render_widget_host.h" | |
17 #include "content/public/browser/render_widget_host_view.h" | |
18 #include "content/public/browser/render_view_host.h" | |
19 #include "content/public/browser/site_instance.h" | |
20 #include "content/public/browser/web_contents.h" | |
21 #include "ppapi/proxy/ppapi_messages.h" | |
22 | |
23 namespace content { | |
24 | |
25 BrowserPluginWebContentsObserver::BrowserPluginWebContentsObserver( | |
26 WebContentsImpl* web_contents) | |
27 : WebContentsObserver(web_contents), | |
28 host_(NULL), | |
29 instance_id_(0) { | |
30 registrar_.Add(this, | |
31 NOTIFICATION_RENDER_WIDGET_VISIBILITY_CHANGED, | |
32 Source<RenderViewHost>( | |
33 web_contents->GetRenderViewHost())); | |
34 } | |
35 | |
36 BrowserPluginWebContentsObserver::~BrowserPluginWebContentsObserver() { | |
37 } | |
38 | |
39 bool BrowserPluginWebContentsObserver::OnMessageReceived( | |
40 const IPC::Message& message) { | |
41 bool handled = true; | |
42 IPC_BEGIN_MESSAGE_MAP(BrowserPluginWebContentsObserver, message) | |
43 IPC_MESSAGE_HANDLER(BrowserPluginHostMsg_ChannelCreated, | |
44 OnRendererPluginChannelCreated) | |
45 IPC_MESSAGE_HANDLER(BrowserPluginHostMsg_OpenChannel, | |
46 OnOpenChannelToBrowserPlugin) | |
47 IPC_MESSAGE_HANDLER(BrowserPluginHostMsg_GuestReady, OnGuestReady) | |
48 IPC_MESSAGE_HANDLER(BrowserPluginHostMsg_ResizeGuest, OnResizeGuest) | |
49 IPC_MESSAGE_UNHANDLED(handled = false) | |
50 IPC_END_MESSAGE_MAP() | |
51 return handled; | |
52 } | |
53 | |
54 void BrowserPluginWebContentsObserver::OnGuestReady() { | |
55 // We only care about handling this message on guests. | |
56 if (!host()) | |
57 return; | |
58 | |
59 // The renderer is now ready to receive ppapi messages. | |
60 // Let's tell it create a channel with the embedder/host. | |
61 BrowserPluginMsg_CreateChannel* msg = | |
62 new BrowserPluginMsg_CreateChannel( | |
63 host()->GetRenderProcessHost()->GetHandle(), | |
64 host()->GetRenderProcessHost()->GetID()); | |
65 msg->set_routing_id(web_contents()->GetRenderViewHost()->GetRoutingID()); | |
66 // TODO(fsamuel): If we aren't able to successfully send this message | |
67 // to the guest then that probably means it crashed. Is there anything | |
68 // we can do that's cleaner than failing a check? | |
69 CHECK(Send(msg)); | |
70 } | |
71 | |
72 void BrowserPluginWebContentsObserver::OnResizeGuest( | |
73 int width, int height) { | |
74 // Tell the RenderWidgetHostView to adjust its size. | |
75 web_contents()->GetRenderViewHost()->GetView()->SetSize( | |
76 gfx::Size(width, height)); | |
77 } | |
78 | |
79 void BrowserPluginWebContentsObserver::OnOpenChannelToBrowserPlugin( | |
80 int instance_id, | |
81 long long frame_id, | |
82 const std::string& src, | |
83 const gfx::Size& size) { | |
84 BrowserContext* browser_context = | |
85 web_contents()->GetRenderViewHost()->GetProcess()->GetBrowserContext(); | |
86 DCHECK(browser_context); | |
87 | |
88 GURL url(src); | |
89 SiteInstance* site_instance = | |
90 SiteInstance::CreateForURL( | |
91 browser_context, url); | |
92 WebContentsImpl* guest_web_contents = | |
93 static_cast<WebContentsImpl*>( | |
94 WebContents::Create( | |
95 browser_context, | |
96 site_instance, | |
97 MSG_ROUTING_NONE, | |
98 NULL, // base WebContents | |
99 NULL // session storage namespace | |
100 )); | |
101 // TODO(fsamuel): Set the WebContentsDelegate here. | |
102 RenderViewHostImpl* guest_render_view_host = | |
103 static_cast<RenderViewHostImpl*>( | |
104 guest_web_contents->GetRenderViewHost()); | |
105 | |
106 // We need to make sure that the RenderViewHost knows that it's | |
107 // hosting a guest RenderView so that it informs the RenderView | |
108 // on a ViewMsg_New. Guest RenderViews will avoid compositing | |
109 // until a guest-to-host channel has been initialized. | |
110 guest_render_view_host->set_guest(true); | |
111 | |
112 guest_web_contents->GetController().LoadURL( | |
113 url, | |
114 Referrer(), | |
115 PAGE_TRANSITION_HOME_PAGE, | |
116 std::string()); | |
117 | |
118 guest_render_view_host->GetView()->SetSize(size); | |
119 BrowserPluginWebContentsObserver* guest_observer = | |
120 guest_web_contents->browser_plugin_web_contents_observer(); | |
121 guest_observer->set_host(static_cast<WebContentsImpl*>(web_contents())); | |
122 guest_observer->set_instance_id(instance_id); | |
123 | |
124 AddGuest(guest_web_contents, frame_id); | |
125 } | |
126 | |
127 void BrowserPluginWebContentsObserver::OnRendererPluginChannelCreated( | |
128 const IPC::ChannelHandle& channel_handle) { | |
129 DCHECK(host()); | |
130 // Prepare the handle to send to the renderer. | |
131 base::ProcessHandle plugin_process = | |
132 web_contents()->GetRenderProcessHost()->GetHandle(); | |
133 #if defined(OS_WIN) | |
134 base::ProcessHandle renderer_process = | |
135 host()->GetRenderProcessHost()->GetHandle(); | |
136 int renderer_id = host()->GetRenderProcessHost()->GetID(); | |
137 | |
138 base::ProcessHandle renderers_plugin_handle = NULL; | |
139 ::DuplicateHandle(::GetCurrentProcess(), plugin_process, | |
140 renderer_process, &renderers_plugin_handle, | |
141 0, FALSE, DUPLICATE_SAME_ACCESS); | |
142 #elif defined(OS_POSIX) | |
143 // Don't need to duplicate anything on POSIX since it's just a PID. | |
144 base::ProcessHandle renderers_plugin_handle = plugin_process; | |
145 #endif | |
146 // Tell the BrowserPLuginPlaceholder in the host that we're done | |
147 // and that it can begin using the guest renderer. | |
148 host()->GetRenderProcessHost()->Send( | |
149 new BrowserPluginMsg_GuestReady_ACK( | |
150 host()->GetRenderViewHost()->GetRoutingID(), | |
151 instance_id(), | |
152 renderers_plugin_handle, | |
153 channel_handle)); | |
154 } | |
155 | |
156 void BrowserPluginWebContentsObserver::AddGuest(WebContentsImpl* guest, | |
157 int64 frame_id) { | |
158 guests_[guest] = frame_id; | |
159 } | |
160 | |
161 void BrowserPluginWebContentsObserver::RemoveGuest(WebContentsImpl* guest) { | |
162 guests_.erase(guest); | |
163 } | |
164 | |
165 void BrowserPluginWebContentsObserver::DestroyGuests() { | |
166 for (GuestMap::const_iterator it = guests_.begin(); | |
167 it != guests_.end(); ++it) { | |
168 const WebContentsImpl* web_contents = it->first; | |
169 delete web_contents; | |
170 } | |
171 guests_.clear(); | |
172 } | |
173 | |
174 void BrowserPluginWebContentsObserver::DidCommitProvisionalLoadForFrame( | |
175 int64 frame_id, | |
176 bool is_main_frame, | |
177 const GURL& url, | |
178 PageTransition transition_type) { | |
179 typedef std::set<WebContentsImpl*> GuestSet; | |
180 GuestSet guests_to_delete; | |
181 for (GuestMap::const_iterator it = guests_.begin(); | |
182 it != guests_.end(); ++it) { | |
183 WebContentsImpl* web_contents = it->first; | |
184 if (it->second == frame_id) { | |
185 guests_to_delete.insert(web_contents); | |
186 } | |
187 } | |
188 for (GuestSet::const_iterator it = guests_to_delete.begin(); | |
189 it != guests_to_delete.end(); ++it) { | |
190 delete *it; | |
191 guests_.erase(*it); | |
192 } | |
193 } | |
194 | |
195 void BrowserPluginWebContentsObserver::RenderViewDeleted( | |
196 RenderViewHost* render_view_host) { | |
197 DestroyGuests(); | |
198 } | |
199 | |
200 void BrowserPluginWebContentsObserver::RenderViewGone( | |
201 base::TerminationStatus status) { | |
202 DestroyGuests(); | |
203 } | |
204 | |
205 void BrowserPluginWebContentsObserver::WebContentsDestroyed( | |
206 WebContents* web_contents) { | |
207 DestroyGuests(); | |
208 } | |
209 | |
210 void BrowserPluginWebContentsObserver::Observe( | |
211 int type, | |
212 const NotificationSource& source, | |
213 const NotificationDetails& details) { | |
214 DCHECK(type == NOTIFICATION_RENDER_WIDGET_VISIBILITY_CHANGED); | |
215 bool visible = *Details<bool>(details).ptr(); | |
216 | |
217 // If the host is hidden we need to hide the guests as well. | |
218 for (GuestMap::const_iterator it = guests_.begin(); | |
219 it != guests_.end(); ++it) { | |
220 WebContentsImpl* web_contents = it->first; | |
221 if (visible) | |
222 web_contents->ShowContents(); | |
223 else | |
224 web_contents->HideContents(); | |
225 } | |
226 } | |
227 | |
228 } // namespace content | |
OLD | NEW |