Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(648)

Unified Diff: content/browser/browser_plugin/browser_plugin_embedder.cc

Issue 10868012: Browser Plugin: New Implementation (Browser Side) (Closed) Base URL: http://git.chromium.org/chromium/src.git@master-trial-obrowser
Patch Set: integrate windows fix by Fady (original cl #10910228) Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..0a4cd01c9dc00288d2178c56d18796d4e6a843b4
--- /dev/null
+++ b/content/browser/browser_plugin/browser_plugin_embedder.cc
@@ -0,0 +1,285 @@
+// 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 <set>
+
+#include "base/logging.h"
+#include "base/stl_util.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));
+
+ // |render_view_host| manages the ownership of this BrowserPluginGuestHelper.
+ new BrowserPluginEmbedderHelper(this, render_view_host);
+}
+
+BrowserPluginEmbedder::~BrowserPluginEmbedder() {
+ // Destroy guests that are managed by the current embedder, for more details
Charlie Reis 2012/09/13 00:51:44 nit: These are two separate sentences. (There hav
lazyboy 2012/09/13 15:56:23 Ok. Done.
+ // 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 =
+ guest_web_contents_by_instance_id_.find(instance_id);
+ if (it != guest_web_contents_by_instance_id_.end())
+ return static_cast<WebContentsImpl*>(it->second)->GetBrowserPluginGuest();
+ return NULL;
+}
+
+void BrowserPluginEmbedder::AddGuest(int instance_id,
+ WebContents* guest_web_contents,
+ int64 frame_id) {
+ DCHECK(guest_web_contents_by_instance_id_.find(instance_id) ==
+ guest_web_contents_by_instance_id_.end());
+ guest_web_contents_by_instance_id_[instance_id] = guest_web_contents;
+ 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) {
+ const std::string& host =
+ render_view_host->GetSiteInstance()->GetSite().host();
+ guest_web_contents = WebContentsImpl::CreateGuest(
+ web_contents()->GetBrowserContext(),
+ host,
+ instance_id);
+
+ guest = guest_web_contents->GetBrowserPluginGuest();
+ guest->set_embedder_render_process_host(
+ render_view_host->GetProcess());
+
+ guest_web_contents->GetMutableRendererPrefs()->
+ throttle_input_events = false;
+ AddGuest(instance_id, guest_web_contents, 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,
+#if defined(OS_WIN)
+ int damage_buffer_size,
+#endif
+ 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,
+#if defined(OS_WIN)
+ damage_buffer_size,
+#endif
+ 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() {
+ STLDeleteContainerPairFirstPointers(guest_web_contents_container_.begin(),
+ guest_web_contents_container_.end());
+ guest_web_contents_container_.clear();
+ guest_web_contents_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());
+
+ // Destroy the guest's web_contents.
+ delete guest_it->first;
+
+ guest_web_contents_container_.erase(guest_it);
+ guest_web_contents_by_instance_id_.erase(instance_id);
+ }
+}
+
+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) clean 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) A WebContents WC1
+ // embeds a guest (therefore it is an embedder E1), call this guest G1. G1
+ // points to a page that has an iframe. Call the iframe's WebContents 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
Charlie Reis 2012/09/13 00:51:44 Is this possible? I didn't think we could support
lazyboy 2012/09/13 15:56:23 We don't forcefully disallow it yet, but we will d
+ // 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 within the top level frame that called commit, so it
+ // won't be cleaned up with the code below; However since WC2 goes away, this
Charlie Reis 2012/09/13 00:51:44 How does WC2 go away? There's no code that I know
lazyboy 2012/09/13 15:56:23 I talked to Fady about this part of the code: it s
Charlie Reis 2012/09/13 16:55:30 Great! I'm not aware of another need for this cod
Fady Samuel 2012/09/13 16:59:57 I don't believe this code is necessary any more. P
lazyboy 2012/09/13 18:52:49 Removed.
+ // eventually destroys G1 since G1 is an observer for WC2.
+ //
+ // The following code is repsonsible for deleting the top-level frame which
+ // will eventually invoke one of the two cases above covering all guests in
+ // the whole frame tree.
+ 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) {
+ if (it->second == frame_id)
+ guests_to_delete.insert(it->first);
+ }
+ 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;
+ }
+}
+
+} // namespace content

Powered by Google App Engine
This is Rietveld 408576698