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