Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(16)

Side by Side Diff: content/browser/browser_plugin/browser_plugin_guest.cc

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

Powered by Google App Engine
This is Rietveld 408576698