Chromium Code Reviews| Index: content/browser/browser_plugin/browser_plugin_guest.cc |
| diff --git a/content/browser/browser_plugin/browser_plugin_guest.cc b/content/browser/browser_plugin/browser_plugin_guest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..9827cf94881a0fd671a9d67d9d332ec89c1acb04 |
| --- /dev/null |
| +++ b/content/browser/browser_plugin/browser_plugin_guest.cc |
| @@ -0,0 +1,246 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "content/browser/browser_plugin/browser_plugin_guest.h" |
| + |
| +#include "base/time.h" |
| +#include "content/browser/browser_plugin/browser_plugin_guest_helper.h" |
| +#include "content/browser/browser_plugin/browser_plugin_host_factory.h" |
| +#include "content/browser/renderer_host/render_view_host_impl.h" |
| +#include "content/browser/renderer_host/render_widget_host_impl.h" |
| +#include "content/browser/web_contents/web_contents_impl.h" |
| +#include "content/common/browser_plugin_messages.h" |
| +#include "content/common/view_messages.h" |
| +#include "content/public/browser/render_process_host.h" |
| +#include "content/public/browser/render_view_host.h" |
| +#include "content/public/browser/render_widget_host_view.h" |
| +#include "content/public/browser/web_contents_observer.h" |
| +#include "content/public/common/result_codes.h" |
| +#include "ui/gfx/size.h" |
| + |
| +namespace content { |
| + |
| +// static |
| +BrowserPluginHostFactory* BrowserPluginGuest::factory_ = NULL; |
| + |
| +namespace { |
| +const int kGuestHangTimeout = 5000; |
| +} |
| + |
| +BrowserPluginGuest::BrowserPluginGuest(int instance_id, |
| + WebContentsImpl* web_contents, |
| + RenderViewHost* render_view_host) |
| + : WebContentsObserver(web_contents), |
|
awong
2012/09/06 00:23:27
the : should only be a 4 space indent.
lazyboy
2012/09/06 04:33:53
Done.
|
| + embedder_render_process_host_(NULL), |
| + instance_id_(instance_id), |
| + damage_buffer_(NULL), |
| + pending_update_counter_(0) { |
| + // render_view_host manages the ownership of this BrowserPluginGuestHelper. |
|
awong
2012/09/06 00:23:27
render_view_host -> |render_view_host|
lazyboy
2012/09/06 04:33:53
Done.
|
| + new BrowserPluginGuestHelper(this, render_view_host); |
| +} |
| + |
| +BrowserPluginGuest::~BrowserPluginGuest() { |
| + if (damage_buffer_) |
|
awong
2012/09/06 00:23:27
Use a scoped_ptr?
lazyboy
2012/09/06 04:33:53
Done.
|
| + delete damage_buffer_; |
| +} |
| + |
| +// static |
| +BrowserPluginGuest* BrowserPluginGuest::Create( |
| + int instance_id, |
| + WebContentsImpl* web_contents, |
| + content::RenderViewHost* render_view_host) { |
| + if (factory_) { |
| + return factory_->CreateBrowserPluginGuest(instance_id, |
| + web_contents, |
| + render_view_host); |
| + } |
| + return new BrowserPluginGuest(instance_id, web_contents, render_view_host); |
| +} |
| + |
| +bool BrowserPluginGuest::ViewTakeFocus(bool reverse) { |
| + SendMessageToEmbedder( |
| + new BrowserPluginMsg_AdvanceFocus(instance_id(), reverse)); |
| + return true; |
| +} |
| + |
| +void BrowserPluginGuest::RendererUnresponsive(WebContents* source) { |
| + base::ProcessHandle process_handle = |
| + web_contents()->GetRenderProcessHost()->GetHandle(); |
| + base::KillProcess(process_handle, RESULT_CODE_HUNG, false); |
|
awong
2012/09/06 00:23:27
Charlie mentioned that 5-second automatic kill is
lazyboy
2012/09/06 04:33:53
This one is related to guest's input event hanging
awong
2012/09/06 19:55:26
Right. Let's file a bug then and also add a commen
lazyboy
2012/09/07 19:33:19
Filed crbug.com/147272.
At this moment, each guest
|
| +} |
| + |
| +void BrowserPluginGuest::SetDamageBuffer( |
| + TransportDIB* damage_buffer, const gfx::Size& size, float scale_factor) { |
| + if (damage_buffer_) |
| + delete damage_buffer_; |
| + damage_buffer_ = damage_buffer; |
| + damage_buffer_size_ = size; |
| + damage_buffer_scale_factor_ = scale_factor; |
| +} |
| + |
| +void BrowserPluginGuest::UpdateRect( |
| + RenderViewHost* render_view_host, |
| + const ViewHostMsg_UpdateRect_Params& params) { |
| + // This handler is only of interest to us for the 2D software rendering path. |
| + // needs_ack should always be true for the 2D path. |
| + // TODO(fsamuel): Do we need to do something different in the 3D case? |
| + if (!params.needs_ack) |
| + return; |
| + |
| + // Only copy damage if the guest's view size is equal to the damage buffer's |
| + // size and the guest's scale factor is equal to the damage buffer's scale |
| + // factor. |
| + if (params.view_size.width() == damage_buffer_size().width() && |
| + params.view_size.height() == damage_buffer_size().height() && |
| + params.scale_factor == damage_buffer_scale_factor()) { |
| + TransportDIB* dib = render_view_host->GetProcess()-> |
| + GetTransportDIB(params.bitmap); |
| + if (dib) { |
| + void* guest_memory = dib->memory(); |
| + void* embedder_memory = damage_buffer_->memory(); |
| + int size = std::min(dib->size(), damage_buffer_->size()); |
|
awong
2012/09/06 00:23:27
size_t instead of int to avoid sign conversion iss
lazyboy
2012/09/06 04:33:53
Done.
|
| + memcpy(embedder_memory, guest_memory, size); |
| + } |
| + } |
| + DCHECK(embedder_render_process_host()); |
| + BrowserPluginMsg_UpdateRect_Params relay_params; |
| + relay_params.bitmap_rect = params.bitmap_rect; |
| + relay_params.dx = params.dx; |
| + relay_params.dy = params.dy; |
| + relay_params.scroll_rect = params.scroll_rect; |
| + relay_params.copy_rects = params.copy_rects; |
| + relay_params.view_size = params.view_size; |
| + relay_params.scale_factor = params.scale_factor; |
| + relay_params.is_resize_ack = ViewHostMsg_UpdateRect_Flags::is_resize_ack( |
| + params.flags); |
| + |
| + // We need to send the ACK to the same render_view_host that issued |
| + // the UpdateRect. We keep track of this correspondence via a message_id. |
| + int message_id = pending_update_counter_++; |
| + pending_updates_.AddWithID(render_view_host, message_id); |
| + |
| + gfx::Size param_size = gfx::Size(params.view_size.width(), |
| + params.view_size.height()); |
| + |
| + SendMessageToEmbedder(new BrowserPluginMsg_UpdateRect(instance_id(), |
| + message_id, |
| + relay_params)); |
| +} |
| + |
| +void BrowserPluginGuest::UpdateRectACK(int message_id, const gfx::Size& size) { |
| + RenderViewHost* render_view_host = pending_updates_.Lookup(message_id); |
| + // If the guest has crashed since it sent the initial ViewHostMsg_UpdateRect |
| + // then the pending_updates_ map will have been cleared. |
| + if (!render_view_host) |
| + return; |
| + pending_updates_.Remove(message_id); |
| + render_view_host->Send( |
| + new ViewMsg_UpdateRect_ACK(render_view_host->GetRoutingID())); |
| + if (!size.IsEmpty()) |
| + render_view_host->GetView()->SetSize(size); |
| +} |
| + |
| +void BrowserPluginGuest::HandleInputEvent(RenderViewHost* render_view_host, |
| + const gfx::Rect& guest_rect, |
| + const WebKit::WebInputEvent& event, |
| + IPC::Message* reply_message) { |
| + guest_rect_ = guest_rect; |
| + RenderViewHostImpl* guest_rvh = static_cast<RenderViewHostImpl*>( |
| + web_contents()->GetRenderViewHost()); |
| + IPC::Message* message = new ViewMsg_HandleInputEvent( |
| + guest_rvh->GetRoutingID()); |
| + |
| + // Copy the WebInputEvent and modify the event type. The guest expects |
| + // WebInputEvent::RawKeyDowns and not KeyDowns. |
| + scoped_array<char> input_buffer(new char[event.size]); |
| + memcpy(input_buffer.get(), &event, event.size); |
| + WebKit::WebInputEvent* input_event = |
| + reinterpret_cast<WebKit::WebInputEvent*>(input_buffer.get()); |
| + if (event.type == WebKit::WebInputEvent::KeyDown) |
| + input_event->type = WebKit::WebInputEvent::RawKeyDown; |
| + |
| + message->WriteData(input_buffer.get(), event.size); |
|
awong
2012/09/06 00:23:27
To make this all consistent, maybe use input_event
lazyboy
2012/09/06 04:33:53
Sorry did you mean to use message->WriteData(input
awong
2012/09/06 19:55:26
Gotcha. Makes sense.
|
| + // TODO(fsamuel): What do we need to do here? This is for keyboard shortcuts. |
| + if (input_event->type == WebKit::WebInputEvent::RawKeyDown) |
| + message->WriteBool(false); |
| + guest_rvh->Send(message); |
| + |
| + DCHECK(!pending_input_event_reply_.get()); |
| + pending_input_event_reply_.reset(reply_message); |
| + guest_rvh->StartHangMonitorTimeout( |
| + base::TimeDelta::FromMilliseconds(kGuestHangTimeout)); |
|
awong
2012/09/06 00:23:27
If a single guest doesn't respond, won't this kill
lazyboy
2012/09/06 04:33:53
If a guest's hang monitor times out, won't that ma
awong
2012/09/06 19:55:26
What I specifically meant was guests in the same r
lazyboy
2012/09/07 19:33:19
See the comment above, the bug will cover this.
Al
|
| +} |
| + |
| +void BrowserPluginGuest::HandleInputEventAck(RenderViewHost* render_view_host, |
| + bool handled) { |
| + DCHECK(pending_input_event_reply_.get()); |
| + IPC::Message* reply_message = pending_input_event_reply_.release(); |
| + BrowserPluginHostMsg_HandleInputEvent::WriteReplyParams(reply_message, |
| + handled, |
| + cursor_); |
| + SendMessageToEmbedder(reply_message); |
| + RenderViewHostImpl* guest_rvh = |
| + static_cast<RenderViewHostImpl*>(render_view_host); |
| + guest_rvh->StopHangMonitorTimeout(); |
| +} |
| + |
| +void BrowserPluginGuest::SetFocus(bool focused) { |
| + RenderViewHost* render_view_host = web_contents()->GetRenderViewHost(); |
| + render_view_host->Send( |
| + new ViewMsg_SetFocus(render_view_host->GetRoutingID(), focused)); |
| +} |
| + |
| +void BrowserPluginGuest::ShowWidget(RenderViewHost* render_view_host, |
| + int route_id, |
| + const gfx::Rect& initial_pos) { |
| + gfx::Rect screen_pos(initial_pos); |
| + screen_pos.Offset(guest_rect_.origin()); |
| + static_cast<WebContentsImpl*>(web_contents())->ShowCreatedWidget(route_id, |
| + screen_pos); |
| +} |
| + |
| +void BrowserPluginGuest::SetCursor(const WebCursor& cursor) { |
| + cursor_ = cursor; |
| +} |
| + |
| +void BrowserPluginGuest::Destroy() { |
| + WebContents* guest_web_contents = web_contents(); |
| + DCHECK(guest_web_contents); |
| + delete guest_web_contents; |
| +} |
| + |
| +void BrowserPluginGuest::DidCommitProvisionalLoadForFrame( |
| + int64 frame_id, |
| + bool is_main_frame, |
| + const GURL& url, |
| + PageTransition transition_type, |
| + RenderViewHost* render_view_host) { |
| + // Inform its embedder of the updated URL. |
| + DCHECK(embedder_render_process_host()); |
| + if (is_main_frame) |
| + SendMessageToEmbedder(new BrowserPluginMsg_DidNavigate(instance_id(), url)); |
| +} |
| + |
| +void BrowserPluginGuest::RenderViewGone(base::TerminationStatus status) { |
| + DCHECK(embedder_render_process_host()); |
| + if (pending_input_event_reply_.get()) { |
| + IPC::Message* reply_message = pending_input_event_reply_.release(); |
| + BrowserPluginHostMsg_HandleInputEvent::WriteReplyParams(reply_message, |
| + false, |
| + cursor_); |
| + SendMessageToEmbedder(reply_message); |
| + } |
| + SendMessageToEmbedder(new BrowserPluginMsg_GuestCrashed(instance_id())); |
| + IDMap<RenderViewHost>::const_iterator iter(&pending_updates_); |
| + while (!iter.IsAtEnd()) { |
| + pending_updates_.Remove(iter.GetCurrentKey()); |
| + iter.Advance(); |
| + } |
| +} |
| + |
| +void BrowserPluginGuest::SendMessageToEmbedder(IPC::Message* msg) { |
| + embedder_render_process_host()->Send(msg); |
| +} |
| + |
| +} // namespace content |