Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(520)

Side by Side Diff: chrome/browser/task_manager/task_manager_guest_resource_provider.cc

Issue 15196003: Create task_manager namespace and wrap classes related to TaskManager with it. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebase Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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.
4
5 #include "chrome/browser/task_manager/task_manager_guest_resource_provider.h"
6
7 #include "base/string16.h"
8 #include "chrome/browser/favicon/favicon_tab_helper.h"
9 #include "chrome/browser/profiles/profile.h"
10 #include "chrome/browser/task_manager/task_manager_render_resource.h"
11 #include "chrome/browser/task_manager/task_manager_resource_util.h"
12 #include "chrome/common/chrome_notification_types.h"
13 #include "content/public/browser/notification_service.h"
14 #include "content/public/browser/render_process_host.h"
15 #include "content/public/browser/render_view_host.h"
16 #include "content/public/browser/site_instance.h"
17 #include "content/public/browser/web_contents.h"
18 #include "grit/generated_resources.h"
19 #include "ui/base/l10n/l10n_util.h"
20 #include "ui/gfx/image/image.h"
21 #include "ui/gfx/image/image_skia.h"
22
23 using content::RenderProcessHost;
24 using content::RenderViewHost;
25 using content::RenderWidgetHost;
26 using content::WebContents;
27 using extensions::Extension;
28
29 class TaskManagerGuestResource : public TaskManagerRendererResource {
30 public:
31 explicit TaskManagerGuestResource(content::RenderViewHost* render_view_host);
32 virtual ~TaskManagerGuestResource();
33
34 // TaskManager::Resource methods:
35 virtual Type GetType() const OVERRIDE;
36 virtual string16 GetTitle() const OVERRIDE;
37 virtual string16 GetProfileName() const OVERRIDE;
38 virtual gfx::ImageSkia GetIcon() const OVERRIDE;
39 virtual content::WebContents* GetWebContents() const OVERRIDE;
40 virtual const extensions::Extension* GetExtension() const OVERRIDE;
41
42 private:
43 DISALLOW_COPY_AND_ASSIGN(TaskManagerGuestResource);
44 };
45
46 TaskManagerGuestResource::TaskManagerGuestResource(
47 RenderViewHost* render_view_host)
48 : TaskManagerRendererResource(
49 render_view_host->GetSiteInstance()->GetProcess()->GetHandle(),
50 render_view_host) {
51 }
52
53 TaskManagerGuestResource::~TaskManagerGuestResource() {
54 }
55
56 TaskManager::Resource::Type TaskManagerGuestResource::GetType() const {
57 return GUEST;
58 }
59
60 string16 TaskManagerGuestResource::GetTitle() const {
61 WebContents* web_contents = GetWebContents();
62 const int message_id = IDS_TASK_MANAGER_WEBVIEW_TAG_PREFIX;
63 if (web_contents) {
64 string16 title = TaskManagerResourceUtil::GetTitleFromWebContents(
65 web_contents);
66 return l10n_util::GetStringFUTF16(message_id, title);
67 }
68 return l10n_util::GetStringFUTF16(message_id, string16());
69 }
70
71 string16 TaskManagerGuestResource::GetProfileName() const {
72 WebContents* web_contents = GetWebContents();
73 if (web_contents) {
74 Profile* profile = Profile::FromBrowserContext(
75 web_contents->GetBrowserContext());
76 return TaskManagerResourceUtil::GetProfileNameFromInfoCache(profile);
77 }
78 return string16();
79 }
80
81 gfx::ImageSkia TaskManagerGuestResource::GetIcon() const {
82 WebContents* web_contents = GetWebContents();
83 if (web_contents && FaviconTabHelper::FromWebContents(web_contents)) {
84 return FaviconTabHelper::FromWebContents(web_contents)->
85 GetFavicon().AsImageSkia();
86 }
87 return gfx::ImageSkia();
88 }
89
90 WebContents* TaskManagerGuestResource::GetWebContents() const {
91 return WebContents::FromRenderViewHost(render_view_host());
92 }
93
94 const Extension* TaskManagerGuestResource::GetExtension() const {
95 return NULL;
96 }
97
98 TaskManagerGuestResourceProvider::
99 TaskManagerGuestResourceProvider(TaskManager* task_manager)
100 : updating_(false),
101 task_manager_(task_manager) {
102 }
103
104 TaskManagerGuestResourceProvider::~TaskManagerGuestResourceProvider() {
105 }
106
107 TaskManager::Resource* TaskManagerGuestResourceProvider::GetResource(
108 int origin_pid,
109 int render_process_host_id,
110 int routing_id) {
111 // If an origin PID was specified then the request originated in a plugin
112 // working on the WebContents's behalf, so ignore it.
113 if (origin_pid)
114 return NULL;
115
116 for (GuestResourceMap::iterator i = resources_.begin();
117 i != resources_.end(); ++i) {
118 WebContents* contents = WebContents::FromRenderViewHost(i->first);
119 if (contents &&
120 contents->GetRenderProcessHost()->GetID() == render_process_host_id &&
121 contents->GetRenderViewHost()->GetRoutingID() == routing_id) {
122 return i->second;
123 }
124 }
125
126 return NULL;
127 }
128
129 void TaskManagerGuestResourceProvider::StartUpdating() {
130 DCHECK(!updating_);
131 updating_ = true;
132
133 // Add all the existing guest WebContents.
134 for (RenderProcessHost::iterator i(
135 RenderProcessHost::AllHostsIterator());
136 !i.IsAtEnd(); i.Advance()) {
137 RenderProcessHost::RenderWidgetHostsIterator iter =
138 i.GetCurrentValue()->GetRenderWidgetHostsIterator();
139 for (; !iter.IsAtEnd(); iter.Advance()) {
140 const RenderWidgetHost* widget = iter.GetCurrentValue();
141 if (widget->IsRenderView()) {
142 RenderViewHost* rvh =
143 RenderViewHost::From(const_cast<RenderWidgetHost*>(widget));
144 if (rvh->IsSubframe())
145 Add(rvh);
146 }
147 }
148 }
149
150 // Then we register for notifications to get new guests.
151 registrar_.Add(this, content::NOTIFICATION_WEB_CONTENTS_CONNECTED,
152 content::NotificationService::AllBrowserContextsAndSources());
153 registrar_.Add(this, content::NOTIFICATION_WEB_CONTENTS_DISCONNECTED,
154 content::NotificationService::AllBrowserContextsAndSources());
155 }
156
157 void TaskManagerGuestResourceProvider::StopUpdating() {
158 DCHECK(updating_);
159 updating_ = false;
160
161 // Unregister for notifications.
162 registrar_.Remove(this, content::NOTIFICATION_WEB_CONTENTS_CONNECTED,
163 content::NotificationService::AllBrowserContextsAndSources());
164 registrar_.Remove(this, content::NOTIFICATION_WEB_CONTENTS_DISCONNECTED,
165 content::NotificationService::AllBrowserContextsAndSources());
166
167 // Delete all the resources.
168 STLDeleteContainerPairSecondPointers(resources_.begin(), resources_.end());
169
170 resources_.clear();
171 }
172
173 void TaskManagerGuestResourceProvider::Add(
174 RenderViewHost* render_view_host) {
175 TaskManagerGuestResource* resource =
176 new TaskManagerGuestResource(render_view_host);
177 resources_[render_view_host] = resource;
178 task_manager_->AddResource(resource);
179 }
180
181 void TaskManagerGuestResourceProvider::Remove(
182 RenderViewHost* render_view_host) {
183 if (!updating_)
184 return;
185
186 GuestResourceMap::iterator iter = resources_.find(render_view_host);
187 if (iter == resources_.end())
188 return;
189
190 TaskManagerGuestResource* resource = iter->second;
191 task_manager_->RemoveResource(resource);
192 resources_.erase(iter);
193 delete resource;
194 }
195
196 void TaskManagerGuestResourceProvider::Observe(int type,
197 const content::NotificationSource& source,
198 const content::NotificationDetails& details) {
199 WebContents* web_contents = content::Source<WebContents>(source).ptr();
200 if (!web_contents || !web_contents->GetRenderViewHost()->IsSubframe())
201 return;
202
203 switch (type) {
204 case content::NOTIFICATION_WEB_CONTENTS_CONNECTED:
205 Add(web_contents->GetRenderViewHost());
206 break;
207 case content::NOTIFICATION_WEB_CONTENTS_DISCONNECTED:
208 Remove(web_contents->GetRenderViewHost());
209 break;
210 default:
211 NOTREACHED() << "Unexpected notification.";
212 }
213 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698