This source file includes following definitions.
- Create
- CreateGuest
- GetGuestByInstanceID
- AddGuest
- RemoveGuest
- CanEmbedderAccessInstanceIDMaybeKill
- OnMessageReceived
- CanEmbedderAccessGuest
- CanEmbedderAccessInstanceID
- GetGuestSiteInstance
- OnUnhandledSwapBuffersACK
- ForEachGuest
#include "content/browser/browser_plugin/browser_plugin_guest_manager.h"
#include "content/browser/browser_plugin/browser_plugin_guest.h"
#include "content/browser/browser_plugin/browser_plugin_host_factory.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/browser_plugin_constants.h"
#include "content/common/browser_plugin/browser_plugin_messages.h"
#include "content/common/content_export.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/user_metrics.h"
#include "content/public/common/result_codes.h"
#include "content/public/common/url_constants.h"
#include "content/public/common/url_utils.h"
#include "net/base/escape.h"
namespace content {
BrowserPluginHostFactory* BrowserPluginGuestManager::factory_ = NULL;
BrowserPluginGuestManager::BrowserPluginGuestManager()
: next_instance_id_(browser_plugin::kInstanceIDNone) {
}
BrowserPluginGuestManager::~BrowserPluginGuestManager() {
}
BrowserPluginGuestManager* BrowserPluginGuestManager::Create() {
if (factory_)
return factory_->CreateBrowserPluginGuestManager();
return new BrowserPluginGuestManager();
}
BrowserPluginGuest* BrowserPluginGuestManager::CreateGuest(
SiteInstance* embedder_site_instance,
int instance_id,
const BrowserPluginHostMsg_Attach_Params& params,
scoped_ptr<base::DictionaryValue> extra_params) {
RenderProcessHost* embedder_process_host =
embedder_site_instance->GetProcess();
if (!IsStringUTF8(params.storage_partition_id)) {
content::RecordAction(
base::UserMetricsAction("BadMessageTerminate_BPGM"));
base::KillProcess(
embedder_process_host->GetHandle(),
content::RESULT_CODE_KILLED_BAD_MESSAGE, false);
return NULL;
}
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();
std::string url_encoded_partition = net::EscapeQueryParamValue(
params.storage_partition_id, false);
GURL guest_site(base::StringPrintf("%s://%s/%s?%s",
kGuestScheme,
host.c_str(),
params.persist_storage ? "persist" : "",
url_encoded_partition.c_str()));
SiteInstance* guest_site_instance = GetGuestSiteInstance(guest_site);
if (!guest_site_instance) {
guest_site_instance = SiteInstance::CreateForURL(
embedder_site_instance->GetBrowserContext(), guest_site);
}
return WebContentsImpl::CreateGuest(
embedder_site_instance->GetBrowserContext(),
guest_site_instance,
instance_id,
extra_params.Pass());
}
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())
return NULL;
return static_cast<WebContentsImpl*>(it->second)->GetBrowserPluginGuest();
}
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;
}
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);
}
bool BrowserPluginGuestManager::CanEmbedderAccessInstanceIDMaybeKill(
int embedder_render_process_id,
int instance_id) const {
if (!CanEmbedderAccessInstanceID(embedder_render_process_id, instance_id)) {
content::RecordAction(
base::UserMetricsAction("BadMessageTerminate_BPGM"));
base::KillProcess(
RenderProcessHost::FromID(embedder_render_process_id)->GetHandle(),
content::RESULT_CODE_KILLED_BAD_MESSAGE, false);
return false;
}
return true;
}
void BrowserPluginGuestManager::OnMessageReceived(const IPC::Message& message,
int render_process_id) {
if (BrowserPluginGuest::ShouldForwardToBrowserPluginGuest(message)) {
int instance_id = 0;
PickleIterator iter(message);
bool success = iter.ReadInt(&instance_id);
DCHECK(success);
BrowserPluginGuest* guest =
GetGuestByInstanceID(instance_id, render_process_id);
if (guest && guest->OnMessageReceivedFromEmbedder(message))
return;
}
IPC_BEGIN_MESSAGE_MAP(BrowserPluginGuestManager, message)
IPC_MESSAGE_HANDLER(BrowserPluginHostMsg_BuffersSwappedACK,
OnUnhandledSwapBuffersACK)
IPC_END_MESSAGE_MAP()
}
bool BrowserPluginGuestManager::CanEmbedderAccessGuest(
int embedder_render_process_id,
BrowserPluginGuest* guest) {
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 {
if (instance_id <= browser_plugin::kInstanceIDNone)
return false;
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;
}
void BrowserPluginGuestManager::OnUnhandledSwapBuffersACK(
int instance_id,
const FrameHostMsg_BuffersSwappedACK_Params& params) {
BrowserPluginGuest::AcknowledgeBufferPresent(params.gpu_route_id,
params.gpu_host_id,
params.mailbox,
params.sync_point);
}
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;
}
}