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_guest.h" | |
6 | |
7 #include <algorithm> | |
8 | |
9 #include "base/time.h" | |
10 #include "content/browser/browser_plugin/browser_plugin_guest_helper.h" | |
11 #include "content/browser/browser_plugin/browser_plugin_host_factory.h" | |
12 #include "content/browser/renderer_host/render_view_host_impl.h" | |
13 #include "content/browser/renderer_host/render_widget_host_impl.h" | |
14 #include "content/browser/web_contents/web_contents_impl.h" | |
15 #include "content/common/browser_plugin_messages.h" | |
16 #include "content/common/view_messages.h" | |
17 #include "content/public/browser/render_process_host.h" | |
18 #include "content/public/browser/render_view_host.h" | |
19 #include "content/public/browser/render_widget_host_view.h" | |
20 #include "content/public/browser/web_contents_observer.h" | |
21 #include "content/public/common/result_codes.h" | |
22 #include "ui/gfx/size.h" | |
23 | |
24 namespace content { | |
25 | |
26 // static | |
27 BrowserPluginHostFactory* BrowserPluginGuest::factory_ = NULL; | |
28 | |
29 namespace { | |
30 const int kGuestHangTimeout = 5000; | |
awong
2012/09/09 18:08:09
Rename this with units.
kGuestHangTimeoutMs
lazyboy
2012/09/10 16:30:17
Done.
| |
31 } | |
32 | |
33 BrowserPluginGuest::BrowserPluginGuest(int instance_id, | |
34 WebContentsImpl* web_contents, | |
35 RenderViewHost* render_view_host) | |
36 : WebContentsObserver(web_contents), | |
37 embedder_render_process_host_(NULL), | |
38 instance_id_(instance_id), | |
39 pending_update_counter_(0) { | |
40 // |render_view_host| manages the ownership of this BrowserPluginGuestHelper. | |
41 new BrowserPluginGuestHelper(this, render_view_host); | |
42 } | |
43 | |
44 BrowserPluginGuest::~BrowserPluginGuest() { | |
45 } | |
46 | |
47 // static | |
48 BrowserPluginGuest* BrowserPluginGuest::Create( | |
49 int instance_id, | |
50 WebContentsImpl* web_contents, | |
51 content::RenderViewHost* render_view_host) { | |
52 if (factory_) { | |
53 return factory_->CreateBrowserPluginGuest(instance_id, | |
54 web_contents, | |
55 render_view_host); | |
56 } | |
57 return new BrowserPluginGuest(instance_id, web_contents, render_view_host); | |
58 } | |
59 | |
60 bool BrowserPluginGuest::ViewTakeFocus(bool reverse) { | |
61 SendMessageToEmbedder( | |
62 new BrowserPluginMsg_AdvanceFocus(instance_id(), reverse)); | |
63 return true; | |
64 } | |
65 | |
66 void BrowserPluginGuest::RendererUnresponsive(WebContents* source) { | |
67 base::ProcessHandle process_handle = | |
68 web_contents()->GetRenderProcessHost()->GetHandle(); | |
69 base::KillProcess(process_handle, RESULT_CODE_HUNG, false); | |
70 } | |
71 | |
72 void BrowserPluginGuest::SetDamageBuffer( | |
73 TransportDIB* damage_buffer, const gfx::Size& size, float scale_factor) { | |
74 damage_buffer_.reset(damage_buffer); | |
75 damage_buffer_size_ = size; | |
76 damage_buffer_scale_factor_ = scale_factor; | |
77 } | |
78 | |
79 void BrowserPluginGuest::UpdateRect( | |
80 RenderViewHost* render_view_host, | |
81 const ViewHostMsg_UpdateRect_Params& params) { | |
82 // This handler is only of interest to us for the 2D software rendering path. | |
83 // needs_ack should always be true for the 2D path. | |
84 // TODO(fsamuel): Do we need to do something different in the 3D case? | |
85 if (!params.needs_ack) | |
86 return; | |
87 | |
88 // Only copy damage if the guest's view size is equal to the damage buffer's | |
89 // size and the guest's scale factor is equal to the damage buffer's scale | |
90 // factor. | |
91 if (params.view_size.width() == damage_buffer_size().width() && | |
92 params.view_size.height() == damage_buffer_size().height() && | |
93 params.scale_factor == damage_buffer_scale_factor()) { | |
94 TransportDIB* dib = render_view_host->GetProcess()-> | |
95 GetTransportDIB(params.bitmap); | |
96 if (dib) { | |
97 void* guest_memory = dib->memory(); | |
98 void* embedder_memory = damage_buffer_->memory(); | |
99 size_t size = std::min(dib->size(), damage_buffer_->size()); | |
100 memcpy(embedder_memory, guest_memory, size); | |
101 } | |
102 } | |
103 DCHECK(embedder_render_process_host()); | |
104 BrowserPluginMsg_UpdateRect_Params relay_params; | |
105 relay_params.bitmap_rect = params.bitmap_rect; | |
106 relay_params.dx = params.dx; | |
107 relay_params.dy = params.dy; | |
108 relay_params.scroll_rect = params.scroll_rect; | |
109 relay_params.copy_rects = params.copy_rects; | |
110 relay_params.view_size = params.view_size; | |
111 relay_params.scale_factor = params.scale_factor; | |
112 relay_params.is_resize_ack = ViewHostMsg_UpdateRect_Flags::is_resize_ack( | |
113 params.flags); | |
114 | |
115 // We need to send the ACK to the same render_view_host that issued | |
116 // the UpdateRect. We keep track of this correspondence via a message_id. | |
117 int message_id = pending_update_counter_++; | |
118 pending_updates_.AddWithID(render_view_host, message_id); | |
119 | |
120 gfx::Size param_size = gfx::Size(params.view_size.width(), | |
121 params.view_size.height()); | |
122 | |
123 SendMessageToEmbedder(new BrowserPluginMsg_UpdateRect(instance_id(), | |
124 message_id, | |
125 relay_params)); | |
126 } | |
127 | |
128 void BrowserPluginGuest::UpdateRectACK(int message_id, const gfx::Size& size) { | |
129 RenderViewHost* render_view_host = pending_updates_.Lookup(message_id); | |
130 // If the guest has crashed since it sent the initial ViewHostMsg_UpdateRect | |
131 // then the pending_updates_ map will have been cleared. | |
132 if (!render_view_host) | |
133 return; | |
134 pending_updates_.Remove(message_id); | |
135 render_view_host->Send( | |
136 new ViewMsg_UpdateRect_ACK(render_view_host->GetRoutingID())); | |
137 if (!size.IsEmpty()) | |
138 render_view_host->GetView()->SetSize(size); | |
139 } | |
140 | |
141 void BrowserPluginGuest::HandleInputEvent(RenderViewHost* render_view_host, | |
142 const gfx::Rect& guest_rect, | |
143 const WebKit::WebInputEvent& event, | |
144 IPC::Message* reply_message) { | |
145 guest_rect_ = guest_rect; | |
146 RenderViewHostImpl* guest_rvh = static_cast<RenderViewHostImpl*>( | |
147 web_contents()->GetRenderViewHost()); | |
148 IPC::Message* message = new ViewMsg_HandleInputEvent( | |
149 guest_rvh->GetRoutingID()); | |
150 | |
151 // Copy the WebInputEvent and modify the event type. The guest expects | |
152 // WebInputEvent::RawKeyDowns and not KeyDowns. | |
153 scoped_array<char> input_buffer(new char[event.size]); | |
154 memcpy(input_buffer.get(), &event, event.size); | |
155 WebKit::WebInputEvent* input_event = | |
156 reinterpret_cast<WebKit::WebInputEvent*>(input_buffer.get()); | |
157 if (event.type == WebKit::WebInputEvent::KeyDown) | |
158 input_event->type = WebKit::WebInputEvent::RawKeyDown; | |
159 | |
160 message->WriteData(input_buffer.get(), event.size); | |
161 // TODO(fsamuel): What do we need to do here? This is for keyboard shortcuts. | |
162 if (input_event->type == WebKit::WebInputEvent::RawKeyDown) | |
163 message->WriteBool(false); | |
awong
2012/09/09 18:08:09
Why do we extend the pickle size for RawKeyDown ev
lazyboy
2012/09/10 16:30:17
The RawKeyDown parameter seems to be optional:
htt
| |
164 bool sent = guest_rvh->Send(message); | |
165 if (!sent) { | |
166 // If the guest has crashed, reply immediately with handled = false so | |
167 // embedder doesn't hang. | |
awong
2012/09/09 18:08:09
Why isn't this caught by the RenderViewGone() hand
lazyboy
2012/09/10 16:30:17
The comment is misleading since the word 'crash' i
| |
168 BrowserPluginHostMsg_HandleInputEvent::WriteReplyParams( | |
169 reply_message, false /* handled */, cursor_); | |
170 embedder_render_process_host()->Send(reply_message); | |
171 return; | |
172 } | |
173 | |
174 DCHECK(!pending_input_event_reply_.get()); | |
awong
2012/09/09 18:08:09
Shouldn't this DCHECK go somewhere before the gues
lazyboy
2012/09/10 16:30:17
Done.
| |
175 pending_input_event_reply_.reset(reply_message); | |
176 // Input events are handled synchronously, meaning it blocks the embedder. We | |
177 // set a hang monitor here that will kill the guest process (5s timeout) if we | |
178 // don't receive an ack. This will kill all the guests that are running in the | |
179 // same process (undesired behavior). | |
180 // TODO(fsamuel,lazyboy): Find a way to get rid of guest process kill | |
181 // behavior. http://crbug.com/147272. | |
182 guest_rvh->StartHangMonitorTimeout( | |
183 base::TimeDelta::FromMilliseconds(kGuestHangTimeout)); | |
184 } | |
185 | |
186 void BrowserPluginGuest::HandleInputEventAck(RenderViewHost* render_view_host, | |
187 bool handled) { | |
188 DCHECK(pending_input_event_reply_.get()); | |
189 IPC::Message* reply_message = pending_input_event_reply_.release(); | |
190 BrowserPluginHostMsg_HandleInputEvent::WriteReplyParams(reply_message, | |
191 handled, | |
192 cursor_); | |
193 SendMessageToEmbedder(reply_message); | |
194 RenderViewHostImpl* guest_rvh = | |
195 static_cast<RenderViewHostImpl*>(render_view_host); | |
196 guest_rvh->StopHangMonitorTimeout(); | |
awong
2012/09/09 18:08:09
Let's stop the hangout monitor first so the exit/e
lazyboy
2012/09/10 16:30:17
Done.
| |
197 } | |
198 | |
199 void BrowserPluginGuest::SetFocus(bool focused) { | |
200 RenderViewHost* render_view_host = web_contents()->GetRenderViewHost(); | |
201 render_view_host->Send( | |
202 new ViewMsg_SetFocus(render_view_host->GetRoutingID(), focused)); | |
203 } | |
204 | |
205 void BrowserPluginGuest::ShowWidget(RenderViewHost* render_view_host, | |
206 int route_id, | |
207 const gfx::Rect& initial_pos) { | |
208 gfx::Rect screen_pos(initial_pos); | |
209 screen_pos.Offset(guest_rect_.origin()); | |
210 static_cast<WebContentsImpl*>(web_contents())->ShowCreatedWidget(route_id, | |
211 screen_pos); | |
212 } | |
213 | |
214 void BrowserPluginGuest::SetCursor(const WebCursor& cursor) { | |
215 cursor_ = cursor; | |
216 } | |
217 | |
218 void BrowserPluginGuest::Destroy() { | |
219 WebContents* guest_web_contents = web_contents(); | |
220 DCHECK(guest_web_contents); | |
awong
2012/09/09 18:08:09
When can guest_web_contents be NULL for this class
lazyboy
2012/09/10 16:30:17
Moved to constructor.
| |
221 delete guest_web_contents; | |
222 } | |
223 | |
224 void BrowserPluginGuest::DidCommitProvisionalLoadForFrame( | |
225 int64 frame_id, | |
226 bool is_main_frame, | |
227 const GURL& url, | |
228 PageTransition transition_type, | |
229 RenderViewHost* render_view_host) { | |
230 // Inform its embedder of the updated URL. | |
231 DCHECK(embedder_render_process_host()); | |
232 if (is_main_frame) | |
233 SendMessageToEmbedder(new BrowserPluginMsg_DidNavigate(instance_id(), url)); | |
234 } | |
235 | |
236 void BrowserPluginGuest::RenderViewGone(base::TerminationStatus status) { | |
237 DCHECK(embedder_render_process_host()); | |
238 if (pending_input_event_reply_.get()) { | |
239 IPC::Message* reply_message = pending_input_event_reply_.release(); | |
240 BrowserPluginHostMsg_HandleInputEvent::WriteReplyParams(reply_message, | |
241 false, | |
242 cursor_); | |
243 SendMessageToEmbedder(reply_message); | |
244 } | |
245 SendMessageToEmbedder(new BrowserPluginMsg_GuestCrashed(instance_id())); | |
246 IDMap<RenderViewHost>::const_iterator iter(&pending_updates_); | |
247 while (!iter.IsAtEnd()) { | |
248 pending_updates_.Remove(iter.GetCurrentKey()); | |
249 iter.Advance(); | |
250 } | |
251 } | |
252 | |
253 void BrowserPluginGuest::SendMessageToEmbedder(IPC::Message* msg) { | |
254 embedder_render_process_host()->Send(msg); | |
255 } | |
256 | |
257 } // namespace content | |
OLD | NEW |