| Index: content/browser/browser_plugin/browser_plugin_guest_manager.cc
 | 
| diff --git a/content/browser/browser_plugin/browser_plugin_guest_manager.cc b/content/browser/browser_plugin/browser_plugin_guest_manager.cc
 | 
| index 443e7c7c230df4a589e4ca078973ef8447cdd7a0..4bc4235a6ca4dfeb8a603821263b737aefda5d0b 100644
 | 
| --- a/content/browser/browser_plugin/browser_plugin_guest_manager.cc
 | 
| +++ b/content/browser/browser_plugin/browser_plugin_guest_manager.cc
 | 
| @@ -11,8 +11,12 @@
 | 
|  #include "content/common/browser_plugin/browser_plugin_constants.h"
 | 
|  #include "content/common/browser_plugin/browser_plugin_messages.h"
 | 
|  #include "content/common/content_export.h"
 | 
| +#include "content/public/browser/browser_context.h"
 | 
| +#include "content/public/browser/browser_plugin_guest_manager_delegate.h"
 | 
| +#include "content/public/browser/content_browser_client.h"
 | 
|  #include "content/public/browser/render_process_host.h"
 | 
|  #include "content/public/browser/user_metrics.h"
 | 
| +#include "content/public/common/content_client.h"
 | 
|  #include "content/public/common/result_codes.h"
 | 
|  #include "content/public/common/url_constants.h"
 | 
|  #include "content/public/common/url_utils.h"
 | 
