Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/service_worker/service_worker_version.h" | 5 #include "content/browser/service_worker/service_worker_version.h" |
| 6 | 6 |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/memory/ref_counted.h" | 8 #include "base/memory/ref_counted.h" |
| 9 #include "base/metrics/histogram_macros.h" | 9 #include "base/metrics/histogram_macros.h" |
| 10 #include "base/stl_util.h" | 10 #include "base/stl_util.h" |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 84 template <typename IDMAP, typename... Params> | 84 template <typename IDMAP, typename... Params> |
| 85 void RunIDMapCallbacks(IDMAP* callbacks, const Params&... params) { | 85 void RunIDMapCallbacks(IDMAP* callbacks, const Params&... params) { |
| 86 typename IDMAP::iterator iter(callbacks); | 86 typename IDMAP::iterator iter(callbacks); |
| 87 while (!iter.IsAtEnd()) { | 87 while (!iter.IsAtEnd()) { |
| 88 iter.GetCurrentValue()->Run(params...); | 88 iter.GetCurrentValue()->Run(params...); |
| 89 iter.Advance(); | 89 iter.Advance(); |
| 90 } | 90 } |
| 91 callbacks->Clear(); | 91 callbacks->Clear(); |
| 92 } | 92 } |
| 93 | 93 |
| 94 void RunStartWorkerCallback( | |
| 95 const StatusCallback& callback, | |
| 96 scoped_refptr<ServiceWorkerRegistration> protect, | |
| 97 ServiceWorkerStatusCode status) { | |
| 98 callback.Run(status); | |
| 99 } | |
| 100 | |
| 94 // A callback adapter to start a |task| after StartWorker. | 101 // A callback adapter to start a |task| after StartWorker. |
| 95 void RunTaskAfterStartWorker( | 102 void RunTaskAfterStartWorker( |
| 96 base::WeakPtr<ServiceWorkerVersion> version, | 103 base::WeakPtr<ServiceWorkerVersion> version, |
| 97 const StatusCallback& error_callback, | 104 const StatusCallback& error_callback, |
| 98 const base::Closure& task, | 105 const base::Closure& task, |
| 99 ServiceWorkerStatusCode status) { | 106 ServiceWorkerStatusCode status) { |
| 100 if (status != SERVICE_WORKER_OK) { | 107 if (status != SERVICE_WORKER_OK) { |
| 101 if (!error_callback.is_null()) | 108 if (!error_callback.is_null()) |
| 102 error_callback.Run(status); | 109 error_callback.Run(status); |
| 103 return; | 110 return; |
| (...skipping 266 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 370 embedded_worker()->worker_devtools_agent_route_id()); | 377 embedded_worker()->worker_devtools_agent_route_id()); |
| 371 } | 378 } |
| 372 | 379 |
| 373 void ServiceWorkerVersion::StartWorker(const StatusCallback& callback) { | 380 void ServiceWorkerVersion::StartWorker(const StatusCallback& callback) { |
| 374 StartWorker(false, callback); | 381 StartWorker(false, callback); |
| 375 } | 382 } |
| 376 | 383 |
| 377 void ServiceWorkerVersion::StartWorker( | 384 void ServiceWorkerVersion::StartWorker( |
| 378 bool pause_after_download, | 385 bool pause_after_download, |
| 379 const StatusCallback& callback) { | 386 const StatusCallback& callback) { |
| 380 if (is_doomed()) { | 387 if (!context_) { |
| 381 RunSoon(base::Bind(callback, SERVICE_WORKER_ERROR_START_WORKER_FAILED)); | 388 RunSoon(base::Bind(callback, SERVICE_WORKER_ERROR_START_WORKER_FAILED)); |
| 382 return; | 389 return; |
| 383 } | 390 } |
| 384 switch (running_status()) { | 391 |
| 385 case RUNNING: | 392 // Ensure the live registration during starting worker so that the worker can |
| 386 RunSoon(base::Bind(callback, SERVICE_WORKER_OK)); | 393 // get associated with it ServiceWorkerDispatcherHost::OnSetHostedVersionId(). |
| 387 return; | 394 scoped_refptr<ServiceWorkerRegistration> protect = |
| 388 case STOPPING: | 395 context_->GetLiveRegistration(registration_id_); |
|
michaeln
2015/03/12 19:34:02
drive by: This call and early return adds no value
nhiroki
2015/03/13 01:21:31
Done.
| |
| 389 case STOPPED: | 396 if (protect) { |
| 390 case STARTING: | 397 DidEnsureLiveRegistrationForStartWorker( |
| 391 if (!timeout_timer_.IsRunning()) | 398 pause_after_download, callback, SERVICE_WORKER_OK, protect); |
| 392 StartTimeoutTimer(); | 399 return; |
| 393 start_callbacks_.push_back(callback); | |
| 394 if (running_status() == STOPPED) { | |
| 395 DCHECK(!cache_listener_.get()); | |
| 396 cache_listener_.reset(new ServiceWorkerCacheListener(this, context_)); | |
| 397 embedded_worker_->Start( | |
| 398 version_id_, scope_, script_url_, pause_after_download, | |
| 399 base::Bind(&ServiceWorkerVersion::OnStartSentAndScriptEvaluated, | |
| 400 weak_factory_.GetWeakPtr())); | |
| 401 } | |
| 402 return; | |
| 403 } | 400 } |
| 401 context_->storage()->FindRegistrationForId( | |
| 402 registration_id_, | |
| 403 scope_.GetOrigin(), | |
| 404 base::Bind(&ServiceWorkerVersion::DidEnsureLiveRegistrationForStartWorker, | |
| 405 weak_factory_.GetWeakPtr(), | |
| 406 pause_after_download, | |
| 407 callback)); | |
| 404 } | 408 } |
| 405 | 409 |
| 406 void ServiceWorkerVersion::StopWorker(const StatusCallback& callback) { | 410 void ServiceWorkerVersion::StopWorker(const StatusCallback& callback) { |
| 407 if (running_status() == STOPPED) { | 411 if (running_status() == STOPPED) { |
| 408 RunSoon(base::Bind(callback, SERVICE_WORKER_OK)); | 412 RunSoon(base::Bind(callback, SERVICE_WORKER_OK)); |
| 409 return; | 413 return; |
| 410 } | 414 } |
| 411 if (stop_callbacks_.empty()) { | 415 if (stop_callbacks_.empty()) { |
| 412 ServiceWorkerStatusCode status = embedded_worker_->Stop(); | 416 ServiceWorkerStatusCode status = embedded_worker_->Stop(); |
| 413 if (status != SERVICE_WORKER_OK) { | 417 if (status != SERVICE_WORKER_OK) { |
| (...skipping 367 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 781 DoomInternal(); | 785 DoomInternal(); |
| 782 } | 786 } |
| 783 | 787 |
| 784 void ServiceWorkerVersion::SetDevToolsAttached(bool attached) { | 788 void ServiceWorkerVersion::SetDevToolsAttached(bool attached) { |
| 785 embedded_worker()->set_devtools_attached(attached); | 789 embedded_worker()->set_devtools_attached(attached); |
| 786 if (attached) { | 790 if (attached) { |
| 787 // Set to null time so we don't record the startup time metric. | 791 // Set to null time so we don't record the startup time metric. |
| 788 ClearTick(&start_time_); | 792 ClearTick(&start_time_); |
| 789 return; | 793 return; |
| 790 } | 794 } |
| 791 if (!timeout_timer_.IsRunning()) | |
| 792 StartTimeoutTimer(); | |
|
kinuko
2015/03/12 15:50:34
nit: Could we do this in another CL, as it's not r
nhiroki
2015/03/13 01:21:30
Reverted this and uploaded a separate patch: https
| |
| 793 } | 795 } |
| 794 | 796 |
| 795 void ServiceWorkerVersion::SetMainScriptHttpResponseInfo( | 797 void ServiceWorkerVersion::SetMainScriptHttpResponseInfo( |
| 796 const net::HttpResponseInfo& http_info) { | 798 const net::HttpResponseInfo& http_info) { |
| 797 main_script_http_info_.reset(new net::HttpResponseInfo(http_info)); | 799 main_script_http_info_.reset(new net::HttpResponseInfo(http_info)); |
| 798 } | 800 } |
| 799 | 801 |
| 800 const net::HttpResponseInfo* | 802 const net::HttpResponseInfo* |
| 801 ServiceWorkerVersion::GetMainScriptHttpResponseInfo() { | 803 ServiceWorkerVersion::GetMainScriptHttpResponseInfo() { |
| 802 return main_script_http_info_.get(); | 804 return main_script_http_info_.get(); |
| (...skipping 603 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1406 callback.Run(SERVICE_WORKER_ERROR_ABORT); | 1408 callback.Run(SERVICE_WORKER_ERROR_ABORT); |
| 1407 return; | 1409 return; |
| 1408 } | 1410 } |
| 1409 registration->ClaimClients(callback); | 1411 registration->ClaimClients(callback); |
| 1410 } | 1412 } |
| 1411 | 1413 |
| 1412 void ServiceWorkerVersion::OnPongFromWorker() { | 1414 void ServiceWorkerVersion::OnPongFromWorker() { |
| 1413 ClearTick(&ping_time_); | 1415 ClearTick(&ping_time_); |
| 1414 } | 1416 } |
| 1415 | 1417 |
| 1418 void ServiceWorkerVersion::DidEnsureLiveRegistrationForStartWorker( | |
| 1419 bool pause_after_download, | |
| 1420 const StatusCallback& callback, | |
| 1421 ServiceWorkerStatusCode status, | |
| 1422 const scoped_refptr<ServiceWorkerRegistration>& protect) { | |
| 1423 if (status != SERVICE_WORKER_OK || is_doomed()) { | |
| 1424 RunSoon(base::Bind(callback, SERVICE_WORKER_ERROR_START_WORKER_FAILED)); | |
| 1425 return; | |
| 1426 } | |
| 1427 | |
| 1428 switch (running_status()) { | |
| 1429 case RUNNING: | |
| 1430 RunSoon(base::Bind(callback, SERVICE_WORKER_OK)); | |
| 1431 return; | |
| 1432 case STOPPING: | |
| 1433 case STOPPED: | |
| 1434 case STARTING: | |
| 1435 if (!timeout_timer_.IsRunning()) | |
| 1436 StartTimeoutTimer(); | |
| 1437 // Start callbacks keep the live registration. | |
| 1438 start_callbacks_.push_back( | |
| 1439 base::Bind(&RunStartWorkerCallback, callback, protect)); | |
| 1440 if (running_status() == STOPPED) { | |
| 1441 DCHECK(!cache_listener_.get()); | |
| 1442 cache_listener_.reset(new ServiceWorkerCacheListener(this, context_)); | |
| 1443 embedded_worker_->Start( | |
| 1444 version_id_, scope_, script_url_, pause_after_download, | |
| 1445 base::Bind(&ServiceWorkerVersion::OnStartSentAndScriptEvaluated, | |
| 1446 weak_factory_.GetWeakPtr())); | |
| 1447 } | |
| 1448 return; | |
| 1449 } | |
| 1450 } | |
| 1451 | |
| 1416 void ServiceWorkerVersion::DidClaimClients( | 1452 void ServiceWorkerVersion::DidClaimClients( |
| 1417 int request_id, ServiceWorkerStatusCode status) { | 1453 int request_id, ServiceWorkerStatusCode status) { |
| 1418 if (status == SERVICE_WORKER_ERROR_STATE) { | 1454 if (status == SERVICE_WORKER_ERROR_STATE) { |
| 1419 embedded_worker_->SendMessage(ServiceWorkerMsg_ClaimClientsError( | 1455 embedded_worker_->SendMessage(ServiceWorkerMsg_ClaimClientsError( |
| 1420 request_id, blink::WebServiceWorkerError::ErrorTypeState, | 1456 request_id, blink::WebServiceWorkerError::ErrorTypeState, |
| 1421 base::ASCIIToUTF16(kClaimClientsStateErrorMesage))); | 1457 base::ASCIIToUTF16(kClaimClientsStateErrorMesage))); |
| 1422 return; | 1458 return; |
| 1423 } | 1459 } |
| 1424 if (status == SERVICE_WORKER_ERROR_ABORT) { | 1460 if (status == SERVICE_WORKER_ERROR_ABORT) { |
| 1425 embedded_worker_->SendMessage(ServiceWorkerMsg_ClaimClientsError( | 1461 embedded_worker_->SendMessage(ServiceWorkerMsg_ClaimClientsError( |
| (...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1615 int request_id) { | 1651 int request_id) { |
| 1616 callbacks->Remove(request_id); | 1652 callbacks->Remove(request_id); |
| 1617 if (is_doomed_) { | 1653 if (is_doomed_) { |
| 1618 // The stop should be already scheduled, but try to stop immediately, in | 1654 // The stop should be already scheduled, but try to stop immediately, in |
| 1619 // order to release worker resources soon. | 1655 // order to release worker resources soon. |
| 1620 StopWorkerIfIdle(); | 1656 StopWorkerIfIdle(); |
| 1621 } | 1657 } |
| 1622 } | 1658 } |
| 1623 | 1659 |
| 1624 } // namespace content | 1660 } // namespace content |
| OLD | NEW |