| 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_worker_resource_provider.h" | |
| 6 | |
| 7 #include <vector> | |
| 8 | |
| 9 #include "base/basictypes.h" | |
| 10 #include "base/string16.h" | |
| 11 #include "base/utf_string_conversions.h" | |
| 12 #include "chrome/browser/devtools/devtools_window.h" | |
| 13 #include "chrome/browser/profiles/profile_manager.h" | |
| 14 #include "chrome/common/chrome_process_type.h" | |
| 15 #include "content/public/browser/browser_thread.h" | |
| 16 #include "content/public/browser/child_process_data.h" | |
| 17 #include "content/public/browser/devtools_agent_host.h" | |
| 18 #include "content/public/browser/worker_service.h" | |
| 19 #include "grit/generated_resources.h" | |
| 20 #include "grit/theme_resources.h" | |
| 21 #include "ui/base/l10n/l10n_util.h" | |
| 22 #include "ui/base/resource/resource_bundle.h" | |
| 23 #include "ui/gfx/image/image_skia.h" | |
| 24 | |
| 25 using content::BrowserThread; | |
| 26 using content::DevToolsAgentHost; | |
| 27 using content::WorkerService; | |
| 28 | |
| 29 //////////////////////////////////////////////////////////////////////////////// | |
| 30 // TaskManagerSharedWorkerResource class | |
| 31 //////////////////////////////////////////////////////////////////////////////// | |
| 32 | |
| 33 // Objects of this class are created on the IO thread and then passed to the UI | |
| 34 // thread where they are passed to the task manager. All methods must be called | |
| 35 // only on the UI thread. Destructor may be called on any thread. | |
| 36 class TaskManagerSharedWorkerResource : public TaskManager::Resource { | |
| 37 public: | |
| 38 TaskManagerSharedWorkerResource(const GURL& url, | |
| 39 const string16& name, | |
| 40 int process_id, | |
| 41 int routing_id, | |
| 42 base::ProcessHandle process_handle); | |
| 43 virtual ~TaskManagerSharedWorkerResource(); | |
| 44 | |
| 45 bool Matches(int process_id, int routing_id) const; | |
| 46 | |
| 47 void UpdateProcessHandle(base::ProcessHandle handle); | |
| 48 base::ProcessHandle handle() const { return handle_; } | |
| 49 int process_id() const { return process_id_; } | |
| 50 | |
| 51 private: | |
| 52 // TaskManager::Resource methods: | |
| 53 virtual string16 GetTitle() const OVERRIDE; | |
| 54 virtual string16 GetProfileName() const OVERRIDE; | |
| 55 virtual gfx::ImageSkia GetIcon() const OVERRIDE; | |
| 56 virtual base::ProcessHandle GetProcess() const OVERRIDE; | |
| 57 virtual int GetUniqueChildProcessId() const OVERRIDE; | |
| 58 virtual Type GetType() const OVERRIDE; | |
| 59 virtual bool CanInspect() const OVERRIDE; | |
| 60 virtual void Inspect() const OVERRIDE; | |
| 61 | |
| 62 virtual bool SupportNetworkUsage() const OVERRIDE; | |
| 63 virtual void SetSupportNetworkUsage() OVERRIDE; | |
| 64 | |
| 65 int process_id_; | |
| 66 int routing_id_; | |
| 67 string16 title_; | |
| 68 base::ProcessHandle handle_; | |
| 69 | |
| 70 static gfx::ImageSkia* default_icon_; | |
| 71 | |
| 72 DISALLOW_COPY_AND_ASSIGN(TaskManagerSharedWorkerResource); | |
| 73 }; | |
| 74 | |
| 75 gfx::ImageSkia* TaskManagerSharedWorkerResource::default_icon_ = NULL; | |
| 76 | |
| 77 TaskManagerSharedWorkerResource::TaskManagerSharedWorkerResource( | |
| 78 const GURL& url, | |
| 79 const string16& name, | |
| 80 int process_id, | |
| 81 int routing_id, | |
| 82 base::ProcessHandle process_handle) | |
| 83 : process_id_(process_id), | |
| 84 routing_id_(routing_id), | |
| 85 handle_(process_handle) { | |
| 86 title_ = UTF8ToUTF16(url.spec()); | |
| 87 if (!name.empty()) | |
| 88 title_ += ASCIIToUTF16(" (") + name + ASCIIToUTF16(")"); | |
| 89 } | |
| 90 | |
| 91 TaskManagerSharedWorkerResource::~TaskManagerSharedWorkerResource() { | |
| 92 } | |
| 93 | |
| 94 bool TaskManagerSharedWorkerResource::Matches(int process_id, | |
| 95 int routing_id) const { | |
| 96 return process_id_ == process_id && routing_id_ == routing_id; | |
| 97 } | |
| 98 | |
| 99 void TaskManagerSharedWorkerResource::UpdateProcessHandle( | |
| 100 base::ProcessHandle handle) { | |
| 101 handle_ = handle; | |
| 102 } | |
| 103 | |
| 104 string16 TaskManagerSharedWorkerResource::GetTitle() const { | |
| 105 return l10n_util::GetStringFUTF16(IDS_TASK_MANAGER_WORKER_PREFIX, title_); | |
| 106 } | |
| 107 | |
| 108 string16 TaskManagerSharedWorkerResource::GetProfileName() const { | |
| 109 return string16(); | |
| 110 } | |
| 111 | |
| 112 gfx::ImageSkia TaskManagerSharedWorkerResource::GetIcon() const { | |
| 113 if (!default_icon_) { | |
| 114 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); | |
| 115 default_icon_ = rb.GetImageSkiaNamed(IDR_PLUGINS_FAVICON); | |
| 116 // TODO(jabdelmalek): use different icon for web workers. | |
| 117 } | |
| 118 return *default_icon_; | |
| 119 } | |
| 120 | |
| 121 base::ProcessHandle TaskManagerSharedWorkerResource::GetProcess() const { | |
| 122 return handle_; | |
| 123 } | |
| 124 | |
| 125 int TaskManagerSharedWorkerResource::GetUniqueChildProcessId() const { | |
| 126 return process_id_; | |
| 127 } | |
| 128 | |
| 129 TaskManager::Resource::Type TaskManagerSharedWorkerResource::GetType() const { | |
| 130 return WORKER; | |
| 131 } | |
| 132 | |
| 133 bool TaskManagerSharedWorkerResource::CanInspect() const { | |
| 134 return true; | |
| 135 } | |
| 136 | |
| 137 void TaskManagerSharedWorkerResource::Inspect() const { | |
| 138 // TODO(yurys): would be better to get profile from one of the tabs connected | |
| 139 // to the worker. | |
| 140 Profile* profile = ProfileManager::GetLastUsedProfile(); | |
| 141 if (!profile) | |
| 142 return; | |
| 143 scoped_refptr<DevToolsAgentHost> agent_host( | |
| 144 DevToolsAgentHost::GetForWorker(process_id_, routing_id_)); | |
| 145 DevToolsWindow::OpenDevToolsWindowForWorker(profile, agent_host); | |
| 146 } | |
| 147 | |
| 148 bool TaskManagerSharedWorkerResource::SupportNetworkUsage() const { | |
| 149 return false; | |
| 150 } | |
| 151 | |
| 152 void TaskManagerSharedWorkerResource::SetSupportNetworkUsage() { | |
| 153 } | |
| 154 | |
| 155 | |
| 156 // This class is needed to ensure that all resources in WorkerResourceList are | |
| 157 // deleted if corresponding task is posted to but not executed on the UI | |
| 158 // thread. | |
| 159 class TaskManagerWorkerResourceProvider::WorkerResourceListHolder { | |
| 160 public: | |
| 161 WorkerResourceListHolder() { | |
| 162 } | |
| 163 | |
| 164 ~WorkerResourceListHolder() { | |
| 165 STLDeleteElements(&resources_); | |
| 166 } | |
| 167 | |
| 168 WorkerResourceList* resources() { | |
| 169 return &resources_; | |
| 170 } | |
| 171 | |
| 172 private: | |
| 173 WorkerResourceList resources_; | |
| 174 }; | |
| 175 | |
| 176 | |
| 177 TaskManagerWorkerResourceProvider:: | |
| 178 TaskManagerWorkerResourceProvider(TaskManager* task_manager) | |
| 179 : updating_(false), | |
| 180 task_manager_(task_manager) { | |
| 181 } | |
| 182 | |
| 183 TaskManagerWorkerResourceProvider::~TaskManagerWorkerResourceProvider() { | |
| 184 DeleteAllResources(); | |
| 185 } | |
| 186 | |
| 187 TaskManager::Resource* TaskManagerWorkerResourceProvider::GetResource( | |
| 188 int origin_pid, | |
| 189 int render_process_host_id, | |
| 190 int routing_id) { | |
| 191 return NULL; | |
| 192 } | |
| 193 | |
| 194 void TaskManagerWorkerResourceProvider::StartUpdating() { | |
| 195 DCHECK(!updating_); | |
| 196 updating_ = true; | |
| 197 // Get existing workers. | |
| 198 BrowserThread::PostTask( | |
| 199 BrowserThread::IO, FROM_HERE, base::Bind( | |
| 200 &TaskManagerWorkerResourceProvider::StartObservingWorkers, | |
| 201 this)); | |
| 202 | |
| 203 BrowserChildProcessObserver::Add(this); | |
| 204 } | |
| 205 | |
| 206 void TaskManagerWorkerResourceProvider::StopUpdating() { | |
| 207 DCHECK(updating_); | |
| 208 updating_ = false; | |
| 209 launching_workers_.clear(); | |
| 210 DeleteAllResources(); | |
| 211 BrowserThread::PostTask( | |
| 212 BrowserThread::IO, FROM_HERE, base::Bind( | |
| 213 &TaskManagerWorkerResourceProvider::StopObservingWorkers, | |
| 214 this)); | |
| 215 | |
| 216 BrowserChildProcessObserver::Remove(this); | |
| 217 } | |
| 218 | |
| 219 void TaskManagerWorkerResourceProvider::BrowserChildProcessHostConnected( | |
| 220 const content::ChildProcessData& data) { | |
| 221 DCHECK(updating_); | |
| 222 | |
| 223 if (data.process_type != content::PROCESS_TYPE_WORKER) | |
| 224 return; | |
| 225 | |
| 226 ProcessIdToWorkerResources::iterator it(launching_workers_.find(data.id)); | |
| 227 if (it == launching_workers_.end()) | |
| 228 return; | |
| 229 WorkerResourceList& resources = it->second; | |
| 230 for (WorkerResourceList::iterator r = resources.begin(); | |
| 231 r != resources.end(); ++r) { | |
| 232 (*r)->UpdateProcessHandle(data.handle); | |
| 233 task_manager_->AddResource(*r); | |
| 234 } | |
| 235 launching_workers_.erase(it); | |
| 236 } | |
| 237 | |
| 238 void TaskManagerWorkerResourceProvider::BrowserChildProcessHostDisconnected( | |
| 239 const content::ChildProcessData& data) { | |
| 240 DCHECK(updating_); | |
| 241 | |
| 242 if (data.process_type != content::PROCESS_TYPE_WORKER) | |
| 243 return; | |
| 244 | |
| 245 // Worker process may be destroyed before WorkerMsg_TerminateWorkerContex | |
| 246 // message is handled and WorkerDestroyed is fired. In this case we won't | |
| 247 // get WorkerDestroyed notification and have to clear resources for such | |
| 248 // workers here when the worker process has been destroyed. | |
| 249 for (WorkerResourceList::iterator it = resources_.begin(); | |
| 250 it != resources_.end();) { | |
| 251 if ((*it)->process_id() == data.id) { | |
| 252 task_manager_->RemoveResource(*it); | |
| 253 delete *it; | |
| 254 it = resources_.erase(it); | |
| 255 } else { | |
| 256 ++it; | |
| 257 } | |
| 258 } | |
| 259 DCHECK(!ContainsKey(launching_workers_, data.id)); | |
| 260 } | |
| 261 | |
| 262 void TaskManagerWorkerResourceProvider::WorkerCreated( | |
| 263 const GURL& url, | |
| 264 const string16& name, | |
| 265 int process_id, | |
| 266 int route_id) { | |
| 267 TaskManagerSharedWorkerResource* resource = | |
| 268 new TaskManagerSharedWorkerResource( | |
| 269 url, name, process_id, route_id, base::kNullProcessHandle); | |
| 270 BrowserThread::PostTask( | |
| 271 BrowserThread::UI, FROM_HERE, | |
| 272 base::Bind(&TaskManagerWorkerResourceProvider::NotifyWorkerCreated, | |
| 273 this, base::Owned(new WorkerResourceHolder(resource)))); | |
| 274 } | |
| 275 | |
| 276 void TaskManagerWorkerResourceProvider::WorkerDestroyed(int process_id, | |
| 277 int route_id) { | |
| 278 BrowserThread::PostTask( | |
| 279 BrowserThread::UI, FROM_HERE, base::Bind( | |
| 280 &TaskManagerWorkerResourceProvider::NotifyWorkerDestroyed, | |
| 281 this, process_id, route_id)); | |
| 282 } | |
| 283 | |
| 284 void TaskManagerWorkerResourceProvider::NotifyWorkerCreated( | |
| 285 WorkerResourceHolder* resource_holder) { | |
| 286 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 287 if (!updating_) | |
| 288 return; | |
| 289 AddResource(resource_holder->release()); | |
| 290 } | |
| 291 | |
| 292 void TaskManagerWorkerResourceProvider::NotifyWorkerDestroyed( | |
| 293 int process_id, int routing_id) { | |
| 294 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 295 if (!updating_) | |
| 296 return; | |
| 297 for (WorkerResourceList::iterator it = resources_.begin(); | |
| 298 it !=resources_.end(); ++it) { | |
| 299 if ((*it)->Matches(process_id, routing_id)) { | |
| 300 task_manager_->RemoveResource(*it); | |
| 301 delete *it; | |
| 302 resources_.erase(it); | |
| 303 return; | |
| 304 } | |
| 305 } | |
| 306 } | |
| 307 | |
| 308 void TaskManagerWorkerResourceProvider::StartObservingWorkers() { | |
| 309 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 310 | |
| 311 scoped_ptr<WorkerResourceListHolder> holder(new WorkerResourceListHolder); | |
| 312 std::vector<WorkerService::WorkerInfo> worker_info = | |
| 313 WorkerService::GetInstance()->GetWorkers(); | |
| 314 | |
| 315 for (size_t i = 0; i < worker_info.size(); ++i) { | |
| 316 holder->resources()->push_back(new TaskManagerSharedWorkerResource( | |
| 317 worker_info[i].url, worker_info[i].name, worker_info[i].process_id, | |
| 318 worker_info[i].route_id, worker_info[i].handle)); | |
| 319 } | |
| 320 | |
| 321 BrowserThread::PostTask( | |
| 322 BrowserThread::UI, FROM_HERE, | |
| 323 base::Bind( | |
| 324 &TaskManagerWorkerResourceProvider::AddWorkerResourceList, | |
| 325 this, base::Owned(holder.release()))); | |
| 326 | |
| 327 WorkerService::GetInstance()->AddObserver(this); | |
| 328 } | |
| 329 | |
| 330 void TaskManagerWorkerResourceProvider::StopObservingWorkers() { | |
| 331 WorkerService::GetInstance()->RemoveObserver(this); | |
| 332 } | |
| 333 | |
| 334 void TaskManagerWorkerResourceProvider::AddWorkerResourceList( | |
| 335 WorkerResourceListHolder* resource_list_holder) { | |
| 336 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 337 if (!updating_) | |
| 338 return; | |
| 339 WorkerResourceList* resources = resource_list_holder->resources(); | |
| 340 for (WorkerResourceList::iterator it = resources->begin(); | |
| 341 it !=resources->end(); ++it) { | |
| 342 AddResource(*it); | |
| 343 } | |
| 344 resources->clear(); | |
| 345 } | |
| 346 | |
| 347 void TaskManagerWorkerResourceProvider::AddResource( | |
| 348 TaskManagerSharedWorkerResource* resource) { | |
| 349 DCHECK(updating_); | |
| 350 resources_.push_back(resource); | |
| 351 if (resource->handle() == base::kNullProcessHandle) { | |
| 352 int process_id = resource->process_id(); | |
| 353 launching_workers_[process_id].push_back(resource); | |
| 354 } else { | |
| 355 task_manager_->AddResource(resource); | |
| 356 } | |
| 357 } | |
| 358 | |
| 359 void TaskManagerWorkerResourceProvider::DeleteAllResources() { | |
| 360 STLDeleteElements(&resources_); | |
| 361 } | |
| OLD | NEW |