| 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/stl_util.h" | 9 #include "base/stl_util.h" |
| 10 #include "base/strings/string16.h" | 10 #include "base/strings/string16.h" |
| (...skipping 18 matching lines...) Expand all Loading... |
| 29 public: | 29 public: |
| 30 GetClientDocumentsCallback(int request_id, | 30 GetClientDocumentsCallback(int request_id, |
| 31 ServiceWorkerVersion* version) | 31 ServiceWorkerVersion* version) |
| 32 : request_id_(request_id), | 32 : request_id_(request_id), |
| 33 version_(version) { | 33 version_(version) { |
| 34 DCHECK(version_); | 34 DCHECK(version_); |
| 35 } | 35 } |
| 36 | 36 |
| 37 void AddClientInfo(int client_id, const ServiceWorkerClientInfo& info) { | 37 void AddClientInfo(int client_id, const ServiceWorkerClientInfo& info) { |
| 38 clients_.push_back(info); | 38 clients_.push_back(info); |
| 39 clients_.back().client_id = client_id; | 39 clients_.back().SetClientID(client_id); |
| 40 } | 40 } |
| 41 | 41 |
| 42 private: | 42 private: |
| 43 friend class base::RefCounted<GetClientDocumentsCallback>; | 43 friend class base::RefCounted<GetClientDocumentsCallback>; |
| 44 | 44 |
| 45 virtual ~GetClientDocumentsCallback() { | 45 virtual ~GetClientDocumentsCallback() { |
| 46 if (version_->running_status() == RUNNING) { | 46 if (version_->running_status() == RUNNING) { |
| 47 version_->embedded_worker_->SendMessage( | 47 version_->embedded_worker_->SendMessage( |
| 48 ServiceWorkerMsg_DidGetClientDocuments(request_id_, clients_)); | 48 ServiceWorkerMsg_DidGetClientDocuments(request_id_, clients_)); |
| 49 } | 49 } |
| (...skipping 659 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 709 SERVICE_WORKER_FETCH_EVENT_RESULT_FALLBACK, | 709 SERVICE_WORKER_FETCH_EVENT_RESULT_FALLBACK, |
| 710 ServiceWorkerResponse()); | 710 ServiceWorkerResponse()); |
| 711 RunIDMapCallbacks(&sync_callbacks_, | 711 RunIDMapCallbacks(&sync_callbacks_, |
| 712 SERVICE_WORKER_ERROR_FAILED); | 712 SERVICE_WORKER_ERROR_FAILED); |
| 713 RunIDMapCallbacks(¬ification_click_callbacks_, | 713 RunIDMapCallbacks(¬ification_click_callbacks_, |
| 714 SERVICE_WORKER_ERROR_FAILED); | 714 SERVICE_WORKER_ERROR_FAILED); |
| 715 RunIDMapCallbacks(&push_callbacks_, | 715 RunIDMapCallbacks(&push_callbacks_, |
| 716 SERVICE_WORKER_ERROR_FAILED); | 716 SERVICE_WORKER_ERROR_FAILED); |
| 717 RunIDMapCallbacks(&geofencing_callbacks_, | 717 RunIDMapCallbacks(&geofencing_callbacks_, |
| 718 SERVICE_WORKER_ERROR_FAILED); | 718 SERVICE_WORKER_ERROR_FAILED); |
| 719 RunIDMapCallbacks(&get_client_info_callbacks_, | |
| 720 SERVICE_WORKER_ERROR_FAILED, | |
| 721 ServiceWorkerClientInfo()); | |
| 722 RunIDMapCallbacks(&cross_origin_connect_callbacks_, | 719 RunIDMapCallbacks(&cross_origin_connect_callbacks_, |
| 723 SERVICE_WORKER_ERROR_FAILED, | 720 SERVICE_WORKER_ERROR_FAILED, |
| 724 false); | 721 false); |
| 725 | 722 |
| 726 streaming_url_request_jobs_.clear(); | 723 streaming_url_request_jobs_.clear(); |
| 727 | 724 |
| 728 FOR_EACH_OBSERVER(Listener, listeners_, OnWorkerStopped(this)); | 725 FOR_EACH_OBSERVER(Listener, listeners_, OnWorkerStopped(this)); |
| 729 | 726 |
| 730 // There should be no more communication from/to a stopped worker. Deleting | 727 // There should be no more communication from/to a stopped worker. Deleting |
| 731 // the listener prevents any pending completion callbacks from causing | 728 // the listener prevents any pending completion callbacks from causing |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 787 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_PushEventFinished, | 784 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_PushEventFinished, |
| 788 OnPushEventFinished) | 785 OnPushEventFinished) |
| 789 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_GeofencingEventFinished, | 786 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_GeofencingEventFinished, |
| 790 OnGeofencingEventFinished) | 787 OnGeofencingEventFinished) |
| 791 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_CrossOriginConnectEventFinished, | 788 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_CrossOriginConnectEventFinished, |
| 792 OnCrossOriginConnectEventFinished) | 789 OnCrossOriginConnectEventFinished) |
| 793 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_PostMessageToDocument, | 790 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_PostMessageToDocument, |
| 794 OnPostMessageToDocument) | 791 OnPostMessageToDocument) |
| 795 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_FocusClient, | 792 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_FocusClient, |
| 796 OnFocusClient) | 793 OnFocusClient) |
| 797 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_GetClientInfoSuccess, | |
| 798 OnGetClientInfoSuccess) | |
| 799 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_GetClientInfoError, | |
| 800 OnGetClientInfoError) | |
| 801 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_SkipWaiting, | 794 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_SkipWaiting, |
| 802 OnSkipWaiting) | 795 OnSkipWaiting) |
| 803 IPC_MESSAGE_UNHANDLED(handled = false) | 796 IPC_MESSAGE_UNHANDLED(handled = false) |
| 804 IPC_END_MESSAGE_MAP() | 797 IPC_END_MESSAGE_MAP() |
| 805 return handled; | 798 return handled; |
| 806 } | 799 } |
| 807 | 800 |
| 808 void ServiceWorkerVersion::OnStartMessageSent( | 801 void ServiceWorkerVersion::OnStartMessageSent( |
| 809 ServiceWorkerStatusCode status) { | 802 ServiceWorkerStatusCode status) { |
| 810 if (status != SERVICE_WORKER_OK) | 803 if (status != SERVICE_WORKER_OK) |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 848 std::vector<ServiceWorkerClientInfo>())); | 841 std::vector<ServiceWorkerClientInfo>())); |
| 849 } | 842 } |
| 850 return; | 843 return; |
| 851 } | 844 } |
| 852 scoped_refptr<GetClientDocumentsCallback> callback( | 845 scoped_refptr<GetClientDocumentsCallback> callback( |
| 853 new GetClientDocumentsCallback(request_id, this)); | 846 new GetClientDocumentsCallback(request_id, this)); |
| 854 ControlleeByIDMap::iterator it(&controllee_by_id_); | 847 ControlleeByIDMap::iterator it(&controllee_by_id_); |
| 855 TRACE_EVENT0("ServiceWorker", | 848 TRACE_EVENT0("ServiceWorker", |
| 856 "ServiceWorkerVersion::OnGetClientDocuments"); | 849 "ServiceWorkerVersion::OnGetClientDocuments"); |
| 857 while (!it.IsAtEnd()) { | 850 while (!it.IsAtEnd()) { |
| 858 int client_request_id = get_client_info_callbacks_.Add( | 851 it.GetCurrentValue()->GetClientInfo( |
| 859 new GetClientInfoCallback(base::Bind( | 852 base::Bind(&ServiceWorkerVersion::DidGetClientInfo, |
| 860 &ServiceWorkerVersion::DidGetClientInfo, | 853 weak_factory_.GetWeakPtr(), it.GetCurrentKey(), callback)); |
| 861 weak_factory_.GetWeakPtr(), it.GetCurrentKey(), callback))); | |
| 862 it.GetCurrentValue()->GetClientInfo(embedded_worker_->embedded_worker_id(), | |
| 863 client_request_id); | |
| 864 it.Advance(); | 854 it.Advance(); |
| 865 } | 855 } |
| 866 } | 856 } |
| 867 | 857 |
| 868 void ServiceWorkerVersion::OnGetClientInfoSuccess( | |
| 869 int request_id, | |
| 870 const ServiceWorkerClientInfo& info) { | |
| 871 GetClientInfoCallback* callback = | |
| 872 get_client_info_callbacks_.Lookup(request_id); | |
| 873 if (!callback) { | |
| 874 // The callback may already have been cleared by OnStopped, just ignore. | |
| 875 return; | |
| 876 } | |
| 877 callback->Run(SERVICE_WORKER_OK, info); | |
| 878 RemoveCallbackAndStopIfDoomed(&get_client_info_callbacks_, request_id); | |
| 879 } | |
| 880 | |
| 881 void ServiceWorkerVersion::OnGetClientInfoError(int request_id) { | |
| 882 GetClientInfoCallback* callback = | |
| 883 get_client_info_callbacks_.Lookup(request_id); | |
| 884 if (!callback) { | |
| 885 // The callback may already have been cleared by OnStopped, just ignore. | |
| 886 return; | |
| 887 } | |
| 888 callback->Run(SERVICE_WORKER_ERROR_FAILED, ServiceWorkerClientInfo()); | |
| 889 RemoveCallbackAndStopIfDoomed(&get_client_info_callbacks_, request_id); | |
| 890 } | |
| 891 | |
| 892 void ServiceWorkerVersion::OnActivateEventFinished( | 858 void ServiceWorkerVersion::OnActivateEventFinished( |
| 893 int request_id, | 859 int request_id, |
| 894 blink::WebServiceWorkerEventResult result) { | 860 blink::WebServiceWorkerEventResult result) { |
| 895 DCHECK(ACTIVATING == status() || | 861 DCHECK(ACTIVATING == status() || |
| 896 REDUNDANT == status()) << status(); | 862 REDUNDANT == status()) << status(); |
| 897 TRACE_EVENT0("ServiceWorker", | 863 TRACE_EVENT0("ServiceWorker", |
| 898 "ServiceWorkerVersion::OnActivateEventFinished"); | 864 "ServiceWorkerVersion::OnActivateEventFinished"); |
| 899 | 865 |
| 900 StatusCallback* callback = activate_callbacks_.Lookup(request_id); | 866 StatusCallback* callback = activate_callbacks_.Lookup(request_id); |
| 901 if (!callback) { | 867 if (!callback) { |
| (...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1101 } | 1067 } |
| 1102 | 1068 |
| 1103 void ServiceWorkerVersion::DidSkipWaiting(int request_id) { | 1069 void ServiceWorkerVersion::DidSkipWaiting(int request_id) { |
| 1104 if (running_status() == STARTING || running_status() == RUNNING) | 1070 if (running_status() == STARTING || running_status() == RUNNING) |
| 1105 embedded_worker_->SendMessage(ServiceWorkerMsg_DidSkipWaiting(request_id)); | 1071 embedded_worker_->SendMessage(ServiceWorkerMsg_DidSkipWaiting(request_id)); |
| 1106 } | 1072 } |
| 1107 | 1073 |
| 1108 void ServiceWorkerVersion::DidGetClientInfo( | 1074 void ServiceWorkerVersion::DidGetClientInfo( |
| 1109 int client_id, | 1075 int client_id, |
| 1110 scoped_refptr<GetClientDocumentsCallback> callback, | 1076 scoped_refptr<GetClientDocumentsCallback> callback, |
| 1111 ServiceWorkerStatusCode status, | |
| 1112 const ServiceWorkerClientInfo& info) { | 1077 const ServiceWorkerClientInfo& info) { |
| 1113 if (status == SERVICE_WORKER_OK) | 1078 DCHECK(!info.IsEmpty()); |
| 1114 callback->AddClientInfo(client_id, info); | 1079 callback->AddClientInfo(client_id, info); |
| 1115 } | 1080 } |
| 1116 | 1081 |
| 1117 void ServiceWorkerVersion::ScheduleStopWorker() { | 1082 void ServiceWorkerVersion::ScheduleStopWorker() { |
| 1118 if (running_status() != RUNNING) | 1083 if (running_status() != RUNNING) |
| 1119 return; | 1084 return; |
| 1120 stop_worker_timer_.Stop(); | 1085 stop_worker_timer_.Stop(); |
| 1121 stop_worker_timer_.Start( | 1086 stop_worker_timer_.Start( |
| 1122 FROM_HERE, base::TimeDelta::FromSeconds( | 1087 FROM_HERE, base::TimeDelta::FromSeconds( |
| 1123 is_doomed_ ? kStopDoomedWorkerDelay : kStopWorkerDelay), | 1088 is_doomed_ ? kStopDoomedWorkerDelay : kStopWorkerDelay), |
| 1124 base::Bind(&ServiceWorkerVersion::StopWorkerIfIdle, | 1089 base::Bind(&ServiceWorkerVersion::StopWorkerIfIdle, |
| (...skipping 17 matching lines...) Expand all Loading... |
| 1142 | 1107 |
| 1143 bool ServiceWorkerVersion::HasInflightRequests() const { | 1108 bool ServiceWorkerVersion::HasInflightRequests() const { |
| 1144 return | 1109 return |
| 1145 !activate_callbacks_.IsEmpty() || | 1110 !activate_callbacks_.IsEmpty() || |
| 1146 !install_callbacks_.IsEmpty() || | 1111 !install_callbacks_.IsEmpty() || |
| 1147 !fetch_callbacks_.IsEmpty() || | 1112 !fetch_callbacks_.IsEmpty() || |
| 1148 !sync_callbacks_.IsEmpty() || | 1113 !sync_callbacks_.IsEmpty() || |
| 1149 !notification_click_callbacks_.IsEmpty() || | 1114 !notification_click_callbacks_.IsEmpty() || |
| 1150 !push_callbacks_.IsEmpty() || | 1115 !push_callbacks_.IsEmpty() || |
| 1151 !geofencing_callbacks_.IsEmpty() || | 1116 !geofencing_callbacks_.IsEmpty() || |
| 1152 !get_client_info_callbacks_.IsEmpty() || | |
| 1153 !cross_origin_connect_callbacks_.IsEmpty() || | 1117 !cross_origin_connect_callbacks_.IsEmpty() || |
| 1154 !streaming_url_request_jobs_.empty(); | 1118 !streaming_url_request_jobs_.empty(); |
| 1155 } | 1119 } |
| 1156 | 1120 |
| 1157 void ServiceWorkerVersion::DoomInternal() { | 1121 void ServiceWorkerVersion::DoomInternal() { |
| 1158 DCHECK(is_doomed_); | 1122 DCHECK(is_doomed_); |
| 1159 DCHECK(!HasControllee()); | 1123 DCHECK(!HasControllee()); |
| 1160 SetStatus(REDUNDANT); | 1124 SetStatus(REDUNDANT); |
| 1161 StopWorkerIfIdle(); | 1125 StopWorkerIfIdle(); |
| 1162 if (!context_) | 1126 if (!context_) |
| 1163 return; | 1127 return; |
| 1164 std::vector<ServiceWorkerDatabase::ResourceRecord> resources; | 1128 std::vector<ServiceWorkerDatabase::ResourceRecord> resources; |
| 1165 script_cache_map_.GetResources(&resources); | 1129 script_cache_map_.GetResources(&resources); |
| 1166 context_->storage()->PurgeResources(resources); | 1130 context_->storage()->PurgeResources(resources); |
| 1167 } | 1131 } |
| 1168 | 1132 |
| 1169 template <typename IDMAP> | 1133 template <typename IDMAP> |
| 1170 void ServiceWorkerVersion::RemoveCallbackAndStopIfDoomed( | 1134 void ServiceWorkerVersion::RemoveCallbackAndStopIfDoomed( |
| 1171 IDMAP* callbacks, | 1135 IDMAP* callbacks, |
| 1172 int request_id) { | 1136 int request_id) { |
| 1173 callbacks->Remove(request_id); | 1137 callbacks->Remove(request_id); |
| 1174 if (is_doomed_) { | 1138 if (is_doomed_) { |
| 1175 // The stop should be already scheduled, but try to stop immediately, in | 1139 // The stop should be already scheduled, but try to stop immediately, in |
| 1176 // order to release worker resources soon. | 1140 // order to release worker resources soon. |
| 1177 StopWorkerIfIdle(); | 1141 StopWorkerIfIdle(); |
| 1178 } | 1142 } |
| 1179 } | 1143 } |
| 1180 | 1144 |
| 1181 } // namespace content | 1145 } // namespace content |
| OLD | NEW |