This source file includes following definitions.
- GetInstance
- CreateSoon
- Remove
- PostTask
- ProcessOneHost
- extension_host_type_
- render_process_host
- render_view_host
- IsRenderViewLive
- CreateRenderViewSoon
- CreateRenderViewNow
- GetURL
- LoadInitialURL
- IsBackgroundPage
- Close
- Observe
- RenderProcessGone
- DidStopLoading
- OnDidStopLoading
- DocumentAvailableInMainFrame
- OnDocumentAvailable
- CloseContents
- OnMessageReceived
- OnRequest
- OnEventAck
- OnIncrementLazyKeepaliveCount
- OnDecrementLazyKeepaliveCount
- RenderViewCreated
- RenderViewDeleted
- GetJavaScriptDialogManager
- AddNewContents
- RenderViewReady
- RequestMediaAccessPermission
#include "extensions/browser/extension_host.h"
#include <list>
#include "base/bind.h"
#include "base/logging.h"
#include "base/memory/singleton.h"
#include "base/memory/weak_ptr.h"
#include "base/message_loop/message_loop.h"
#include "base/metrics/histogram.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/chrome_notification_types.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/content_browser_client.h"
#include "content/public/browser/native_web_keyboard_event.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/site_instance.h"
#include "content/public/browser/web_contents.h"
#include "extensions/browser/event_router.h"
#include "extensions/browser/extension_error.h"
#include "extensions/browser/extension_host_delegate.h"
#include "extensions/browser/extension_system.h"
#include "extensions/browser/extensions_browser_client.h"
#include "extensions/browser/process_manager.h"
#include "extensions/browser/runtime_data.h"
#include "extensions/browser/view_type_utils.h"
#include "extensions/common/extension.h"
#include "extensions/common/extension_messages.h"
#include "extensions/common/extension_urls.h"
#include "extensions/common/feature_switch.h"
#include "extensions/common/manifest_handlers/background_info.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/window_open_disposition.h"
using content::BrowserContext;
using content::OpenURLParams;
using content::RenderProcessHost;
using content::RenderViewHost;
using content::SiteInstance;
using content::WebContents;
namespace extensions {
class ExtensionHost::ProcessCreationQueue {
public:
static ProcessCreationQueue* GetInstance() {
return Singleton<ProcessCreationQueue>::get();
}
void CreateSoon(ExtensionHost* host) {
queue_.push_back(host);
PostTask();
}
void Remove(ExtensionHost* host) {
Queue::iterator it = std::find(queue_.begin(), queue_.end(), host);
if (it != queue_.end())
queue_.erase(it);
}
private:
friend class Singleton<ProcessCreationQueue>;
friend struct DefaultSingletonTraits<ProcessCreationQueue>;
ProcessCreationQueue()
: pending_create_(false),
ptr_factory_(this) {}
void PostTask() {
if (!pending_create_) {
base::MessageLoop::current()->PostTask(FROM_HERE,
base::Bind(&ProcessCreationQueue::ProcessOneHost,
ptr_factory_.GetWeakPtr()));
pending_create_ = true;
}
}
void ProcessOneHost() {
pending_create_ = false;
if (queue_.empty())
return;
queue_.front()->CreateRenderViewNow();
queue_.pop_front();
if (!queue_.empty())
PostTask();
}
typedef std::list<ExtensionHost*> Queue;
Queue queue_;
bool pending_create_;
base::WeakPtrFactory<ProcessCreationQueue> ptr_factory_;
};
ExtensionHost::ExtensionHost(const Extension* extension,
SiteInstance* site_instance,
const GURL& url,
ViewType host_type)
: delegate_(ExtensionsBrowserClient::Get()->CreateExtensionHostDelegate()),
extension_(extension),
extension_id_(extension->id()),
browser_context_(site_instance->GetBrowserContext()),
render_view_host_(NULL),
did_stop_loading_(false),
document_element_available_(false),
initial_url_(url),
extension_function_dispatcher_(browser_context_, this),
extension_host_type_(host_type) {
DCHECK(host_type == VIEW_TYPE_EXTENSION_BACKGROUND_PAGE ||
host_type == VIEW_TYPE_EXTENSION_DIALOG ||
host_type == VIEW_TYPE_EXTENSION_INFOBAR ||
host_type == VIEW_TYPE_EXTENSION_POPUP);
host_contents_.reset(WebContents::Create(
WebContents::CreateParams(browser_context_, site_instance))),
content::WebContentsObserver::Observe(host_contents_.get());
host_contents_->SetDelegate(this);
SetViewType(host_contents_.get(), host_type);
render_view_host_ = host_contents_->GetRenderViewHost();
registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED,
content::Source<BrowserContext>(browser_context_));
delegate_->OnExtensionHostCreated(host_contents());
}
ExtensionHost::~ExtensionHost() {
if (extension_host_type_ == VIEW_TYPE_EXTENSION_BACKGROUND_PAGE &&
extension_ && BackgroundInfo::HasLazyBackgroundPage(extension_)) {
UMA_HISTOGRAM_LONG_TIMES("Extensions.EventPageActiveTime",
since_created_.Elapsed());
}
content::NotificationService::current()->Notify(
chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED,
content::Source<BrowserContext>(browser_context_),
content::Details<ExtensionHost>(this));
ProcessCreationQueue::GetInstance()->Remove(this);
}
content::RenderProcessHost* ExtensionHost::render_process_host() const {
return render_view_host()->GetProcess();
}
RenderViewHost* ExtensionHost::render_view_host() const {
return render_view_host_;
}
bool ExtensionHost::IsRenderViewLive() const {
return render_view_host()->IsRenderViewLive();
}
void ExtensionHost::CreateRenderViewSoon() {
if ((render_process_host() && render_process_host()->HasConnection())) {
CreateRenderViewNow();
} else {
ProcessCreationQueue::GetInstance()->CreateSoon(this);
}
}
void ExtensionHost::CreateRenderViewNow() {
LoadInitialURL();
if (IsBackgroundPage()) {
DCHECK(IsRenderViewLive());
delegate_->OnRenderViewCreatedForBackgroundPage(this);
}
}
const GURL& ExtensionHost::GetURL() const {
return host_contents()->GetURL();
}
void ExtensionHost::LoadInitialURL() {
host_contents_->GetController().LoadURL(
initial_url_, content::Referrer(), content::PAGE_TRANSITION_LINK,
std::string());
}
bool ExtensionHost::IsBackgroundPage() const {
DCHECK(extension_host_type_ == VIEW_TYPE_EXTENSION_BACKGROUND_PAGE);
return true;
}
void ExtensionHost::Close() {
content::NotificationService::current()->Notify(
chrome::NOTIFICATION_EXTENSION_HOST_VIEW_SHOULD_CLOSE,
content::Source<BrowserContext>(browser_context_),
content::Details<ExtensionHost>(this));
}
void ExtensionHost::Observe(int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) {
switch (type) {
case chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED:
if (extension_ == content::Details<UnloadedExtensionInfo>(details)->
extension) {
extension_ = NULL;
}
break;
default:
NOTREACHED() << "Unexpected notification sent.";
break;
}
}
void ExtensionHost::RenderProcessGone(base::TerminationStatus status) {
RenderProcessHost* process_host = host_contents_->GetRenderProcessHost();
if (process_host && process_host->FastShutdownStarted())
return;
if (!extension_)
return;
content::NotificationService::current()->Notify(
chrome::NOTIFICATION_EXTENSION_PROCESS_TERMINATED,
content::Source<BrowserContext>(browser_context_),
content::Details<ExtensionHost>(this));
}
void ExtensionHost::DidStopLoading(content::RenderViewHost* render_view_host) {
bool notify = !did_stop_loading_;
did_stop_loading_ = true;
OnDidStopLoading();
if (notify) {
if (extension_host_type_ == VIEW_TYPE_EXTENSION_BACKGROUND_PAGE) {
if (extension_ && BackgroundInfo::HasLazyBackgroundPage(extension_)) {
UMA_HISTOGRAM_TIMES("Extensions.EventPageLoadTime",
since_created_.Elapsed());
} else {
UMA_HISTOGRAM_TIMES("Extensions.BackgroundPageLoadTime",
since_created_.Elapsed());
}
} else if (extension_host_type_ == VIEW_TYPE_EXTENSION_DIALOG) {
UMA_HISTOGRAM_TIMES("Extensions.DialogLoadTime",
since_created_.Elapsed());
} else if (extension_host_type_ == VIEW_TYPE_EXTENSION_POPUP) {
UMA_HISTOGRAM_TIMES("Extensions.PopupLoadTime",
since_created_.Elapsed());
} else if (extension_host_type_ == VIEW_TYPE_EXTENSION_INFOBAR) {
UMA_HISTOGRAM_TIMES("Extensions.InfobarLoadTime",
since_created_.Elapsed());
}
content::NotificationService::current()->Notify(
chrome::NOTIFICATION_EXTENSION_HOST_DID_STOP_LOADING,
content::Source<BrowserContext>(browser_context_),
content::Details<ExtensionHost>(this));
}
}
void ExtensionHost::OnDidStopLoading() {
DCHECK(extension_host_type_ == VIEW_TYPE_EXTENSION_BACKGROUND_PAGE);
}
void ExtensionHost::DocumentAvailableInMainFrame() {
if (document_element_available_)
return;
document_element_available_ = true;
OnDocumentAvailable();
}
void ExtensionHost::OnDocumentAvailable() {
DCHECK(extension_host_type_ == VIEW_TYPE_EXTENSION_BACKGROUND_PAGE);
ExtensionSystem::Get(browser_context_)
->runtime_data()
->SetBackgroundPageReady(extension_, true);
content::NotificationService::current()->Notify(
chrome::NOTIFICATION_EXTENSION_BACKGROUND_PAGE_READY,
content::Source<const Extension>(extension_),
content::NotificationService::NoDetails());
}
void ExtensionHost::CloseContents(WebContents* contents) {
Close();
}
bool ExtensionHost::OnMessageReceived(const IPC::Message& message) {
bool handled = true;
IPC_BEGIN_MESSAGE_MAP(ExtensionHost, message)
IPC_MESSAGE_HANDLER(ExtensionHostMsg_Request, OnRequest)
IPC_MESSAGE_HANDLER(ExtensionHostMsg_EventAck, OnEventAck)
IPC_MESSAGE_HANDLER(ExtensionHostMsg_IncrementLazyKeepaliveCount,
OnIncrementLazyKeepaliveCount)
IPC_MESSAGE_HANDLER(ExtensionHostMsg_DecrementLazyKeepaliveCount,
OnDecrementLazyKeepaliveCount)
IPC_MESSAGE_UNHANDLED(handled = false)
IPC_END_MESSAGE_MAP()
return handled;
}
void ExtensionHost::OnRequest(const ExtensionHostMsg_Request_Params& params) {
extension_function_dispatcher_.Dispatch(params, render_view_host());
}
void ExtensionHost::OnEventAck() {
EventRouter* router = ExtensionSystem::Get(browser_context_)->event_router();
if (router)
router->OnEventAck(browser_context_, extension_id());
}
void ExtensionHost::OnIncrementLazyKeepaliveCount() {
ProcessManager* pm = ExtensionSystem::Get(
browser_context_)->process_manager();
if (pm)
pm->IncrementLazyKeepaliveCount(extension());
}
void ExtensionHost::OnDecrementLazyKeepaliveCount() {
ProcessManager* pm = ExtensionSystem::Get(
browser_context_)->process_manager();
if (pm)
pm->DecrementLazyKeepaliveCount(extension());
}
void ExtensionHost::RenderViewCreated(RenderViewHost* render_view_host) {
render_view_host_ = render_view_host;
}
void ExtensionHost::RenderViewDeleted(RenderViewHost* render_view_host) {
if (render_view_host == render_view_host_)
render_view_host_ = host_contents_->GetRenderViewHost();
}
content::JavaScriptDialogManager* ExtensionHost::GetJavaScriptDialogManager() {
return delegate_->GetJavaScriptDialogManager();
}
void ExtensionHost::AddNewContents(WebContents* source,
WebContents* new_contents,
WindowOpenDisposition disposition,
const gfx::Rect& initial_pos,
bool user_gesture,
bool* was_blocked) {
if (disposition != NEW_POPUP) {
WebContents* associated_contents = GetAssociatedWebContents();
if (associated_contents &&
associated_contents->GetBrowserContext() ==
new_contents->GetBrowserContext()) {
WebContentsDelegate* delegate = associated_contents->GetDelegate();
if (delegate) {
delegate->AddNewContents(
associated_contents, new_contents, disposition, initial_pos,
user_gesture, was_blocked);
return;
}
}
}
delegate_->CreateTab(
new_contents, extension_id_, disposition, initial_pos, user_gesture);
}
void ExtensionHost::RenderViewReady() {
content::NotificationService::current()->Notify(
chrome::NOTIFICATION_EXTENSION_HOST_CREATED,
content::Source<BrowserContext>(browser_context_),
content::Details<ExtensionHost>(this));
}
void ExtensionHost::RequestMediaAccessPermission(
content::WebContents* web_contents,
const content::MediaStreamRequest& request,
const content::MediaResponseCallback& callback) {
delegate_->ProcessMediaAccessRequest(
web_contents, request, callback, extension());
}
}