1 // Copyright 2013 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_process_resource_provider.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/chrome_notification_types.h"
11 #include "chrome/browser/devtools/devtools_window.h"
12 #include "chrome/browser/extensions/extension_host.h"
13 #include "chrome/browser/extensions/extension_system.h"
14 #include "chrome/browser/profiles/profile.h"
15 #include "chrome/browser/profiles/profile_manager.h"
16 #include "chrome/browser/task_manager/resource_provider.h"
17 #include "chrome/browser/task_manager/task_manager.h"
18 #include "chrome/browser/task_manager/task_manager_util.h"
19 #include "content/public/browser/notification_details.h"
20 #include "content/public/browser/notification_service.h"
21 #include "content/public/browser/render_frame_host.h"
22 #include "content/public/browser/render_process_host.h"
23 #include "content/public/browser/render_view_host.h"
24 #include "content/public/browser/site_instance.h"
25 #include "content/public/browser/web_contents.h"
26 #include "extensions/browser/process_manager.h"
27 #include "extensions/browser/view_type_utils.h"
28 #include "extensions/common/extension.h"
29 #include "grit/theme_resources.h"
30 #include "ui/base/l10n/l10n_util.h"
31 #include "ui/base/resource/resource_bundle.h"
32 #include "ui/gfx/image/image_skia.h"
34 using content::WebContents
;
35 using extensions::Extension
;
37 namespace task_manager
{
39 class ExtensionProcessResource
: public Resource
{
41 explicit ExtensionProcessResource(
42 content::RenderViewHost
* render_view_host
);
43 virtual ~ExtensionProcessResource();
46 virtual base::string16
GetTitle() const OVERRIDE
;
47 virtual base::string16
GetProfileName() const OVERRIDE
;
48 virtual gfx::ImageSkia
GetIcon() const OVERRIDE
;
49 virtual base::ProcessHandle
GetProcess() const OVERRIDE
;
50 virtual int GetUniqueChildProcessId() const OVERRIDE
;
51 virtual Type
GetType() const OVERRIDE
;
52 virtual bool CanInspect() const OVERRIDE
;
53 virtual void Inspect() const OVERRIDE
;
54 virtual bool SupportNetworkUsage() const OVERRIDE
;
55 virtual void SetSupportNetworkUsage() OVERRIDE
;
56 virtual const extensions::Extension
* GetExtension() const OVERRIDE
;
58 // Returns the pid of the extension process.
59 int process_id() const { return pid_
; }
61 // Returns true if the associated extension has a background page.
62 virtual bool IsBackground() const OVERRIDE
;
65 // The icon painted for the extension process.
66 static gfx::ImageSkia
* default_icon_
;
68 content::RenderViewHost
* render_view_host_
;
70 // Cached data about the extension.
71 base::ProcessHandle process_handle_
;
73 int unique_process_id_
;
74 base::string16 title_
;
76 DISALLOW_COPY_AND_ASSIGN(ExtensionProcessResource
);
79 gfx::ImageSkia
* ExtensionProcessResource::default_icon_
= NULL
;
81 ExtensionProcessResource::ExtensionProcessResource(
82 content::RenderViewHost
* render_view_host
)
83 : render_view_host_(render_view_host
) {
85 ResourceBundle
& rb
= ResourceBundle::GetSharedInstance();
86 default_icon_
= rb
.GetImageSkiaNamed(IDR_PLUGINS_FAVICON
);
88 process_handle_
= render_view_host_
->GetProcess()->GetHandle();
89 unique_process_id_
= render_view_host
->GetProcess()->GetID();
90 pid_
= base::GetProcId(process_handle_
);
91 base::string16 extension_name
= base::UTF8ToUTF16(GetExtension()->name());
92 DCHECK(!extension_name
.empty());
94 Profile
* profile
= Profile::FromBrowserContext(
95 render_view_host
->GetProcess()->GetBrowserContext());
96 int message_id
= util::GetMessagePrefixID(
97 GetExtension()->is_app(),
99 profile
->IsOffTheRecord(),
100 false, // is_prerender
102 title_
= l10n_util::GetStringFUTF16(message_id
, extension_name
);
105 ExtensionProcessResource::~ExtensionProcessResource() {
108 base::string16
ExtensionProcessResource::GetTitle() const {
112 base::string16
ExtensionProcessResource::GetProfileName() const {
113 return util::GetProfileNameFromInfoCache(
114 Profile::FromBrowserContext(
115 render_view_host_
->GetProcess()->GetBrowserContext()));
118 gfx::ImageSkia
ExtensionProcessResource::GetIcon() const {
119 return *default_icon_
;
122 base::ProcessHandle
ExtensionProcessResource::GetProcess() const {
123 return process_handle_
;
126 int ExtensionProcessResource::GetUniqueChildProcessId() const {
127 return unique_process_id_
;
130 Resource::Type
ExtensionProcessResource::GetType() const {
134 bool ExtensionProcessResource::CanInspect() const {
138 void ExtensionProcessResource::Inspect() const {
139 DevToolsWindow::OpenDevToolsWindow(render_view_host_
);
142 bool ExtensionProcessResource::SupportNetworkUsage() const {
146 void ExtensionProcessResource::SetSupportNetworkUsage() {
150 const Extension
* ExtensionProcessResource::GetExtension() const {
151 Profile
* profile
= Profile::FromBrowserContext(
152 render_view_host_
->GetProcess()->GetBrowserContext());
153 extensions::ProcessManager
* process_manager
=
154 extensions::ExtensionSystem::Get(profile
)->process_manager();
155 return process_manager
->GetExtensionForRenderViewHost(render_view_host_
);
158 bool ExtensionProcessResource::IsBackground() const {
159 WebContents
* web_contents
=
160 WebContents::FromRenderViewHost(render_view_host_
);
161 extensions::ViewType view_type
= extensions::GetViewType(web_contents
);
162 return view_type
== extensions::VIEW_TYPE_EXTENSION_BACKGROUND_PAGE
;
165 ////////////////////////////////////////////////////////////////////////////////
166 // ExtensionProcessResourceProvider class
167 ////////////////////////////////////////////////////////////////////////////////
169 ExtensionProcessResourceProvider::
170 ExtensionProcessResourceProvider(TaskManager
* task_manager
)
171 : task_manager_(task_manager
),
175 ExtensionProcessResourceProvider::~ExtensionProcessResourceProvider() {
178 Resource
* ExtensionProcessResourceProvider::GetResource(
182 // If an origin PID was specified, the request is from a plugin, not the
183 // render view host process
187 content::RenderFrameHost
* rfh
=
188 content::RenderFrameHost::FromID(child_id
, route_id
);
189 content::WebContents
* web_contents
=
190 content::WebContents::FromRenderFrameHost(rfh
);
192 for (ExtensionRenderViewHostMap::iterator i
= resources_
.begin();
193 i
!= resources_
.end(); i
++) {
194 content::WebContents
* view_contents
=
195 content::WebContents::FromRenderViewHost(i
->first
);
196 if (web_contents
== view_contents
)
200 // Can happen if the page went away while a network request was being
205 void ExtensionProcessResourceProvider::StartUpdating() {
209 // Add all the existing extension views from all Profiles, including those
210 // from incognito split mode.
211 ProfileManager
* profile_manager
= g_browser_process
->profile_manager();
212 std::vector
<Profile
*> profiles(profile_manager
->GetLoadedProfiles());
213 size_t num_default_profiles
= profiles
.size();
214 for (size_t i
= 0; i
< num_default_profiles
; ++i
) {
215 if (profiles
[i
]->HasOffTheRecordProfile()) {
216 profiles
.push_back(profiles
[i
]->GetOffTheRecordProfile());
220 for (size_t i
= 0; i
< profiles
.size(); ++i
) {
221 extensions::ProcessManager
* process_manager
=
222 extensions::ExtensionSystem::Get(profiles
[i
])->process_manager();
223 if (process_manager
) {
224 const extensions::ProcessManager::ViewSet all_views
=
225 process_manager
->GetAllViews();
226 extensions::ProcessManager::ViewSet::const_iterator jt
=
228 for (; jt
!= all_views
.end(); ++jt
) {
229 content::RenderViewHost
* rvh
= *jt
;
230 // Don't add dead extension processes.
231 if (!rvh
->IsRenderViewLive())
234 AddToTaskManager(rvh
);
239 // Register for notifications about extension process changes.
240 registrar_
.Add(this, chrome::NOTIFICATION_EXTENSION_VIEW_REGISTERED
,
241 content::NotificationService::AllBrowserContextsAndSources());
242 registrar_
.Add(this, chrome::NOTIFICATION_EXTENSION_PROCESS_TERMINATED
,
243 content::NotificationService::AllBrowserContextsAndSources());
244 registrar_
.Add(this, chrome::NOTIFICATION_EXTENSION_VIEW_UNREGISTERED
,
245 content::NotificationService::AllBrowserContextsAndSources());
248 void ExtensionProcessResourceProvider::StopUpdating() {
252 // Unregister for notifications about extension process changes.
254 this, chrome::NOTIFICATION_EXTENSION_VIEW_REGISTERED
,
255 content::NotificationService::AllBrowserContextsAndSources());
257 this, chrome::NOTIFICATION_EXTENSION_PROCESS_TERMINATED
,
258 content::NotificationService::AllBrowserContextsAndSources());
260 this, chrome::NOTIFICATION_EXTENSION_VIEW_UNREGISTERED
,
261 content::NotificationService::AllBrowserContextsAndSources());
263 // Delete all the resources.
264 STLDeleteContainerPairSecondPointers(resources_
.begin(), resources_
.end());
269 void ExtensionProcessResourceProvider::Observe(
271 const content::NotificationSource
& source
,
272 const content::NotificationDetails
& details
) {
274 case chrome::NOTIFICATION_EXTENSION_VIEW_REGISTERED
:
276 content::Details
<content::RenderViewHost
>(details
).ptr());
278 case chrome::NOTIFICATION_EXTENSION_PROCESS_TERMINATED
:
279 RemoveFromTaskManager(
280 content::Details
<extensions::ExtensionHost
>(details
).ptr()->
283 case chrome::NOTIFICATION_EXTENSION_VIEW_UNREGISTERED
:
284 RemoveFromTaskManager(
285 content::Details
<content::RenderViewHost
>(details
).ptr());
288 NOTREACHED() << "Unexpected notification.";
293 bool ExtensionProcessResourceProvider::
294 IsHandledByThisProvider(content::RenderViewHost
* render_view_host
) {
295 WebContents
* web_contents
= WebContents::FromRenderViewHost(render_view_host
);
296 // Don't add WebContents that belong to a guest (those are handled by
297 // GuestResourceProvider). Otherwise they will be added twice, and
298 // in this case they will have the app's name as a title (due to the
299 // ExtensionProcessResource constructor).
300 if (web_contents
->GetRenderProcessHost()->IsGuest())
302 extensions::ViewType view_type
= extensions::GetViewType(web_contents
);
303 // Don't add WebContents (those are handled by
304 // TabContentsResourceProvider) or background contents (handled
305 // by BackgroundResourceProvider).
307 return (view_type
!= extensions::VIEW_TYPE_TAB_CONTENTS
&&
308 view_type
!= extensions::VIEW_TYPE_BACKGROUND_CONTENTS
);
310 return (view_type
!= extensions::VIEW_TYPE_TAB_CONTENTS
&&
311 view_type
!= extensions::VIEW_TYPE_BACKGROUND_CONTENTS
&&
312 view_type
!= extensions::VIEW_TYPE_PANEL
);
316 void ExtensionProcessResourceProvider::AddToTaskManager(
317 content::RenderViewHost
* render_view_host
) {
318 if (!IsHandledByThisProvider(render_view_host
))
321 ExtensionProcessResource
* resource
=
322 new ExtensionProcessResource(render_view_host
);
323 CHECK(resources_
.find(render_view_host
) == resources_
.end());
324 resources_
[render_view_host
] = resource
;
325 task_manager_
->AddResource(resource
);
328 void ExtensionProcessResourceProvider::RemoveFromTaskManager(
329 content::RenderViewHost
* render_view_host
) {
332 std::map
<content::RenderViewHost
*, ExtensionProcessResource
*>
333 ::iterator iter
= resources_
.find(render_view_host
);
334 if (iter
== resources_
.end())
337 // Remove the resource from the Task Manager.
338 ExtensionProcessResource
* resource
= iter
->second
;
339 task_manager_
->RemoveResource(resource
);
341 // Remove it from the provider.
342 resources_
.erase(iter
);
344 // Finally, delete the resource.
348 } // namespace task_manager