OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/renderer/browser_plugin/browser_plugin.h" | 5 #include "content/renderer/browser_plugin/browser_plugin.h" |
6 | 6 |
7 #include "base/message_loop.h" | 7 #include "base/message_loop.h" |
8 #include "base/string_util.h" | 8 #include "base/string_util.h" |
| 9 #if defined (OS_WIN) |
| 10 #include "base/sys_info.h" |
| 11 #endif |
9 #include "content/common/browser_plugin_messages.h" | 12 #include "content/common/browser_plugin_messages.h" |
10 #include "content/public/common/content_client.h" | 13 #include "content/public/common/content_client.h" |
11 #include "content/public/renderer/content_renderer_client.h" | 14 #include "content/public/renderer/content_renderer_client.h" |
12 #include "content/renderer/browser_plugin/browser_plugin_bindings.h" | 15 #include "content/renderer/browser_plugin/browser_plugin_bindings.h" |
13 #include "content/renderer/browser_plugin/browser_plugin_manager.h" | 16 #include "content/renderer/browser_plugin/browser_plugin_manager.h" |
14 #include "content/renderer/render_process_impl.h" | 17 #include "content/renderer/render_process_impl.h" |
15 #include "content/renderer/render_thread_impl.h" | 18 #include "content/renderer/render_thread_impl.h" |
16 #include "skia/ext/platform_canvas.h" | 19 #include "skia/ext/platform_canvas.h" |
17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebBindings.h" | 20 #include "third_party/WebKit/Source/WebKit/chromium/public/WebBindings.h" |
18 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" | 21 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" |
(...skipping 28 matching lines...) Expand all Loading... |
47 RenderViewImpl* render_view, | 50 RenderViewImpl* render_view, |
48 WebKit::WebFrame* frame, | 51 WebKit::WebFrame* frame, |
49 const WebPluginParams& params) | 52 const WebPluginParams& params) |
50 : instance_id_(instance_id), | 53 : instance_id_(instance_id), |
51 render_view_(render_view), | 54 render_view_(render_view), |
52 container_(NULL), | 55 container_(NULL), |
53 damage_buffer_(NULL), | 56 damage_buffer_(NULL), |
54 sad_guest_(NULL), | 57 sad_guest_(NULL), |
55 guest_crashed_(false), | 58 guest_crashed_(false), |
56 resize_pending_(false), | 59 resize_pending_(false), |
| 60 navigate_src_sent_(false), |
57 parent_frame_(frame->identifier()) { | 61 parent_frame_(frame->identifier()) { |
58 BrowserPluginManager::Get()->AddBrowserPlugin(instance_id, this); | 62 BrowserPluginManager::Get()->AddBrowserPlugin(instance_id, this); |
59 bindings_.reset(new BrowserPluginBindings(this)); | 63 bindings_.reset(new BrowserPluginBindings(this)); |
60 | 64 |
61 std::string src; | 65 std::string src; |
62 if (ParseSrcAttribute(params, &src)) | 66 if (ParseSrcAttribute(params, &src)) |
63 SetSrcAttribute(src); | 67 SetSrcAttribute(src); |
64 } | 68 } |
65 | 69 |
66 BrowserPlugin::~BrowserPlugin() { | 70 BrowserPlugin::~BrowserPlugin() { |
(...skipping 16 matching lines...) Expand all Loading... |
83 } | 87 } |
84 } | 88 } |
85 | 89 |
86 std::string BrowserPlugin::GetSrcAttribute() const { | 90 std::string BrowserPlugin::GetSrcAttribute() const { |
87 return src_; | 91 return src_; |
88 } | 92 } |
89 | 93 |
90 void BrowserPlugin::SetSrcAttribute(const std::string& src) { | 94 void BrowserPlugin::SetSrcAttribute(const std::string& src) { |
91 if (src == src_ && !guest_crashed_) | 95 if (src == src_ && !guest_crashed_) |
92 return; | 96 return; |
93 if (!src.empty()) { | 97 if (!src.empty() || navigate_src_sent_) { |
94 BrowserPluginManager::Get()->Send( | 98 BrowserPluginManager::Get()->Send( |
95 new BrowserPluginHostMsg_NavigateOrCreateGuest( | 99 new BrowserPluginHostMsg_NavigateGuest( |
96 render_view_->GetRoutingID(), | 100 render_view_->GetRoutingID(), |
97 instance_id_, | 101 instance_id_, |
98 parent_frame_, | 102 parent_frame_, |
99 src)); | 103 src, |
| 104 gfx::Size(width(), height()))); |
| 105 // Record that we sent a NavigateGuest message to embedder. Once we send |
| 106 // such a message, subsequent SetSrcAttribute() calls must always send |
| 107 // NavigateGuest messages to the embedder (even if |src| is empty), so |
| 108 // resize works correctly for all cases (e.g. The embedder can reset the |
| 109 // guest's |src| to empty value, resize and then set the |src| to a |
| 110 // non-empty value). |
| 111 navigate_src_sent_ = true; |
100 } | 112 } |
101 src_ = src; | 113 src_ = src; |
102 guest_crashed_ = false; | 114 guest_crashed_ = false; |
103 } | 115 } |
104 | 116 |
105 bool BrowserPlugin::ParseSrcAttribute( | 117 bool BrowserPlugin::ParseSrcAttribute( |
106 const WebKit::WebPluginParams& params, | 118 const WebKit::WebPluginParams& params, |
107 std::string* src) { | 119 std::string* src) { |
108 // Get the src attribute from the attributes vector | 120 // Get the src attribute from the attributes vector |
109 for (unsigned i = 0; i < params.attributeNames.size(); ++i) { | 121 for (unsigned i = 0; i < params.attributeNames.size(); ++i) { |
(...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
303 SkIntToScalar(0), | 315 SkIntToScalar(0), |
304 SkIntToScalar(plugin_rect_.width()), | 316 SkIntToScalar(plugin_rect_.width()), |
305 SkIntToScalar(plugin_rect_.height())); | 317 SkIntToScalar(plugin_rect_.height())); |
306 canvas->clipRect(image_data_rect); | 318 canvas->clipRect(image_data_rect); |
307 // Paint white in case we have nothing in our backing store or we need to | 319 // Paint white in case we have nothing in our backing store or we need to |
308 // show a gutter. | 320 // show a gutter. |
309 SkPaint paint; | 321 SkPaint paint; |
310 paint.setStyle(SkPaint::kFill_Style); | 322 paint.setStyle(SkPaint::kFill_Style); |
311 paint.setColor(SK_ColorWHITE); | 323 paint.setColor(SK_ColorWHITE); |
312 canvas->drawRect(image_data_rect, paint); | 324 canvas->drawRect(image_data_rect, paint); |
313 // Stay at white if we have no src set, or we don't yet have a backing store. | 325 // Stay at white if we have never set a non-empty src, or we don't yet have a |
314 if (!backing_store_.get() || src_.empty()) | 326 // backing store. |
| 327 if (!backing_store_.get() || !navigate_src_sent_) |
315 return; | 328 return; |
316 float inverse_scale_factor = 1.0f / backing_store_->GetScaleFactor(); | 329 float inverse_scale_factor = 1.0f / backing_store_->GetScaleFactor(); |
317 canvas->scale(inverse_scale_factor, inverse_scale_factor); | 330 canvas->scale(inverse_scale_factor, inverse_scale_factor); |
318 canvas->drawBitmap(backing_store_->GetBitmap(), 0, 0); | 331 canvas->drawBitmap(backing_store_->GetBitmap(), 0, 0); |
319 } | 332 } |
320 | 333 |
321 void BrowserPlugin::updateGeometry( | 334 void BrowserPlugin::updateGeometry( |
322 const WebRect& window_rect, | 335 const WebRect& window_rect, |
323 const WebRect& clip_rect, | 336 const WebRect& clip_rect, |
324 const WebVector<WebRect>& cut_outs_rects, | 337 const WebVector<WebRect>& cut_outs_rects, |
325 bool is_visible) { | 338 bool is_visible) { |
326 int old_width = width(); | 339 int old_width = width(); |
327 int old_height = height(); | 340 int old_height = height(); |
328 plugin_rect_ = window_rect; | 341 plugin_rect_ = window_rect; |
329 if (old_width == window_rect.width && | 342 if (old_width == window_rect.width && |
330 old_height == window_rect.height) | 343 old_height == window_rect.height) { |
| 344 return; |
| 345 } |
| 346 // Until an actual navigation occurs, there is no browser side embedder |
| 347 // present to notify about geometry updates. In this case, after we've updated |
| 348 // the BrowserPlugin's state we are done and can return immediately. |
| 349 if (!navigate_src_sent_) |
331 return; | 350 return; |
332 | 351 |
333 const size_t stride = skia::PlatformCanvas::StrideForWidth(window_rect.width); | 352 const size_t stride = skia::PlatformCanvas::StrideForWidth(window_rect.width); |
334 const size_t size = window_rect.height * | 353 // Make sure the size of the damage buffer is at least four bytes so that we |
335 stride * | 354 // can fit in a magic word to verify that the memory is shared correctly. |
336 GetDeviceScaleFactor() * | 355 size_t size = |
337 GetDeviceScaleFactor(); | 356 std::max(sizeof(unsigned int), |
| 357 static_cast<size_t>(window_rect.height * |
| 358 stride * |
| 359 GetDeviceScaleFactor() * |
| 360 GetDeviceScaleFactor())); |
338 | 361 |
339 // Don't drop the old damage buffer until after we've made sure that the | 362 // Don't drop the old damage buffer until after we've made sure that the |
340 // browser process has dropped it. | 363 // browser process has dropped it. |
341 TransportDIB* new_damage_buffer = | 364 TransportDIB* new_damage_buffer = NULL; |
342 RenderProcess::current()->CreateTransportDIB(size); | 365 #if defined(OS_WIN) |
343 DCHECK(new_damage_buffer); | 366 size_t allocation_granularity = base::SysInfo::VMAllocationGranularity(); |
| 367 size_t shared_mem_size = size / allocation_granularity + 1; |
| 368 shared_mem_size = shared_mem_size * allocation_granularity; |
| 369 |
| 370 base::SharedMemory shared_mem; |
| 371 if (!shared_mem.CreateAnonymous(shared_mem_size)) |
| 372 NOTREACHED() << "Unable to create shared memory of size:" << size; |
| 373 new_damage_buffer = TransportDIB::Map(shared_mem.handle()); |
| 374 #else |
| 375 new_damage_buffer = RenderProcess::current()->CreateTransportDIB(size); |
| 376 #endif |
| 377 if (!new_damage_buffer) |
| 378 NOTREACHED() << "Unable to create damage buffer"; |
| 379 DCHECK(new_damage_buffer->memory()); |
| 380 // Insert the magic word. |
| 381 *static_cast<unsigned int*>(new_damage_buffer->memory()) = 0xdeadbeef; |
344 | 382 |
345 BrowserPluginHostMsg_ResizeGuest_Params params; | 383 BrowserPluginHostMsg_ResizeGuest_Params params; |
346 params.damage_buffer_id = new_damage_buffer->id(); | 384 params.damage_buffer_id = new_damage_buffer->id(); |
| 385 #if defined(OS_WIN) |
| 386 params.damage_buffer_size = size; |
| 387 #endif |
347 params.width = window_rect.width; | 388 params.width = window_rect.width; |
348 params.height = window_rect.height; | 389 params.height = window_rect.height; |
349 params.resize_pending = resize_pending_; | 390 params.resize_pending = resize_pending_; |
350 params.scale_factor = GetDeviceScaleFactor(); | 391 params.scale_factor = GetDeviceScaleFactor(); |
351 BrowserPluginManager::Get()->Send(new BrowserPluginHostMsg_ResizeGuest( | 392 BrowserPluginManager::Get()->Send(new BrowserPluginHostMsg_ResizeGuest( |
352 render_view_->GetRoutingID(), | 393 render_view_->GetRoutingID(), |
353 instance_id_, | 394 instance_id_, |
354 params)); | 395 params)); |
355 resize_pending_ = true; | 396 resize_pending_ = true; |
356 | 397 |
(...skipping 13 matching lines...) Expand all Loading... |
370 | 411 |
371 void BrowserPlugin::updateVisibility(bool visible) { | 412 void BrowserPlugin::updateVisibility(bool visible) { |
372 } | 413 } |
373 | 414 |
374 bool BrowserPlugin::acceptsInputEvents() { | 415 bool BrowserPlugin::acceptsInputEvents() { |
375 return true; | 416 return true; |
376 } | 417 } |
377 | 418 |
378 bool BrowserPlugin::handleInputEvent(const WebKit::WebInputEvent& event, | 419 bool BrowserPlugin::handleInputEvent(const WebKit::WebInputEvent& event, |
379 WebKit::WebCursorInfo& cursor_info) { | 420 WebKit::WebCursorInfo& cursor_info) { |
380 if (guest_crashed_ || src_.empty()) | 421 if (guest_crashed_ || !navigate_src_sent_) |
381 return false; | 422 return false; |
382 bool handled = false; | 423 bool handled = false; |
383 WebCursor cursor; | 424 WebCursor cursor; |
384 IPC::Message* message = | 425 IPC::Message* message = |
385 new BrowserPluginHostMsg_HandleInputEvent( | 426 new BrowserPluginHostMsg_HandleInputEvent( |
386 render_view_->GetRoutingID(), | 427 render_view_->GetRoutingID(), |
387 &handled, | 428 &handled, |
388 &cursor); | 429 &cursor); |
389 message->WriteInt(instance_id_); | 430 message->WriteInt(instance_id_); |
390 message->WriteData(reinterpret_cast<const char*>(&plugin_rect_), | 431 message->WriteData(reinterpret_cast<const char*>(&plugin_rect_), |
(...skipping 21 matching lines...) Expand all Loading... |
412 void* notify_data) { | 453 void* notify_data) { |
413 } | 454 } |
414 | 455 |
415 void BrowserPlugin::didFailLoadingFrameRequest( | 456 void BrowserPlugin::didFailLoadingFrameRequest( |
416 const WebKit::WebURL& url, | 457 const WebKit::WebURL& url, |
417 void* notify_data, | 458 void* notify_data, |
418 const WebKit::WebURLError& error) { | 459 const WebKit::WebURLError& error) { |
419 } | 460 } |
420 | 461 |
421 } // namespace content | 462 } // namespace content |
OLD | NEW |