| 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/containers/scoped_ptr_hash_map.h" | 8 #include "base/containers/scoped_ptr_hash_map.h" |
| 9 #include "base/strings/string_number_conversions.h" | 9 #include "base/strings/string_number_conversions.h" |
| 10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
| (...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 126 } | 126 } |
| 127 | 127 |
| 128 scoped_refptr<ServiceWorkerRegistration> CreateRegistrationDictionaryValue( | 128 scoped_refptr<ServiceWorkerRegistration> CreateRegistrationDictionaryValue( |
| 129 const ServiceWorkerRegistrationInfo& registration_info) { | 129 const ServiceWorkerRegistrationInfo& registration_info) { |
| 130 scoped_refptr<ServiceWorkerRegistration> registration( | 130 scoped_refptr<ServiceWorkerRegistration> registration( |
| 131 ServiceWorkerRegistration::Create() | 131 ServiceWorkerRegistration::Create() |
| 132 ->set_registration_id( | 132 ->set_registration_id( |
| 133 base::Int64ToString(registration_info.registration_id)) | 133 base::Int64ToString(registration_info.registration_id)) |
| 134 ->set_scope_url(registration_info.pattern.spec()) | 134 ->set_scope_url(registration_info.pattern.spec()) |
| 135 ->set_is_deleted(registration_info.delete_flag == | 135 ->set_is_deleted(registration_info.delete_flag == |
| 136 ServiceWorkerRegistrationInfo::IS_DELETED) | 136 ServiceWorkerRegistrationInfo::IS_DELETED)); |
| 137 ->set_force_update_on_page_load( | |
| 138 registration_info.force_update_on_page_load == | |
| 139 ServiceWorkerRegistrationInfo::IS_FORCED)); | |
| 140 return registration; | 137 return registration; |
| 141 } | 138 } |
| 142 | 139 |
| 143 scoped_refptr<ServiceWorkerDevToolsAgentHost> GetMatchingServiceWorker( | 140 scoped_refptr<ServiceWorkerDevToolsAgentHost> GetMatchingServiceWorker( |
| 144 const ServiceWorkerDevToolsAgentHost::List& agent_hosts, | 141 const ServiceWorkerDevToolsAgentHost::List& agent_hosts, |
| 145 const GURL& url) { | 142 const GURL& url) { |
| 146 scoped_refptr<ServiceWorkerDevToolsAgentHost> best_host; | 143 scoped_refptr<ServiceWorkerDevToolsAgentHost> best_host; |
| 147 bool best_host_scope_matched = false; | 144 bool best_host_scope_matched = false; |
| 148 int best_host_scope_length = 0; | 145 int best_host_scope_length = 0; |
| 149 | 146 |
| (...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 304 | 301 |
| 305 Response ServiceWorkerHandler::Enable() { | 302 Response ServiceWorkerHandler::Enable() { |
| 306 if (enabled_) | 303 if (enabled_) |
| 307 return Response::OK(); | 304 return Response::OK(); |
| 308 if (!context_) | 305 if (!context_) |
| 309 return Response::InternalError("Could not connect to the context"); | 306 return Response::InternalError("Could not connect to the context"); |
| 310 enabled_ = true; | 307 enabled_ = true; |
| 311 | 308 |
| 312 ServiceWorkerDevToolsManager::GetInstance()->AddObserver(this); | 309 ServiceWorkerDevToolsManager::GetInstance()->AddObserver(this); |
| 313 | 310 |
| 314 client_->DebugOnStartUpdated( | |
| 315 DebugOnStartUpdatedParams::Create()->set_debug_on_start( | |
| 316 ServiceWorkerDevToolsManager::GetInstance() | |
| 317 ->debug_service_worker_on_start())); | |
| 318 | |
| 319 context_watcher_ = new ServiceWorkerContextWatcher( | 311 context_watcher_ = new ServiceWorkerContextWatcher( |
| 320 context_, base::Bind(&ServiceWorkerHandler::OnWorkerRegistrationUpdated, | 312 context_, base::Bind(&ServiceWorkerHandler::OnWorkerRegistrationUpdated, |
| 321 weak_factory_.GetWeakPtr()), | 313 weak_factory_.GetWeakPtr()), |
| 322 base::Bind(&ServiceWorkerHandler::OnWorkerVersionUpdated, | 314 base::Bind(&ServiceWorkerHandler::OnWorkerVersionUpdated, |
| 323 weak_factory_.GetWeakPtr()), | 315 weak_factory_.GetWeakPtr()), |
| 324 base::Bind(&ServiceWorkerHandler::OnErrorReported, | 316 base::Bind(&ServiceWorkerHandler::OnErrorReported, |
| 325 weak_factory_.GetWeakPtr())); | 317 weak_factory_.GetWeakPtr())); |
| 326 context_watcher_->Start(); | 318 context_watcher_->Start(); |
| 327 | 319 |
| 328 UpdateHosts(); | 320 UpdateHosts(); |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 415 if (!base::StringToInt64(version_id, &id)) | 407 if (!base::StringToInt64(version_id, &id)) |
| 416 return CreateInvalidVersionIdErrorResponse(); | 408 return CreateInvalidVersionIdErrorResponse(); |
| 417 BrowserThread::PostTask( | 409 BrowserThread::PostTask( |
| 418 BrowserThread::IO, FROM_HERE, | 410 BrowserThread::IO, FROM_HERE, |
| 419 base::Bind(&GetDevToolsRouteInfoOnIO, context_, id, | 411 base::Bind(&GetDevToolsRouteInfoOnIO, context_, id, |
| 420 base::Bind(&ServiceWorkerHandler::OpenNewDevToolsWindow, | 412 base::Bind(&ServiceWorkerHandler::OpenNewDevToolsWindow, |
| 421 weak_factory_.GetWeakPtr()))); | 413 weak_factory_.GetWeakPtr()))); |
| 422 return Response::OK(); | 414 return Response::OK(); |
| 423 } | 415 } |
| 424 | 416 |
| 425 Response ServiceWorkerHandler::SetDebugOnStart(bool debug_on_start) { | |
| 426 ServiceWorkerDevToolsManager::GetInstance() | |
| 427 ->set_debug_service_worker_on_start(debug_on_start); | |
| 428 return Response::OK(); | |
| 429 } | |
| 430 | |
| 431 Response ServiceWorkerHandler::SetForceUpdateOnPageLoad( | 417 Response ServiceWorkerHandler::SetForceUpdateOnPageLoad( |
| 432 const std::string& registration_id, | |
| 433 bool force_update_on_page_load) { | 418 bool force_update_on_page_load) { |
| 434 if (!context_) | 419 if (!context_) |
| 435 return CreateContextErrorResponse(); | 420 return CreateContextErrorResponse(); |
| 436 int64_t id = kInvalidServiceWorkerRegistrationId; | 421 context_->SetForceUpdateOnPageLoad(force_update_on_page_load); |
| 437 if (!base::StringToInt64(registration_id, &id)) | |
| 438 return CreateInvalidVersionIdErrorResponse(); | |
| 439 if (force_update_on_page_load) | |
| 440 force_update_enabled_registrations_.insert(id); | |
| 441 else | |
| 442 force_update_enabled_registrations_.erase(id); | |
| 443 context_->SetForceUpdateOnPageLoad(id, force_update_on_page_load); | |
| 444 return Response::OK(); | 422 return Response::OK(); |
| 445 } | 423 } |
| 446 | 424 |
| 447 Response ServiceWorkerHandler::DeliverPushMessage( | 425 Response ServiceWorkerHandler::DeliverPushMessage( |
| 448 const std::string& origin, | 426 const std::string& origin, |
| 449 const std::string& registration_id, | 427 const std::string& registration_id, |
| 450 const std::string& data) { | 428 const std::string& data) { |
| 451 if (!enabled_) | 429 if (!enabled_) |
| 452 return Response::OK(); | 430 return Response::OK(); |
| 453 if (!render_frame_host_) | 431 if (!render_frame_host_) |
| (...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 579 return; | 557 return; |
| 580 } | 558 } |
| 581 UpdateHosts(); | 559 UpdateHosts(); |
| 582 } | 560 } |
| 583 | 561 |
| 584 void ServiceWorkerHandler::WorkerDestroyed( | 562 void ServiceWorkerHandler::WorkerDestroyed( |
| 585 ServiceWorkerDevToolsAgentHost* host) { | 563 ServiceWorkerDevToolsAgentHost* host) { |
| 586 UpdateHosts(); | 564 UpdateHosts(); |
| 587 } | 565 } |
| 588 | 566 |
| 589 void ServiceWorkerHandler::DebugOnStartUpdated(bool debug_on_start) { | |
| 590 client_->DebugOnStartUpdated( | |
| 591 DebugOnStartUpdatedParams::Create()->set_debug_on_start(debug_on_start)); | |
| 592 } | |
| 593 | |
| 594 void ServiceWorkerHandler::ReportWorkerCreated( | 567 void ServiceWorkerHandler::ReportWorkerCreated( |
| 595 ServiceWorkerDevToolsAgentHost* host) { | 568 ServiceWorkerDevToolsAgentHost* host) { |
| 596 if (host->IsAttached()) | 569 if (host->IsAttached()) |
| 597 return; | 570 return; |
| 598 attached_hosts_[host->GetId()] = host; | 571 attached_hosts_[host->GetId()] = host; |
| 599 host->AttachClient(this); | 572 host->AttachClient(this); |
| 600 client_->WorkerCreated(WorkerCreatedParams::Create() | 573 client_->WorkerCreated(WorkerCreatedParams::Create() |
| 601 ->set_worker_id(host->GetId()) | 574 ->set_worker_id(host->GetId()) |
| 602 ->set_url(host->GetURL().spec()) | 575 ->set_url(host->GetURL().spec()) |
| 603 ->set_version_id(base::Int64ToString( | 576 ->set_version_id(base::Int64ToString( |
| 604 host->service_worker_version_id()))); | 577 host->service_worker_version_id()))); |
| 605 } | 578 } |
| 606 | 579 |
| 607 void ServiceWorkerHandler::ReportWorkerTerminated( | 580 void ServiceWorkerHandler::ReportWorkerTerminated( |
| 608 ServiceWorkerDevToolsAgentHost* host) { | 581 ServiceWorkerDevToolsAgentHost* host) { |
| 609 auto it = attached_hosts_.find(host->GetId()); | 582 auto it = attached_hosts_.find(host->GetId()); |
| 610 if (it == attached_hosts_.end()) | 583 if (it == attached_hosts_.end()) |
| 611 return; | 584 return; |
| 612 host->DetachClient(); | 585 host->DetachClient(); |
| 613 client_->WorkerTerminated(WorkerTerminatedParams::Create()-> | 586 client_->WorkerTerminated(WorkerTerminatedParams::Create()-> |
| 614 set_worker_id(host->GetId())); | 587 set_worker_id(host->GetId())); |
| 615 attached_hosts_.erase(it); | 588 attached_hosts_.erase(it); |
| 616 } | 589 } |
| 617 | 590 |
| 618 void ServiceWorkerHandler::ClearForceUpdate() { | 591 void ServiceWorkerHandler::ClearForceUpdate() { |
| 619 if (context_) { | 592 if (context_) |
| 620 for (const auto registration_id : force_update_enabled_registrations_) | 593 context_->SetForceUpdateOnPageLoad(false); |
| 621 context_->SetForceUpdateOnPageLoad(registration_id, false); | |
| 622 } | |
| 623 force_update_enabled_registrations_.clear(); | |
| 624 } | 594 } |
| 625 | 595 |
| 626 } // namespace service_worker | 596 } // namespace service_worker |
| 627 } // namespace devtools | 597 } // namespace devtools |
| 628 } // namespace content | 598 } // namespace content |
| OLD | NEW |