OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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_notification_resource_provide
r.h" | 5 #include "chrome/browser/task_manager/notification_resource_provider.h" |
6 | 6 |
7 #include "base/string16.h" | 7 #include "base/string16.h" |
8 #include "chrome/browser/browser_process.h" | 8 #include "chrome/browser/browser_process.h" |
9 #include "chrome/browser/devtools/devtools_window.h" | 9 #include "chrome/browser/devtools/devtools_window.h" |
10 #include "chrome/browser/notifications/balloon_host.h" | 10 #include "chrome/browser/notifications/balloon_host.h" |
11 #include "chrome/browser/notifications/balloon_notification_ui_manager.h" | 11 #include "chrome/browser/notifications/balloon_notification_ui_manager.h" |
12 #include "chrome/common/chrome_notification_types.h" | 12 #include "chrome/common/chrome_notification_types.h" |
13 #include "content/public/browser/notification_service.h" | 13 #include "content/public/browser/notification_service.h" |
14 #include "content/public/browser/render_process_host.h" | 14 #include "content/public/browser/render_process_host.h" |
15 #include "content/public/browser/web_contents.h" | 15 #include "content/public/browser/web_contents.h" |
16 #include "grit/generated_resources.h" | 16 #include "grit/generated_resources.h" |
17 #include "grit/theme_resources.h" | 17 #include "grit/theme_resources.h" |
18 #include "ui/base/l10n/l10n_util.h" | 18 #include "ui/base/l10n/l10n_util.h" |
19 #include "ui/base/resource/resource_bundle.h" | 19 #include "ui/base/resource/resource_bundle.h" |
20 #include "ui/gfx/image/image_skia.h" | 20 #include "ui/gfx/image/image_skia.h" |
21 | 21 |
22 class TaskManagerNotificationResource : public TaskManager::Resource { | 22 namespace task_manager { |
| 23 |
| 24 class NotificationResource : public TaskManager::Resource { |
23 public: | 25 public: |
24 explicit TaskManagerNotificationResource(BalloonHost* balloon_host); | 26 explicit NotificationResource(BalloonHost* balloon_host); |
25 virtual ~TaskManagerNotificationResource(); | 27 virtual ~NotificationResource(); |
26 | 28 |
27 // TaskManager::Resource interface | 29 // TaskManager::Resource interface |
28 virtual string16 GetTitle() const OVERRIDE; | 30 virtual string16 GetTitle() const OVERRIDE; |
29 virtual string16 GetProfileName() const OVERRIDE; | 31 virtual string16 GetProfileName() const OVERRIDE; |
30 virtual gfx::ImageSkia GetIcon() const OVERRIDE; | 32 virtual gfx::ImageSkia GetIcon() const OVERRIDE; |
31 virtual base::ProcessHandle GetProcess() const OVERRIDE; | 33 virtual base::ProcessHandle GetProcess() const OVERRIDE; |
32 virtual int GetUniqueChildProcessId() const OVERRIDE; | 34 virtual int GetUniqueChildProcessId() const OVERRIDE; |
33 virtual Type GetType() const OVERRIDE; | 35 virtual Type GetType() const OVERRIDE; |
34 virtual bool CanInspect() const OVERRIDE; | 36 virtual bool CanInspect() const OVERRIDE; |
35 virtual void Inspect() const OVERRIDE; | 37 virtual void Inspect() const OVERRIDE; |
36 virtual bool SupportNetworkUsage() const OVERRIDE; | 38 virtual bool SupportNetworkUsage() const OVERRIDE; |
37 virtual void SetSupportNetworkUsage() OVERRIDE { } | 39 virtual void SetSupportNetworkUsage() OVERRIDE { } |
38 | 40 |
39 private: | 41 private: |
40 // The icon painted for notifications. . | 42 // The icon painted for notifications. . |
41 static gfx::ImageSkia* default_icon_; | 43 static gfx::ImageSkia* default_icon_; |
42 | 44 |
43 // Non-owned pointer to the balloon host. | 45 // Non-owned pointer to the balloon host. |
44 BalloonHost* balloon_host_; | 46 BalloonHost* balloon_host_; |
45 | 47 |
46 // Cached data about the balloon host. | 48 // Cached data about the balloon host. |
47 base::ProcessHandle process_handle_; | 49 base::ProcessHandle process_handle_; |
48 int pid_; | 50 int pid_; |
49 int unique_process_id_; | 51 int unique_process_id_; |
50 string16 title_; | 52 string16 title_; |
51 | 53 |
52 DISALLOW_COPY_AND_ASSIGN(TaskManagerNotificationResource); | 54 DISALLOW_COPY_AND_ASSIGN(NotificationResource); |
53 }; | 55 }; |
54 | 56 |
55 gfx::ImageSkia* TaskManagerNotificationResource::default_icon_ = NULL; | 57 gfx::ImageSkia* NotificationResource::default_icon_ = NULL; |
56 | 58 |
57 TaskManagerNotificationResource::TaskManagerNotificationResource( | 59 NotificationResource::NotificationResource(BalloonHost* balloon_host) |
58 BalloonHost* balloon_host) | |
59 : balloon_host_(balloon_host) { | 60 : balloon_host_(balloon_host) { |
60 if (!default_icon_) { | 61 if (!default_icon_) { |
61 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); | 62 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); |
62 default_icon_ = rb.GetImageSkiaNamed(IDR_PLUGINS_FAVICON); | 63 default_icon_ = rb.GetImageSkiaNamed(IDR_PLUGINS_FAVICON); |
63 } | 64 } |
64 process_handle_ = | 65 process_handle_ = |
65 balloon_host_->web_contents()->GetRenderProcessHost()->GetHandle(); | 66 balloon_host_->web_contents()->GetRenderProcessHost()->GetHandle(); |
66 unique_process_id_ = | 67 unique_process_id_ = |
67 balloon_host_->web_contents()->GetRenderProcessHost()->GetID(); | 68 balloon_host_->web_contents()->GetRenderProcessHost()->GetID(); |
68 pid_ = base::GetProcId(process_handle_); | 69 pid_ = base::GetProcId(process_handle_); |
69 title_ = l10n_util::GetStringFUTF16(IDS_TASK_MANAGER_NOTIFICATION_PREFIX, | 70 title_ = l10n_util::GetStringFUTF16(IDS_TASK_MANAGER_NOTIFICATION_PREFIX, |
70 balloon_host_->GetSource()); | 71 balloon_host_->GetSource()); |
71 } | 72 } |
72 | 73 |
73 TaskManagerNotificationResource::~TaskManagerNotificationResource() { | 74 NotificationResource::~NotificationResource() { |
74 } | 75 } |
75 | 76 |
76 string16 TaskManagerNotificationResource::GetTitle() const { | 77 string16 NotificationResource::GetTitle() const { |
77 return title_; | 78 return title_; |
78 } | 79 } |
79 | 80 |
80 string16 TaskManagerNotificationResource::GetProfileName() const { | 81 string16 NotificationResource::GetProfileName() const { |
81 return string16(); | 82 return string16(); |
82 } | 83 } |
83 | 84 |
84 gfx::ImageSkia TaskManagerNotificationResource::GetIcon() const { | 85 gfx::ImageSkia NotificationResource::GetIcon() const { |
85 return *default_icon_; | 86 return *default_icon_; |
86 } | 87 } |
87 | 88 |
88 base::ProcessHandle TaskManagerNotificationResource::GetProcess() const { | 89 base::ProcessHandle NotificationResource::GetProcess() const { |
89 return process_handle_; | 90 return process_handle_; |
90 } | 91 } |
91 | 92 |
92 int TaskManagerNotificationResource::GetUniqueChildProcessId() const { | 93 int NotificationResource::GetUniqueChildProcessId() const { |
93 return unique_process_id_; | 94 return unique_process_id_; |
94 } | 95 } |
95 | 96 |
96 TaskManager::Resource::Type TaskManagerNotificationResource::GetType() const { | 97 TaskManager::Resource::Type NotificationResource::GetType() const { |
97 return NOTIFICATION; | 98 return NOTIFICATION; |
98 } | 99 } |
99 | 100 |
100 bool TaskManagerNotificationResource::CanInspect() const { | 101 bool NotificationResource::CanInspect() const { |
101 return true; | 102 return true; |
102 } | 103 } |
103 | 104 |
104 void TaskManagerNotificationResource::Inspect() const { | 105 void NotificationResource::Inspect() const { |
105 DevToolsWindow::OpenDevToolsWindow( | 106 DevToolsWindow::OpenDevToolsWindow( |
106 balloon_host_->web_contents()->GetRenderViewHost()); | 107 balloon_host_->web_contents()->GetRenderViewHost()); |
107 } | 108 } |
108 | 109 |
109 bool TaskManagerNotificationResource::SupportNetworkUsage() const { | 110 bool NotificationResource::SupportNetworkUsage() const { |
110 return false; | 111 return false; |
111 } | 112 } |
112 | 113 |
113 //////////////////////////////////////////////////////////////////////////////// | 114 //////////////////////////////////////////////////////////////////////////////// |
114 // TaskManagerNotificationResourceProvider class | 115 // NotificationResourceProvider class |
115 //////////////////////////////////////////////////////////////////////////////// | 116 //////////////////////////////////////////////////////////////////////////////// |
116 | 117 |
117 // static | 118 // static |
118 TaskManagerNotificationResourceProvider* | 119 NotificationResourceProvider* |
119 TaskManagerNotificationResourceProvider::Create(TaskManager* task_manager) { | 120 NotificationResourceProvider::Create(TaskManager* task_manager) { |
120 return new TaskManagerNotificationResourceProvider(task_manager); | 121 return new NotificationResourceProvider(task_manager); |
121 } | 122 } |
122 | 123 |
123 TaskManagerNotificationResourceProvider:: | 124 NotificationResourceProvider:: |
124 TaskManagerNotificationResourceProvider(TaskManager* task_manager) | 125 NotificationResourceProvider(TaskManager* task_manager) |
125 : task_manager_(task_manager), | 126 : task_manager_(task_manager), |
126 updating_(false) { | 127 updating_(false) { |
127 } | 128 } |
128 | 129 |
129 TaskManagerNotificationResourceProvider:: | 130 NotificationResourceProvider::~NotificationResourceProvider() { |
130 ~TaskManagerNotificationResourceProvider() { | |
131 } | 131 } |
132 | 132 |
133 TaskManager::Resource* TaskManagerNotificationResourceProvider::GetResource( | 133 TaskManager::Resource* NotificationResourceProvider::GetResource( |
134 int origin_pid, | 134 int origin_pid, |
135 int render_process_host_id, | 135 int render_process_host_id, |
136 int routing_id) { | 136 int routing_id) { |
137 // TODO(johnnyg): provide resources by pid if necessary. | 137 // TODO(johnnyg): provide resources by pid if necessary. |
138 return NULL; | 138 return NULL; |
139 } | 139 } |
140 | 140 |
141 void TaskManagerNotificationResourceProvider::StartUpdating() { | 141 void NotificationResourceProvider::StartUpdating() { |
142 // MessageCenter does not use Balloons. | 142 // MessageCenter does not use Balloons. |
143 if (NotificationUIManager::DelegatesToMessageCenter()) | 143 if (NotificationUIManager::DelegatesToMessageCenter()) |
144 return; | 144 return; |
145 | 145 |
146 DCHECK(!updating_); | 146 DCHECK(!updating_); |
147 updating_ = true; | 147 updating_ = true; |
148 | 148 |
149 // Add all the existing BalloonHosts. | 149 // Add all the existing BalloonHosts. |
150 BalloonNotificationUIManager* balloon_manager = | 150 BalloonNotificationUIManager* balloon_manager = |
151 static_cast<BalloonNotificationUIManager*>( | 151 static_cast<BalloonNotificationUIManager*>( |
152 g_browser_process->notification_ui_manager()); | 152 g_browser_process->notification_ui_manager()); |
153 BalloonCollection* collection = balloon_manager->balloon_collection(); | 153 BalloonCollection* collection = balloon_manager->balloon_collection(); |
154 const BalloonCollection::Balloons& balloons = | 154 const BalloonCollection::Balloons& balloons = |
155 collection->GetActiveBalloons(); | 155 collection->GetActiveBalloons(); |
156 for (BalloonCollection::Balloons::const_iterator it = balloons.begin(); | 156 for (BalloonCollection::Balloons::const_iterator it = balloons.begin(); |
157 it != balloons.end(); ++it) { | 157 it != balloons.end(); ++it) { |
158 BalloonHost* balloon_host = (*it)->balloon_view()->GetHost(); | 158 BalloonHost* balloon_host = (*it)->balloon_view()->GetHost(); |
159 if (balloon_host) | 159 if (balloon_host) |
160 AddToTaskManager(balloon_host); | 160 AddToTaskManager(balloon_host); |
161 } | 161 } |
162 // Register for notifications about extension process changes. | 162 // Register for notifications about extension process changes. |
163 registrar_.Add(this, chrome::NOTIFICATION_NOTIFY_BALLOON_CONNECTED, | 163 registrar_.Add(this, chrome::NOTIFICATION_NOTIFY_BALLOON_CONNECTED, |
164 content::NotificationService::AllSources()); | 164 content::NotificationService::AllSources()); |
165 registrar_.Add(this, chrome::NOTIFICATION_NOTIFY_BALLOON_DISCONNECTED, | 165 registrar_.Add(this, chrome::NOTIFICATION_NOTIFY_BALLOON_DISCONNECTED, |
166 content::NotificationService::AllSources()); | 166 content::NotificationService::AllSources()); |
167 } | 167 } |
168 | 168 |
169 void TaskManagerNotificationResourceProvider::StopUpdating() { | 169 void NotificationResourceProvider::StopUpdating() { |
170 // MessageCenter does not use Balloons. | 170 // MessageCenter does not use Balloons. |
171 if (NotificationUIManager::DelegatesToMessageCenter()) | 171 if (NotificationUIManager::DelegatesToMessageCenter()) |
172 return; | 172 return; |
173 | 173 |
174 DCHECK(updating_); | 174 DCHECK(updating_); |
175 updating_ = false; | 175 updating_ = false; |
176 | 176 |
177 // Unregister for notifications about extension process changes. | 177 // Unregister for notifications about extension process changes. |
178 registrar_.Remove(this, chrome::NOTIFICATION_NOTIFY_BALLOON_CONNECTED, | 178 registrar_.Remove(this, chrome::NOTIFICATION_NOTIFY_BALLOON_CONNECTED, |
179 content::NotificationService::AllSources()); | 179 content::NotificationService::AllSources()); |
180 registrar_.Remove(this, chrome::NOTIFICATION_NOTIFY_BALLOON_DISCONNECTED, | 180 registrar_.Remove(this, chrome::NOTIFICATION_NOTIFY_BALLOON_DISCONNECTED, |
181 content::NotificationService::AllSources()); | 181 content::NotificationService::AllSources()); |
182 | 182 |
183 // Delete all the resources. | 183 // Delete all the resources. |
184 STLDeleteContainerPairSecondPointers(resources_.begin(), resources_.end()); | 184 STLDeleteContainerPairSecondPointers(resources_.begin(), resources_.end()); |
185 resources_.clear(); | 185 resources_.clear(); |
186 } | 186 } |
187 | 187 |
188 void TaskManagerNotificationResourceProvider::Observe( | 188 void NotificationResourceProvider::Observe( |
189 int type, | 189 int type, |
190 const content::NotificationSource& source, | 190 const content::NotificationSource& source, |
191 const content::NotificationDetails& details) { | 191 const content::NotificationDetails& details) { |
192 switch (type) { | 192 switch (type) { |
193 case chrome::NOTIFICATION_NOTIFY_BALLOON_CONNECTED: | 193 case chrome::NOTIFICATION_NOTIFY_BALLOON_CONNECTED: |
194 AddToTaskManager(content::Source<BalloonHost>(source).ptr()); | 194 AddToTaskManager(content::Source<BalloonHost>(source).ptr()); |
195 break; | 195 break; |
196 case chrome::NOTIFICATION_NOTIFY_BALLOON_DISCONNECTED: | 196 case chrome::NOTIFICATION_NOTIFY_BALLOON_DISCONNECTED: |
197 RemoveFromTaskManager(content::Source<BalloonHost>(source).ptr()); | 197 RemoveFromTaskManager(content::Source<BalloonHost>(source).ptr()); |
198 break; | 198 break; |
199 default: | 199 default: |
200 NOTREACHED() << "Unexpected notification."; | 200 NOTREACHED() << "Unexpected notification."; |
201 return; | 201 return; |
202 } | 202 } |
203 } | 203 } |
204 | 204 |
205 void TaskManagerNotificationResourceProvider::AddToTaskManager( | 205 void NotificationResourceProvider::AddToTaskManager( |
206 BalloonHost* balloon_host) { | 206 BalloonHost* balloon_host) { |
207 TaskManagerNotificationResource* resource = | 207 NotificationResource* resource = new NotificationResource(balloon_host); |
208 new TaskManagerNotificationResource(balloon_host); | |
209 DCHECK(resources_.find(balloon_host) == resources_.end()); | 208 DCHECK(resources_.find(balloon_host) == resources_.end()); |
210 resources_[balloon_host] = resource; | 209 resources_[balloon_host] = resource; |
211 task_manager_->AddResource(resource); | 210 task_manager_->AddResource(resource); |
212 } | 211 } |
213 | 212 |
214 void TaskManagerNotificationResourceProvider::RemoveFromTaskManager( | 213 void NotificationResourceProvider::RemoveFromTaskManager( |
215 BalloonHost* balloon_host) { | 214 BalloonHost* balloon_host) { |
216 if (!updating_) | 215 if (!updating_) |
217 return; | 216 return; |
218 std::map<BalloonHost*, TaskManagerNotificationResource*>::iterator iter = | 217 std::map<BalloonHost*, NotificationResource*>::iterator iter = |
219 resources_.find(balloon_host); | 218 resources_.find(balloon_host); |
220 if (iter == resources_.end()) | 219 if (iter == resources_.end()) |
221 return; | 220 return; |
222 | 221 |
223 // Remove the resource from the Task Manager. | 222 // Remove the resource from the Task Manager. |
224 TaskManagerNotificationResource* resource = iter->second; | 223 NotificationResource* resource = iter->second; |
225 task_manager_->RemoveResource(resource); | 224 task_manager_->RemoveResource(resource); |
226 | 225 |
227 // Remove it from the map. | 226 // Remove it from the map. |
228 resources_.erase(iter); | 227 resources_.erase(iter); |
229 | 228 |
230 // Finally, delete the resource. | 229 // Finally, delete the resource. |
231 delete resource; | 230 delete resource; |
232 } | 231 } |
| 232 |
| 233 } // namespace task_manager |
OLD | NEW |