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" | |
jam
2012/09/17 17:47:52
this is included in the header. please go through
lazyboy
2012/09/17 20:22:24
Hmmm, I was somehow counting on cpplint.py for the
jam
2012/09/17 20:47:57
cpplint doesn't (and can't) figure this out
lazyboy
2012/09/17 21:07:54
OK.
| |
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" | |
jam
2012/09/17 17:47:52
don't include public headers when you're including
lazyboy
2012/09/17 20:22:24
Done.
| |
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 base::TimeDelta kGuestHangTimeout = | |
31 base::TimeDelta::FromMilliseconds(5000); | |
32 } | |
33 | |
34 BrowserPluginGuest::BrowserPluginGuest(int instance_id, | |
35 WebContentsImpl* web_contents, | |
36 RenderViewHost* render_view_host) | |
37 : WebContentsObserver(web_contents), | |
38 embedder_render_process_host_(NULL), | |
39 instance_id_(instance_id), | |
40 #if defined(OS_WIN) | |
41 damage_buffer_size_(0), | |
42 #endif | |
43 pending_update_counter_(0), | |
44 guest_hang_timeout_(kGuestHangTimeout) { | |
45 DCHECK(web_contents); | |
46 // |render_view_host| manages the ownership of this BrowserPluginGuestHelper. | |
47 new BrowserPluginGuestHelper(this, render_view_host); | |
48 } | |
49 | |
50 BrowserPluginGuest::~BrowserPluginGuest() { | |
51 } | |
52 | |
53 // static | |
54 BrowserPluginGuest* BrowserPluginGuest::Create( | |
55 int instance_id, | |
56 WebContentsImpl* web_contents, | |
57 content::RenderViewHost* render_view_host) { | |
58 if (factory_) { | |
59 return factory_->CreateBrowserPluginGuest(instance_id, | |
60 web_contents, | |
61 render_view_host); | |
62 } | |
63 return new BrowserPluginGuest(instance_id, web_contents, render_view_host); | |
64 } | |
65 | |
66 bool BrowserPluginGuest::ViewTakeFocus(bool reverse) { | |
67 SendMessageToEmbedder( | |
68 new BrowserPluginMsg_AdvanceFocus(instance_id(), reverse)); | |
69 return true; | |
70 } | |
71 | |
72 void BrowserPluginGuest::RendererUnresponsive(WebContents* source) { | |
73 base::ProcessHandle process_handle = | |
74 web_contents()->GetRenderProcessHost()->GetHandle(); | |
75 base::KillProcess(process_handle, RESULT_CODE_HUNG, false); | |
76 } | |
77 | |
78 void BrowserPluginGuest::SetDamageBuffer( | |
79 TransportDIB* damage_buffer, | |
80 #if defined(OS_WIN) | |
81 int damage_buffer_size, | |
82 #endif | |
83 const gfx::Size& damage_view_size, | |
84 float scale_factor) { | |
85 // Sanity check: Verify that we've correctly shared the damage buffer memory | |
86 // between the embedder and browser processes. | |
87 DCHECK(*static_cast<unsigned int*>(damage_buffer->memory()) == 0xdeadbeef); | |
88 damage_buffer_.reset(damage_buffer); | |
89 #if defined(OS_WIN) | |
90 damage_buffer_size_ = damage_buffer_size; | |
91 #endif | |
92 damage_view_size_ = damage_view_size; | |
93 damage_buffer_scale_factor_ = scale_factor; | |
94 } | |
95 | |
96 void BrowserPluginGuest::UpdateRect( | |
97 RenderViewHost* render_view_host, | |
98 const ViewHostMsg_UpdateRect_Params& params) { | |
99 RenderWidgetHostImpl* render_widget_host = | |
100 RenderWidgetHostImpl::From(render_view_host); | |
101 render_widget_host->ResetSizeAndRepaintPendingFlags(); | |
102 // This handler is only of interest to us for the 2D software rendering path. | |
103 // needs_ack should always be true for the 2D path. | |
104 // TODO(fsamuel): Do we need to do something different in the 3D case? | |
105 if (!params.needs_ack) | |
106 return; | |
107 | |
108 // Only copy damage if the guest's view size is equal to the damage buffer's | |
109 // size and the guest's scale factor is equal to the damage buffer's scale | |
110 // factor. | |
111 // The scaling change can happen due to asynchronous updates of the DPI on a | |
112 // resolution change. | |
113 if (params.view_size.width() == damage_view_size().width() && | |
114 params.view_size.height() == damage_view_size().height() && | |
115 params.scale_factor == damage_buffer_scale_factor()) { | |
116 TransportDIB* dib = render_view_host->GetProcess()-> | |
117 GetTransportDIB(params.bitmap); | |
118 if (dib) { | |
119 #if defined(OS_WIN) | |
120 size_t guest_damage_buffer_size = params.bitmap_rect.width() * | |
121 params.bitmap_rect.height() * 4; | |
122 size_t embedder_damage_buffer_size = damage_buffer_size_; | |
123 #else | |
124 size_t guest_damage_buffer_size = dib->size(); | |
125 size_t embedder_damage_buffer_size = damage_buffer_->size(); | |
126 #endif | |
127 void* guest_memory = dib->memory(); | |
128 void* embedder_memory = damage_buffer_->memory(); | |
129 size_t size = std::min(guest_damage_buffer_size, | |
130 embedder_damage_buffer_size); | |
131 memcpy(embedder_memory, guest_memory, size); | |
132 } | |
133 } | |
134 DCHECK(embedder_render_process_host()); | |
135 BrowserPluginMsg_UpdateRect_Params relay_params; | |
136 relay_params.bitmap_rect = params.bitmap_rect; | |
137 relay_params.dx = params.dx; | |
138 relay_params.dy = params.dy; | |
139 relay_params.scroll_rect = params.scroll_rect; | |
140 relay_params.copy_rects = params.copy_rects; | |
141 relay_params.view_size = params.view_size; | |
142 relay_params.scale_factor = params.scale_factor; | |
143 relay_params.is_resize_ack = ViewHostMsg_UpdateRect_Flags::is_resize_ack( | |
144 params.flags); | |
145 | |
146 // We need to send the ACK to the same render_view_host that issued | |
147 // the UpdateRect. We keep track of this correspondence via a message_id. | |
148 int message_id = pending_update_counter_++; | |
149 pending_updates_.AddWithID(render_view_host, message_id); | |
150 | |
151 gfx::Size param_size = gfx::Size(params.view_size.width(), | |
152 params.view_size.height()); | |
153 | |
154 SendMessageToEmbedder(new BrowserPluginMsg_UpdateRect(instance_id(), | |
155 message_id, | |
156 relay_params)); | |
157 } | |
158 | |
159 void BrowserPluginGuest::UpdateRectACK(int message_id, const gfx::Size& size) { | |
160 RenderViewHost* render_view_host = pending_updates_.Lookup(message_id); | |
161 // If the guest has crashed since it sent the initial ViewHostMsg_UpdateRect | |
162 // then the pending_updates_ map will have been cleared. | |
163 if (!render_view_host) | |
164 return; | |
165 pending_updates_.Remove(message_id); | |
166 render_view_host->Send( | |
167 new ViewMsg_UpdateRect_ACK(render_view_host->GetRoutingID())); | |
168 if (!size.IsEmpty()) | |
169 render_view_host->GetView()->SetSize(size); | |
170 } | |
171 | |
172 void BrowserPluginGuest::HandleInputEvent(RenderViewHost* render_view_host, | |
173 const gfx::Rect& guest_rect, | |
174 const WebKit::WebInputEvent& event, | |
175 IPC::Message* reply_message) { | |
176 DCHECK(!pending_input_event_reply_.get()); | |
177 guest_rect_ = guest_rect; | |
178 RenderViewHostImpl* guest_rvh = static_cast<RenderViewHostImpl*>( | |
179 web_contents()->GetRenderViewHost()); | |
180 IPC::Message* message = new ViewMsg_HandleInputEvent( | |
181 guest_rvh->GetRoutingID()); | |
182 | |
183 // Copy the WebInputEvent and modify the event type. The guest expects | |
184 // WebInputEvent::RawKeyDowns and not KeyDowns. | |
185 scoped_array<char> input_buffer(new char[event.size]); | |
186 memcpy(input_buffer.get(), &event, event.size); | |
187 WebKit::WebInputEvent* input_event = | |
188 reinterpret_cast<WebKit::WebInputEvent*>(input_buffer.get()); | |
189 if (event.type == WebKit::WebInputEvent::KeyDown) | |
190 input_event->type = WebKit::WebInputEvent::RawKeyDown; | |
191 | |
192 message->WriteData(input_buffer.get(), event.size); | |
193 // TODO(fsamuel): What do we need to do here? This is for keyboard shortcuts. | |
194 if (input_event->type == WebKit::WebInputEvent::RawKeyDown) | |
195 message->WriteBool(false); | |
196 bool sent = guest_rvh->Send(message); | |
197 if (!sent) { | |
198 // If the embedder is waiting for a previous input ack, a new input message | |
199 // won't get sent to the guest. Reply immediately with handled = false so | |
200 // embedder doesn't hang. | |
201 BrowserPluginHostMsg_HandleInputEvent::WriteReplyParams( | |
202 reply_message, false /* handled */, cursor_); | |
203 embedder_render_process_host()->Send(reply_message); | |
204 return; | |
205 } | |
206 | |
207 pending_input_event_reply_.reset(reply_message); | |
208 // Input events are handled synchronously, meaning it blocks the embedder. We | |
209 // set a hang monitor here that will kill the guest process (5s timeout) if we | |
210 // don't receive an ack. This will kill all the guests that are running in the | |
211 // same process (undesired behavior). | |
212 // TODO(fsamuel,lazyboy): Find a way to get rid of guest process kill | |
213 // behavior. http://crbug.com/147272. | |
214 guest_rvh->StartHangMonitorTimeout(guest_hang_timeout_); | |
215 } | |
216 | |
217 void BrowserPluginGuest::HandleInputEventAck(RenderViewHost* render_view_host, | |
218 bool handled) { | |
219 RenderViewHostImpl* guest_rvh = | |
220 static_cast<RenderViewHostImpl*>(render_view_host); | |
221 guest_rvh->StopHangMonitorTimeout(); | |
222 DCHECK(pending_input_event_reply_.get()); | |
223 IPC::Message* reply_message = pending_input_event_reply_.release(); | |
224 BrowserPluginHostMsg_HandleInputEvent::WriteReplyParams(reply_message, | |
225 handled, | |
226 cursor_); | |
227 SendMessageToEmbedder(reply_message); | |
228 } | |
229 | |
230 void BrowserPluginGuest::SetFocus(bool focused) { | |
231 RenderViewHost* render_view_host = web_contents()->GetRenderViewHost(); | |
232 render_view_host->Send( | |
233 new ViewMsg_SetFocus(render_view_host->GetRoutingID(), focused)); | |
234 } | |
235 | |
236 void BrowserPluginGuest::ShowWidget(RenderViewHost* render_view_host, | |
237 int route_id, | |
238 const gfx::Rect& initial_pos) { | |
239 gfx::Rect screen_pos(initial_pos); | |
240 screen_pos.Offset(guest_rect_.origin()); | |
241 static_cast<WebContentsImpl*>(web_contents())->ShowCreatedWidget(route_id, | |
242 screen_pos); | |
243 } | |
244 | |
245 void BrowserPluginGuest::SetCursor(const WebCursor& cursor) { | |
246 cursor_ = cursor; | |
247 } | |
248 | |
249 void BrowserPluginGuest::DidCommitProvisionalLoadForFrame( | |
250 int64 frame_id, | |
251 bool is_main_frame, | |
252 const GURL& url, | |
253 PageTransition transition_type, | |
254 RenderViewHost* render_view_host) { | |
255 // Inform its embedder of the updated URL. | |
256 DCHECK(embedder_render_process_host()); | |
257 if (is_main_frame) | |
258 SendMessageToEmbedder(new BrowserPluginMsg_DidNavigate(instance_id(), url)); | |
259 } | |
260 | |
261 void BrowserPluginGuest::RenderViewGone(base::TerminationStatus status) { | |
262 DCHECK(embedder_render_process_host()); | |
263 if (pending_input_event_reply_.get()) { | |
264 IPC::Message* reply_message = pending_input_event_reply_.release(); | |
265 BrowserPluginHostMsg_HandleInputEvent::WriteReplyParams(reply_message, | |
266 false, | |
267 cursor_); | |
268 SendMessageToEmbedder(reply_message); | |
269 } | |
270 SendMessageToEmbedder(new BrowserPluginMsg_GuestCrashed(instance_id())); | |
271 IDMap<RenderViewHost>::const_iterator iter(&pending_updates_); | |
272 while (!iter.IsAtEnd()) { | |
273 pending_updates_.Remove(iter.GetCurrentKey()); | |
274 iter.Advance(); | |
275 } | |
276 } | |
277 | |
278 void BrowserPluginGuest::SendMessageToEmbedder(IPC::Message* msg) { | |
279 embedder_render_process_host()->Send(msg); | |
280 } | |
281 | |
282 } // namespace content | |
OLD | NEW |