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/guest_resource_provider.h"
7 #include "base/strings/string16.h"
8 #include "chrome/browser/chrome_notification_types.h"
9 #include "chrome/browser/favicon/favicon_tab_helper.h"
10 #include "chrome/browser/profiles/profile.h"
11 #include "chrome/browser/task_manager/renderer_resource.h"
12 #include "chrome/browser/task_manager/resource_provider.h"
13 #include "chrome/browser/task_manager/task_manager.h"
14 #include "chrome/browser/task_manager/task_manager_util.h"
15 #include "content/public/browser/notification_service.h"
16 #include "content/public/browser/render_process_host.h"
17 #include "content/public/browser/render_view_host.h"
18 #include "content/public/browser/render_widget_host_iterator.h"
19 #include "content/public/browser/site_instance.h"
20 #include "content/public/browser/web_contents.h"
21 #include "grit/generated_resources.h"
22 #include "ui/base/l10n/l10n_util.h"
23 #include "ui/gfx/image/image.h"
24 #include "ui/gfx/image/image_skia.h"
26 using content::RenderProcessHost;
27 using content::RenderViewHost;
28 using content::RenderWidgetHost;
29 using content::WebContents;
30 using extensions::Extension;
32 namespace task_manager {
34 class GuestResource : public RendererResource {
36 explicit GuestResource(content::RenderViewHost* render_view_host);
37 virtual ~GuestResource();
40 virtual Type GetType() const OVERRIDE;
41 virtual string16 GetTitle() const OVERRIDE;
42 virtual string16 GetProfileName() const OVERRIDE;
43 virtual gfx::ImageSkia GetIcon() const OVERRIDE;
44 virtual content::WebContents* GetWebContents() const OVERRIDE;
45 virtual const extensions::Extension* GetExtension() const OVERRIDE;
48 DISALLOW_COPY_AND_ASSIGN(GuestResource);
51 GuestResource::GuestResource(RenderViewHost* render_view_host)
53 render_view_host->GetSiteInstance()->GetProcess()->GetHandle(),
57 GuestResource::~GuestResource() {
60 Resource::Type GuestResource::GetType() const {
64 string16 GuestResource::GetTitle() const {
65 WebContents* web_contents = GetWebContents();
66 const int message_id = IDS_TASK_MANAGER_WEBVIEW_TAG_PREFIX;
68 string16 title = util::GetTitleFromWebContents(web_contents);
69 return l10n_util::GetStringFUTF16(message_id, title);
71 return l10n_util::GetStringFUTF16(message_id, string16());
74 string16 GuestResource::GetProfileName() const {
75 WebContents* web_contents = GetWebContents();
77 Profile* profile = Profile::FromBrowserContext(
78 web_contents->GetBrowserContext());
79 return util::GetProfileNameFromInfoCache(profile);
84 gfx::ImageSkia GuestResource::GetIcon() const {
85 WebContents* web_contents = GetWebContents();
86 if (web_contents && FaviconTabHelper::FromWebContents(web_contents)) {
87 return FaviconTabHelper::FromWebContents(web_contents)->
88 GetFavicon().AsImageSkia();
90 return gfx::ImageSkia();
93 WebContents* GuestResource::GetWebContents() const {
94 return WebContents::FromRenderViewHost(render_view_host());
97 const Extension* GuestResource::GetExtension() const {
101 GuestResourceProvider::GuestResourceProvider(TaskManager* task_manager)
103 task_manager_(task_manager) {
106 GuestResourceProvider::~GuestResourceProvider() {
109 Resource* GuestResourceProvider::GetResource(
111 int render_process_host_id,
113 // If an origin PID was specified then the request originated in a plugin
114 // working on the WebContents's behalf, so ignore it.
118 for (GuestResourceMap::iterator i = resources_.begin();
119 i != resources_.end(); ++i) {
120 WebContents* contents = WebContents::FromRenderViewHost(i->first);
122 contents->GetRenderProcessHost()->GetID() == render_process_host_id &&
123 contents->GetRenderViewHost()->GetRoutingID() == routing_id) {
131 void GuestResourceProvider::StartUpdating() {
135 // Add all the existing guest WebContents.
136 scoped_ptr<content::RenderWidgetHostIterator> widgets(
137 RenderWidgetHost::GetRenderWidgetHosts());
138 while (content::RenderWidgetHost* widget = widgets->GetNextHost()) {
139 if (widget->IsRenderView()) {
140 RenderViewHost* rvh = RenderViewHost::From(widget);
141 if (rvh->IsSubframe())
146 // Then we register for notifications to get new guests.
147 registrar_.Add(this, content::NOTIFICATION_WEB_CONTENTS_CONNECTED,
148 content::NotificationService::AllBrowserContextsAndSources());
149 registrar_.Add(this, content::NOTIFICATION_WEB_CONTENTS_DISCONNECTED,
150 content::NotificationService::AllBrowserContextsAndSources());
153 void GuestResourceProvider::StopUpdating() {
157 // Unregister for notifications.
158 registrar_.Remove(this, content::NOTIFICATION_WEB_CONTENTS_CONNECTED,
159 content::NotificationService::AllBrowserContextsAndSources());
160 registrar_.Remove(this, content::NOTIFICATION_WEB_CONTENTS_DISCONNECTED,
161 content::NotificationService::AllBrowserContextsAndSources());
163 // Delete all the resources.
164 STLDeleteContainerPairSecondPointers(resources_.begin(), resources_.end());
169 void GuestResourceProvider::Add(RenderViewHost* render_view_host) {
170 GuestResource* resource = new GuestResource(render_view_host);
171 resources_[render_view_host] = resource;
172 task_manager_->AddResource(resource);
175 void GuestResourceProvider::Remove(RenderViewHost* render_view_host) {
179 GuestResourceMap::iterator iter = resources_.find(render_view_host);
180 if (iter == resources_.end())
183 GuestResource* resource = iter->second;
184 task_manager_->RemoveResource(resource);
185 resources_.erase(iter);
189 void GuestResourceProvider::Observe(int type,
190 const content::NotificationSource& source,
191 const content::NotificationDetails& details) {
192 WebContents* web_contents = content::Source<WebContents>(source).ptr();
193 if (!web_contents || !web_contents->GetRenderViewHost()->IsSubframe())
197 case content::NOTIFICATION_WEB_CONTENTS_CONNECTED:
198 Add(web_contents->GetRenderViewHost());
200 case content::NOTIFICATION_WEB_CONTENTS_DISCONNECTED:
201 Remove(web_contents->GetRenderViewHost());
204 NOTREACHED() << "Unexpected notification.";
208 } // namespace task_manager