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