OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 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_resource_providers.h" |
| 6 |
| 7 #include "base/basictypes.h" |
| 8 #include "base/stl_util-inl.h" |
| 9 #include "chrome/browser/browser_process.h" |
| 10 #include "chrome/browser/notifications/balloon_collection.h" |
| 11 #include "chrome/browser/notifications/balloon_host.h" |
| 12 #include "chrome/browser/notifications/notification_ui_manager.h" |
| 13 #include "content/browser/renderer_host/render_process_host.h" |
| 14 #include "content/browser/renderer_host/render_view_host.h" |
| 15 #include "content/common/notification_service.h" |
| 16 #include "grit/generated_resources.h" |
| 17 #include "grit/theme_resources.h" |
| 18 #include "third_party/skia/include/core/SkBitmap.h" |
| 19 #include "ui/base/l10n/l10n_util.h" |
| 20 #include "ui/base/resource/resource_bundle.h" |
| 21 |
| 22 //////////////////////////////////////////////////////////////////////////////// |
| 23 // TaskManagerNotificationResource class |
| 24 //////////////////////////////////////////////////////////////////////////////// |
| 25 |
| 26 SkBitmap* TaskManagerNotificationResource::default_icon_ = NULL; |
| 27 |
| 28 TaskManagerNotificationResource::TaskManagerNotificationResource( |
| 29 BalloonHost* balloon_host) |
| 30 : balloon_host_(balloon_host) { |
| 31 if (!default_icon_) { |
| 32 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); |
| 33 default_icon_ = rb.GetBitmapNamed(IDR_PLUGIN); |
| 34 } |
| 35 process_handle_ = balloon_host_->render_view_host()->process()->GetHandle(); |
| 36 pid_ = base::GetProcId(process_handle_); |
| 37 title_ = l10n_util::GetStringFUTF16(IDS_TASK_MANAGER_NOTIFICATION_PREFIX, |
| 38 balloon_host_->GetSource()); |
| 39 } |
| 40 |
| 41 TaskManagerNotificationResource::~TaskManagerNotificationResource() { |
| 42 } |
| 43 |
| 44 string16 TaskManagerNotificationResource::GetTitle() const { |
| 45 return title_; |
| 46 } |
| 47 |
| 48 SkBitmap TaskManagerNotificationResource::GetIcon() const { |
| 49 return *default_icon_; |
| 50 } |
| 51 |
| 52 base::ProcessHandle TaskManagerNotificationResource::GetProcess() const { |
| 53 return process_handle_; |
| 54 } |
| 55 |
| 56 TaskManager::Resource::Type TaskManagerNotificationResource::GetType() const { |
| 57 return NOTIFICATION; |
| 58 } |
| 59 |
| 60 bool TaskManagerNotificationResource::SupportNetworkUsage() const { |
| 61 return false; |
| 62 } |
| 63 |
| 64 //////////////////////////////////////////////////////////////////////////////// |
| 65 // TaskManagerNotificationResourceProvider class |
| 66 //////////////////////////////////////////////////////////////////////////////// |
| 67 |
| 68 // static |
| 69 TaskManagerNotificationResourceProvider* |
| 70 TaskManagerNotificationResourceProvider::Create(TaskManager* task_manager) { |
| 71 return new TaskManagerNotificationResourceProvider(task_manager); |
| 72 } |
| 73 |
| 74 TaskManagerNotificationResourceProvider:: |
| 75 TaskManagerNotificationResourceProvider(TaskManager* task_manager) |
| 76 : task_manager_(task_manager), |
| 77 updating_(false) { |
| 78 } |
| 79 |
| 80 TaskManagerNotificationResourceProvider:: |
| 81 ~TaskManagerNotificationResourceProvider() { |
| 82 } |
| 83 |
| 84 TaskManager::Resource* TaskManagerNotificationResourceProvider::GetResource( |
| 85 int origin_pid, |
| 86 int render_process_host_id, |
| 87 int routing_id) { |
| 88 // TODO(johnnyg): provide resources by pid if necessary. |
| 89 return NULL; |
| 90 } |
| 91 |
| 92 void TaskManagerNotificationResourceProvider::StartUpdating() { |
| 93 DCHECK(!updating_); |
| 94 updating_ = true; |
| 95 |
| 96 // Add all the existing BalloonHosts. |
| 97 BalloonCollection* collection = |
| 98 g_browser_process->notification_ui_manager()->balloon_collection(); |
| 99 const BalloonCollection::Balloons& balloons = collection->GetActiveBalloons(); |
| 100 for (BalloonCollection::Balloons::const_iterator it = balloons.begin(); |
| 101 it != balloons.end(); ++it) { |
| 102 AddToTaskManager((*it)->view()->GetHost()); |
| 103 } |
| 104 |
| 105 // Register for notifications about extension process changes. |
| 106 registrar_.Add(this, NotificationType::NOTIFY_BALLOON_CONNECTED, |
| 107 NotificationService::AllSources()); |
| 108 registrar_.Add(this, NotificationType::NOTIFY_BALLOON_DISCONNECTED, |
| 109 NotificationService::AllSources()); |
| 110 } |
| 111 |
| 112 void TaskManagerNotificationResourceProvider::StopUpdating() { |
| 113 DCHECK(updating_); |
| 114 updating_ = false; |
| 115 |
| 116 // Unregister for notifications about extension process changes. |
| 117 registrar_.Remove(this, NotificationType::NOTIFY_BALLOON_CONNECTED, |
| 118 NotificationService::AllSources()); |
| 119 registrar_.Remove(this, NotificationType::NOTIFY_BALLOON_DISCONNECTED, |
| 120 NotificationService::AllSources()); |
| 121 |
| 122 // Delete all the resources. |
| 123 STLDeleteContainerPairSecondPointers(resources_.begin(), resources_.end()); |
| 124 resources_.clear(); |
| 125 } |
| 126 |
| 127 void TaskManagerNotificationResourceProvider::Observe( |
| 128 NotificationType type, |
| 129 const NotificationSource& source, |
| 130 const NotificationDetails& details) { |
| 131 switch (type.value) { |
| 132 case NotificationType::NOTIFY_BALLOON_CONNECTED: |
| 133 AddToTaskManager(Source<BalloonHost>(source).ptr()); |
| 134 break; |
| 135 case NotificationType::NOTIFY_BALLOON_DISCONNECTED: |
| 136 RemoveFromTaskManager(Source<BalloonHost>(source).ptr()); |
| 137 break; |
| 138 default: |
| 139 NOTREACHED() << "Unexpected notification."; |
| 140 return; |
| 141 } |
| 142 } |
| 143 |
| 144 void TaskManagerNotificationResourceProvider::AddToTaskManager( |
| 145 BalloonHost* balloon_host) { |
| 146 TaskManagerNotificationResource* resource = |
| 147 new TaskManagerNotificationResource(balloon_host); |
| 148 DCHECK(resources_.find(balloon_host) == resources_.end()); |
| 149 resources_[balloon_host] = resource; |
| 150 task_manager_->AddResource(resource); |
| 151 } |
| 152 |
| 153 void TaskManagerNotificationResourceProvider::RemoveFromTaskManager( |
| 154 BalloonHost* balloon_host) { |
| 155 if (!updating_) |
| 156 return; |
| 157 std::map<BalloonHost*, TaskManagerNotificationResource*>::iterator iter = |
| 158 resources_.find(balloon_host); |
| 159 if (iter == resources_.end()) |
| 160 return; |
| 161 |
| 162 // Remove the resource from the Task Manager. |
| 163 TaskManagerNotificationResource* resource = iter->second; |
| 164 task_manager_->RemoveResource(resource); |
| 165 |
| 166 // Remove it from the map. |
| 167 resources_.erase(iter); |
| 168 |
| 169 // Finally, delete the resource. |
| 170 delete resource; |
| 171 } |
OLD | NEW |