1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/task_manager/extension_information.h"
7 #include "base/strings/string16.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "chrome/browser/browser_process.h"
10 #include "chrome/browser/profiles/profile.h"
11 #include "chrome/browser/profiles/profile_manager.h"
12 #include "chrome/browser/task_manager/renderer_resource.h"
13 #include "chrome/browser/task_manager/task_manager_util.h"
14 #include "content/public/browser/render_frame_host.h"
15 #include "content/public/browser/render_process_host.h"
16 #include "content/public/browser/render_view_host.h"
17 #include "content/public/browser/web_contents.h"
18 #include "extensions/browser/extension_system.h"
19 #include "extensions/browser/process_manager.h"
20 #include "extensions/browser/view_type_utils.h"
21 #include "extensions/common/extension.h"
22 #include "grit/theme_resources.h"
23 #include "ui/base/l10n/l10n_util.h"
24 #include "ui/base/resource/resource_bundle.h"
25 #include "ui/gfx/image/image_skia.h"
27 using content::WebContents
;
28 using extensions::Extension
;
32 const Extension
* GetExtensionForWebContents(WebContents
* web_contents
) {
34 Profile::FromBrowserContext(web_contents
->GetBrowserContext());
35 extensions::ProcessManager
* process_manager
=
36 extensions::ExtensionSystem::Get(profile
)->process_manager();
37 return process_manager
->GetExtensionForRenderViewHost(
38 web_contents
->GetRenderViewHost());
43 namespace task_manager
{
45 class ExtensionProcessResource
: public RendererResource
{
47 explicit ExtensionProcessResource(const Extension
* extension
,
48 content::RenderViewHost
* render_view_host
);
49 virtual ~ExtensionProcessResource();
52 virtual base::string16
GetTitle() const OVERRIDE
;
53 virtual gfx::ImageSkia
GetIcon() const OVERRIDE
;
54 virtual Type
GetType() const OVERRIDE
;
57 // Returns true if the associated extension has a background page.
58 bool IsBackground() const;
60 // The icon painted for the extension process.
61 static gfx::ImageSkia
* default_icon_
;
63 // Cached data about the extension.
64 base::string16 title_
;
66 DISALLOW_COPY_AND_ASSIGN(ExtensionProcessResource
);
69 gfx::ImageSkia
* ExtensionProcessResource::default_icon_
= NULL
;
71 ExtensionProcessResource::ExtensionProcessResource(
72 const Extension
* extension
,
73 content::RenderViewHost
* render_view_host
)
74 : RendererResource(render_view_host
->GetProcess()->GetHandle(),
77 ResourceBundle
& rb
= ResourceBundle::GetSharedInstance();
78 default_icon_
= rb
.GetImageSkiaNamed(IDR_PLUGINS_FAVICON
);
80 base::string16 extension_name
= base::UTF8ToUTF16(extension
->name());
81 DCHECK(!extension_name
.empty());
83 Profile
* profile
= Profile::FromBrowserContext(
84 render_view_host
->GetProcess()->GetBrowserContext());
85 int message_id
= util::GetMessagePrefixID(extension
->is_app(),
87 profile
->IsOffTheRecord(),
88 false, // is_prerender
90 title_
= l10n_util::GetStringFUTF16(message_id
, extension_name
);
93 ExtensionProcessResource::~ExtensionProcessResource() {}
95 base::string16
ExtensionProcessResource::GetTitle() const { return title_
; }
97 gfx::ImageSkia
ExtensionProcessResource::GetIcon() const {
98 return *default_icon_
;
101 Resource::Type
ExtensionProcessResource::GetType() const { return EXTENSION
; }
103 bool ExtensionProcessResource::IsBackground() const {
104 WebContents
* web_contents
=
105 WebContents::FromRenderViewHost(render_view_host());
106 extensions::ViewType view_type
= extensions::GetViewType(web_contents
);
107 return view_type
== extensions::VIEW_TYPE_EXTENSION_BACKGROUND_PAGE
;
110 ////////////////////////////////////////////////////////////////////////////////
111 // ExtensionInformation class
112 ////////////////////////////////////////////////////////////////////////////////
114 ExtensionInformation::ExtensionInformation() {}
116 ExtensionInformation::~ExtensionInformation() {}
118 void ExtensionInformation::GetAll(const NewWebContentsCallback
& callback
) {
119 // Add all the existing extension views from all Profiles, including those
120 // from incognito split mode.
121 ProfileManager
* profile_manager
= g_browser_process
->profile_manager();
122 std::vector
<Profile
*> profiles(profile_manager
->GetLoadedProfiles());
123 size_t num_default_profiles
= profiles
.size();
124 for (size_t i
= 0; i
< num_default_profiles
; ++i
) {
125 if (profiles
[i
]->HasOffTheRecordProfile()) {
126 profiles
.push_back(profiles
[i
]->GetOffTheRecordProfile());
130 for (size_t i
= 0; i
< profiles
.size(); ++i
) {
131 extensions::ProcessManager
* process_manager
=
132 extensions::ExtensionSystem::Get(profiles
[i
])->process_manager();
133 if (process_manager
) {
134 const extensions::ProcessManager::ViewSet all_views
=
135 process_manager
->GetAllViews();
136 extensions::ProcessManager::ViewSet::const_iterator jt
=
138 for (; jt
!= all_views
.end(); ++jt
) {
139 WebContents
* web_contents
= WebContents::FromRenderViewHost(*jt
);
140 if (CheckOwnership(web_contents
))
141 callback
.Run(web_contents
);
147 bool ExtensionInformation::CheckOwnership(WebContents
* web_contents
) {
148 // Don't add WebContents that belong to a guest (those are handled by
149 // GuestInformation). Otherwise they will be added twice.
150 if (web_contents
->GetRenderProcessHost()->IsGuest())
153 // Extension WebContents tracked by this class will always host extension
155 if (!GetExtensionForWebContents(web_contents
))
158 extensions::ViewType view_type
= extensions::GetViewType(web_contents
);
160 // Don't add tab contents (those are handled by TabContentsResourceProvider)
161 // or background contents (handled by BackgroundInformation) or panels
162 // (handled by PanelInformation)
163 return (view_type
!= extensions::VIEW_TYPE_INVALID
&&
164 view_type
!= extensions::VIEW_TYPE_TAB_CONTENTS
&&
165 view_type
!= extensions::VIEW_TYPE_BACKGROUND_CONTENTS
&&
166 view_type
!= extensions::VIEW_TYPE_PANEL
);
169 scoped_ptr
<RendererResource
> ExtensionInformation::MakeResource(
170 WebContents
* web_contents
) {
171 const Extension
* extension
= GetExtensionForWebContents(web_contents
);
174 return scoped_ptr
<RendererResource
>();
176 return scoped_ptr
<RendererResource
>(new ExtensionProcessResource(
177 extension
, web_contents
->GetRenderViewHost()));
180 } // namespace task_manager