| 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 704 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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 DCHECK_EQ(SERVICE_WORKER_PROVIDER_FOR_WINDOW, info.type); |
| 969 provider_host->CompleteNavigationInitialized(render_process_id_, | 968 provider_host->CompleteNavigationInitialized(render_process_id_, |
| 970 std::move(info), this); | 969 std::move(info), this); |
| 971 GetContext()->AddProviderHost(std::move(provider_host)); | 970 GetContext()->AddProviderHost(std::move(provider_host)); |
| 972 } else { | 971 } else { |
| 972 // Provider host for controller should be pre-created on StartWorker in |
| 973 // ServiceWorkerVersion. |
| 974 DCHECK_NE(SERVICE_WORKER_PROVIDER_FOR_CONTROLLER, info.type); |
| 973 if (ServiceWorkerUtils::IsBrowserAssignedProviderId(info.provider_id)) { | 975 if (ServiceWorkerUtils::IsBrowserAssignedProviderId(info.provider_id)) { |
| 974 bad_message::ReceivedBadMessage( | 976 bad_message::ReceivedBadMessage( |
| 975 this, bad_message::SWDH_PROVIDER_CREATED_NO_HOST); | 977 this, bad_message::SWDH_PROVIDER_CREATED_NO_HOST); |
| 976 return; | 978 return; |
| 977 } | 979 } |
| 978 GetContext()->AddProviderHost(ServiceWorkerProviderHost::Create( | 980 GetContext()->AddProviderHost(ServiceWorkerProviderHost::Create( |
| 979 render_process_id_, std::move(info), GetContext()->AsWeakPtr(), this)); | 981 render_process_id_, std::move(info), GetContext()->AsWeakPtr(), this)); |
| 980 } | 982 } |
| 981 } | 983 } |
| 982 | 984 |
| 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> | 985 template <typename SourceInfo> |
| 1066 void ServiceWorkerDispatcherHost::DispatchExtendableMessageEventInternal( | 986 void ServiceWorkerDispatcherHost::DispatchExtendableMessageEventInternal( |
| 1067 scoped_refptr<ServiceWorkerVersion> worker, | 987 scoped_refptr<ServiceWorkerVersion> worker, |
| 1068 const base::string16& message, | 988 const base::string16& message, |
| 1069 const url::Origin& source_origin, | 989 const url::Origin& source_origin, |
| 1070 const std::vector<MessagePort>& sent_message_ports, | 990 const std::vector<MessagePort>& sent_message_ports, |
| 1071 const base::Optional<base::TimeDelta>& timeout, | 991 const base::Optional<base::TimeDelta>& timeout, |
| 1072 const StatusCallback& callback, | 992 const StatusCallback& callback, |
| 1073 const SourceInfo& source_info) { | 993 const SourceInfo& source_info) { |
| 1074 if (!source_info.IsValid()) { | 994 if (!source_info.IsValid()) { |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1171 handle->registration()->id() == registration_id) { | 1091 handle->registration()->id() == registration_id) { |
| 1172 return handle; | 1092 return handle; |
| 1173 } | 1093 } |
| 1174 } | 1094 } |
| 1175 return nullptr; | 1095 return nullptr; |
| 1176 } | 1096 } |
| 1177 | 1097 |
| 1178 void ServiceWorkerDispatcherHost::GetRegistrationObjectInfoAndVersionAttributes( | 1098 void ServiceWorkerDispatcherHost::GetRegistrationObjectInfoAndVersionAttributes( |
| 1179 base::WeakPtr<ServiceWorkerProviderHost> provider_host, | 1099 base::WeakPtr<ServiceWorkerProviderHost> provider_host, |
| 1180 ServiceWorkerRegistration* registration, | 1100 ServiceWorkerRegistration* registration, |
| 1181 ServiceWorkerRegistrationObjectInfo* info, | 1101 ServiceWorkerRegistrationObjectInfo* out_info, |
| 1182 ServiceWorkerVersionAttributes* attrs) { | 1102 ServiceWorkerVersionAttributes* out_attrs) { |
| 1183 ServiceWorkerRegistrationHandle* handle = | 1103 ServiceWorkerRegistrationHandle* handle = |
| 1184 GetOrCreateRegistrationHandle(provider_host, registration); | 1104 GetOrCreateRegistrationHandle(provider_host, registration); |
| 1185 *info = handle->GetObjectInfo(); | 1105 *out_info = handle->GetObjectInfo(); |
| 1186 | 1106 |
| 1187 attrs->installing = provider_host->GetOrCreateServiceWorkerHandle( | 1107 out_attrs->installing = provider_host->GetOrCreateServiceWorkerHandle( |
| 1188 registration->installing_version()); | 1108 registration->installing_version()); |
| 1189 attrs->waiting = provider_host->GetOrCreateServiceWorkerHandle( | 1109 out_attrs->waiting = provider_host->GetOrCreateServiceWorkerHandle( |
| 1190 registration->waiting_version()); | 1110 registration->waiting_version()); |
| 1191 attrs->active = provider_host->GetOrCreateServiceWorkerHandle( | 1111 out_attrs->active = provider_host->GetOrCreateServiceWorkerHandle( |
| 1192 registration->active_version()); | 1112 registration->active_version()); |
| 1193 } | 1113 } |
| 1194 | 1114 |
| 1195 void ServiceWorkerDispatcherHost::RegistrationComplete( | 1115 void ServiceWorkerDispatcherHost::RegistrationComplete( |
| 1196 int thread_id, | 1116 int thread_id, |
| 1197 int provider_id, | 1117 int provider_id, |
| 1198 int request_id, | 1118 int request_id, |
| 1199 ServiceWorkerStatusCode status, | 1119 ServiceWorkerStatusCode status, |
| 1200 const std::string& status_message, | 1120 const std::string& status_message, |
| 1201 int64_t registration_id) { | 1121 int64_t registration_id) { |
| (...skipping 367 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1569 if (!handle) { | 1489 if (!handle) { |
| 1570 bad_message::ReceivedBadMessage(this, | 1490 bad_message::ReceivedBadMessage(this, |
| 1571 bad_message::SWDH_TERMINATE_BAD_HANDLE); | 1491 bad_message::SWDH_TERMINATE_BAD_HANDLE); |
| 1572 return; | 1492 return; |
| 1573 } | 1493 } |
| 1574 handle->version()->StopWorker( | 1494 handle->version()->StopWorker( |
| 1575 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); | 1495 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); |
| 1576 } | 1496 } |
| 1577 | 1497 |
| 1578 } // namespace content | 1498 } // namespace content |
| OLD | NEW |