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