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_dispatcher_host.h" | 5 #include "content/browser/service_worker/service_worker_dispatcher_host.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/debug/crash_logging.h" | 9 #include "base/debug/crash_logging.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 227 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
238 ServiceWorkerDispatcherHost::GetOrCreateRegistrationHandle( | 238 ServiceWorkerDispatcherHost::GetOrCreateRegistrationHandle( |
239 base::WeakPtr<ServiceWorkerProviderHost> provider_host, | 239 base::WeakPtr<ServiceWorkerProviderHost> provider_host, |
240 ServiceWorkerRegistration* registration) { | 240 ServiceWorkerRegistration* registration) { |
241 DCHECK(provider_host); | 241 DCHECK(provider_host); |
242 ServiceWorkerRegistrationHandle* existing_handle = | 242 ServiceWorkerRegistrationHandle* existing_handle = |
243 FindRegistrationHandle(provider_host->provider_id(), registration->id()); | 243 FindRegistrationHandle(provider_host->provider_id(), registration->id()); |
244 if (existing_handle) { | 244 if (existing_handle) { |
245 existing_handle->IncrementRefCount(); | 245 existing_handle->IncrementRefCount(); |
246 return existing_handle; | 246 return existing_handle; |
247 } | 247 } |
248 | |
249 std::unique_ptr<ServiceWorkerRegistrationHandle> new_handle( | 248 std::unique_ptr<ServiceWorkerRegistrationHandle> new_handle( |
250 new ServiceWorkerRegistrationHandle(GetContext()->AsWeakPtr(), | 249 new ServiceWorkerRegistrationHandle(GetContext()->AsWeakPtr(), |
251 provider_host, registration)); | 250 provider_host, registration)); |
252 ServiceWorkerRegistrationHandle* new_handle_ptr = new_handle.get(); | 251 ServiceWorkerRegistrationHandle* new_handle_ptr = new_handle.get(); |
253 RegisterServiceWorkerRegistrationHandle(std::move(new_handle)); | 252 RegisterServiceWorkerRegistrationHandle(std::move(new_handle)); |
254 return new_handle_ptr; | 253 return new_handle_ptr; |
255 } | 254 } |
256 | 255 |
257 void ServiceWorkerDispatcherHost::OnRegisterServiceWorker( | 256 void ServiceWorkerDispatcherHost::OnRegisterServiceWorker( |
258 int thread_id, | 257 int thread_id, |
(...skipping 699 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
958 | 957 |
959 // If no host is found, create one. | 958 // If no host is found, create one. |
960 if (provider_host == nullptr) { | 959 if (provider_host == nullptr) { |
961 GetContext()->AddProviderHost( | 960 GetContext()->AddProviderHost( |
962 ServiceWorkerProviderHost::Create(render_process_id_, std::move(info), | 961 ServiceWorkerProviderHost::Create(render_process_id_, std::move(info), |
963 GetContext()->AsWeakPtr(), this)); | 962 GetContext()->AsWeakPtr(), this)); |
964 return; | 963 return; |
965 } | 964 } |
966 | 965 |
967 // Otherwise, completed the initialization of the pre-created host. | 966 // Otherwise, completed the initialization of the pre-created host. |
968 DCHECK_EQ(SERVICE_WORKER_PROVIDER_FOR_WINDOW, info.type); | 967 if (info.type != SERVICE_WORKER_PROVIDER_FOR_WINDOW) { |
| 968 bad_message::ReceivedBadMessage( |
| 969 this, bad_message::SWDH_PROVIDER_CREATED_ILLEGAL_TYPE); |
| 970 return; |
| 971 } |
969 provider_host->CompleteNavigationInitialized(render_process_id_, | 972 provider_host->CompleteNavigationInitialized(render_process_id_, |
970 std::move(info), this); | 973 std::move(info), this); |
971 GetContext()->AddProviderHost(std::move(provider_host)); | 974 GetContext()->AddProviderHost(std::move(provider_host)); |
972 } else { | 975 } else { |
| 976 // Provider host for controller should be pre-created on StartWorker in |
| 977 // ServiceWorkerVersion. |
| 978 if (info.type == SERVICE_WORKER_PROVIDER_FOR_CONTROLLER) { |
| 979 bad_message::ReceivedBadMessage( |
| 980 this, bad_message::SWDH_PROVIDER_CREATED_ILLEGAL_TYPE); |
| 981 return; |
| 982 } |
973 if (ServiceWorkerUtils::IsBrowserAssignedProviderId(info.provider_id)) { | 983 if (ServiceWorkerUtils::IsBrowserAssignedProviderId(info.provider_id)) { |
974 bad_message::ReceivedBadMessage( | 984 bad_message::ReceivedBadMessage( |
975 this, bad_message::SWDH_PROVIDER_CREATED_NO_HOST); | 985 this, bad_message::SWDH_PROVIDER_CREATED_NO_HOST); |
976 return; | 986 return; |
977 } | 987 } |
978 GetContext()->AddProviderHost(ServiceWorkerProviderHost::Create( | 988 GetContext()->AddProviderHost(ServiceWorkerProviderHost::Create( |
979 render_process_id_, std::move(info), GetContext()->AsWeakPtr(), this)); | 989 render_process_id_, std::move(info), GetContext()->AsWeakPtr(), this)); |
980 } | 990 } |
981 } | 991 } |
982 | 992 |
983 void ServiceWorkerDispatcherHost::OnSetHostedVersionId(int provider_id, | |
984 int64_t version_id, | |
985 int embedded_worker_id) { | |
986 TRACE_EVENT0("ServiceWorker", | |
987 "ServiceWorkerDispatcherHost::OnSetHostedVersionId"); | |
988 if (!GetContext()) | |
989 return; | |
990 ServiceWorkerProviderHost* provider_host = | |
991 GetContext()->GetProviderHost(render_process_id_, provider_id); | |
992 if (!provider_host) { | |
993 bad_message::ReceivedBadMessage( | |
994 this, bad_message::SWDH_SET_HOSTED_VERSION_NO_HOST); | |
995 return; | |
996 } | |
997 | |
998 // This provider host must be specialized for a controller. | |
999 if (provider_host->IsProviderForClient()) { | |
1000 bad_message::ReceivedBadMessage( | |
1001 this, bad_message::SWDH_SET_HOSTED_VERSION_INVALID_HOST); | |
1002 return; | |
1003 } | |
1004 | |
1005 // A service worker context associated with this provider host was destroyed | |
1006 // due to restarting the service worker system etc. | |
1007 if (!provider_host->IsContextAlive()) | |
1008 return; | |
1009 | |
1010 // We might not be STARTING if the stop sequence was entered (STOPPING) or | |
1011 // ended up being detached (STOPPED). | |
1012 ServiceWorkerVersion* version = GetContext()->GetLiveVersion(version_id); | |
1013 if (!version || version->running_status() != EmbeddedWorkerStatus::STARTING) | |
1014 return; | |
1015 | |
1016 // If the version has a different embedded worker, assume the message is about | |
1017 // a detached worker and ignore. | |
1018 if (version->embedded_worker()->embedded_worker_id() != embedded_worker_id) | |
1019 return; | |
1020 | |
1021 // A process for the worker must be equal to a process for the provider host. | |
1022 if (version->embedded_worker()->process_id() != provider_host->process_id()) { | |
1023 // Temporary debugging for https://crbug.com/668633 | |
1024 base::debug::ScopedCrashKey scope_worker_pid( | |
1025 "swdh_set_hosted_version_worker_pid", | |
1026 base::IntToString(version->embedded_worker()->process_id())); | |
1027 base::debug::ScopedCrashKey scope_provider_host_pid( | |
1028 "swdh_set_hosted_version_host_pid", | |
1029 base::IntToString(provider_host->process_id())); | |
1030 if (version->embedded_worker()->process_id() != | |
1031 ChildProcessHost::kInvalidUniqueID) { | |
1032 base::debug::ScopedCrashKey scope_is_new_process( | |
1033 "swdh_set_hosted_version_is_new_process", | |
1034 version->embedded_worker()->is_new_process() ? "true" : "false"); | |
1035 } | |
1036 base::debug::ScopedCrashKey scope_worker_restart_count( | |
1037 "swdh_set_hosted_version_restart_count", | |
1038 base::IntToString(version->embedded_worker()->restart_count())); | |
1039 bad_message::ReceivedBadMessage( | |
1040 this, bad_message::SWDH_SET_HOSTED_VERSION_PROCESS_MISMATCH); | |
1041 return; | |
1042 } | |
1043 | |
1044 provider_host->SetHostedVersion(version); | |
1045 | |
1046 // Retrieve the registration associated with |version|. The registration | |
1047 // must be alive because the version keeps it during starting worker. | |
1048 ServiceWorkerRegistration* registration = | |
1049 GetContext()->GetLiveRegistration(version->registration_id()); | |
1050 DCHECK(registration); | |
1051 | |
1052 // Set the document URL to the script url in order to allow | |
1053 // register/unregister/getRegistration on ServiceWorkerGlobalScope. | |
1054 provider_host->SetDocumentUrl(version->script_url()); | |
1055 | |
1056 ServiceWorkerRegistrationObjectInfo info; | |
1057 ServiceWorkerVersionAttributes attrs; | |
1058 GetRegistrationObjectInfoAndVersionAttributes( | |
1059 provider_host->AsWeakPtr(), registration, &info, &attrs); | |
1060 | |
1061 Send(new ServiceWorkerMsg_AssociateRegistration(kDocumentMainThreadId, | |
1062 provider_id, info, attrs)); | |
1063 } | |
1064 | |
1065 template <typename SourceInfo> | 993 template <typename SourceInfo> |
1066 void ServiceWorkerDispatcherHost::DispatchExtendableMessageEventInternal( | 994 void ServiceWorkerDispatcherHost::DispatchExtendableMessageEventInternal( |
1067 scoped_refptr<ServiceWorkerVersion> worker, | 995 scoped_refptr<ServiceWorkerVersion> worker, |
1068 const base::string16& message, | 996 const base::string16& message, |
1069 const url::Origin& source_origin, | 997 const url::Origin& source_origin, |
1070 const std::vector<MessagePort>& sent_message_ports, | 998 const std::vector<MessagePort>& sent_message_ports, |
1071 const base::Optional<base::TimeDelta>& timeout, | 999 const base::Optional<base::TimeDelta>& timeout, |
1072 const StatusCallback& callback, | 1000 const StatusCallback& callback, |
1073 const SourceInfo& source_info) { | 1001 const SourceInfo& source_info) { |
1074 if (!source_info.IsValid()) { | 1002 if (!source_info.IsValid()) { |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1171 handle->registration()->id() == registration_id) { | 1099 handle->registration()->id() == registration_id) { |
1172 return handle; | 1100 return handle; |
1173 } | 1101 } |
1174 } | 1102 } |
1175 return nullptr; | 1103 return nullptr; |
1176 } | 1104 } |
1177 | 1105 |
1178 void ServiceWorkerDispatcherHost::GetRegistrationObjectInfoAndVersionAttributes( | 1106 void ServiceWorkerDispatcherHost::GetRegistrationObjectInfoAndVersionAttributes( |
1179 base::WeakPtr<ServiceWorkerProviderHost> provider_host, | 1107 base::WeakPtr<ServiceWorkerProviderHost> provider_host, |
1180 ServiceWorkerRegistration* registration, | 1108 ServiceWorkerRegistration* registration, |
1181 ServiceWorkerRegistrationObjectInfo* info, | 1109 ServiceWorkerRegistrationObjectInfo* out_info, |
1182 ServiceWorkerVersionAttributes* attrs) { | 1110 ServiceWorkerVersionAttributes* out_attrs) { |
1183 ServiceWorkerRegistrationHandle* handle = | 1111 ServiceWorkerRegistrationHandle* handle = |
1184 GetOrCreateRegistrationHandle(provider_host, registration); | 1112 GetOrCreateRegistrationHandle(provider_host, registration); |
1185 *info = handle->GetObjectInfo(); | 1113 *out_info = handle->GetObjectInfo(); |
1186 | 1114 |
1187 attrs->installing = provider_host->GetOrCreateServiceWorkerHandle( | 1115 out_attrs->installing = provider_host->GetOrCreateServiceWorkerHandle( |
1188 registration->installing_version()); | 1116 registration->installing_version()); |
1189 attrs->waiting = provider_host->GetOrCreateServiceWorkerHandle( | 1117 out_attrs->waiting = provider_host->GetOrCreateServiceWorkerHandle( |
1190 registration->waiting_version()); | 1118 registration->waiting_version()); |
1191 attrs->active = provider_host->GetOrCreateServiceWorkerHandle( | 1119 out_attrs->active = provider_host->GetOrCreateServiceWorkerHandle( |
1192 registration->active_version()); | 1120 registration->active_version()); |
1193 } | 1121 } |
1194 | 1122 |
1195 void ServiceWorkerDispatcherHost::RegistrationComplete( | 1123 void ServiceWorkerDispatcherHost::RegistrationComplete( |
1196 int thread_id, | 1124 int thread_id, |
1197 int provider_id, | 1125 int provider_id, |
1198 int request_id, | 1126 int request_id, |
1199 ServiceWorkerStatusCode status, | 1127 ServiceWorkerStatusCode status, |
1200 const std::string& status_message, | 1128 const std::string& status_message, |
1201 int64_t registration_id) { | 1129 int64_t registration_id) { |
(...skipping 367 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1569 if (!handle) { | 1497 if (!handle) { |
1570 bad_message::ReceivedBadMessage(this, | 1498 bad_message::ReceivedBadMessage(this, |
1571 bad_message::SWDH_TERMINATE_BAD_HANDLE); | 1499 bad_message::SWDH_TERMINATE_BAD_HANDLE); |
1572 return; | 1500 return; |
1573 } | 1501 } |
1574 handle->version()->StopWorker( | 1502 handle->version()->StopWorker( |
1575 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); | 1503 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); |
1576 } | 1504 } |
1577 | 1505 |
1578 } // namespace content | 1506 } // namespace content |
OLD | NEW |