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