| @@ -23,18 +27,44 @@ namespace content {
 | 
|  // static
 | 
|  BrowserPluginHostFactory* BrowserPluginGuestManager::factory_ = NULL;
 | 
|  
 | 
| -BrowserPluginGuestManager::BrowserPluginGuestManager()
 | 
| -    : next_instance_id_(browser_plugin::kInstanceIDNone) {
 | 
| +BrowserPluginGuestManager::BrowserPluginGuestManager(BrowserContext* context)
 | 
| +    : context_(context) {}
 | 
| +
 | 
| +BrowserPluginGuestManagerDelegate*
 | 
| +BrowserPluginGuestManager::GetDelegate() const {
 | 
| +  return context_->GetGuestManagerDelegate();
 | 
|  }
 | 
|  
 | 
|  BrowserPluginGuestManager::~BrowserPluginGuestManager() {
 | 
|  }
 | 
|  
 | 
| +// static.
 | 
| +BrowserPluginGuestManager* BrowserPluginGuestManager::FromBrowserContext(
 | 
| +    BrowserContext* context) {
 | 
| +  BrowserPluginGuestManager* guest_manager =
 | 
| +      static_cast<BrowserPluginGuestManager*>(
 | 
| +        context->GetUserData(
 | 
| +            browser_plugin::kBrowserPluginGuestManagerKeyName));
 | 
| +  if (!guest_manager) {
 | 
| +    guest_manager = BrowserPluginGuestManager::Create(context);
 | 
| +    context->SetUserData(browser_plugin::kBrowserPluginGuestManagerKeyName,
 | 
| +                         guest_manager);
 | 
| +  }
 | 
| +  return guest_manager;
 | 
| +}
 | 
| +
 | 
|  // static
 | 
| -BrowserPluginGuestManager* BrowserPluginGuestManager::Create() {
 | 
| +BrowserPluginGuestManager* BrowserPluginGuestManager::Create(
 | 
| +    BrowserContext* context) {
 | 
|    if (factory_)
 | 
| -    return factory_->CreateBrowserPluginGuestManager();
 | 
| -  return new BrowserPluginGuestManager();
 | 
| +    return factory_->CreateBrowserPluginGuestManager(context);
 | 
| +  return new BrowserPluginGuestManager(context);
 | 
| +}
 | 
| +
 | 
| +int BrowserPluginGuestManager::GetNextInstanceID() {
 | 
| +  if (!GetDelegate())
 | 
| +    return 0;
 | 
| +  return GetDelegate()->GetNextInstanceID();
 | 
|  }
 | 
|  
 | 
|  BrowserPluginGuest* BrowserPluginGuestManager::CreateGuest(
 | 
| @@ -57,15 +87,8 @@ BrowserPluginGuest* BrowserPluginGuestManager::CreateGuest(
 | 
|      return NULL;
 | 
|    }
 | 
|  
 | 
| -  // We usually require BrowserPlugins to be hosted by a storage isolated
 | 
| -  // extension. We treat WebUI pages as a special case if they host the
 | 
| -  // BrowserPlugin in a component extension iframe. In that case, we use the
 | 
| -  // iframe's URL to determine the extension.
 | 
|    const GURL& embedder_site_url = embedder_site_instance->GetSiteURL();
 | 
| -  GURL validated_frame_url(params.embedder_frame_url);
 | 
| -  embedder_process_host->FilterURL(false, &validated_frame_url);
 | 
| -  const std::string& host = content::HasWebUIScheme(embedder_site_url) ?
 | 
| -       validated_frame_url.host() : embedder_site_url.host();
 | 
| +  const std::string& host = embedder_site_url.host();
 | 
|  
 | 
|    std::string url_encoded_partition = net::EscapeQueryParamValue(
 | 
|        params.storage_partition_id, false);
 | 
| @@ -101,43 +124,38 @@ BrowserPluginGuest* BrowserPluginGuestManager::CreateGuest(
 | 
|  BrowserPluginGuest* BrowserPluginGuestManager::GetGuestByInstanceID(
 | 
|      int instance_id,
 | 
|      int embedder_render_process_id) const {
 | 
| -  if (!CanEmbedderAccessInstanceIDMaybeKill(embedder_render_process_id,
 | 
| -                                            instance_id)) {
 | 
| -    return NULL;
 | 
| -  }
 | 
| -  GuestInstanceMap::const_iterator it =
 | 
| -      guest_web_contents_by_instance_id_.find(instance_id);
 | 
| -  if (it == guest_web_contents_by_instance_id_.end())
 | 
| +  if (!GetDelegate())
 | 
|      return NULL;
 | 
| -  return static_cast<WebContentsImpl*>(it->second)->GetBrowserPluginGuest();
 | 
| +
 | 
| +  WebContentsImpl* guest_web_contents = static_cast<WebContentsImpl*>(
 | 
| +      GetDelegate()->GetGuestByInstanceID(instance_id,
 | 
| +                                          embedder_render_process_id));
 | 
| +
 | 
| +  return guest_web_contents ?
 | 
| +      guest_web_contents->GetBrowserPluginGuest() : NULL;
 | 
|  }
 | 
|  
 | 
|  void BrowserPluginGuestManager::AddGuest(int instance_id,
 | 
| -                                         WebContentsImpl* guest_web_contents) {
 | 
| -  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;
 | 
| +                                         WebContents* guest_web_contents) {
 | 
| +  if (!GetDelegate())
 | 
| +    return;
 | 
| +  GetDelegate()->AddGuest(instance_id, guest_web_contents);
 | 
|  }
 | 
|  
 | 
|  void BrowserPluginGuestManager::RemoveGuest(int instance_id) {
 | 
| -  DCHECK(guest_web_contents_by_instance_id_.find(instance_id) !=
 | 
| -         guest_web_contents_by_instance_id_.end());
 | 
| -  guest_web_contents_by_instance_id_.erase(instance_id);
 | 
| +  if (!GetDelegate())
 | 
| +    return;
 | 
| +  GetDelegate()->RemoveGuest(instance_id);
 | 
|  }
 | 
|  
 | 
|  bool BrowserPluginGuestManager::CanEmbedderAccessInstanceIDMaybeKill(
 | 
|      int embedder_render_process_id,
 | 
|      int instance_id) const {
 | 
| -  if (!CanEmbedderAccessInstanceID(embedder_render_process_id, instance_id)) {
 | 
| -    // The embedder process is trying to access a guest it does not own.
 | 
| -    content::RecordAction(
 | 
| -        base::UserMetricsAction("BadMessageTerminate_BPGM"));
 | 
| -    base::KillProcess(
 | 
| -        RenderProcessHost::FromID(embedder_render_process_id)->GetHandle(),
 | 
| -        content::RESULT_CODE_KILLED_BAD_MESSAGE, false);
 | 
| +  if (!GetDelegate())
 | 
|      return false;
 | 
| -  }
 | 
| -  return true;
 | 
| +
 | 
| +  return GetDelegate()->CanEmbedderAccessInstanceIDMaybeKill(
 | 
| +      embedder_render_process_id, instance_id);
 | 
|  }
 | 
|  
 | 
|  void BrowserPluginGuestManager::OnMessageReceived(const IPC::Message& message,
 | 
| @@ -155,74 +173,27 @@ void BrowserPluginGuestManager::OnMessageReceived(const IPC::Message& message,
 | 
|    guest->OnMessageReceivedFromEmbedder(message);
 | 
|  }
 | 
|  
 | 
| -// static
 | 
| -bool BrowserPluginGuestManager::CanEmbedderAccessGuest(
 | 
| -    int embedder_render_process_id,
 | 
| -    BrowserPluginGuest* guest) {
 | 
| -  // The embedder can access the guest if it has not been attached and its
 | 
| -  // opener's embedder lives in the same process as the given embedder.
 | 
| -  if (!guest->attached()) {
 | 
| -    if (!guest->opener())
 | 
| -      return false;
 | 
| -
 | 
| -    return embedder_render_process_id ==
 | 
| -        guest->opener()->embedder_web_contents()->GetRenderProcessHost()->
 | 
| -            GetID();
 | 
| -  }
 | 
| -
 | 
| -  return embedder_render_process_id ==
 | 
| -      guest->embedder_web_contents()->GetRenderProcessHost()->GetID();
 | 
| -}
 | 
| -
 | 
| -bool BrowserPluginGuestManager::CanEmbedderAccessInstanceID(
 | 
| -    int embedder_render_process_id,
 | 
| -    int instance_id) const {
 | 
| -  // The embedder is trying to access a guest with a negative or zero
 | 
| -  // instance ID.
 | 
| -  if (instance_id <= browser_plugin::kInstanceIDNone)
 | 
| -    return false;
 | 
| -
 | 
| -  // The embedder is trying to access an instance ID that has not yet been
 | 
| -  // allocated by BrowserPluginGuestManager. This could cause instance ID
 | 
| -  // collisions in the future, and potentially give one embedder access to a
 | 
| -  // guest it does not own.
 | 
| -  if (instance_id > next_instance_id_)
 | 
| -    return false;
 | 
| -
 | 
| -  GuestInstanceMap::const_iterator it =
 | 
| -      guest_web_contents_by_instance_id_.find(instance_id);
 | 
| -  if (it == guest_web_contents_by_instance_id_.end())
 | 
| -    return true;
 | 
| -  BrowserPluginGuest* guest =
 | 
| -      static_cast<WebContentsImpl*>(it->second)->GetBrowserPluginGuest();
 | 
| -
 | 
| -  return CanEmbedderAccessGuest(embedder_render_process_id, guest);
 | 
| -}
 | 
| -
 | 
|  SiteInstance* BrowserPluginGuestManager::GetGuestSiteInstance(
 | 
|      const GURL& guest_site) {
 | 
| -  for (GuestInstanceMap::const_iterator it =
 | 
| -       guest_web_contents_by_instance_id_.begin();
 | 
| -       it != guest_web_contents_by_instance_id_.end(); ++it) {
 | 
| -    if (it->second->GetSiteInstance()->GetSiteURL() == guest_site)
 | 
| -      return it->second->GetSiteInstance();
 | 
| -  }
 | 
| -  return NULL;
 | 
| +  if (!GetDelegate())
 | 
| +    return NULL;
 | 
| +  return GetDelegate()->GetGuestSiteInstance(guest_site);
 | 
| +}
 | 
| +
 | 
| +static bool BrowserPluginGuestCallback(
 | 
| +    const BrowserPluginGuestManager::GuestCallback& callback,
 | 
| +    WebContents* guest_web_contents) {
 | 
| +  return callback.Run(static_cast<WebContentsImpl*>(guest_web_contents)
 | 
| +                          ->GetBrowserPluginGuest());
 | 
|  }
 | 
|  
 | 
|  bool BrowserPluginGuestManager::ForEachGuest(
 | 
| -    WebContentsImpl* embedder_web_contents, const GuestCallback& callback) {
 | 
| -  for (GuestInstanceMap::iterator it =
 | 
| -           guest_web_contents_by_instance_id_.begin();
 | 
| -       it != guest_web_contents_by_instance_id_.end(); ++it) {
 | 
| -    BrowserPluginGuest* guest = it->second->GetBrowserPluginGuest();
 | 
| -    if (embedder_web_contents != guest->embedder_web_contents())
 | 
| -      continue;
 | 
| -
 | 
| -    if (callback.Run(guest))
 | 
| -      return true;
 | 
| -  }
 | 
| -  return false;
 | 
| +    WebContents* embedder_web_contents, const GuestCallback& callback) {
 | 
| +  if (!GetDelegate())
 | 
| +    return false;
 | 
| +  return GetDelegate()->ForEachGuest(embedder_web_contents,
 | 
| +                                     base::Bind(&BrowserPluginGuestCallback,
 | 
| +                                                callback));
 | 
|  }
 | 
|  
 | 
|  }  // namespace content
 | 
| 
 |