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 in SWDispatcherHost::OnSetHostedVersionId(). |
387 return; | 394 context_->storage()->FindRegistrationForId( |
388 case STOPPING: | 395 registration_id_, |
389 case STOPPED: | 396 scope_.GetOrigin(), |
390 case STARTING: | 397 base::Bind(&ServiceWorkerVersion::DidEnsureLiveRegistrationForStartWorker, |
391 if (!timeout_timer_.IsRunning()) | 398 weak_factory_.GetWeakPtr(), |
392 StartTimeoutTimer(); | 399 pause_after_download, |
393 start_callbacks_.push_back(callback); | 400 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 } | |
404 } | 401 } |
405 | 402 |
406 void ServiceWorkerVersion::StopWorker(const StatusCallback& callback) { | 403 void ServiceWorkerVersion::StopWorker(const StatusCallback& callback) { |
407 if (running_status() == STOPPED) { | 404 if (running_status() == STOPPED) { |
408 RunSoon(base::Bind(callback, SERVICE_WORKER_OK)); | 405 RunSoon(base::Bind(callback, SERVICE_WORKER_OK)); |
409 return; | 406 return; |
410 } | 407 } |
411 if (stop_callbacks_.empty()) { | 408 if (stop_callbacks_.empty()) { |
412 ServiceWorkerStatusCode status = embedded_worker_->Stop(); | 409 ServiceWorkerStatusCode status = embedded_worker_->Stop(); |
413 if (status != SERVICE_WORKER_OK) { | 410 if (status != SERVICE_WORKER_OK) { |
(...skipping 992 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1406 callback.Run(SERVICE_WORKER_ERROR_ABORT); | 1403 callback.Run(SERVICE_WORKER_ERROR_ABORT); |
1407 return; | 1404 return; |
1408 } | 1405 } |
1409 registration->ClaimClients(callback); | 1406 registration->ClaimClients(callback); |
1410 } | 1407 } |
1411 | 1408 |
1412 void ServiceWorkerVersion::OnPongFromWorker() { | 1409 void ServiceWorkerVersion::OnPongFromWorker() { |
1413 ClearTick(&ping_time_); | 1410 ClearTick(&ping_time_); |
1414 } | 1411 } |
1415 | 1412 |
| 1413 void ServiceWorkerVersion::DidEnsureLiveRegistrationForStartWorker( |
| 1414 bool pause_after_download, |
| 1415 const StatusCallback& callback, |
| 1416 ServiceWorkerStatusCode status, |
| 1417 const scoped_refptr<ServiceWorkerRegistration>& protect) { |
| 1418 if (status != SERVICE_WORKER_OK || is_doomed()) { |
| 1419 RunSoon(base::Bind(callback, SERVICE_WORKER_ERROR_START_WORKER_FAILED)); |
| 1420 return; |
| 1421 } |
| 1422 |
| 1423 switch (running_status()) { |
| 1424 case RUNNING: |
| 1425 RunSoon(base::Bind(callback, SERVICE_WORKER_OK)); |
| 1426 return; |
| 1427 case STOPPING: |
| 1428 case STOPPED: |
| 1429 case STARTING: |
| 1430 if (!timeout_timer_.IsRunning()) |
| 1431 StartTimeoutTimer(); |
| 1432 // Start callbacks keep the live registration. |
| 1433 start_callbacks_.push_back( |
| 1434 base::Bind(&RunStartWorkerCallback, callback, protect)); |
| 1435 if (running_status() == STOPPED) { |
| 1436 DCHECK(!cache_listener_.get()); |
| 1437 cache_listener_.reset(new ServiceWorkerCacheListener(this, context_)); |
| 1438 embedded_worker_->Start( |
| 1439 version_id_, scope_, script_url_, pause_after_download, |
| 1440 base::Bind(&ServiceWorkerVersion::OnStartSentAndScriptEvaluated, |
| 1441 weak_factory_.GetWeakPtr())); |
| 1442 } |
| 1443 return; |
| 1444 } |
| 1445 } |
| 1446 |
1416 void ServiceWorkerVersion::DidClaimClients( | 1447 void ServiceWorkerVersion::DidClaimClients( |
1417 int request_id, ServiceWorkerStatusCode status) { | 1448 int request_id, ServiceWorkerStatusCode status) { |
1418 if (status == SERVICE_WORKER_ERROR_STATE) { | 1449 if (status == SERVICE_WORKER_ERROR_STATE) { |
1419 embedded_worker_->SendMessage(ServiceWorkerMsg_ClaimClientsError( | 1450 embedded_worker_->SendMessage(ServiceWorkerMsg_ClaimClientsError( |
1420 request_id, blink::WebServiceWorkerError::ErrorTypeState, | 1451 request_id, blink::WebServiceWorkerError::ErrorTypeState, |
1421 base::ASCIIToUTF16(kClaimClientsStateErrorMesage))); | 1452 base::ASCIIToUTF16(kClaimClientsStateErrorMesage))); |
1422 return; | 1453 return; |
1423 } | 1454 } |
1424 if (status == SERVICE_WORKER_ERROR_ABORT) { | 1455 if (status == SERVICE_WORKER_ERROR_ABORT) { |
1425 embedded_worker_->SendMessage(ServiceWorkerMsg_ClaimClientsError( | 1456 embedded_worker_->SendMessage(ServiceWorkerMsg_ClaimClientsError( |
(...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1615 int request_id) { | 1646 int request_id) { |
1616 callbacks->Remove(request_id); | 1647 callbacks->Remove(request_id); |
1617 if (is_doomed_) { | 1648 if (is_doomed_) { |
1618 // The stop should be already scheduled, but try to stop immediately, in | 1649 // The stop should be already scheduled, but try to stop immediately, in |
1619 // order to release worker resources soon. | 1650 // order to release worker resources soon. |
1620 StopWorkerIfIdle(); | 1651 StopWorkerIfIdle(); |
1621 } | 1652 } |
1622 } | 1653 } |
1623 | 1654 |
1624 } // namespace content | 1655 } // namespace content |
OLD | NEW |