| 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 434 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 445 } | 445 } |
| 446 | 446 |
| 447 void ServiceWorkerVersion::StartWorker(const StatusCallback& callback) { | 447 void ServiceWorkerVersion::StartWorker(const StatusCallback& callback) { |
| 448 StartWorker(false, callback); | 448 StartWorker(false, callback); |
| 449 } | 449 } |
| 450 | 450 |
| 451 void ServiceWorkerVersion::StartWorker( | 451 void ServiceWorkerVersion::StartWorker( |
| 452 bool pause_after_download, | 452 bool pause_after_download, |
| 453 const StatusCallback& callback) { | 453 const StatusCallback& callback) { |
| 454 if (!context_) { | 454 if (!context_) { |
| 455 RunSoon(base::Bind(callback, SERVICE_WORKER_ERROR_START_WORKER_FAILED)); | 455 RunSoon(base::Bind(callback, SERVICE_WORKER_ERROR_ABORT)); |
| 456 return; | 456 return; |
| 457 } | 457 } |
| 458 | 458 |
| 459 // Ensure the live registration during starting worker so that the worker can | 459 // Ensure the live registration during starting worker so that the worker can |
| 460 // get associated with it in SWDispatcherHost::OnSetHostedVersionId(). | 460 // get associated with it in SWDispatcherHost::OnSetHostedVersionId(). |
| 461 context_->storage()->FindRegistrationForId( | 461 context_->storage()->FindRegistrationForId( |
| 462 registration_id_, | 462 registration_id_, |
| 463 scope_.GetOrigin(), | 463 scope_.GetOrigin(), |
| 464 base::Bind(&ServiceWorkerVersion::DidEnsureLiveRegistrationForStartWorker, | 464 base::Bind(&ServiceWorkerVersion::DidEnsureLiveRegistrationForStartWorker, |
| 465 weak_factory_.GetWeakPtr(), | 465 weak_factory_.GetWeakPtr(), |
| (...skipping 469 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 935 StopTimeoutTimer(); | 935 StopTimeoutTimer(); |
| 936 if (ping_state_ == PING_TIMED_OUT) | 936 if (ping_state_ == PING_TIMED_OUT) |
| 937 should_restart = false; | 937 should_restart = false; |
| 938 | 938 |
| 939 // Fire all stop callbacks. | 939 // Fire all stop callbacks. |
| 940 RunCallbacks(this, &stop_callbacks_, SERVICE_WORKER_OK); | 940 RunCallbacks(this, &stop_callbacks_, SERVICE_WORKER_OK); |
| 941 | 941 |
| 942 if (!should_restart) { | 942 if (!should_restart) { |
| 943 // Let all start callbacks fail. | 943 // Let all start callbacks fail. |
| 944 RunCallbacks(this, &start_callbacks_, | 944 RunCallbacks(this, &start_callbacks_, |
| 945 SERVICE_WORKER_ERROR_START_WORKER_FAILED); | 945 DeduceStartWorkerFailureReason( |
| 946 SERVICE_WORKER_ERROR_START_WORKER_FAILED)); |
| 946 } | 947 } |
| 947 | 948 |
| 948 // Let all message callbacks fail (this will also fire and clear all | 949 // Let all message callbacks fail (this will also fire and clear all |
| 949 // callbacks for events). | 950 // callbacks for events). |
| 950 // TODO(kinuko): Consider if we want to add queue+resend mechanism here. | 951 // TODO(kinuko): Consider if we want to add queue+resend mechanism here. |
| 951 RunIDMapCallbacks(&activate_callbacks_, | 952 RunIDMapCallbacks(&activate_callbacks_, |
| 952 SERVICE_WORKER_ERROR_ACTIVATE_WORKER_FAILED); | 953 SERVICE_WORKER_ERROR_ACTIVATE_WORKER_FAILED); |
| 953 RunIDMapCallbacks(&install_callbacks_, | 954 RunIDMapCallbacks(&install_callbacks_, |
| 954 SERVICE_WORKER_ERROR_INSTALL_WORKER_FAILED); | 955 SERVICE_WORKER_ERROR_INSTALL_WORKER_FAILED); |
| 955 RunIDMapCallbacks(&fetch_callbacks_, | 956 RunIDMapCallbacks(&fetch_callbacks_, |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1040 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_ClaimClients, | 1041 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_ClaimClients, |
| 1041 OnClaimClients) | 1042 OnClaimClients) |
| 1042 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_Pong, OnPongFromWorker) | 1043 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_Pong, OnPongFromWorker) |
| 1043 IPC_MESSAGE_UNHANDLED(handled = false) | 1044 IPC_MESSAGE_UNHANDLED(handled = false) |
| 1044 IPC_END_MESSAGE_MAP() | 1045 IPC_END_MESSAGE_MAP() |
| 1045 return handled; | 1046 return handled; |
| 1046 } | 1047 } |
| 1047 | 1048 |
| 1048 void ServiceWorkerVersion::OnStartSentAndScriptEvaluated( | 1049 void ServiceWorkerVersion::OnStartSentAndScriptEvaluated( |
| 1049 ServiceWorkerStatusCode status) { | 1050 ServiceWorkerStatusCode status) { |
| 1050 if (status != SERVICE_WORKER_OK) | 1051 if (status != SERVICE_WORKER_OK) { |
| 1051 RunCallbacks(this, &start_callbacks_, status); | 1052 RunCallbacks(this, &start_callbacks_, |
| 1053 DeduceStartWorkerFailureReason(status)); |
| 1054 } |
| 1052 } | 1055 } |
| 1053 | 1056 |
| 1054 void ServiceWorkerVersion::DispatchInstallEventAfterStartWorker( | 1057 void ServiceWorkerVersion::DispatchInstallEventAfterStartWorker( |
| 1055 const StatusCallback& callback) { | 1058 const StatusCallback& callback) { |
| 1056 DCHECK_EQ(RUNNING, running_status()) | 1059 DCHECK_EQ(RUNNING, running_status()) |
| 1057 << "Worker stopped too soon after it was started."; | 1060 << "Worker stopped too soon after it was started."; |
| 1058 | 1061 |
| 1059 int request_id = AddRequest(callback, &install_callbacks_, REQUEST_INSTALL); | 1062 int request_id = AddRequest(callback, &install_callbacks_, REQUEST_INSTALL); |
| 1060 ServiceWorkerStatusCode status = embedded_worker_->SendMessage( | 1063 ServiceWorkerStatusCode status = embedded_worker_->SendMessage( |
| 1061 ServiceWorkerMsg_InstallEvent(request_id)); | 1064 ServiceWorkerMsg_InstallEvent(request_id)); |
| (...skipping 793 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1855 std::queue<RequestInfo> new_requests; | 1858 std::queue<RequestInfo> new_requests; |
| 1856 while (!requests_.empty()) { | 1859 while (!requests_.empty()) { |
| 1857 RequestInfo info = requests_.front(); | 1860 RequestInfo info = requests_.front(); |
| 1858 info.time = ticks; | 1861 info.time = ticks; |
| 1859 new_requests.push(info); | 1862 new_requests.push(info); |
| 1860 requests_.pop(); | 1863 requests_.pop(); |
| 1861 } | 1864 } |
| 1862 requests_ = new_requests; | 1865 requests_ = new_requests; |
| 1863 } | 1866 } |
| 1864 | 1867 |
| 1868 ServiceWorkerStatusCode ServiceWorkerVersion::DeduceStartWorkerFailureReason( |
| 1869 ServiceWorkerStatusCode default_code) { |
| 1870 if (ping_state_ == PING_TIMED_OUT) |
| 1871 return SERVICE_WORKER_ERROR_TIMEOUT; |
| 1872 |
| 1873 const net::URLRequestStatus& main_script_status = |
| 1874 script_cache_map()->main_script_status(); |
| 1875 if (main_script_status.status() != net::URLRequestStatus::SUCCESS) { |
| 1876 switch (main_script_status.error()) { |
| 1877 case net::ERR_INSECURE_RESPONSE: |
| 1878 case net::ERR_UNSAFE_REDIRECT: |
| 1879 return SERVICE_WORKER_ERROR_SECURITY; |
| 1880 case net::ERR_ABORTED: |
| 1881 return SERVICE_WORKER_ERROR_ABORT; |
| 1882 default: |
| 1883 return SERVICE_WORKER_ERROR_NETWORK; |
| 1884 } |
| 1885 } |
| 1886 |
| 1887 return default_code; |
| 1888 } |
| 1889 |
| 1865 } // namespace content | 1890 } // namespace content |
| OLD | NEW |