blob: 5dcedf1a1bb6579a079b579ee42a4982e6a90112 [file] [log] [blame]
[email protected]7efaed22013-05-13 15:18:071// 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.
4
[email protected]44a41aa2013-05-20 11:49:225#include "chrome/browser/task_manager/guest_resource_provider.h"
[email protected]7efaed22013-05-13 15:18:076
[email protected]9f0abdb2013-06-10 21:49:347#include "base/strings/string16.h"
[email protected]7efaed22013-05-13 15:18:078#include "chrome/browser/favicon/favicon_tab_helper.h"
9#include "chrome/browser/profiles/profile.h"
[email protected]44a41aa2013-05-20 11:49:2210#include "chrome/browser/task_manager/renderer_resource.h"
[email protected]b18943a2013-05-23 15:59:1511#include "chrome/browser/task_manager/resource_provider.h"
12#include "chrome/browser/task_manager/task_manager.h"
[email protected]44a41aa2013-05-20 11:49:2213#include "chrome/browser/task_manager/task_manager_util.h"
[email protected]7efaed22013-05-13 15:18:0714#include "chrome/common/chrome_notification_types.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/site_instance.h"
19#include "content/public/browser/web_contents.h"
20#include "grit/generated_resources.h"
21#include "ui/base/l10n/l10n_util.h"
[email protected]299e4542013-05-16 03:09:0622#include "ui/gfx/image/image.h"
[email protected]6a9d9d72013-05-15 15:49:0923#include "ui/gfx/image/image_skia.h"
[email protected]7efaed22013-05-13 15:18:0724
25using content::RenderProcessHost;
26using content::RenderViewHost;
27using content::RenderWidgetHost;
28using content::WebContents;
29using extensions::Extension;
30
[email protected]44a41aa2013-05-20 11:49:2231namespace task_manager {
32
33class GuestResource : public RendererResource {
[email protected]6a9d9d72013-05-15 15:49:0934 public:
[email protected]44a41aa2013-05-20 11:49:2235 explicit GuestResource(content::RenderViewHost* render_view_host);
36 virtual ~GuestResource();
[email protected]7efaed22013-05-13 15:18:0737
[email protected]b18943a2013-05-23 15:59:1538 // Resource methods:
[email protected]6a9d9d72013-05-15 15:49:0939 virtual Type GetType() const OVERRIDE;
40 virtual string16 GetTitle() const OVERRIDE;
41 virtual string16 GetProfileName() const OVERRIDE;
42 virtual gfx::ImageSkia GetIcon() const OVERRIDE;
43 virtual content::WebContents* GetWebContents() const OVERRIDE;
44 virtual const extensions::Extension* GetExtension() const OVERRIDE;
[email protected]7efaed22013-05-13 15:18:0745
[email protected]6a9d9d72013-05-15 15:49:0946 private:
[email protected]44a41aa2013-05-20 11:49:2247 DISALLOW_COPY_AND_ASSIGN(GuestResource);
[email protected]6a9d9d72013-05-15 15:49:0948};
[email protected]7efaed22013-05-13 15:18:0749
[email protected]44a41aa2013-05-20 11:49:2250GuestResource::GuestResource(RenderViewHost* render_view_host)
51 : RendererResource(
[email protected]7efaed22013-05-13 15:18:0752 render_view_host->GetSiteInstance()->GetProcess()->GetHandle(),
53 render_view_host) {
54}
55
[email protected]44a41aa2013-05-20 11:49:2256GuestResource::~GuestResource() {
[email protected]7efaed22013-05-13 15:18:0757}
58
[email protected]b18943a2013-05-23 15:59:1559Resource::Type GuestResource::GetType() const {
[email protected]7efaed22013-05-13 15:18:0760 return GUEST;
61}
62
[email protected]44a41aa2013-05-20 11:49:2263string16 GuestResource::GetTitle() const {
[email protected]7efaed22013-05-13 15:18:0764 WebContents* web_contents = GetWebContents();
65 const int message_id = IDS_TASK_MANAGER_WEBVIEW_TAG_PREFIX;
66 if (web_contents) {
[email protected]44a41aa2013-05-20 11:49:2267 string16 title = util::GetTitleFromWebContents(web_contents);
[email protected]7efaed22013-05-13 15:18:0768 return l10n_util::GetStringFUTF16(message_id, title);
69 }
70 return l10n_util::GetStringFUTF16(message_id, string16());
71}
72
[email protected]44a41aa2013-05-20 11:49:2273string16 GuestResource::GetProfileName() const {
[email protected]7efaed22013-05-13 15:18:0774 WebContents* web_contents = GetWebContents();
75 if (web_contents) {
76 Profile* profile = Profile::FromBrowserContext(
77 web_contents->GetBrowserContext());
[email protected]44a41aa2013-05-20 11:49:2278 return util::GetProfileNameFromInfoCache(profile);
[email protected]7efaed22013-05-13 15:18:0779 }
80 return string16();
81}
82
[email protected]44a41aa2013-05-20 11:49:2283gfx::ImageSkia GuestResource::GetIcon() const {
[email protected]7efaed22013-05-13 15:18:0784 WebContents* web_contents = GetWebContents();
85 if (web_contents && FaviconTabHelper::FromWebContents(web_contents)) {
86 return FaviconTabHelper::FromWebContents(web_contents)->
87 GetFavicon().AsImageSkia();
88 }
89 return gfx::ImageSkia();
90}
91
[email protected]44a41aa2013-05-20 11:49:2292WebContents* GuestResource::GetWebContents() const {
[email protected]7efaed22013-05-13 15:18:0793 return WebContents::FromRenderViewHost(render_view_host());
94}
95
[email protected]44a41aa2013-05-20 11:49:2296const Extension* GuestResource::GetExtension() const {
[email protected]7efaed22013-05-13 15:18:0797 return NULL;
98}
99
[email protected]44a41aa2013-05-20 11:49:22100GuestResourceProvider::GuestResourceProvider(TaskManager* task_manager)
[email protected]7efaed22013-05-13 15:18:07101 : updating_(false),
102 task_manager_(task_manager) {
103}
104
[email protected]44a41aa2013-05-20 11:49:22105GuestResourceProvider::~GuestResourceProvider() {
[email protected]7efaed22013-05-13 15:18:07106}
107
[email protected]b18943a2013-05-23 15:59:15108Resource* GuestResourceProvider::GetResource(
[email protected]7efaed22013-05-13 15:18:07109 int origin_pid,
110 int render_process_host_id,
111 int routing_id) {
112 // If an origin PID was specified then the request originated in a plugin
113 // working on the WebContents's behalf, so ignore it.
114 if (origin_pid)
115 return NULL;
116
117 for (GuestResourceMap::iterator i = resources_.begin();
118 i != resources_.end(); ++i) {
119 WebContents* contents = WebContents::FromRenderViewHost(i->first);
120 if (contents &&
121 contents->GetRenderProcessHost()->GetID() == render_process_host_id &&
122 contents->GetRenderViewHost()->GetRoutingID() == routing_id) {
123 return i->second;
124 }
125 }
126
127 return NULL;
128}
129
[email protected]44a41aa2013-05-20 11:49:22130void GuestResourceProvider::StartUpdating() {
[email protected]7efaed22013-05-13 15:18:07131 DCHECK(!updating_);
132 updating_ = true;
133
134 // Add all the existing guest WebContents.
[email protected]039b84a42013-06-21 20:23:37135 RenderWidgetHost::List widgets = RenderWidgetHost::GetRenderWidgetHosts();
136 for (size_t i = 0; i < widgets.size(); ++i) {
137 if (widgets[i]->IsRenderView()) {
138 RenderViewHost* rvh = RenderViewHost::From(widgets[i]);
139 if (rvh->IsSubframe())
140 Add(rvh);
[email protected]7efaed22013-05-13 15:18:07141 }
142 }
143
144 // Then we register for notifications to get new guests.
145 registrar_.Add(this, content::NOTIFICATION_WEB_CONTENTS_CONNECTED,
146 content::NotificationService::AllBrowserContextsAndSources());
147 registrar_.Add(this, content::NOTIFICATION_WEB_CONTENTS_DISCONNECTED,
148 content::NotificationService::AllBrowserContextsAndSources());
149}
150
[email protected]44a41aa2013-05-20 11:49:22151void GuestResourceProvider::StopUpdating() {
[email protected]7efaed22013-05-13 15:18:07152 DCHECK(updating_);
153 updating_ = false;
154
155 // Unregister for notifications.
156 registrar_.Remove(this, content::NOTIFICATION_WEB_CONTENTS_CONNECTED,
157 content::NotificationService::AllBrowserContextsAndSources());
158 registrar_.Remove(this, content::NOTIFICATION_WEB_CONTENTS_DISCONNECTED,
159 content::NotificationService::AllBrowserContextsAndSources());
160
161 // Delete all the resources.
162 STLDeleteContainerPairSecondPointers(resources_.begin(), resources_.end());
163
164 resources_.clear();
165}
166
[email protected]44a41aa2013-05-20 11:49:22167void GuestResourceProvider::Add(RenderViewHost* render_view_host) {
168 GuestResource* resource = new GuestResource(render_view_host);
[email protected]7efaed22013-05-13 15:18:07169 resources_[render_view_host] = resource;
170 task_manager_->AddResource(resource);
171}
172
[email protected]44a41aa2013-05-20 11:49:22173void GuestResourceProvider::Remove(RenderViewHost* render_view_host) {
[email protected]7efaed22013-05-13 15:18:07174 if (!updating_)
175 return;
176
177 GuestResourceMap::iterator iter = resources_.find(render_view_host);
178 if (iter == resources_.end())
179 return;
180
[email protected]44a41aa2013-05-20 11:49:22181 GuestResource* resource = iter->second;
[email protected]7efaed22013-05-13 15:18:07182 task_manager_->RemoveResource(resource);
183 resources_.erase(iter);
184 delete resource;
185}
186
[email protected]44a41aa2013-05-20 11:49:22187void GuestResourceProvider::Observe(int type,
[email protected]7efaed22013-05-13 15:18:07188 const content::NotificationSource& source,
189 const content::NotificationDetails& details) {
190 WebContents* web_contents = content::Source<WebContents>(source).ptr();
191 if (!web_contents || !web_contents->GetRenderViewHost()->IsSubframe())
192 return;
193
194 switch (type) {
195 case content::NOTIFICATION_WEB_CONTENTS_CONNECTED:
196 Add(web_contents->GetRenderViewHost());
197 break;
198 case content::NOTIFICATION_WEB_CONTENTS_DISCONNECTED:
199 Remove(web_contents->GetRenderViewHost());
200 break;
201 default:
202 NOTREACHED() << "Unexpected notification.";
203 }
204}
[email protected]44a41aa2013-05-20 11:49:22205
206} // namespace task_manager