Chromium Code Reviews| Index: content/browser/browser_plugin/browser_plugin_host.cc |
| diff --git a/content/browser/browser_plugin/browser_plugin_host.cc b/content/browser/browser_plugin/browser_plugin_host.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..96a3e3cd8b4444d69ee0cb59b5d87b12a970fef0 |
| --- /dev/null |
| +++ b/content/browser/browser_plugin/browser_plugin_host.cc |
| @@ -0,0 +1,423 @@ |
| +// 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_host.h" |
| + |
| +#include "base/utf_string_conversions.h" |
| +#include "content/browser/browser_plugin/browser_plugin_host_helper.h" |
| +#include "content/browser/renderer_host/render_view_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/port/browser/render_widget_host_view_port.h" |
| +#include "content/public/browser/notification_details.h" |
| +#include "content/public/browser/notification_service.h" |
| +#include "content/public/browser/notification_source.h" |
| +#include "content/public/browser/notification_types.h" |
| +#include "content/public/browser/render_process_host.h" |
| +#include "content/public/browser/render_widget_host_view.h" |
| +#include "content/public/browser/site_instance.h" |
| +#include "content/public/browser/web_contents_view.h" |
| +#include "content/public/common/result_codes.h" |
| +#include "content/public/common/url_constants.h" |
| + |
| +namespace content { |
| + |
| +namespace { |
| +const int kGuestHangTimeout = 5000; |
| +} |
| + |
| +BrowserPluginHost::BrowserPluginHost( |
| + WebContentsImpl* web_contents) |
| + : WebContentsObserver(web_contents), |
| + embedder_render_process_host_(NULL), |
| + instance_id_(0), |
| + damage_buffer_(NULL), |
| + pending_update_counter_(0) { |
| + // Listen to visibility changes so that an embedder hides its guests |
| + // as well. |
| + registrar_.Add(this, |
| + NOTIFICATION_WEB_CONTENTS_VISIBILITY_CHANGED, |
| + Source<WebContents>(web_contents)); |
| + // Construct plumbing helpers when a new RenderViewHost is created for |
| + // this BrowserPluginHost's WebContentsImpl. |
| + registrar_.Add(this, |
| + NOTIFICATION_WEB_CONTENTS_RENDER_VIEW_HOST_CREATED, |
| + Source<WebContents>(web_contents)); |
| +} |
| + |
| +BrowserPluginHost::~BrowserPluginHost() { |
| + if (damage_buffer_) |
| + delete damage_buffer_; |
| +} |
| + |
| +BrowserPluginHost* BrowserPluginHost::GetGuestByInstanceID( |
| + int container_id) const { |
| + ContainerInstanceMap::const_iterator it = |
| + guests_by_instance_id_.find(container_id); |
| + if (it != guests_by_instance_id_.end()) |
| + return it->second; |
| + return NULL; |
| +} |
| + |
| +void BrowserPluginHost::AddGuest(int instance_id, |
| + BrowserPluginHost* guest, |
| + int64 frame_id) { |
| + DCHECK(guests_by_instance_id_.find(instance_id) == |
| + guests_by_instance_id_.end()); |
| + guests_by_instance_id_[instance_id] = guest; |
| + guests_[guest->web_contents()] = frame_id; |
| +} |
| + |
| +bool BrowserPluginHost::TakeFocus(bool reverse) { |
| + embedder_render_process_host()->Send( |
| + new BrowserPluginMsg_AdvanceFocus(instance_id(), reverse)); |
| + return true; |
| +} |
| + |
| +void BrowserPluginHost::RendererUnresponsive(WebContents* source) { |
| + base::ProcessHandle process_handle = |
| + web_contents()->GetRenderProcessHost()->GetHandle(); |
| + base::KillProcess(process_handle, RESULT_CODE_HUNG, false); |
|
Charlie Reis
2012/08/13 17:50:14
I don't think we want to kill an unresponsive rend
Fady Samuel
2012/08/13 21:30:08
Discussed offline. For the time being this still n
|
| +} |
| + |
| +bool BrowserPluginHost::OnMessageReceived(const IPC::Message& message) { |
| + return false; |
| +} |
| + |
| +void BrowserPluginHost::NavigateOrCreateGuest( |
| + RenderViewHost* render_view_host, |
| + int instance_id, |
| + long long frame_id, |
|
Charlie Reis
2012/08/13 17:50:14
Most frame_ids in view_messages.h are int64. Why
Fady Samuel
2012/08/13 21:30:08
Done.
|
| + const std::string& src, |
| + const gfx::Size& size) { |
| + BrowserPluginHost* guest = |
| + GetGuestByInstanceID(instance_id); |
| + WebContentsImpl* guest_web_contents = |
| + guest ? |
| + static_cast<WebContentsImpl*>(guest->web_contents()): NULL; |
|
Charlie Reis
2012/08/13 17:50:14
This feels awkward. Why not just set it to null h
|
| + GURL url(src); |
| + if (!guest) { |
| + std::string host = render_view_host->GetSiteInstance()->GetSite().host(); |
| + GURL guest_url( |
| + base::StringPrintf("%s://%s", chrome::kGuestScheme, host.c_str())); |
|
Charlie Reis
2012/08/13 17:50:14
nit: guest_url -> guest_site
|
| + // The SiteInstance of a given guest is based on the fact that it's a guest |
| + // in addition to which platform application the guest belongs to, rather |
| + // than the URL that the guest is being navigated to. |
|
Charlie Reis
2012/08/13 17:50:14
nit: Please move this comment above the host decla
|
| + SiteInstance* guest_site_instance = |
| + SiteInstance::CreateForURL(web_contents()->GetBrowserContext(), |
| + guest_url); |
|
Charlie Reis
2012/08/13 17:50:14
nit: Wrong indent (should be even with the first a
|
| + guest_web_contents = |
| + static_cast<WebContentsImpl*>( |
| + WebContents::Create( |
| + web_contents()->GetBrowserContext(), |
| + guest_site_instance, |
| + MSG_ROUTING_NONE, |
| + NULL, // base WebContents |
| + NULL // session storage namespace |
| + )); |
| + guest = guest_web_contents->browser_plugin_host(); |
|
Charlie Reis
2012/08/13 17:50:14
I find it odd that all WebContents automatically h
|
| + guest->set_embedder_render_process_host( |
| + render_view_host->GetProcess()); |
| + guest->set_instance_id(instance_id); |
| + guest_web_contents->GetMutableRendererPrefs()-> |
| + throttle_input_events = false; |
| + AddGuest(instance_id, guest, frame_id); |
| + } |
| + guest->web_contents()->SetDelegate(guest); |
| + guest->web_contents()->GetController().LoadURL( |
| + url, |
| + Referrer(), |
| + PAGE_TRANSITION_AUTO_SUBFRAME, |
| + std::string()); |
| + if (!size.IsEmpty()) |
| + guest_web_contents->GetView()->SizeContents(size); |
| +} |
| + |
| +void BrowserPluginHost::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 BrowserPluginHost::ResizeGuest(int instance_id, |
| + TransportDIB* damage_buffer, |
| + int width, int height, |
| + bool resize_pending, |
| + float scale_factor) { |
| + BrowserPluginHost* guest = GetGuestByInstanceID(instance_id); |
| + if (!guest) |
| + return; |
| + WebContentsImpl* guest_web_contents = |
| + guest ? static_cast<WebContentsImpl*>(guest->web_contents()): NULL; |
|
Charlie Reis
2012/08/13 17:50:14
nit: Space before :
|
| + guest->SetDamageBuffer(damage_buffer, gfx::Size(width, height), scale_factor); |
|
Charlie Reis
2012/08/13 17:50:14
Won't this crash if guest is null? (You just null
|
| + if (!resize_pending) |
| + guest_web_contents->GetView()->SizeContents(gfx::Size(width, height)); |
| +} |
| + |
| +void BrowserPluginHost::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()); |
| + 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()); |
| + content::NotificationService::current()->Notify( |
| + NOTIFICATION_BROWSER_PLUGIN_UPDATE_RECT, |
| + content::Source<BrowserPluginHost>(this), |
| + content::Details<const gfx::Size>(¶m_size)); |
| + |
| + embedder_render_process_host()->Send( |
| + new BrowserPluginMsg_UpdateRect(instance_id(), |
| + message_id, |
| + relay_params)); |
| +} |
| + |
| +void BrowserPluginHost::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 BrowserPluginHost::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); |
| + // 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)); |
| +} |
| + |
| +void BrowserPluginHost::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_); |
| + embedder_render_process_host()->Send(reply_message); |
| + RenderViewHostImpl* guest_rvh = |
| + static_cast<RenderViewHostImpl*>(render_view_host); |
| + guest_rvh->StopHangMonitorTimeout(); |
| +} |
| + |
| +void BrowserPluginHost::SetFocus(bool focused) { |
| + RenderViewHost* render_view_host = web_contents()->GetRenderViewHost(); |
| + render_view_host->Send( |
| + new ViewMsg_SetFocus(render_view_host->GetRoutingID(), focused)); |
| +} |
| + |
| +void BrowserPluginHost::ShowWidget(RenderViewHost* render_view_host, |
| + int route_id, |
| + const gfx::Rect& initial_pos) { |
| + RenderWidgetHostView* popup_rwhv = |
| + static_cast<WebContentsImpl*>(web_contents())-> |
| + GetCreatedWidget(route_id); |
| + |
| + RenderWidgetHostViewPort* widget_host_view = |
| + RenderWidgetHostViewPort::FromRWHV(popup_rwhv); |
| + if (!widget_host_view) |
| + return; |
| + gfx::Rect screen_pos(initial_pos); |
| + screen_pos.Offset(guest_rect_.origin()); |
| + widget_host_view->InitAsPopup(web_contents()->GetRenderWidgetHostView(), |
| + screen_pos); |
| + RenderWidgetHostImpl* render_widget_host_impl = |
| + RenderWidgetHostImpl::From(widget_host_view->GetRenderWidgetHost()); |
| + render_widget_host_impl->Init(); |
| + render_widget_host_impl->set_allow_privileged_mouse_lock(false); |
| +} |
| + |
| +void BrowserPluginHost::SetCursor(const WebCursor& cursor) { |
| + cursor_ = cursor; |
| +} |
| + |
| +void BrowserPluginHost::DestroyGuests() { |
| + for (GuestMap::const_iterator it = guests_.begin(); |
| + it != guests_.end(); ++it) { |
| + WebContents* web_contents = it->first; |
| + delete web_contents; |
| + } |
| + guests_.clear(); |
| + guests_by_instance_id_.clear(); |
| +} |
| + |
| +void BrowserPluginHost::DestroyGuestByInstanceID(int instance_id) { |
| + |
|
Charlie Reis
2012/08/13 17:50:14
nit: No empty line.
|
| + BrowserPluginHost* guest = GetGuestByInstanceID(instance_id); |
| + if (!guest) |
| + return; |
| + WebContents* guest_web_contents = guest->web_contents(); |
| + DCHECK(guest_web_contents); |
| + delete guest_web_contents; |
| + |
| + GuestMap::iterator guest_it = guests_.find(guest_web_contents); |
| + DCHECK(guest_it != guests_.end()); |
| + |
| + guests_.erase(guest_it); |
| + guests_by_instance_id_.erase(instance_id); |
| +} |
| + |
| +void BrowserPluginHost::DidCommitProvisionalLoadForFrame( |
| + int64 frame_id, |
| + bool is_main_frame, |
| + const GURL& url, |
| + PageTransition transition_type, |
| + RenderViewHost* render_view_host) { |
| + // Clean-up guests that lie in the frame that we're navigating. |
| + typedef std::set<WebContents*> GuestSet; |
| + GuestSet guests_to_delete; |
|
Charlie Reis
2012/08/13 17:50:14
This looks like it finds all the guests in the fra
|
| + for (GuestMap::const_iterator it = guests_.begin(); |
| + it != guests_.end(); ++it) { |
| + WebContents* web_contents = it->first; |
| + if (it->second == frame_id) { |
|
Charlie Reis
2012/08/13 17:50:14
nit: No braces needed for one-line if.
|
| + guests_to_delete.insert(web_contents); |
| + } |
| + } |
| + for (GuestSet::const_iterator it = guests_to_delete.begin(); |
| + it != guests_to_delete.end(); ++it) { |
| + int instance_id = static_cast<WebContentsImpl*>(*it)-> |
| + browser_plugin_host()->instance_id(); |
| + DestroyGuestByInstanceID(instance_id); |
| + } |
| + // If this is a guest, inform its embedder of the updated URL. |
| + // TODO(creis, fsamuel): Ensure this is safe/secure. |
| + if (is_main_frame && embedder_render_process_host()) { |
| + embedder_render_process_host()->Send( |
| + new BrowserPluginMsg_DidNavigate(instance_id(), url)); |
| + } |
| +} |
| + |
| +void BrowserPluginHost::RenderViewDeleted(RenderViewHost* render_view_host) { |
| + DestroyGuests(); |
| +} |
| + |
| +void BrowserPluginHost::RenderViewGone(base::TerminationStatus status) { |
| + DestroyGuests(); |
| + if (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_); |
| + embedder_render_process_host()->Send(reply_message); |
| + } |
| + embedder_render_process_host()->Send( |
| + new BrowserPluginMsg_GuestCrashed(instance_id())); |
| + IDMap<RenderViewHost>::const_iterator iter(&pending_updates_); |
| + while (!iter.IsAtEnd()) { |
| + pending_updates_.Remove(iter.GetCurrentKey()); |
| + iter.Advance(); |
| + } |
| + content::NotificationService::current()->Notify( |
| + NOTIFICATION_BROWSER_PLUGIN_GUEST_CRASHED, |
| + content::Source<BrowserPluginHost>(this), |
| + content::NotificationService::NoDetails()); |
| + } |
| +} |
| + |
| +void BrowserPluginHost::Observe( |
| + int type, |
| + const NotificationSource& source, |
| + const NotificationDetails& details) { |
| + switch (type) { |
| + case NOTIFICATION_WEB_CONTENTS_RENDER_VIEW_HOST_CREATED: { |
| + RenderViewHost* render_view_host = |
| + Details<RenderViewHost>(details).ptr(); |
| + // BrowserPluginHostHelper is destroyed when its associated RenderViewHost |
| + // is destroyed. |
| + new BrowserPluginHostHelper(this, render_view_host); |
| + break; |
| + } |
| + case NOTIFICATION_WEB_CONTENTS_VISIBILITY_CHANGED: { |
| + bool visible = *Details<bool>(details).ptr(); |
| + // If the embedder is hidden we need to hide the guests as well. |
| + for (GuestMap::const_iterator it = guests_.begin(); |
| + it != guests_.end(); ++it) { |
| + WebContents* web_contents = it->first; |
| + if (visible) |
| + web_contents->WasShown(); |
| + else |
| + web_contents->WasHidden(); |
| + } |
| + break; |
| + } |
| + default: |
| + NOTREACHED() << "Unexpected notification type: " << type; |
| + } |
| +} |
| + |
| +} // namespace content |