Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "content/browser/devtools/protocol/service_worker_handler.h" | 5 #include "content/browser/devtools/protocol/service_worker_handler.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/strings/string_number_conversions.h" | 8 #include "base/strings/string_number_conversions.h" |
| 9 #include "content/browser/devtools/service_worker_devtools_agent_host.h" | 9 #include "content/browser/devtools/service_worker_devtools_agent_host.h" |
| 10 #include "content/browser/devtools/service_worker_devtools_manager.h" | 10 #include "content/browser/devtools/service_worker_devtools_manager.h" |
| 11 #include "content/browser/frame_host/frame_tree.h" | |
| 12 #include "content/browser/frame_host/frame_tree_node.h" | |
| 13 #include "content/browser/frame_host/render_frame_host_impl.h" | |
| 11 #include "content/browser/service_worker/service_worker_context_observer.h" | 14 #include "content/browser/service_worker/service_worker_context_observer.h" |
| 12 #include "content/browser/service_worker/service_worker_context_wrapper.h" | 15 #include "content/browser/service_worker/service_worker_context_wrapper.h" |
| 13 #include "content/public/browser/browser_context.h" | 16 #include "content/public/browser/browser_context.h" |
| 14 #include "content/public/browser/browser_thread.h" | 17 #include "content/public/browser/browser_thread.h" |
| 15 #include "content/public/browser/devtools_agent_host.h" | 18 #include "content/public/browser/devtools_agent_host.h" |
| 16 #include "content/public/browser/render_frame_host.h" | 19 #include "content/public/browser/render_frame_host.h" |
| 17 #include "content/public/browser/render_process_host.h" | 20 #include "content/public/browser/render_process_host.h" |
| 18 #include "content/public/browser/storage_partition.h" | 21 #include "content/public/browser/storage_partition.h" |
| 19 #include "url/gurl.h" | 22 #include "url/gurl.h" |
| 20 | 23 |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 95 CreateVersionDictionaryValue(registration_info.waiting_version)); | 98 CreateVersionDictionaryValue(registration_info.waiting_version)); |
| 96 } | 99 } |
| 97 if (registration_info.installing_version.version_id != | 100 if (registration_info.installing_version.version_id != |
| 98 kInvalidServiceWorkerVersionId) { | 101 kInvalidServiceWorkerVersionId) { |
| 99 registration->set_installing_version( | 102 registration->set_installing_version( |
| 100 CreateVersionDictionaryValue(registration_info.installing_version)); | 103 CreateVersionDictionaryValue(registration_info.installing_version)); |
| 101 } | 104 } |
| 102 return registration; | 105 return registration; |
| 103 } | 106 } |
| 104 | 107 |
| 108 static scoped_refptr<ServiceWorkerDevToolsAgentHost> GetMatchingServiceWorker( | |
|
dgozman
2015/03/13 18:01:52
drop static
pfeldman
2015/03/13 18:05:36
Done.
| |
| 109 const ServiceWorkerDevToolsAgentHost::List& agent_hosts, | |
| 110 const GURL& url) { | |
| 111 scoped_refptr<ServiceWorkerDevToolsAgentHost> best_host; | |
| 112 std::string best_scope; | |
| 113 for (auto host : agent_hosts) { | |
| 114 if (host->GetURL().host() != url.host()) | |
| 115 continue; | |
| 116 std::string path = host->GetURL().path(); | |
| 117 std::string file = host->GetURL().ExtractFileName(); | |
| 118 std::string scope = path.substr(0, path.length() - file.length()); | |
| 119 if (scope.length() > best_scope.length()) { | |
| 120 best_host = host; | |
| 121 best_scope = scope; | |
| 122 } | |
| 123 } | |
| 124 return best_host; | |
| 125 } | |
| 126 | |
| 127 static ServiceWorkerDevToolsAgentHost::Set GetMatchingServiceWorkers( | |
|
dgozman
2015/03/13 18:01:52
ditto
pfeldman
2015/03/13 18:05:36
Done.
| |
| 128 const std::set<GURL>& urls) { | |
| 129 ServiceWorkerDevToolsAgentHost::List agent_hosts; | |
| 130 ServiceWorkerDevToolsManager::GetInstance()-> | |
| 131 AddAllAgentHosts(&agent_hosts); | |
| 132 ServiceWorkerDevToolsAgentHost::Set result; | |
| 133 for (const GURL& url : urls) { | |
| 134 scoped_refptr<ServiceWorkerDevToolsAgentHost> host = | |
| 135 GetMatchingServiceWorker(agent_hosts, url); | |
| 136 if (host) | |
| 137 result.insert(host); | |
| 138 } | |
| 139 return result; | |
| 140 } | |
| 141 | |
| 142 | |
| 105 } // namespace | 143 } // namespace |
| 106 | 144 |
| 107 using Response = DevToolsProtocolClient::Response; | 145 using Response = DevToolsProtocolClient::Response; |
| 108 | 146 |
| 109 class ServiceWorkerHandler::ContextObserver | 147 class ServiceWorkerHandler::ContextObserver |
| 110 : public ServiceWorkerContextObserver, | 148 : public ServiceWorkerContextObserver, |
| 111 public base::RefCountedThreadSafe<ContextObserver> { | 149 public base::RefCountedThreadSafe<ContextObserver> { |
| 112 public: | 150 public: |
| 113 ContextObserver(scoped_refptr<ServiceWorkerContextWrapper> context, | 151 ContextObserver(scoped_refptr<ServiceWorkerContextWrapper> context, |
| 114 base::WeakPtr<ServiceWorkerHandler> handler); | 152 base::WeakPtr<ServiceWorkerHandler> handler); |
| (...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 259 | 297 |
| 260 ServiceWorkerHandler::ServiceWorkerHandler() | 298 ServiceWorkerHandler::ServiceWorkerHandler() |
| 261 : enabled_(false), weak_factory_(this) { | 299 : enabled_(false), weak_factory_(this) { |
| 262 } | 300 } |
| 263 | 301 |
| 264 ServiceWorkerHandler::~ServiceWorkerHandler() { | 302 ServiceWorkerHandler::~ServiceWorkerHandler() { |
| 265 Disable(); | 303 Disable(); |
| 266 } | 304 } |
| 267 | 305 |
| 268 void ServiceWorkerHandler::SetRenderFrameHost( | 306 void ServiceWorkerHandler::SetRenderFrameHost( |
| 269 RenderFrameHost* render_frame_host) { | 307 RenderFrameHostImpl* render_frame_host) { |
| 308 render_frame_host_ = render_frame_host; | |
|
dgozman
2015/03/13 18:01:52
UpdateURLs();
pfeldman
2015/03/13 18:05:36
Done.
| |
| 270 if (!render_frame_host) { | 309 if (!render_frame_host) { |
| 271 context_ = nullptr; | 310 context_ = nullptr; |
| 272 return; | 311 return; |
| 273 } | 312 } |
| 274 StoragePartition* partition = BrowserContext::GetStoragePartition( | 313 StoragePartition* partition = BrowserContext::GetStoragePartition( |
| 275 render_frame_host->GetProcess()->GetBrowserContext(), | 314 render_frame_host->GetProcess()->GetBrowserContext(), |
| 276 render_frame_host->GetSiteInstance()); | 315 render_frame_host->GetSiteInstance()); |
| 277 DCHECK(partition); | 316 DCHECK(partition); |
| 278 context_ = static_cast<ServiceWorkerContextWrapper*>( | 317 context_ = static_cast<ServiceWorkerContextWrapper*>( |
| 279 partition->GetServiceWorkerContext()); | 318 partition->GetServiceWorkerContext()); |
| 280 } | 319 } |
| 281 | 320 |
| 282 void ServiceWorkerHandler::SetClient(scoped_ptr<Client> client) { | 321 void ServiceWorkerHandler::SetClient(scoped_ptr<Client> client) { |
| 283 client_.swap(client); | 322 client_.swap(client); |
| 284 } | 323 } |
| 285 | 324 |
| 286 void ServiceWorkerHandler::SetURL(const GURL& url) { | 325 static bool CollectURLs(std::set<GURL>* urls, FrameTreeNode* tree_node) { |
|
dgozman
2015/03/13 18:01:52
move to namespace above
pfeldman
2015/03/13 18:05:36
Done.
| |
| 287 url_ = url; | 326 urls->insert(tree_node->current_url()); |
| 288 if (enabled_) { | 327 return false; |
| 289 for (auto pair : attached_hosts_) { | 328 } |
| 290 if (!MatchesInspectedPage(pair.second.get())) | |
| 291 WorkerDestroyed(pair.second.get()); | |
| 292 } | |
| 293 | 329 |
| 294 ServiceWorkerDevToolsAgentHost::List agent_hosts; | 330 void ServiceWorkerHandler::UpdateURLs() { |
| 295 ServiceWorkerDevToolsManager::GetInstance()-> | 331 if (!enabled_) |
| 296 AddAllAgentHosts(&agent_hosts); | 332 return; |
| 297 for (auto host : agent_hosts) { | 333 |
| 298 if (!MatchesInspectedPage(host.get())) | 334 urls_.clear(); |
| 299 continue; | 335 render_frame_host_->frame_tree_node()->frame_tree()->ForEach( |
| 300 if (attached_hosts_.find(host->GetId()) != attached_hosts_.end()) | 336 base::Bind(&CollectURLs, &urls_)); |
| 301 continue; | 337 |
| 302 // TODO(pfeldman): workers are created concurrently, we need | 338 AttachedHosts old_hosts = attached_hosts_; |
| 303 // to get notification earlier to go through the Created/Ready | 339 ServiceWorkerDevToolsAgentHost::Set new_hosts = |
| 304 // lifecycle. | 340 GetMatchingServiceWorkers(urls_); |
| 305 WorkerReadyForInspection(host.get()); | 341 |
| 306 } | 342 for (auto pair : old_hosts) { |
| 343 if (new_hosts.find(pair.second) == new_hosts.end()) | |
|
dgozman
2015/03/13 18:01:52
indentation
pfeldman
2015/03/13 18:05:36
Done.
| |
| 344 WorkerDestroyed(pair.second.get()); | |
| 345 } | |
| 346 | |
| 347 for (auto host : new_hosts) { | |
| 348 if (old_hosts.find(host->GetId()) == old_hosts.end()) | |
| 349 ReportWorkerCreated(host.get()); | |
| 307 } | 350 } |
| 308 } | 351 } |
| 309 | 352 |
| 310 void ServiceWorkerHandler::Detached() { | 353 void ServiceWorkerHandler::Detached() { |
| 311 Disable(); | 354 Disable(); |
| 312 } | 355 } |
| 313 | 356 |
| 314 Response ServiceWorkerHandler::Enable() { | 357 Response ServiceWorkerHandler::Enable() { |
| 315 if (enabled_) | 358 if (enabled_) |
| 316 return Response::OK(); | 359 return Response::OK(); |
| 317 if (!context_) | 360 if (!context_) |
| 318 return Response::InternalError("Could not connect to the context"); | 361 return Response::InternalError("Could not connect to the context"); |
| 319 enabled_ = true; | 362 enabled_ = true; |
| 320 | 363 |
| 321 ServiceWorkerDevToolsManager::GetInstance()->AddObserver(this); | 364 ServiceWorkerDevToolsManager::GetInstance()->AddObserver(this); |
| 322 | |
| 323 ServiceWorkerDevToolsAgentHost::List agent_hosts; | |
| 324 ServiceWorkerDevToolsManager::GetInstance()->AddAllAgentHosts(&agent_hosts); | |
| 325 for (auto host : agent_hosts) | |
| 326 WorkerReadyForInspection(host.get()); | |
| 327 | |
| 328 context_observer_ = new ContextObserver(context_, weak_factory_.GetWeakPtr()); | 365 context_observer_ = new ContextObserver(context_, weak_factory_.GetWeakPtr()); |
| 329 context_observer_->Start(); | 366 context_observer_->Start(); |
| 367 UpdateURLs(); | |
| 330 return Response::OK(); | 368 return Response::OK(); |
| 331 } | 369 } |
| 332 | 370 |
| 333 Response ServiceWorkerHandler::Disable() { | 371 Response ServiceWorkerHandler::Disable() { |
| 334 if (!enabled_) | 372 if (!enabled_) |
| 335 return Response::OK(); | 373 return Response::OK(); |
| 336 enabled_ = false; | 374 enabled_ = false; |
| 337 | 375 |
| 338 ServiceWorkerDevToolsManager::GetInstance()->RemoveObserver(this); | 376 ServiceWorkerDevToolsManager::GetInstance()->RemoveObserver(this); |
| 339 for (const auto& pair : attached_hosts_) | 377 for (const auto& pair : attached_hosts_) |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 407 } | 445 } |
| 408 | 446 |
| 409 void ServiceWorkerHandler::AgentHostClosed( | 447 void ServiceWorkerHandler::AgentHostClosed( |
| 410 DevToolsAgentHost* host, | 448 DevToolsAgentHost* host, |
| 411 bool replaced_with_another_client) { | 449 bool replaced_with_another_client) { |
| 412 WorkerDestroyed(static_cast<ServiceWorkerDevToolsAgentHost*>(host)); | 450 WorkerDestroyed(static_cast<ServiceWorkerDevToolsAgentHost*>(host)); |
| 413 } | 451 } |
| 414 | 452 |
| 415 void ServiceWorkerHandler::WorkerCreated( | 453 void ServiceWorkerHandler::WorkerCreated( |
| 416 ServiceWorkerDevToolsAgentHost* host) { | 454 ServiceWorkerDevToolsAgentHost* host) { |
| 417 if (!MatchesInspectedPage(host)) | 455 if (MatchesInspectedPage(host)) |
| 418 return; | 456 host->PauseForDebugOnStart(); |
| 419 host->PauseForDebugOnStart(); | |
| 420 } | 457 } |
| 421 | 458 |
| 422 void ServiceWorkerHandler::WorkerReadyForInspection( | 459 void ServiceWorkerHandler::WorkerReadyForInspection( |
| 423 ServiceWorkerDevToolsAgentHost* host) { | 460 ServiceWorkerDevToolsAgentHost* host) { |
| 424 if (host->IsAttached() || !MatchesInspectedPage(host)) | 461 if (MatchesInspectedPage(host)) |
| 425 return; | 462 ReportWorkerCreated(host); |
| 426 | |
| 427 attached_hosts_[host->GetId()] = host; | |
| 428 host->AttachClient(this); | |
| 429 client_->WorkerCreated(WorkerCreatedParams::Create()-> | |
| 430 set_worker_id(host->GetId())-> | |
| 431 set_url(host->GetURL().spec())); | |
| 432 } | 463 } |
| 433 | 464 |
| 434 void ServiceWorkerHandler::WorkerDestroyed( | 465 void ServiceWorkerHandler::WorkerDestroyed( |
| 435 ServiceWorkerDevToolsAgentHost* host) { | 466 ServiceWorkerDevToolsAgentHost* host) { |
| 436 auto it = attached_hosts_.find(host->GetId()); | 467 auto it = attached_hosts_.find(host->GetId()); |
| 437 if (it == attached_hosts_.end()) | 468 if (it == attached_hosts_.end()) |
| 438 return; | 469 return; |
| 439 it->second->DetachClient(); | 470 it->second->DetachClient(); |
| 440 client_->WorkerTerminated(WorkerTerminatedParams::Create()-> | 471 client_->WorkerTerminated(WorkerTerminatedParams::Create()-> |
| 441 set_worker_id(host->GetId())); | 472 set_worker_id(host->GetId())); |
| 442 attached_hosts_.erase(it); | 473 attached_hosts_.erase(it); |
| 443 } | 474 } |
| 444 | 475 |
| 476 void ServiceWorkerHandler::ReportWorkerCreated( | |
| 477 ServiceWorkerDevToolsAgentHost* host) { | |
| 478 if (host->IsAttached()) | |
| 479 return; | |
| 480 attached_hosts_[host->GetId()] = host; | |
| 481 host->AttachClient(this); | |
| 482 client_->WorkerCreated(WorkerCreatedParams::Create()-> | |
| 483 set_worker_id(host->GetId())-> | |
| 484 set_url(host->GetURL().spec())); | |
| 485 } | |
| 486 | |
| 445 bool ServiceWorkerHandler::MatchesInspectedPage( | 487 bool ServiceWorkerHandler::MatchesInspectedPage( |
| 446 ServiceWorkerDevToolsAgentHost* host) { | 488 ServiceWorkerDevToolsAgentHost* host) { |
| 447 // TODO(pfeldman): match based on scope. | 489 auto hosts = GetMatchingServiceWorkers(urls_); |
| 448 // TODO(pfeldman): match iframes. | 490 return hosts.find(host) != hosts.end(); |
| 449 return host->GetURL().host() == url_.host(); | |
| 450 } | 491 } |
| 451 | 492 |
| 452 } // namespace service_worker | 493 } // namespace service_worker |
| 453 } // namespace devtools | 494 } // namespace devtools |
| 454 } // namespace content | 495 } // namespace content |
| OLD | NEW |