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