| 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..676ebd30cc51716596ec097fb86de4528ed0aaae
 | 
| --- /dev/null
 | 
| +++ b/content/browser/browser_plugin/browser_plugin_host.cc
 | 
| @@ -0,0 +1,228 @@
 | 
| +// 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 "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/public/browser/notification_details.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 "ppapi/proxy/ppapi_messages.h"
 | 
| +
 | 
| +namespace content {
 | 
| +
 | 
| +BrowserPluginHost::BrowserPluginHost(
 | 
| +    WebContentsImpl* web_contents)
 | 
| +    : WebContentsObserver(web_contents),
 | 
| +      embedder_(NULL),
 | 
| +      instance_id_(0) {
 | 
| +  registrar_.Add(this,
 | 
| +                 NOTIFICATION_RENDER_WIDGET_VISIBILITY_CHANGED,
 | 
| +                 Source<RenderViewHost>(
 | 
| +                    web_contents->GetRenderViewHost()));
 | 
| +}
 | 
| +
 | 
| +BrowserPluginHost::~BrowserPluginHost() {
 | 
| +}
 | 
| +
 | 
| +BrowserPluginHost* BrowserPluginHost::GetGuestByContainerID(int container_id) {
 | 
| +  ContainerInstanceMap::const_iterator it =
 | 
| +      guests_by_container_id_.find(container_id);
 | 
| +  if (it != guests_by_container_id_.end())
 | 
| +    return it->second;
 | 
| +  return NULL;
 | 
| +}
 | 
| +
 | 
| +void BrowserPluginHost::RegisterContainerInstance(
 | 
| +    int container_id,
 | 
| +    BrowserPluginHost* observer) {
 | 
| +  DCHECK(guests_by_container_id_.find(container_id) ==
 | 
| +      guests_by_container_id_.end());
 | 
| +  guests_by_container_id_[container_id] = observer;
 | 
| +}
 | 
| +
 | 
| +bool BrowserPluginHost::OnMessageReceived(const IPC::Message& message) {
 | 
| +  bool handled = true;
 | 
| +  IPC_BEGIN_MESSAGE_MAP(BrowserPluginHost, message)
 | 
| +    IPC_MESSAGE_HANDLER(BrowserPluginHostMsg_ConnectToChannel,
 | 
| +                        OnConnectToChannel)
 | 
| +    IPC_MESSAGE_HANDLER(BrowserPluginHostMsg_NavigateFromEmbedder,
 | 
| +                        OnNavigateFromEmbedder)
 | 
| +    IPC_MESSAGE_HANDLER(BrowserPluginHostMsg_NavigateFromGuest,
 | 
| +                        OnNavigateFromGuest)
 | 
| +    IPC_MESSAGE_HANDLER(BrowserPluginHostMsg_MapInstance,
 | 
| +                        OnMapInstance)
 | 
| +    IPC_MESSAGE_HANDLER(BrowserPluginHostMsg_ResizeGuest, OnResizeGuest)
 | 
| +    IPC_MESSAGE_UNHANDLED(handled = false)
 | 
| +  IPC_END_MESSAGE_MAP()
 | 
| +  return handled;
 | 
| +}
 | 
| +
 | 
| +void BrowserPluginHost::OnResizeGuest(int width, int height) {
 | 
| +  // Tell the RenderWidgetHostView to adjust its size.
 | 
| +  web_contents()->GetRenderViewHost()->GetView()->SetSize(
 | 
| +      gfx::Size(width, height));
 | 
| +}
 | 
| +
 | 
| +void BrowserPluginHost::OnNavigateFromEmbedder(
 | 
| +    int instance_id,
 | 
| +    long long frame_id,
 | 
| +    const std::string& src,
 | 
| +    const gfx::Size& size) {
 | 
| +  BrowserPluginHost* guest_observer = GetGuestByContainerID(instance_id);
 | 
| +  WebContentsImpl* guest_web_contents =
 | 
| +      guest_observer ?
 | 
| +          static_cast<WebContentsImpl*>(guest_observer->web_contents()): NULL;
 | 
| +  if (!guest_observer) {
 | 
| +    GURL url(src);
 | 
| +    guest_web_contents =
 | 
| +        static_cast<WebContentsImpl*>(
 | 
| +            WebContents::Create(
 | 
| +                web_contents()->GetBrowserContext(),
 | 
| +                web_contents()->GetSiteInstance(),
 | 
| +                MSG_ROUTING_NONE,
 | 
| +                NULL, // base WebContents
 | 
| +                NULL  // session storage namespace
 | 
| +            ));
 | 
| +    guest_observer =
 | 
| +        guest_web_contents->browser_plugin_host();
 | 
| +    guest_observer->set_embedder(static_cast<WebContentsImpl*>(web_contents()));
 | 
| +    guest_observer->set_instance_id(instance_id);
 | 
| +    guest_observer->set_url(url);
 | 
| +    guest_observer->set_initial_size(size);
 | 
| +    RegisterContainerInstance(instance_id, guest_observer);
 | 
| +    AddGuest(guest_web_contents, frame_id);
 | 
| +  }
 | 
| +
 | 
| +  // TODO(fsamuel): Set the WebContentsDelegate here.
 | 
| +  guest_observer->web_contents()->GetController().LoadURL(
 | 
| +      guest_observer->url(),
 | 
| +      Referrer(),
 | 
| +      PAGE_TRANSITION_HOME_PAGE,
 | 
| +      std::string());
 | 
| +}
 | 
| +
 | 
| +void BrowserPluginHost::OnNavigateFromGuest(
 | 
| +    PP_Instance instance,
 | 
| +    const std::string& src) {
 | 
| +  DCHECK(embedder());
 | 
| +  GURL url(src);
 | 
| +  web_contents()->GetController().LoadURL(
 | 
| +      url,
 | 
| +      Referrer(),
 | 
| +      PAGE_TRANSITION_HOME_PAGE,
 | 
| +      std::string());
 | 
| +}
 | 
| +
 | 
| +void BrowserPluginHost::OnMapInstance(int container_id, PP_Instance instance) {
 | 
| +  BrowserPluginHost* guest_observer = GetGuestByContainerID(container_id);
 | 
| +  WebContentsImpl* guest_web_contents =
 | 
| +    static_cast<WebContentsImpl*>(guest_observer->web_contents());
 | 
| +  RenderViewHost* rvh = guest_web_contents->GetPendingRenderViewHost() ?
 | 
| +      guest_web_contents->GetPendingRenderViewHost() :
 | 
| +          guest_web_contents->GetRenderViewHost();
 | 
| +  // TODO(fsamuel): This is wrong. The current size of the plugin container
 | 
| +  // might not be equal to initial size on subsequent navigations.
 | 
| +  // BrowserPluginHost_MapInstance should also report the current size of
 | 
| +  // the container to the browser process.
 | 
| +  rvh->GetView()->SetSize(guest_observer->initial_size());
 | 
| +
 | 
| +  rvh->Send(new BrowserPluginMsg_CompleteNavigation(
 | 
| +                rvh->GetRoutingID(),
 | 
| +                instance));
 | 
| +}
 | 
| +
 | 
| +void BrowserPluginHost::OnConnectToChannel(
 | 
| +    const IPC::ChannelHandle& channel_handle) {
 | 
| +  DCHECK(embedder());
 | 
| +  WebContentsImpl* embedder_web_contents =
 | 
| +      static_cast<WebContentsImpl*>(web_contents());
 | 
| +  RenderViewHost* rvh =
 | 
| +      embedder_web_contents->GetPendingRenderViewHost() ?
 | 
| +          embedder_web_contents->GetPendingRenderViewHost() :
 | 
| +          embedder_web_contents->GetRenderViewHost();
 | 
| +  // Tell the BrowserPlugin in the embedder that we're done and that it can
 | 
| +  // begin using the guest renderer.
 | 
| +  embedder()->GetRenderProcessHost()->Send(
 | 
| +      new BrowserPluginMsg_LoadGuest(
 | 
| +          instance_id(),
 | 
| +          rvh->GetProcess()->GetID(),
 | 
| +          channel_handle));
 | 
| +}
 | 
| +
 | 
| +void BrowserPluginHost::AddGuest(WebContentsImpl* guest, int64 frame_id) {
 | 
| +  guests_[guest] = frame_id;
 | 
| +}
 | 
| +
 | 
| +void BrowserPluginHost::RemoveGuest(WebContentsImpl* guest) {
 | 
| +  guests_.erase(guest);
 | 
| +}
 | 
| +
 | 
| +void BrowserPluginHost::DestroyGuests() {
 | 
| +  for (GuestMap::const_iterator it = guests_.begin();
 | 
| +       it != guests_.end(); ++it) {
 | 
| +    WebContentsImpl* web_contents = it->first;
 | 
| +    delete web_contents;
 | 
| +  }
 | 
| +  guests_.clear();
 | 
| +  guests_by_container_id_.clear();
 | 
| +}
 | 
| +
 | 
| +void BrowserPluginHost::DidCommitProvisionalLoadForFrame(
 | 
| +    int64 frame_id,
 | 
| +    bool is_main_frame,
 | 
| +    const GURL& url,
 | 
| +    PageTransition transition_type) {
 | 
| +  typedef std::set<WebContentsImpl*> GuestSet;
 | 
| +  GuestSet guests_to_delete;
 | 
| +  for (GuestMap::const_iterator it = guests_.begin();
 | 
| +       it != guests_.end(); ++it) {
 | 
| +    WebContentsImpl* 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) {
 | 
| +    delete *it;
 | 
| +    guests_.erase(*it);
 | 
| +  }
 | 
| +}
 | 
| +
 | 
| +void BrowserPluginHost::RenderViewDeleted(RenderViewHost* render_view_host) {
 | 
| +  DestroyGuests();
 | 
| +}
 | 
| +
 | 
| +void BrowserPluginHost::RenderViewGone(base::TerminationStatus status) {
 | 
| +  DestroyGuests();
 | 
| +}
 | 
| +
 | 
| +void BrowserPluginHost::WebContentsDestroyed(WebContents* web_contents) {
 | 
| +  DestroyGuests();
 | 
| +}
 | 
| +
 | 
| +void BrowserPluginHost::Observe(
 | 
| +    int type,
 | 
| +    const NotificationSource& source,
 | 
| +    const NotificationDetails& details) {
 | 
| +  DCHECK(type == NOTIFICATION_RENDER_WIDGET_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) {
 | 
| +    WebContentsImpl* web_contents = it->first;
 | 
| +    if (visible)
 | 
| +      web_contents->ShowContents();
 | 
| +    else
 | 
| +      web_contents->HideContents();
 | 
| +  }
 | 
| +}
 | 
| +
 | 
| +}  // namespace content
 | 
| 
 |