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

Side by Side Diff: chrome/browser/task_manager/background_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_background_resource_provider. h" 5 #include "chrome/browser/task_manager/background_resource_provider.h"
6 6
7 #include <string>
8
9 #include "base/basictypes.h"
10 #include "base/i18n/rtl.h" 7 #include "base/i18n/rtl.h"
11 #include "base/string16.h" 8 #include "base/string16.h"
12 #include "base/utf_string_conversions.h" 9 #include "base/utf_string_conversions.h"
13 #include "chrome/browser/background/background_contents_service.h" 10 #include "chrome/browser/background/background_contents_service.h"
14 #include "chrome/browser/background/background_contents_service_factory.h" 11 #include "chrome/browser/background/background_contents_service_factory.h"
15 #include "chrome/browser/browser_process.h" 12 #include "chrome/browser/browser_process.h"
16 #include "chrome/browser/extensions/extension_service.h" 13 #include "chrome/browser/extensions/extension_service.h"
17 #include "chrome/browser/profiles/profile.h" 14 #include "chrome/browser/profiles/profile.h"
18 #include "chrome/browser/profiles/profile_manager.h" 15 #include "chrome/browser/profiles/profile_manager.h"
19 #include "chrome/browser/tab_contents/background_contents.h" 16 #include "chrome/browser/tab_contents/background_contents.h"
17 #include "chrome/browser/task_manager/renderer_resource.h"
20 #include "chrome/common/chrome_notification_types.h" 18 #include "chrome/common/chrome_notification_types.h"
21 #include "chrome/common/extensions/extension.h" 19 #include "chrome/common/extensions/extension.h"
22 #include "content/public/browser/notification_service.h" 20 #include "content/public/browser/notification_service.h"
23 #include "content/public/browser/render_process_host.h" 21 #include "content/public/browser/render_process_host.h"
24 #include "content/public/browser/render_view_host.h" 22 #include "content/public/browser/render_view_host.h"
25 #include "content/public/browser/web_contents.h" 23 #include "content/public/browser/web_contents.h"
26 #include "grit/generated_resources.h" 24 #include "grit/generated_resources.h"
27 #include "grit/theme_resources.h" 25 #include "grit/theme_resources.h"
28 #include "ui/base/l10n/l10n_util.h" 26 #include "ui/base/l10n/l10n_util.h"
29 #include "ui/base/resource/resource_bundle.h" 27 #include "ui/base/resource/resource_bundle.h"
28 #include "ui/gfx/image/image_skia.h"
30 29
31 using content::RenderProcessHost; 30 using content::RenderProcessHost;
32 using content::RenderViewHost; 31 using content::RenderViewHost;
33 using content::WebContents; 32 using content::WebContents;
34 using extensions::Extension; 33 using extensions::Extension;
35 34
36 //////////////////////////////////////////////////////////////////////////////// 35 namespace task_manager {
37 // TaskManagerBackgroundContentsResource class
38 ////////////////////////////////////////////////////////////////////////////////
39 36
40 gfx::ImageSkia* TaskManagerBackgroundContentsResource::default_icon_ = NULL; 37 class BackgroundContentsResource : public RendererResource {
38 public:
39 BackgroundContentsResource(
40 BackgroundContents* background_contents,
41 const string16& application_name);
42 virtual ~BackgroundContentsResource();
43
44 // TaskManager::Resource methods:
45 virtual string16 GetTitle() const OVERRIDE;
46 virtual string16 GetProfileName() const OVERRIDE;
47 virtual gfx::ImageSkia GetIcon() const OVERRIDE;
48 virtual bool IsBackground() const OVERRIDE;
49
50 const string16& application_name() const { return application_name_; }
51 private:
52 BackgroundContents* background_contents_;
53
54 string16 application_name_;
55
56 // The icon painted for BackgroundContents.
57 // TODO(atwilson): Use the favicon when there's a way to get the favicon for
58 // BackgroundContents.
59 static gfx::ImageSkia* default_icon_;
60
61 DISALLOW_COPY_AND_ASSIGN(BackgroundContentsResource);
62 };
63
64 gfx::ImageSkia* BackgroundContentsResource::default_icon_ = NULL;
41 65
42 // TODO(atwilson): http://crbug.com/116893 66 // TODO(atwilson): http://crbug.com/116893
43 // HACK: if the process handle is invalid, we use the current process's handle. 67 // HACK: if the process handle is invalid, we use the current process's handle.
44 // This preserves old behavior but is incorrect, and should be fixed. 68 // This preserves old behavior but is incorrect, and should be fixed.
45 TaskManagerBackgroundContentsResource::TaskManagerBackgroundContentsResource( 69 BackgroundContentsResource::BackgroundContentsResource(
46 BackgroundContents* background_contents, 70 BackgroundContents* background_contents,
47 const string16& application_name) 71 const string16& application_name)
48 : TaskManagerRendererResource( 72 : RendererResource(
49 background_contents->web_contents()->GetRenderProcessHost()-> 73 background_contents->web_contents()->GetRenderProcessHost()->
50 GetHandle() ? 74 GetHandle() ?
51 background_contents->web_contents()->GetRenderProcessHost()-> 75 background_contents->web_contents()->GetRenderProcessHost()->
52 GetHandle() : 76 GetHandle() :
53 base::Process::Current().handle(), 77 base::Process::Current().handle(),
54 background_contents->web_contents()->GetRenderViewHost()), 78 background_contents->web_contents()->GetRenderViewHost()),
55 background_contents_(background_contents), 79 background_contents_(background_contents),
56 application_name_(application_name) { 80 application_name_(application_name) {
57 // Just use the same icon that other extension resources do. 81 // Just use the same icon that other extension resources do.
58 // TODO(atwilson): Use the favicon when that's available. 82 // TODO(atwilson): Use the favicon when that's available.
59 if (!default_icon_) { 83 if (!default_icon_) {
60 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); 84 ResourceBundle& rb = ResourceBundle::GetSharedInstance();
61 default_icon_ = rb.GetImageSkiaNamed(IDR_PLUGINS_FAVICON); 85 default_icon_ = rb.GetImageSkiaNamed(IDR_PLUGINS_FAVICON);
62 } 86 }
63 // Ensure that the string has the appropriate direction markers (see comment 87 // Ensure that the string has the appropriate direction markers (see comment
64 // in TaskManagerTabContentsResource::GetTitle()). 88 // in TaskManagerTabContentsResource::GetTitle()).
65 base::i18n::AdjustStringForLocaleDirection(&application_name_); 89 base::i18n::AdjustStringForLocaleDirection(&application_name_);
66 } 90 }
67 91
68 TaskManagerBackgroundContentsResource::~TaskManagerBackgroundContentsResource( 92 BackgroundContentsResource::~BackgroundContentsResource() {
69 ) {
70 } 93 }
71 94
72 string16 TaskManagerBackgroundContentsResource::GetTitle() const { 95 string16 BackgroundContentsResource::GetTitle() const {
73 string16 title = application_name_; 96 string16 title = application_name_;
74 97
75 if (title.empty()) { 98 if (title.empty()) {
76 // No title (can't locate the parent app for some reason) so just display 99 // No title (can't locate the parent app for some reason) so just display
77 // the URL (properly forced to be LTR). 100 // the URL (properly forced to be LTR).
78 title = base::i18n::GetDisplayStringInLTRDirectionality( 101 title = base::i18n::GetDisplayStringInLTRDirectionality(
79 UTF8ToUTF16(background_contents_->GetURL().spec())); 102 UTF8ToUTF16(background_contents_->GetURL().spec()));
80 } 103 }
81 return l10n_util::GetStringFUTF16(IDS_TASK_MANAGER_BACKGROUND_PREFIX, title); 104 return l10n_util::GetStringFUTF16(IDS_TASK_MANAGER_BACKGROUND_PREFIX, title);
82 } 105 }
83 106
84 string16 TaskManagerBackgroundContentsResource::GetProfileName() const { 107 string16 BackgroundContentsResource::GetProfileName() const {
85 return string16(); 108 return string16();
86 } 109 }
87 110
88 gfx::ImageSkia TaskManagerBackgroundContentsResource::GetIcon() const { 111 gfx::ImageSkia BackgroundContentsResource::GetIcon() const {
89 return *default_icon_; 112 return *default_icon_;
90 } 113 }
91 114
92 bool TaskManagerBackgroundContentsResource::IsBackground() const { 115 bool BackgroundContentsResource::IsBackground() const {
93 return true; 116 return true;
94 } 117 }
95 118
96 //////////////////////////////////////////////////////////////////////////////// 119 ////////////////////////////////////////////////////////////////////////////////
97 // TaskManagerBackgroundContentsResourceProvider class 120 // BackgroundContentsResourceProvider class
98 //////////////////////////////////////////////////////////////////////////////// 121 ////////////////////////////////////////////////////////////////////////////////
99 122
100 TaskManagerBackgroundContentsResourceProvider:: 123 BackgroundContentsResourceProvider::
101 TaskManagerBackgroundContentsResourceProvider(TaskManager* task_manager) 124 BackgroundContentsResourceProvider(TaskManager* task_manager)
102 : updating_(false), 125 : updating_(false),
103 task_manager_(task_manager) { 126 task_manager_(task_manager) {
104 } 127 }
105 128
106 TaskManagerBackgroundContentsResourceProvider:: 129 BackgroundContentsResourceProvider::~BackgroundContentsResourceProvider() {
107 ~TaskManagerBackgroundContentsResourceProvider() {
108 } 130 }
109 131
110 TaskManager::Resource* 132 TaskManager::Resource* BackgroundContentsResourceProvider::GetResource(
111 TaskManagerBackgroundContentsResourceProvider::GetResource(
112 int origin_pid, 133 int origin_pid,
113 int render_process_host_id, 134 int render_process_host_id,
114 int routing_id) { 135 int routing_id) {
115 // If an origin PID was specified, the request is from a plugin, not the 136 // If an origin PID was specified, the request is from a plugin, not the
116 // render view host process 137 // render view host process
117 if (origin_pid) 138 if (origin_pid)
118 return NULL; 139 return NULL;
119 140
120 for (Resources::iterator i = resources_.begin(); i != resources_.end(); i++) { 141 for (Resources::iterator i = resources_.begin(); i != resources_.end(); i++) {
121 WebContents* tab = i->first->web_contents(); 142 WebContents* tab = i->first->web_contents();
122 if (tab->GetRenderProcessHost()->GetID() == render_process_host_id 143 if (tab->GetRenderProcessHost()->GetID() == render_process_host_id
123 && tab->GetRenderViewHost()->GetRoutingID() == routing_id) { 144 && tab->GetRenderViewHost()->GetRoutingID() == routing_id) {
124 return i->second; 145 return i->second;
125 } 146 }
126 } 147 }
127 148
128 // Can happen if the page went away while a network request was being 149 // Can happen if the page went away while a network request was being
129 // performed. 150 // performed.
130 return NULL; 151 return NULL;
131 } 152 }
132 153
133 void TaskManagerBackgroundContentsResourceProvider::StartUpdating() { 154 void BackgroundContentsResourceProvider::StartUpdating() {
134 DCHECK(!updating_); 155 DCHECK(!updating_);
135 updating_ = true; 156 updating_ = true;
136 157
137 // Add all the existing BackgroundContents from every profile, including 158 // Add all the existing BackgroundContents from every profile, including
138 // incognito profiles. 159 // incognito profiles.
139 ProfileManager* profile_manager = g_browser_process->profile_manager(); 160 ProfileManager* profile_manager = g_browser_process->profile_manager();
140 std::vector<Profile*> profiles(profile_manager->GetLoadedProfiles()); 161 std::vector<Profile*> profiles(profile_manager->GetLoadedProfiles());
141 size_t num_default_profiles = profiles.size(); 162 size_t num_default_profiles = profiles.size();
142 for (size_t i = 0; i < num_default_profiles; ++i) { 163 for (size_t i = 0; i < num_default_profiles; ++i) {
143 if (profiles[i]->HasOffTheRecordProfile()) { 164 if (profiles[i]->HasOffTheRecordProfile()) {
(...skipping 24 matching lines...) Expand all
168 189
169 // Then we register for notifications to get new BackgroundContents. 190 // Then we register for notifications to get new BackgroundContents.
170 registrar_.Add(this, chrome::NOTIFICATION_BACKGROUND_CONTENTS_OPENED, 191 registrar_.Add(this, chrome::NOTIFICATION_BACKGROUND_CONTENTS_OPENED,
171 content::NotificationService::AllBrowserContextsAndSources()); 192 content::NotificationService::AllBrowserContextsAndSources());
172 registrar_.Add(this, chrome::NOTIFICATION_BACKGROUND_CONTENTS_NAVIGATED, 193 registrar_.Add(this, chrome::NOTIFICATION_BACKGROUND_CONTENTS_NAVIGATED,
173 content::NotificationService::AllBrowserContextsAndSources()); 194 content::NotificationService::AllBrowserContextsAndSources());
174 registrar_.Add(this, chrome::NOTIFICATION_BACKGROUND_CONTENTS_DELETED, 195 registrar_.Add(this, chrome::NOTIFICATION_BACKGROUND_CONTENTS_DELETED,
175 content::NotificationService::AllBrowserContextsAndSources()); 196 content::NotificationService::AllBrowserContextsAndSources());
176 } 197 }
177 198
178 void TaskManagerBackgroundContentsResourceProvider::StopUpdating() { 199 void BackgroundContentsResourceProvider::StopUpdating() {
179 DCHECK(updating_); 200 DCHECK(updating_);
180 updating_ = false; 201 updating_ = false;
181 202
182 // Unregister for notifications 203 // Unregister for notifications
183 registrar_.Remove( 204 registrar_.Remove(
184 this, chrome::NOTIFICATION_BACKGROUND_CONTENTS_OPENED, 205 this, chrome::NOTIFICATION_BACKGROUND_CONTENTS_OPENED,
185 content::NotificationService::AllBrowserContextsAndSources()); 206 content::NotificationService::AllBrowserContextsAndSources());
186 registrar_.Remove( 207 registrar_.Remove(
187 this, chrome::NOTIFICATION_BACKGROUND_CONTENTS_NAVIGATED, 208 this, chrome::NOTIFICATION_BACKGROUND_CONTENTS_NAVIGATED,
188 content::NotificationService::AllBrowserContextsAndSources()); 209 content::NotificationService::AllBrowserContextsAndSources());
189 registrar_.Remove( 210 registrar_.Remove(
190 this, chrome::NOTIFICATION_BACKGROUND_CONTENTS_DELETED, 211 this, chrome::NOTIFICATION_BACKGROUND_CONTENTS_DELETED,
191 content::NotificationService::AllBrowserContextsAndSources()); 212 content::NotificationService::AllBrowserContextsAndSources());
192 213
193 // Delete all the resources. 214 // Delete all the resources.
194 STLDeleteContainerPairSecondPointers(resources_.begin(), resources_.end()); 215 STLDeleteContainerPairSecondPointers(resources_.begin(), resources_.end());
195 216
196 resources_.clear(); 217 resources_.clear();
197 } 218 }
198 219
199 void TaskManagerBackgroundContentsResourceProvider::AddToTaskManager( 220 void BackgroundContentsResourceProvider::AddToTaskManager(
200 BackgroundContents* background_contents, 221 BackgroundContents* background_contents,
201 const string16& application_name) { 222 const string16& application_name) {
202 TaskManagerBackgroundContentsResource* resource = 223 BackgroundContentsResource* resource =
203 new TaskManagerBackgroundContentsResource(background_contents, 224 new BackgroundContentsResource(background_contents,
204 application_name); 225 application_name);
205 resources_[background_contents] = resource; 226 resources_[background_contents] = resource;
206 task_manager_->AddResource(resource); 227 task_manager_->AddResource(resource);
207 } 228 }
208 229
209 void TaskManagerBackgroundContentsResourceProvider::Add( 230 void BackgroundContentsResourceProvider::Add(
210 BackgroundContents* contents, const string16& application_name) { 231 BackgroundContents* contents, const string16& application_name) {
211 if (!updating_) 232 if (!updating_)
212 return; 233 return;
213 234
214 // TODO(atwilson): http://crbug.com/116893 235 // TODO(atwilson): http://crbug.com/116893
215 // We should check that the process handle is valid here, but it won't 236 // We should check that the process handle is valid here, but it won't
216 // be in the case of NOTIFICATION_BACKGROUND_CONTENTS_OPENED. 237 // be in the case of NOTIFICATION_BACKGROUND_CONTENTS_OPENED.
217 238
218 // Should never add the same BackgroundContents twice. 239 // Should never add the same BackgroundContents twice.
219 DCHECK(resources_.find(contents) == resources_.end()); 240 DCHECK(resources_.find(contents) == resources_.end());
220 AddToTaskManager(contents, application_name); 241 AddToTaskManager(contents, application_name);
221 } 242 }
222 243
223 void TaskManagerBackgroundContentsResourceProvider::Remove( 244 void BackgroundContentsResourceProvider::Remove(BackgroundContents* contents) {
224 BackgroundContents* contents) {
225 if (!updating_) 245 if (!updating_)
226 return; 246 return;
227 Resources::iterator iter = resources_.find(contents); 247 Resources::iterator iter = resources_.find(contents);
228 DCHECK(iter != resources_.end()); 248 DCHECK(iter != resources_.end());
229 249
230 // Remove the resource from the Task Manager. 250 // Remove the resource from the Task Manager.
231 TaskManagerBackgroundContentsResource* resource = iter->second; 251 BackgroundContentsResource* resource = iter->second;
232 task_manager_->RemoveResource(resource); 252 task_manager_->RemoveResource(resource);
233 // And from the provider. 253 // And from the provider.
234 resources_.erase(iter); 254 resources_.erase(iter);
235 // Finally, delete the resource. 255 // Finally, delete the resource.
236 delete resource; 256 delete resource;
237 } 257 }
238 258
239 void TaskManagerBackgroundContentsResourceProvider::Observe( 259 void BackgroundContentsResourceProvider::Observe(
240 int type, 260 int type,
241 const content::NotificationSource& source, 261 const content::NotificationSource& source,
242 const content::NotificationDetails& details) { 262 const content::NotificationDetails& details) {
243 switch (type) { 263 switch (type) {
244 case chrome::NOTIFICATION_BACKGROUND_CONTENTS_OPENED: { 264 case chrome::NOTIFICATION_BACKGROUND_CONTENTS_OPENED: {
245 // Get the name from the parent application. If no parent application is 265 // Get the name from the parent application. If no parent application is
246 // found, just pass an empty string - BackgroundContentsResource::GetTitle 266 // found, just pass an empty string - BackgroundContentsResource::GetTitle
247 // will display the URL instead in this case. This should never happen 267 // will display the URL instead in this case. This should never happen
248 // except in rare cases when an extension is being unloaded or chrome is 268 // except in rare cases when an extension is being unloaded or chrome is
249 // exiting while the task manager is displayed. 269 // exiting while the task manager is displayed.
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
283 Remove(content::Details<BackgroundContents>(details).ptr()); 303 Remove(content::Details<BackgroundContents>(details).ptr());
284 // Closing a BackgroundContents needs to force the display to refresh 304 // Closing a BackgroundContents needs to force the display to refresh
285 // (applications may now be considered "foreground" that weren't before). 305 // (applications may now be considered "foreground" that weren't before).
286 task_manager_->ModelChanged(); 306 task_manager_->ModelChanged();
287 break; 307 break;
288 default: 308 default:
289 NOTREACHED() << "Unexpected notification."; 309 NOTREACHED() << "Unexpected notification.";
290 return; 310 return;
291 } 311 }
292 } 312 }
313
314 } // namespace task_manager
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698