This source file includes following definitions.
- GetExtensionForWebContents
- GetTitle
- GetIcon
- GetType
- IsBackground
- GetAll
- CheckOwnership
- MakeResource
#include "chrome/browser/task_manager/extension_information.h"
#include "base/strings/string16.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/task_manager/renderer_resource.h"
#include "chrome/browser/task_manager/task_manager_util.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/web_contents.h"
#include "extensions/browser/extension_system.h"
#include "extensions/browser/process_manager.h"
#include "extensions/browser/view_type_utils.h"
#include "extensions/common/extension.h"
#include "grit/theme_resources.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/gfx/image/image_skia.h"
using content::WebContents;
using extensions::Extension;
namespace {
const Extension* GetExtensionForWebContents(WebContents* web_contents) {
Profile* profile =
Profile::FromBrowserContext(web_contents->GetBrowserContext());
extensions::ProcessManager* process_manager =
extensions::ExtensionSystem::Get(profile)->process_manager();
return process_manager->GetExtensionForRenderViewHost(
web_contents->GetRenderViewHost());
}
}
namespace task_manager {
class ExtensionProcessResource : public RendererResource {
public:
explicit ExtensionProcessResource(const Extension* extension,
content::RenderViewHost* render_view_host);
virtual ~ExtensionProcessResource();
virtual base::string16 GetTitle() const OVERRIDE;
virtual gfx::ImageSkia GetIcon() const OVERRIDE;
virtual Type GetType() const OVERRIDE;
private:
bool IsBackground() const;
static gfx::ImageSkia* default_icon_;
base::string16 title_;
DISALLOW_COPY_AND_ASSIGN(ExtensionProcessResource);
};
gfx::ImageSkia* ExtensionProcessResource::default_icon_ = NULL;
ExtensionProcessResource::ExtensionProcessResource(
const Extension* extension,
content::RenderViewHost* render_view_host)
: RendererResource(render_view_host->GetProcess()->GetHandle(),
render_view_host) {
if (!default_icon_) {
ResourceBundle& rb = ResourceBundle::GetSharedInstance();
default_icon_ = rb.GetImageSkiaNamed(IDR_PLUGINS_FAVICON);
}
base::string16 extension_name = base::UTF8ToUTF16(extension->name());
DCHECK(!extension_name.empty());
Profile* profile = Profile::FromBrowserContext(
render_view_host->GetProcess()->GetBrowserContext());
int message_id = util::GetMessagePrefixID(extension->is_app(),
true,
profile->IsOffTheRecord(),
false,
IsBackground());
title_ = l10n_util::GetStringFUTF16(message_id, extension_name);
}
ExtensionProcessResource::~ExtensionProcessResource() {}
base::string16 ExtensionProcessResource::GetTitle() const { return title_; }
gfx::ImageSkia ExtensionProcessResource::GetIcon() const {
return *default_icon_;
}
Resource::Type ExtensionProcessResource::GetType() const { return EXTENSION; }
bool ExtensionProcessResource::IsBackground() const {
WebContents* web_contents =
WebContents::FromRenderViewHost(render_view_host());
extensions::ViewType view_type = extensions::GetViewType(web_contents);
return view_type == extensions::VIEW_TYPE_EXTENSION_BACKGROUND_PAGE;
}
ExtensionInformation::ExtensionInformation() {}
ExtensionInformation::~ExtensionInformation() {}
void ExtensionInformation::GetAll(const NewWebContentsCallback& callback) {
ProfileManager* profile_manager = g_browser_process->profile_manager();
std::vector<Profile*> profiles(profile_manager->GetLoadedProfiles());
size_t num_default_profiles = profiles.size();
for (size_t i = 0; i < num_default_profiles; ++i) {
if (profiles[i]->HasOffTheRecordProfile()) {
profiles.push_back(profiles[i]->GetOffTheRecordProfile());
}
}
for (size_t i = 0; i < profiles.size(); ++i) {
extensions::ProcessManager* process_manager =
extensions::ExtensionSystem::Get(profiles[i])->process_manager();
if (process_manager) {
const extensions::ProcessManager::ViewSet all_views =
process_manager->GetAllViews();
extensions::ProcessManager::ViewSet::const_iterator jt =
all_views.begin();
for (; jt != all_views.end(); ++jt) {
WebContents* web_contents = WebContents::FromRenderViewHost(*jt);
if (CheckOwnership(web_contents))
callback.Run(web_contents);
}
}
}
}
bool ExtensionInformation::CheckOwnership(WebContents* web_contents) {
if (web_contents->GetRenderProcessHost()->IsGuest())
return false;
if (!GetExtensionForWebContents(web_contents))
return false;
extensions::ViewType view_type = extensions::GetViewType(web_contents);
return (view_type != extensions::VIEW_TYPE_INVALID &&
view_type != extensions::VIEW_TYPE_TAB_CONTENTS &&
view_type != extensions::VIEW_TYPE_BACKGROUND_CONTENTS &&
view_type != extensions::VIEW_TYPE_PANEL);
}
scoped_ptr<RendererResource> ExtensionInformation::MakeResource(
WebContents* web_contents) {
const Extension* extension = GetExtensionForWebContents(web_contents);
if (!extension) {
NOTREACHED();
return scoped_ptr<RendererResource>();
}
return scoped_ptr<RendererResource>(new ExtensionProcessResource(
extension, web_contents->GetRenderViewHost()));
}
}