| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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_worker_resource_provider.h" | 5 #include "chrome/browser/task_manager/task_manager_worker_resource_provider.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
| 9 #include "base/stl_util.h" | 9 #include "base/stl_util.h" |
| 10 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
| 11 #include "chrome/browser/browser_process.h" | 11 #include "chrome/browser/browser_process.h" |
| 12 #include "chrome/browser/debugger/devtools_window.h" | 12 #include "chrome/browser/debugger/devtools_window.h" |
| 13 #include "chrome/browser/profiles/profile_manager.h" | 13 #include "chrome/browser/profiles/profile_manager.h" |
| 14 #include "content/browser/browser_child_process_host.h" | 14 #include "content/browser/browser_child_process_host.h" |
| 15 #include "content/browser/worker_host/worker_process_host.h" | 15 #include "content/browser/worker_host/worker_process_host.h" |
| 16 #include "content/browser/worker_host/worker_service.h" | 16 #include "content/browser/worker_host/worker_service.h" |
| 17 #include "content/browser/worker_host/worker_service_observer.h" | 17 #include "content/browser/worker_host/worker_service_observer.h" |
| 18 #include "content/public/browser/browser_thread.h" | 18 #include "content/public/browser/browser_thread.h" |
| 19 #include "content/public/browser/child_process_data.h" |
| 19 #include "content/public/browser/devtools_agent_host_registry.h" | 20 #include "content/public/browser/devtools_agent_host_registry.h" |
| 20 #include "content/public/browser/notification_service.h" | 21 #include "content/public/browser/notification_service.h" |
| 21 #include "content/public/browser/notification_types.h" | 22 #include "content/public/browser/notification_types.h" |
| 22 #include "content/public/common/process_type.h" | |
| 23 #include "grit/generated_resources.h" | 23 #include "grit/generated_resources.h" |
| 24 #include "grit/theme_resources_standard.h" | 24 #include "grit/theme_resources_standard.h" |
| 25 #include "ui/base/l10n/l10n_util.h" | 25 #include "ui/base/l10n/l10n_util.h" |
| 26 #include "ui/base/resource/resource_bundle.h" | 26 #include "ui/base/resource/resource_bundle.h" |
| 27 | 27 |
| 28 using content::BrowserThread; | 28 using content::BrowserThread; |
| 29 using content::DevToolsAgentHost; | 29 using content::DevToolsAgentHost; |
| 30 using content::DevToolsAgentHostRegistry; | 30 using content::DevToolsAgentHostRegistry; |
| 31 | 31 |
| 32 // Objects of this class are created on the IO thread and then passed to the UI | 32 // Objects of this class are created on the IO thread and then passed to the UI |
| 33 // thread where they are passed to the task manager. All methods must be called | 33 // thread where they are passed to the task manager. All methods must be called |
| 34 // only on the UI thread. Destructor may be called on any thread. | 34 // only on the UI thread. Destructor may be called on any thread. |
| 35 class TaskManagerSharedWorkerResource : public TaskManager::Resource { | 35 class TaskManagerSharedWorkerResource : public TaskManager::Resource { |
| 36 public: | 36 public: |
| 37 TaskManagerSharedWorkerResource(const ChildProcessInfo& process_info, | 37 TaskManagerSharedWorkerResource(const content::ChildProcessData& process_data, |
| 38 int routing_id, const GURL& url, | 38 int routing_id, const GURL& url, |
| 39 const string16& name); | 39 const string16& name); |
| 40 virtual ~TaskManagerSharedWorkerResource(); | 40 virtual ~TaskManagerSharedWorkerResource(); |
| 41 | 41 |
| 42 bool Matches(int process_id, int routing_id) const; | 42 bool Matches(int process_id, int routing_id) const; |
| 43 | 43 |
| 44 void UpdateProcessInfo(const ChildProcessInfo& process_info); | 44 void UpdateProcessData(const content::ChildProcessData& process_data); |
| 45 const ChildProcessInfo& process_info() { return process_info_; } | 45 const content::ChildProcessData& process_data() { return process_data_; } |
| 46 | 46 |
| 47 private: | 47 private: |
| 48 // TaskManager::Resource methods: | 48 // TaskManager::Resource methods: |
| 49 virtual string16 GetTitle() const OVERRIDE; | 49 virtual string16 GetTitle() const OVERRIDE; |
| 50 virtual string16 GetProfileName() const OVERRIDE; | 50 virtual string16 GetProfileName() const OVERRIDE; |
| 51 virtual SkBitmap GetIcon() const OVERRIDE; | 51 virtual SkBitmap GetIcon() const OVERRIDE; |
| 52 virtual base::ProcessHandle GetProcess() const OVERRIDE; | 52 virtual base::ProcessHandle GetProcess() const OVERRIDE; |
| 53 virtual Type GetType() const OVERRIDE; | 53 virtual Type GetType() const OVERRIDE; |
| 54 virtual bool CanInspect() const OVERRIDE; | 54 virtual bool CanInspect() const OVERRIDE; |
| 55 virtual void Inspect() const OVERRIDE; | 55 virtual void Inspect() const OVERRIDE; |
| 56 | 56 |
| 57 virtual bool SupportNetworkUsage() const OVERRIDE; | 57 virtual bool SupportNetworkUsage() const OVERRIDE; |
| 58 virtual void SetSupportNetworkUsage() OVERRIDE; | 58 virtual void SetSupportNetworkUsage() OVERRIDE; |
| 59 | 59 |
| 60 ChildProcessInfo process_info_; | 60 content::ChildProcessData process_data_; |
| 61 int routing_id_; | 61 int routing_id_; |
| 62 string16 title_; | 62 string16 title_; |
| 63 | 63 |
| 64 static SkBitmap* default_icon_; | 64 static SkBitmap* default_icon_; |
| 65 | 65 |
| 66 DISALLOW_COPY_AND_ASSIGN(TaskManagerSharedWorkerResource); | 66 DISALLOW_COPY_AND_ASSIGN(TaskManagerSharedWorkerResource); |
| 67 }; | 67 }; |
| 68 | 68 |
| 69 SkBitmap* TaskManagerSharedWorkerResource::default_icon_ = NULL; | 69 SkBitmap* TaskManagerSharedWorkerResource::default_icon_ = NULL; |
| 70 | 70 |
| 71 TaskManagerSharedWorkerResource::TaskManagerSharedWorkerResource( | 71 TaskManagerSharedWorkerResource::TaskManagerSharedWorkerResource( |
| 72 const ChildProcessInfo& process_info, | 72 const content::ChildProcessData& process_data, |
| 73 int routing_id, | 73 int routing_id, |
| 74 const GURL& url, | 74 const GURL& url, |
| 75 const string16& name) | 75 const string16& name) |
| 76 : process_info_(process_info), | 76 : process_data_(process_data), |
| 77 routing_id_(routing_id) { | 77 routing_id_(routing_id) { |
| 78 title_ = UTF8ToUTF16(url.spec()); | 78 title_ = UTF8ToUTF16(url.spec()); |
| 79 if (!name.empty()) | 79 if (!name.empty()) |
| 80 title_ += ASCIIToUTF16(" (") + name + ASCIIToUTF16(")"); | 80 title_ += ASCIIToUTF16(" (") + name + ASCIIToUTF16(")"); |
| 81 } | 81 } |
| 82 | 82 |
| 83 TaskManagerSharedWorkerResource::~TaskManagerSharedWorkerResource() { | 83 TaskManagerSharedWorkerResource::~TaskManagerSharedWorkerResource() { |
| 84 } | 84 } |
| 85 | 85 |
| 86 bool TaskManagerSharedWorkerResource::Matches(int process_id, | 86 bool TaskManagerSharedWorkerResource::Matches(int process_id, |
| 87 int routing_id) const { | 87 int routing_id) const { |
| 88 return process_info_.id() == process_id && routing_id_ == routing_id; | 88 return process_data_.id == process_id && routing_id_ == routing_id; |
| 89 } | 89 } |
| 90 | 90 |
| 91 void TaskManagerSharedWorkerResource::UpdateProcessInfo( | 91 void TaskManagerSharedWorkerResource::UpdateProcessData( |
| 92 const ChildProcessInfo& process_info) { | 92 const content::ChildProcessData& process_data) { |
| 93 process_info_ = process_info; | 93 process_data_ = process_data; |
| 94 } | 94 } |
| 95 | 95 |
| 96 string16 TaskManagerSharedWorkerResource::GetTitle() const { | 96 string16 TaskManagerSharedWorkerResource::GetTitle() const { |
| 97 return l10n_util::GetStringFUTF16(IDS_TASK_MANAGER_WORKER_PREFIX, title_); | 97 return l10n_util::GetStringFUTF16(IDS_TASK_MANAGER_WORKER_PREFIX, title_); |
| 98 } | 98 } |
| 99 | 99 |
| 100 string16 TaskManagerSharedWorkerResource::GetProfileName() const { | 100 string16 TaskManagerSharedWorkerResource::GetProfileName() const { |
| 101 return string16(); | 101 return string16(); |
| 102 } | 102 } |
| 103 | 103 |
| 104 SkBitmap TaskManagerSharedWorkerResource::GetIcon() const { | 104 SkBitmap TaskManagerSharedWorkerResource::GetIcon() const { |
| 105 if (!default_icon_) { | 105 if (!default_icon_) { |
| 106 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); | 106 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); |
| 107 default_icon_ = rb.GetBitmapNamed(IDR_PLUGIN); | 107 default_icon_ = rb.GetBitmapNamed(IDR_PLUGIN); |
| 108 // TODO(jabdelmalek): use different icon for web workers. | 108 // TODO(jabdelmalek): use different icon for web workers. |
| 109 } | 109 } |
| 110 return *default_icon_; | 110 return *default_icon_; |
| 111 } | 111 } |
| 112 | 112 |
| 113 base::ProcessHandle TaskManagerSharedWorkerResource::GetProcess() const { | 113 base::ProcessHandle TaskManagerSharedWorkerResource::GetProcess() const { |
| 114 return process_info_.handle(); | 114 return process_data_.handle; |
| 115 } | 115 } |
| 116 | 116 |
| 117 TaskManager::Resource::Type TaskManagerSharedWorkerResource::GetType() const { | 117 TaskManager::Resource::Type TaskManagerSharedWorkerResource::GetType() const { |
| 118 return WORKER; | 118 return WORKER; |
| 119 } | 119 } |
| 120 | 120 |
| 121 bool TaskManagerSharedWorkerResource::CanInspect() const { | 121 bool TaskManagerSharedWorkerResource::CanInspect() const { |
| 122 return true; | 122 return true; |
| 123 } | 123 } |
| 124 | 124 |
| 125 void TaskManagerSharedWorkerResource::Inspect() const { | 125 void TaskManagerSharedWorkerResource::Inspect() const { |
| 126 // TODO(yurys): would be better to get profile from one of the tabs connected | 126 // TODO(yurys): would be better to get profile from one of the tabs connected |
| 127 // to the worker. | 127 // to the worker. |
| 128 Profile* profile = ProfileManager::GetLastUsedProfile(); | 128 Profile* profile = ProfileManager::GetLastUsedProfile(); |
| 129 if (!profile) | 129 if (!profile) |
| 130 return; | 130 return; |
| 131 DevToolsAgentHost* agent_host = | 131 DevToolsAgentHost* agent_host = |
| 132 DevToolsAgentHostRegistry::GetDevToolsAgentHostForWorker( | 132 DevToolsAgentHostRegistry::GetDevToolsAgentHostForWorker( |
| 133 process_info_.id(), | 133 process_data_.id, |
| 134 routing_id_); | 134 routing_id_); |
| 135 DevToolsWindow::OpenDevToolsWindowForWorker(profile, agent_host); | 135 DevToolsWindow::OpenDevToolsWindowForWorker(profile, agent_host); |
| 136 } | 136 } |
| 137 | 137 |
| 138 bool TaskManagerSharedWorkerResource::SupportNetworkUsage() const { | 138 bool TaskManagerSharedWorkerResource::SupportNetworkUsage() const { |
| 139 return false; | 139 return false; |
| 140 } | 140 } |
| 141 | 141 |
| 142 void TaskManagerSharedWorkerResource::SetSupportNetworkUsage() { | 142 void TaskManagerSharedWorkerResource::SetSupportNetworkUsage() { |
| 143 } | 143 } |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 210 BrowserThread::PostTask( | 210 BrowserThread::PostTask( |
| 211 BrowserThread::IO, FROM_HERE, base::Bind( | 211 BrowserThread::IO, FROM_HERE, base::Bind( |
| 212 &TaskManagerWorkerResourceProvider::StopObservingWorkers, | 212 &TaskManagerWorkerResourceProvider::StopObservingWorkers, |
| 213 this)); | 213 this)); |
| 214 } | 214 } |
| 215 | 215 |
| 216 void TaskManagerWorkerResourceProvider::WorkerCreated( | 216 void TaskManagerWorkerResourceProvider::WorkerCreated( |
| 217 WorkerProcessHost* process, | 217 WorkerProcessHost* process, |
| 218 const WorkerProcessHost::WorkerInstance& instance) { | 218 const WorkerProcessHost::WorkerInstance& instance) { |
| 219 TaskManagerSharedWorkerResource* resource = | 219 TaskManagerSharedWorkerResource* resource = |
| 220 new TaskManagerSharedWorkerResource(*process, instance.worker_route_id(), | 220 new TaskManagerSharedWorkerResource(process->data(), |
| 221 instance.worker_route_id(), |
| 221 instance.url(), instance.name()); | 222 instance.url(), instance.name()); |
| 222 BrowserThread::PostTask( | 223 BrowserThread::PostTask( |
| 223 BrowserThread::UI, FROM_HERE, | 224 BrowserThread::UI, FROM_HERE, |
| 224 base::Bind(&TaskManagerWorkerResourceProvider::NotifyWorkerCreated, | 225 base::Bind(&TaskManagerWorkerResourceProvider::NotifyWorkerCreated, |
| 225 this, base::Owned(new WorkerResourceHolder(resource)))); | 226 this, base::Owned(new WorkerResourceHolder(resource)))); |
| 226 } | 227 } |
| 227 | 228 |
| 228 void TaskManagerWorkerResourceProvider::WorkerDestroyed( | 229 void TaskManagerWorkerResourceProvider::WorkerDestroyed( |
| 229 WorkerProcessHost* process, | 230 WorkerProcessHost* process, |
| 230 int worker_route_id) { | 231 int worker_route_id) { |
| 231 BrowserThread::PostTask( | 232 BrowserThread::PostTask( |
| 232 BrowserThread::UI, FROM_HERE, base::Bind( | 233 BrowserThread::UI, FROM_HERE, base::Bind( |
| 233 &TaskManagerWorkerResourceProvider::NotifyWorkerDestroyed, | 234 &TaskManagerWorkerResourceProvider::NotifyWorkerDestroyed, |
| 234 this, process->id(), worker_route_id)); | 235 this, process->id(), worker_route_id)); |
| 235 } | 236 } |
| 236 | 237 |
| 237 void TaskManagerWorkerResourceProvider::Observe( | 238 void TaskManagerWorkerResourceProvider::Observe( |
| 238 int type, | 239 int type, |
| 239 const content::NotificationSource& source, | 240 const content::NotificationSource& source, |
| 240 const content::NotificationDetails& details) { | 241 const content::NotificationDetails& details) { |
| 241 ChildProcessInfo* process_info = | 242 content::ChildProcessData* process_data = |
| 242 content::Details<ChildProcessInfo>(details).ptr(); | 243 content::Details<content::ChildProcessData>(details).ptr(); |
| 243 if (process_info->type() != content::PROCESS_TYPE_WORKER) | 244 if (process_data->type != content::PROCESS_TYPE_WORKER) |
| 244 return; | 245 return; |
| 245 if (type == content::NOTIFICATION_CHILD_PROCESS_HOST_CONNECTED) { | 246 if (type == content::NOTIFICATION_CHILD_PROCESS_HOST_CONNECTED) { |
| 246 ProcessIdToWorkerResources::iterator it = | 247 ProcessIdToWorkerResources::iterator it = |
| 247 launching_workers_.find(process_info->id()); | 248 launching_workers_.find(process_data->id); |
| 248 if (it == launching_workers_.end()) | 249 if (it == launching_workers_.end()) |
| 249 return; | 250 return; |
| 250 WorkerResourceList& resources = it->second; | 251 WorkerResourceList& resources = it->second; |
| 251 for (WorkerResourceList::iterator r = resources.begin(); | 252 for (WorkerResourceList::iterator r = resources.begin(); |
| 252 r !=resources.end(); ++r) { | 253 r !=resources.end(); ++r) { |
| 253 (*r)->UpdateProcessInfo(*process_info); | 254 (*r)->UpdateProcessData(*process_data); |
| 254 task_manager_->AddResource(*r); | 255 task_manager_->AddResource(*r); |
| 255 } | 256 } |
| 256 launching_workers_.erase(it); | 257 launching_workers_.erase(it); |
| 257 } else if (type == content::NOTIFICATION_CHILD_PROCESS_HOST_DISCONNECTED) { | 258 } else if (type == content::NOTIFICATION_CHILD_PROCESS_HOST_DISCONNECTED) { |
| 258 // Worker process may be destroyed before WorkerMsg_TerminateWorkerContex | 259 // Worker process may be destroyed before WorkerMsg_TerminateWorkerContex |
| 259 // message is handled and WorkerDestroyed is fired. In this case we won't | 260 // message is handled and WorkerDestroyed is fired. In this case we won't |
| 260 // get WorkerDestroyed notification and have to clear resources for such | 261 // get WorkerDestroyed notification and have to clear resources for such |
| 261 // workers here when the worker process has been destroyed. | 262 // workers here when the worker process has been destroyed. |
| 262 for (WorkerResourceList::iterator it = resources_.begin(); | 263 for (WorkerResourceList::iterator it = resources_.begin(); |
| 263 it !=resources_.end();) { | 264 it !=resources_.end();) { |
| 264 if ((*it)->process_info().id() == process_info->id()) { | 265 if ((*it)->process_data().id == process_data->id) { |
| 265 task_manager_->RemoveResource(*it); | 266 task_manager_->RemoveResource(*it); |
| 266 delete *it; | 267 delete *it; |
| 267 it = resources_.erase(it); | 268 it = resources_.erase(it); |
| 268 } else { | 269 } else { |
| 269 ++it; | 270 ++it; |
| 270 } | 271 } |
| 271 } | 272 } |
| 272 DCHECK(launching_workers_.find(process_info->id()) == | 273 DCHECK(launching_workers_.find(process_data->id) == |
| 273 launching_workers_.end()); | 274 launching_workers_.end()); |
| 274 } | 275 } |
| 275 } | 276 } |
| 276 | 277 |
| 277 void TaskManagerWorkerResourceProvider::NotifyWorkerCreated( | 278 void TaskManagerWorkerResourceProvider::NotifyWorkerCreated( |
| 278 WorkerResourceHolder* resource_holder) { | 279 WorkerResourceHolder* resource_holder) { |
| 279 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 280 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 280 if (!updating_) | 281 if (!updating_) |
| 281 return; | 282 return; |
| 282 AddResource(resource_holder->release()); | 283 AddResource(resource_holder->release()); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 302 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 303 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 303 | 304 |
| 304 scoped_ptr<WorkerResourceListHolder> holder(new WorkerResourceListHolder); | 305 scoped_ptr<WorkerResourceListHolder> holder(new WorkerResourceListHolder); |
| 305 BrowserChildProcessHost::Iterator iter(content::PROCESS_TYPE_WORKER); | 306 BrowserChildProcessHost::Iterator iter(content::PROCESS_TYPE_WORKER); |
| 306 for (; !iter.Done(); ++iter) { | 307 for (; !iter.Done(); ++iter) { |
| 307 WorkerProcessHost* worker = static_cast<WorkerProcessHost*>(*iter); | 308 WorkerProcessHost* worker = static_cast<WorkerProcessHost*>(*iter); |
| 308 const WorkerProcessHost::Instances& instances = worker->instances(); | 309 const WorkerProcessHost::Instances& instances = worker->instances(); |
| 309 for (WorkerProcessHost::Instances::const_iterator i = instances.begin(); | 310 for (WorkerProcessHost::Instances::const_iterator i = instances.begin(); |
| 310 i != instances.end(); ++i) { | 311 i != instances.end(); ++i) { |
| 311 holder->resources()->push_back(new TaskManagerSharedWorkerResource( | 312 holder->resources()->push_back(new TaskManagerSharedWorkerResource( |
| 312 **iter, i->worker_route_id(), i->url(), i->name())); | 313 (*iter)->data(), i->worker_route_id(), i->url(), i->name())); |
| 313 } | 314 } |
| 314 } | 315 } |
| 315 | 316 |
| 316 BrowserThread::PostTask( | 317 BrowserThread::PostTask( |
| 317 BrowserThread::UI, FROM_HERE, | 318 BrowserThread::UI, FROM_HERE, |
| 318 base::Bind( | 319 base::Bind( |
| 319 &TaskManagerWorkerResourceProvider::AddWorkerResourceList, | 320 &TaskManagerWorkerResourceProvider::AddWorkerResourceList, |
| 320 this, base::Owned(holder.release()))); | 321 this, base::Owned(holder.release()))); |
| 321 | 322 |
| 322 WorkerService::GetInstance()->AddObserver(this); | 323 WorkerService::GetInstance()->AddObserver(this); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 336 it !=resources->end(); ++it) { | 337 it !=resources->end(); ++it) { |
| 337 AddResource(*it); | 338 AddResource(*it); |
| 338 } | 339 } |
| 339 resources->clear(); | 340 resources->clear(); |
| 340 } | 341 } |
| 341 | 342 |
| 342 void TaskManagerWorkerResourceProvider::AddResource( | 343 void TaskManagerWorkerResourceProvider::AddResource( |
| 343 TaskManagerSharedWorkerResource* resource) { | 344 TaskManagerSharedWorkerResource* resource) { |
| 344 DCHECK(updating_); | 345 DCHECK(updating_); |
| 345 resources_.push_back(resource); | 346 resources_.push_back(resource); |
| 346 if (resource->process_info().handle() == base::kNullProcessHandle) { | 347 if (resource->process_data().handle == base::kNullProcessHandle) { |
| 347 int process_id = resource->process_info().id(); | 348 int process_id = resource->process_data().id; |
| 348 launching_workers_[process_id].push_back(resource); | 349 launching_workers_[process_id].push_back(resource); |
| 349 } else { | 350 } else { |
| 350 task_manager_->AddResource(resource); | 351 task_manager_->AddResource(resource); |
| 351 } | 352 } |
| 352 } | 353 } |
| 353 | 354 |
| 354 void TaskManagerWorkerResourceProvider::DeleteAllResources() { | 355 void TaskManagerWorkerResourceProvider::DeleteAllResources() { |
| 355 STLDeleteElements(&resources_); | 356 STLDeleteElements(&resources_); |
| 356 } | 357 } |
| OLD | NEW |