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