Index: content/browser/browser_plugin/browser_plugin_embedder.cc |
diff --git a/content/browser/browser_plugin/browser_plugin_embedder.cc b/content/browser/browser_plugin/browser_plugin_embedder.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..b39fcc9d6c3424d7857e509f438db0184b214ef5 |
--- /dev/null |
+++ b/content/browser/browser_plugin/browser_plugin_embedder.cc |
@@ -0,0 +1,288 @@ |
+// 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_embedder.h" |
+ |
+#include "base/logging.h" |
+#include "base/time.h" |
+#include "content/browser/browser_plugin/browser_plugin_embedder_helper.h" |
+#include "content/browser/browser_plugin/browser_plugin_guest.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/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_view_host.h" |
+#include "content/public/browser/render_widget_host_view.h" |
+#include "content/public/browser/web_contents_observer.h" |
+#include "content/public/browser/web_contents_view.h" |
+#include "content/public/common/url_constants.h" |
+#include "ui/gfx/size.h" |
+ |
+namespace content { |
+ |
+// static |
+BrowserPluginHostFactory* BrowserPluginEmbedder::factory_ = NULL; |
+ |
+BrowserPluginEmbedder::BrowserPluginEmbedder( |
+ WebContentsImpl* web_contents, |
+ RenderViewHost* render_view_host) |
+ : WebContentsObserver(web_contents), |
+ render_view_host_(render_view_host) { |
+ // 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)); |
+ |
+ new BrowserPluginEmbedderHelper(this, render_view_host); |
+} |
+ |
+BrowserPluginEmbedder::~BrowserPluginEmbedder() { |
+ // Destroy guests that are managed by the current embedder, for more details |
lazyboy
2012/08/31 00:38:04
We are destroying all guests in destructor now, th
|
+ // see BrowserPluginEmbedder::DidCommitProvisionalLoadForFrame. |
+ DestroyGuests(); |
+} |
+ |
+// static |
+BrowserPluginEmbedder* BrowserPluginEmbedder::Create( |
+ WebContentsImpl* web_contents, |
+ content::RenderViewHost* render_view_host) { |
+ if (factory_) { |
+ return factory_->CreateBrowserPluginEmbedder(web_contents, |
+ render_view_host); |
+ } |
+ return new BrowserPluginEmbedder(web_contents, render_view_host); |
+} |
+ |
+BrowserPluginGuest* BrowserPluginEmbedder:: GetGuestByInstanceID( |
+ int instance_id) const { |
+ ContainerInstanceMap::const_iterator it = |
+ guests_by_instance_id_.find(instance_id); |
+ if (it != guests_by_instance_id_.end()) |
+ return it->second; |
+ return NULL; |
+} |
+ |
+void BrowserPluginEmbedder::AddGuest(int instance_id, |
+ BrowserPluginGuest* guest, |
+ int64 frame_id) { |
+ DCHECK(guests_by_instance_id_.find(instance_id) == |
+ guests_by_instance_id_.end()); |
+ guests_by_instance_id_[instance_id] = guest; |
+ guest_web_contents_container_[guest->web_contents()] = frame_id; |
+} |
+ |
+void BrowserPluginEmbedder::NavigateGuest(RenderViewHost* render_view_host, |
+ int instance_id, |
+ int64 frame_id, |
+ const std::string& src, |
+ const gfx::Size& size) { |
+ BrowserPluginGuest* guest = GetGuestByInstanceID(instance_id); |
+ WebContentsImpl* guest_web_contents = NULL; |
+ GURL url(src); |
+ if (!guest) { |
+ // 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. |
+ std::string host = render_view_host->GetSiteInstance()->GetSite().host(); |
+ GURL guest_site( |
+ base::StringPrintf("%s://%s", chrome::kGuestScheme, host.c_str())); |
+ SiteInstance* guest_site_instance = |
+ SiteInstance::CreateForURL(web_contents()->GetBrowserContext(), |
+ guest_site); |
+ 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->SetBrowserPluginGuest(instance_id); |
+ guest->set_embedder_render_process_host( |
+ render_view_host->GetProcess()); |
+ |
+ guest_web_contents->GetMutableRendererPrefs()-> |
+ throttle_input_events = false; |
+ AddGuest(instance_id, guest, frame_id); |
+ guest_web_contents->SetDelegate(guest); |
+ } else { |
+ guest_web_contents = static_cast<WebContentsImpl*>(guest->web_contents()); |
+ } |
+ guest_web_contents->GetController().LoadURL(url, |
+ Referrer(), |
+ PAGE_TRANSITION_AUTO_SUBFRAME, |
+ std::string()); |
+ if (!size.IsEmpty()) |
+ guest_web_contents->GetView()->SizeContents(size); |
+} |
+ |
+void BrowserPluginEmbedder::UpdateRectACK(int instance_id, |
+ int message_id, |
+ const gfx::Size& size) { |
+ BrowserPluginGuest* guest = GetGuestByInstanceID(instance_id); |
+ if (guest) |
+ guest->UpdateRectACK(message_id, size); |
+} |
+ |
+void BrowserPluginEmbedder::ResizeGuest(int instance_id, |
+ TransportDIB* damage_buffer, |
+ int width, |
+ int height, |
+ bool resize_pending, |
+ float scale_factor) { |
+ BrowserPluginGuest* guest = GetGuestByInstanceID(instance_id); |
+ if (!guest) |
+ return; |
+ WebContentsImpl* guest_web_contents = |
+ static_cast<WebContentsImpl*>(guest->web_contents()); |
+ guest->SetDamageBuffer(damage_buffer, gfx::Size(width, height), scale_factor); |
+ if (!resize_pending) |
+ guest_web_contents->GetView()->SizeContents(gfx::Size(width, height)); |
+} |
+ |
+void BrowserPluginEmbedder::SetFocus(int instance_id, |
+ bool focused) { |
+ BrowserPluginGuest* guest = GetGuestByInstanceID(instance_id); |
+ if (guest) |
+ guest->SetFocus(focused); |
+} |
+ |
+void BrowserPluginEmbedder::DestroyGuests() { |
+ for (GuestWebContentsMap::const_iterator it = |
+ guest_web_contents_container_.begin(); |
+ it != guest_web_contents_container_.end(); ++it) { |
+ WebContents* web_contents = it->first; |
+ delete web_contents; |
+ } |
+ guest_web_contents_container_.clear(); |
+ guests_by_instance_id_.clear(); |
+} |
+ |
+void BrowserPluginEmbedder::HandleInputEvent(int instance_id, |
+ RenderViewHost* render_view_host, |
+ const gfx::Rect& guest_rect, |
+ const WebKit::WebInputEvent& event, |
+ IPC::Message* reply_message) { |
+ BrowserPluginGuest* guest = GetGuestByInstanceID(instance_id); |
+ if (guest) |
+ guest->HandleInputEvent(render_view_host, guest_rect, event, reply_message); |
+} |
+ |
+void BrowserPluginEmbedder::DestroyGuestByInstanceID(int instance_id) { |
+ BrowserPluginGuest* guest = GetGuestByInstanceID(instance_id); |
+ if (guest) { |
+ WebContents* guest_web_contents = guest->web_contents(); |
+ |
+ GuestWebContentsMap::iterator guest_it = guest_web_contents_container_.find( |
+ guest_web_contents); |
+ DCHECK(guest_it != guest_web_contents_container_.end()); |
+ |
+ guest_web_contents_container_.erase(guest_it); |
+ guests_by_instance_id_.erase(instance_id); |
+ |
+ guest->Destroy(); |
+ } |
+} |
+ |
+void BrowserPluginEmbedder::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. |
+ // |
+ // Subframe's guest(s) cleaning up is not handled here, they are handled by |
+ // either the lifetime of WebContents associated directly with the guest(s) or |
+ // by the lifetime of their embedders. Consider the following cases: |
+ // |
+ // Case 1: (Subframe's guest clean up managed by embedder) An WebContents WC1 |
+ // embeds a guest (therefore its an embedder E1), call the guest G1. G1 points |
+ // to a page that has an iframe, and its WebContents is WC2. Now the iframe |
+ // has a guest of its own in it, lets call it G2, which makes WC2 an embedder |
+ // as well, call it E2. Now when DidCommitProvisionalLoadForFrame is called, |
+ // G2 won't be cleaned up by the code below since it doesn't directly belong |
+ // to the top level frame. However when the WebContents WC2 gets destroyed, |
+ // it destroys the embedder E2 associated with it, eventually destroying all |
+ // of its guests (G2). |
+ // |
+ // Case 2: (Subframe's guest cleanup managed by WebContents) A page has an |
+ // iframe which points to a page that has a guest G1. Therefore this makes top |
+ // level page's WebContents WC1 an embedder E1. Lets say the WebContents |
+ // associated with G1 is WC2. When DidCommitProvisionalLoadForFrame occurs, G1 |
+ // is not a guest lying in the top level frame that called commit, so it won't |
+ // be cleaned up with the code below; However since WC2 goes away, this |
+ // eventually destroys G1 since G1 is an observer for WC2. |
+ typedef std::set<WebContents*> GuestSet; |
+ GuestSet guests_to_delete; |
+ for (GuestWebContentsMap::const_iterator it = |
+ guest_web_contents_container_.begin(); |
+ it != guest_web_contents_container_.end(); ++it) { |
+ WebContents* web_contents = it->first; |
+ if (it->second == frame_id) |
+ 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)-> |
+ GetBrowserPluginGuest()->instance_id(); |
+ DestroyGuestByInstanceID(instance_id); |
+ } |
+} |
+ |
+void BrowserPluginEmbedder::RenderViewDeleted( |
+ RenderViewHost* render_view_host) { |
+ DestroyGuests(); |
+} |
+ |
+void BrowserPluginEmbedder::RenderViewGone(base::TerminationStatus status) { |
+ DestroyGuests(); |
+} |
+ |
+void BrowserPluginEmbedder::WebContentsVisibilityChanged(bool visible) { |
+ // If the embedder is hidden we need to hide the guests as well. |
+ for (GuestWebContentsMap::const_iterator it = |
+ guest_web_contents_container_.begin(); |
+ it != guest_web_contents_container_.end(); ++it) { |
+ WebContents* web_contents = it->first; |
+ if (visible) |
+ web_contents->WasShown(); |
+ else |
+ web_contents->WasHidden(); |
+ } |
+} |
+ |
+void BrowserPluginEmbedder::PluginDestroyed(int instance_id) { |
+ DestroyGuestByInstanceID(instance_id); |
+} |
+ |
+void BrowserPluginEmbedder::Observe(int type, |
+ const NotificationSource& source, |
+ const NotificationDetails& details) { |
+ switch (type) { |
+ case NOTIFICATION_WEB_CONTENTS_VISIBILITY_CHANGED: { |
+ bool visible = *Details<bool>(details).ptr(); |
+ WebContentsVisibilityChanged(visible); |
+ break; |
+ } |
+ default: |
+ NOTREACHED() << "Unexpected notification type: " << type; |
+ } |
+} |
+ |
+bool BrowserPluginEmbedder::IsForRenderViewHost(RenderViewHost* rvh) { |
+ return render_view_host_ == rvh; |
+} |
+ |
+} // namespace content |