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

Side by Side Diff: chrome/browser/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: RendererResource 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
1 // Copyright 2013 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/task_manager/task_manager_guest_resource_provider.h" 5 #include "chrome/browser/task_manager/guest_resource_provider.h"
6 6
7 #include <string>
8
9 #include "base/basictypes.h"
10 #include "base/i18n/rtl.h"
11 #include "base/string16.h" 7 #include "base/string16.h"
12 #include "base/utf_string_conversions.h"
13 #include "chrome/browser/browser_process.h"
14 #include "chrome/browser/favicon/favicon_tab_helper.h" 8 #include "chrome/browser/favicon/favicon_tab_helper.h"
15 #include "chrome/browser/profiles/profile.h" 9 #include "chrome/browser/profiles/profile.h"
16 #include "chrome/browser/profiles/profile_info_cache.h" 10 #include "chrome/browser/task_manager/renderer_resource.h"
17 #include "chrome/browser/profiles/profile_manager.h" 11 #include "chrome/browser/task_manager/resource_util.h"
18 #include "chrome/common/chrome_notification_types.h" 12 #include "chrome/common/chrome_notification_types.h"
19 #include "content/public/browser/notification_service.h" 13 #include "content/public/browser/notification_service.h"
20 #include "content/public/browser/render_process_host.h" 14 #include "content/public/browser/render_process_host.h"
21 #include "content/public/browser/render_view_host.h" 15 #include "content/public/browser/render_view_host.h"
22 #include "content/public/browser/site_instance.h" 16 #include "content/public/browser/site_instance.h"
23 #include "content/public/browser/web_contents.h" 17 #include "content/public/browser/web_contents.h"
24 #include "grit/generated_resources.h" 18 #include "grit/generated_resources.h"
25 #include "ui/base/l10n/l10n_util.h" 19 #include "ui/base/l10n/l10n_util.h"
20 #include "ui/gfx/image/image_skia.h"
26 21
27 using content::RenderProcessHost; 22 using content::RenderProcessHost;
28 using content::RenderViewHost; 23 using content::RenderViewHost;
29 using content::RenderWidgetHost; 24 using content::RenderWidgetHost;
30 using content::WebContents; 25 using content::WebContents;
31 using extensions::Extension; 26 using extensions::Extension;
32 27
33 namespace { 28 namespace task_manager {
34 29
35 string16 GetProfileNameFromInfoCache(Profile* profile) { 30 class GuestResource : public RendererResource {
36 ProfileInfoCache& cache = 31 public:
37 g_browser_process->profile_manager()->GetProfileInfoCache(); 32 explicit GuestResource(content::RenderViewHost* render_view_host);
38 size_t index = cache.GetIndexOfProfileWithPath( 33 virtual ~GuestResource();
39 profile->GetOriginalProfile()->GetPath());
40 if (index == std::string::npos)
41 return string16();
42 else
43 return cache.GetNameOfProfileAtIndex(index);
44 }
45 34
46 string16 GetTitleFromWebContents(WebContents* web_contents) { 35 // TaskManager::Resource methods:
47 string16 title = web_contents->GetTitle(); 36 virtual Type GetType() const OVERRIDE;
48 if (title.empty()) { 37 virtual string16 GetTitle() const OVERRIDE;
49 GURL url = web_contents->GetURL(); 38 virtual string16 GetProfileName() const OVERRIDE;
50 title = UTF8ToUTF16(url.spec()); 39 virtual gfx::ImageSkia GetIcon() const OVERRIDE;
51 // Force URL to be LTR. 40 virtual content::WebContents* GetWebContents() const OVERRIDE;
52 title = base::i18n::GetDisplayStringInLTRDirectionality(title); 41 virtual const extensions::Extension* GetExtension() const OVERRIDE;
53 } else {
54 // Since the tab_title will be concatenated with
55 // IDS_TASK_MANAGER_TAB_PREFIX, we need to explicitly set the tab_title to
56 // be LTR format if there is no strong RTL charater in it. Otherwise, if
57 // IDS_TASK_MANAGER_TAB_PREFIX is an RTL word, the concatenated result
58 // might be wrong. For example, http://mail.yahoo.com, whose title is
59 // "Yahoo! Mail: The best web-based Email!", without setting it explicitly
60 // as LTR format, the concatenated result will be "!Yahoo! Mail: The best
61 // web-based Email :BAT", in which the capital letters "BAT" stands for
62 // the Hebrew word for "tab".
63 base::i18n::AdjustStringForLocaleDirection(&title);
64 }
65 return title;
66 }
67 42
68 } // namespace 43 private:
44 DISALLOW_COPY_AND_ASSIGN(GuestResource);
45 };
69 46
70 //////////////////////////////////////////////////////////////////////////////// 47 GuestResource::GuestResource(RenderViewHost* render_view_host)
71 // TaskManagerGuestResource class 48 : RendererResource(
72 ////////////////////////////////////////////////////////////////////////////////
73
74 TaskManagerGuestResource::TaskManagerGuestResource(
75 RenderViewHost* render_view_host)
76 : TaskManagerRendererResource(
77 render_view_host->GetSiteInstance()->GetProcess()->GetHandle(), 49 render_view_host->GetSiteInstance()->GetProcess()->GetHandle(),
78 render_view_host) { 50 render_view_host) {
79 } 51 }
80 52
81 TaskManagerGuestResource::~TaskManagerGuestResource() { 53 GuestResource::~GuestResource() {
82 } 54 }
83 55
84 TaskManager::Resource::Type TaskManagerGuestResource::GetType() const { 56 TaskManager::Resource::Type GuestResource::GetType() const {
85 return GUEST; 57 return GUEST;
86 } 58 }
87 59
88 string16 TaskManagerGuestResource::GetTitle() const { 60 string16 GuestResource::GetTitle() const {
89 WebContents* web_contents = GetWebContents(); 61 WebContents* web_contents = GetWebContents();
90 const int message_id = IDS_TASK_MANAGER_WEBVIEW_TAG_PREFIX; 62 const int message_id = IDS_TASK_MANAGER_WEBVIEW_TAG_PREFIX;
91 if (web_contents) { 63 if (web_contents) {
92 string16 title = GetTitleFromWebContents(web_contents); 64 string16 title = ResourceUtil::GetTitleFromWebContents(web_contents);
93 return l10n_util::GetStringFUTF16(message_id, title); 65 return l10n_util::GetStringFUTF16(message_id, title);
94 } 66 }
95 return l10n_util::GetStringFUTF16(message_id, string16()); 67 return l10n_util::GetStringFUTF16(message_id, string16());
96 } 68 }
97 69
98 string16 TaskManagerGuestResource::GetProfileName() const { 70 string16 GuestResource::GetProfileName() const {
99 WebContents* web_contents = GetWebContents(); 71 WebContents* web_contents = GetWebContents();
100 if (web_contents) { 72 if (web_contents) {
101 Profile* profile = Profile::FromBrowserContext( 73 Profile* profile = Profile::FromBrowserContext(
102 web_contents->GetBrowserContext()); 74 web_contents->GetBrowserContext());
103 return GetProfileNameFromInfoCache(profile); 75 return ResourceUtil::GetProfileNameFromInfoCache(profile);
104 } 76 }
105 return string16(); 77 return string16();
106 } 78 }
107 79
108 gfx::ImageSkia TaskManagerGuestResource::GetIcon() const { 80 gfx::ImageSkia GuestResource::GetIcon() const {
109 WebContents* web_contents = GetWebContents(); 81 WebContents* web_contents = GetWebContents();
110 if (web_contents && FaviconTabHelper::FromWebContents(web_contents)) { 82 if (web_contents && FaviconTabHelper::FromWebContents(web_contents)) {
111 return FaviconTabHelper::FromWebContents(web_contents)-> 83 return FaviconTabHelper::FromWebContents(web_contents)->
112 GetFavicon().AsImageSkia(); 84 GetFavicon().AsImageSkia();
113 } 85 }
114 return gfx::ImageSkia(); 86 return gfx::ImageSkia();
115 } 87 }
116 88
117 WebContents* TaskManagerGuestResource::GetWebContents() const { 89 WebContents* GuestResource::GetWebContents() const {
118 return WebContents::FromRenderViewHost(render_view_host()); 90 return WebContents::FromRenderViewHost(render_view_host());
119 } 91 }
120 92
121 const Extension* TaskManagerGuestResource::GetExtension() const { 93 const Extension* GuestResource::GetExtension() const {
122 return NULL; 94 return NULL;
123 } 95 }
124 96
125 //////////////////////////////////////////////////////////////////////////////// 97 GuestResourceProvider::GuestResourceProvider(TaskManager* task_manager)
126 // TaskManagerGuestContentsResourceProvider class
127 ////////////////////////////////////////////////////////////////////////////////
128
129 TaskManagerGuestResourceProvider::
130 TaskManagerGuestResourceProvider(TaskManager* task_manager)
131 : updating_(false), 98 : updating_(false),
132 task_manager_(task_manager) { 99 task_manager_(task_manager) {
133 } 100 }
134 101
135 TaskManagerGuestResourceProvider::~TaskManagerGuestResourceProvider() { 102 GuestResourceProvider::~GuestResourceProvider() {
136 } 103 }
137 104
138 TaskManager::Resource* TaskManagerGuestResourceProvider::GetResource( 105 TaskManager::Resource* GuestResourceProvider::GetResource(
139 int origin_pid, 106 int origin_pid,
140 int render_process_host_id, 107 int render_process_host_id,
141 int routing_id) { 108 int routing_id) {
142 // If an origin PID was specified then the request originated in a plugin 109 // If an origin PID was specified then the request originated in a plugin
143 // working on the WebContents's behalf, so ignore it. 110 // working on the WebContents's behalf, so ignore it.
144 if (origin_pid) 111 if (origin_pid)
145 return NULL; 112 return NULL;
146 113
147 for (GuestResourceMap::iterator i = resources_.begin(); 114 for (GuestResourceMap::iterator i = resources_.begin();
148 i != resources_.end(); ++i) { 115 i != resources_.end(); ++i) {
149 WebContents* contents = WebContents::FromRenderViewHost(i->first); 116 WebContents* contents = WebContents::FromRenderViewHost(i->first);
150 if (contents && 117 if (contents &&
151 contents->GetRenderProcessHost()->GetID() == render_process_host_id && 118 contents->GetRenderProcessHost()->GetID() == render_process_host_id &&
152 contents->GetRenderViewHost()->GetRoutingID() == routing_id) { 119 contents->GetRenderViewHost()->GetRoutingID() == routing_id) {
153 return i->second; 120 return i->second;
154 } 121 }
155 } 122 }
156 123
157 return NULL; 124 return NULL;
158 } 125 }
159 126
160 void TaskManagerGuestResourceProvider::StartUpdating() { 127 void GuestResourceProvider::StartUpdating() {
161 DCHECK(!updating_); 128 DCHECK(!updating_);
162 updating_ = true; 129 updating_ = true;
163 130
164 // Add all the existing guest WebContents. 131 // Add all the existing guest WebContents.
165 for (RenderProcessHost::iterator i( 132 for (RenderProcessHost::iterator i(
166 RenderProcessHost::AllHostsIterator()); 133 RenderProcessHost::AllHostsIterator());
167 !i.IsAtEnd(); i.Advance()) { 134 !i.IsAtEnd(); i.Advance()) {
168 RenderProcessHost::RenderWidgetHostsIterator iter = 135 RenderProcessHost::RenderWidgetHostsIterator iter =
169 i.GetCurrentValue()->GetRenderWidgetHostsIterator(); 136 i.GetCurrentValue()->GetRenderWidgetHostsIterator();
170 for (; !iter.IsAtEnd(); iter.Advance()) { 137 for (; !iter.IsAtEnd(); iter.Advance()) {
171 const RenderWidgetHost* widget = iter.GetCurrentValue(); 138 const RenderWidgetHost* widget = iter.GetCurrentValue();
172 if (widget->IsRenderView()) { 139 if (widget->IsRenderView()) {
173 RenderViewHost* rvh = 140 RenderViewHost* rvh =
174 RenderViewHost::From(const_cast<RenderWidgetHost*>(widget)); 141 RenderViewHost::From(const_cast<RenderWidgetHost*>(widget));
175 if (rvh->IsSubframe()) 142 if (rvh->IsSubframe())
176 Add(rvh); 143 Add(rvh);
177 } 144 }
178 } 145 }
179 } 146 }
180 147
181 // Then we register for notifications to get new guests. 148 // Then we register for notifications to get new guests.
182 registrar_.Add(this, content::NOTIFICATION_WEB_CONTENTS_CONNECTED, 149 registrar_.Add(this, content::NOTIFICATION_WEB_CONTENTS_CONNECTED,
183 content::NotificationService::AllBrowserContextsAndSources()); 150 content::NotificationService::AllBrowserContextsAndSources());
184 registrar_.Add(this, content::NOTIFICATION_WEB_CONTENTS_DISCONNECTED, 151 registrar_.Add(this, content::NOTIFICATION_WEB_CONTENTS_DISCONNECTED,
185 content::NotificationService::AllBrowserContextsAndSources()); 152 content::NotificationService::AllBrowserContextsAndSources());
186 } 153 }
187 154
188 void TaskManagerGuestResourceProvider::StopUpdating() { 155 void GuestResourceProvider::StopUpdating() {
189 DCHECK(updating_); 156 DCHECK(updating_);
190 updating_ = false; 157 updating_ = false;
191 158
192 // Unregister for notifications. 159 // Unregister for notifications.
193 registrar_.Remove(this, content::NOTIFICATION_WEB_CONTENTS_CONNECTED, 160 registrar_.Remove(this, content::NOTIFICATION_WEB_CONTENTS_CONNECTED,
194 content::NotificationService::AllBrowserContextsAndSources()); 161 content::NotificationService::AllBrowserContextsAndSources());
195 registrar_.Remove(this, content::NOTIFICATION_WEB_CONTENTS_DISCONNECTED, 162 registrar_.Remove(this, content::NOTIFICATION_WEB_CONTENTS_DISCONNECTED,
196 content::NotificationService::AllBrowserContextsAndSources()); 163 content::NotificationService::AllBrowserContextsAndSources());
197 164
198 // Delete all the resources. 165 // Delete all the resources.
199 STLDeleteContainerPairSecondPointers(resources_.begin(), resources_.end()); 166 STLDeleteContainerPairSecondPointers(resources_.begin(), resources_.end());
200 167
201 resources_.clear(); 168 resources_.clear();
202 } 169 }
203 170
204 void TaskManagerGuestResourceProvider::Add( 171 void GuestResourceProvider::Add(RenderViewHost* render_view_host) {
205 RenderViewHost* render_view_host) { 172 GuestResource* resource = new GuestResource(render_view_host);
206 TaskManagerGuestResource* resource =
207 new TaskManagerGuestResource(render_view_host);
208 resources_[render_view_host] = resource; 173 resources_[render_view_host] = resource;
209 task_manager_->AddResource(resource); 174 task_manager_->AddResource(resource);
210 } 175 }
211 176
212 void TaskManagerGuestResourceProvider::Remove( 177 void GuestResourceProvider::Remove(RenderViewHost* render_view_host) {
213 RenderViewHost* render_view_host) {
214 if (!updating_) 178 if (!updating_)
215 return; 179 return;
216 180
217 GuestResourceMap::iterator iter = resources_.find(render_view_host); 181 GuestResourceMap::iterator iter = resources_.find(render_view_host);
218 if (iter == resources_.end()) 182 if (iter == resources_.end())
219 return; 183 return;
220 184
221 TaskManagerGuestResource* resource = iter->second; 185 GuestResource* resource = iter->second;
222 task_manager_->RemoveResource(resource); 186 task_manager_->RemoveResource(resource);
223 resources_.erase(iter); 187 resources_.erase(iter);
224 delete resource; 188 delete resource;
225 } 189 }
226 190
227 void TaskManagerGuestResourceProvider::Observe(int type, 191 void GuestResourceProvider::Observe(int type,
228 const content::NotificationSource& source, 192 const content::NotificationSource& source,
229 const content::NotificationDetails& details) { 193 const content::NotificationDetails& details) {
230 WebContents* web_contents = content::Source<WebContents>(source).ptr(); 194 WebContents* web_contents = content::Source<WebContents>(source).ptr();
231 if (!web_contents || !web_contents->GetRenderViewHost()->IsSubframe()) 195 if (!web_contents || !web_contents->GetRenderViewHost()->IsSubframe())
232 return; 196 return;
233 197
234 switch (type) { 198 switch (type) {
235 case content::NOTIFICATION_WEB_CONTENTS_CONNECTED: 199 case content::NOTIFICATION_WEB_CONTENTS_CONNECTED:
236 Add(web_contents->GetRenderViewHost()); 200 Add(web_contents->GetRenderViewHost());
237 break; 201 break;
238 case content::NOTIFICATION_WEB_CONTENTS_DISCONNECTED: 202 case content::NOTIFICATION_WEB_CONTENTS_DISCONNECTED:
239 Remove(web_contents->GetRenderViewHost()); 203 Remove(web_contents->GetRenderViewHost());
240 break; 204 break;
241 default: 205 default:
242 NOTREACHED() << "Unexpected notification."; 206 NOTREACHED() << "Unexpected notification.";
243 } 207 }
244 } 208 }
209
210 } // namespace task_manager
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698