| 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 217 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 228 ServiceWorkerDispatcherHost::GetOrCreateRegistrationHandle( | 228 ServiceWorkerDispatcherHost::GetOrCreateRegistrationHandle( |
| 229 base::WeakPtr<ServiceWorkerProviderHost> provider_host, | 229 base::WeakPtr<ServiceWorkerProviderHost> provider_host, |
| 230 ServiceWorkerRegistration* registration) { | 230 ServiceWorkerRegistration* registration) { |
| 231 DCHECK(provider_host); | 231 DCHECK(provider_host); |
| 232 ServiceWorkerRegistrationHandle* existing_handle = | 232 ServiceWorkerRegistrationHandle* existing_handle = |
| 233 FindRegistrationHandle(provider_host->provider_id(), registration->id()); | 233 FindRegistrationHandle(provider_host->provider_id(), registration->id()); |
| 234 if (existing_handle) { | 234 if (existing_handle) { |
| 235 existing_handle->IncrementRefCount(); | 235 existing_handle->IncrementRefCount(); |
| 236 return existing_handle; | 236 return existing_handle; |
| 237 } | 237 } |
| 238 |
| 238 std::unique_ptr<ServiceWorkerRegistrationHandle> new_handle( | 239 std::unique_ptr<ServiceWorkerRegistrationHandle> new_handle( |
| 239 new ServiceWorkerRegistrationHandle(GetContext()->AsWeakPtr(), | 240 new ServiceWorkerRegistrationHandle(GetContext()->AsWeakPtr(), |
| 240 provider_host, registration)); | 241 provider_host, registration)); |
| 241 ServiceWorkerRegistrationHandle* new_handle_ptr = new_handle.get(); | 242 ServiceWorkerRegistrationHandle* new_handle_ptr = new_handle.get(); |
| 242 RegisterServiceWorkerRegistrationHandle(std::move(new_handle)); | 243 RegisterServiceWorkerRegistrationHandle(std::move(new_handle)); |
| 243 return new_handle_ptr; | 244 return new_handle_ptr; |
| 244 } | 245 } |
| 245 | 246 |
| 246 void ServiceWorkerDispatcherHost::OnRegisterServiceWorker( | 247 void ServiceWorkerDispatcherHost::OnRegisterServiceWorker( |
| 247 int thread_id, | 248 int thread_id, |
| (...skipping 708 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 956 // Otherwise, completed the initialization of the pre-created host. | 957 // Otherwise, completed the initialization of the pre-created host. |
| 957 if (info.type != SERVICE_WORKER_PROVIDER_FOR_WINDOW) { | 958 if (info.type != SERVICE_WORKER_PROVIDER_FOR_WINDOW) { |
| 958 bad_message::ReceivedBadMessage( | 959 bad_message::ReceivedBadMessage( |
| 959 this, bad_message::SWDH_PROVIDER_CREATED_ILLEGAL_TYPE_NOT_WINDOW); | 960 this, bad_message::SWDH_PROVIDER_CREATED_ILLEGAL_TYPE_NOT_WINDOW); |
| 960 return; | 961 return; |
| 961 } | 962 } |
| 962 provider_host->CompleteNavigationInitialized(render_process_id_, | 963 provider_host->CompleteNavigationInitialized(render_process_id_, |
| 963 std::move(info), this); | 964 std::move(info), this); |
| 964 GetContext()->AddProviderHost(std::move(provider_host)); | 965 GetContext()->AddProviderHost(std::move(provider_host)); |
| 965 } else { | 966 } else { |
| 966 // Provider host for controller should be pre-created on StartWorker in | |
| 967 // ServiceWorkerVersion. | |
| 968 if (info.type == SERVICE_WORKER_PROVIDER_FOR_CONTROLLER) { | |
| 969 bad_message::ReceivedBadMessage( | |
| 970 this, bad_message::SWDH_PROVIDER_CREATED_ILLEGAL_TYPE_CONTROLLER); | |
| 971 return; | |
| 972 } | |
| 973 if (ServiceWorkerUtils::IsBrowserAssignedProviderId(info.provider_id)) { | 967 if (ServiceWorkerUtils::IsBrowserAssignedProviderId(info.provider_id)) { |
| 974 bad_message::ReceivedBadMessage( | 968 bad_message::ReceivedBadMessage( |
| 975 this, bad_message::SWDH_PROVIDER_CREATED_BAD_ID); | 969 this, bad_message::SWDH_PROVIDER_CREATED_BAD_ID); |
| 976 return; | 970 return; |
| 977 } | 971 } |
| 978 GetContext()->AddProviderHost(ServiceWorkerProviderHost::Create( | 972 GetContext()->AddProviderHost(ServiceWorkerProviderHost::Create( |
| 979 render_process_id_, std::move(info), GetContext()->AsWeakPtr(), this)); | 973 render_process_id_, std::move(info), GetContext()->AsWeakPtr(), this)); |
| 980 } | 974 } |
| 981 } | 975 } |
| 982 | 976 |
| 977 void ServiceWorkerDispatcherHost::OnSetHostedVersionId(int provider_id, |
| 978 int64_t version_id, |
| 979 int embedded_worker_id) { |
| 980 TRACE_EVENT0("ServiceWorker", |
| 981 "ServiceWorkerDispatcherHost::OnSetHostedVersionId"); |
| 982 if (!GetContext()) |
| 983 return; |
| 984 ServiceWorkerProviderHost* provider_host = |
| 985 GetContext()->GetProviderHost(render_process_id_, provider_id); |
| 986 if (!provider_host) { |
| 987 bad_message::ReceivedBadMessage( |
| 988 this, bad_message::SWDH_SET_HOSTED_VERSION_NO_HOST); |
| 989 return; |
| 990 } |
| 991 |
| 992 // This provider host must be specialized for a controller. |
| 993 if (provider_host->IsProviderForClient()) { |
| 994 bad_message::ReceivedBadMessage( |
| 995 this, bad_message::SWDH_SET_HOSTED_VERSION_INVALID_HOST); |
| 996 return; |
| 997 } |
| 998 |
| 999 // A service worker context associated with this provider host was destroyed |
| 1000 // due to restarting the service worker system etc. |
| 1001 if (!provider_host->IsContextAlive()) |
| 1002 return; |
| 1003 |
| 1004 // We might not be STARTING if the stop sequence was entered (STOPPING) or |
| 1005 // ended up being detached (STOPPED). |
| 1006 ServiceWorkerVersion* version = GetContext()->GetLiveVersion(version_id); |
| 1007 if (!version || version->running_status() != EmbeddedWorkerStatus::STARTING) |
| 1008 return; |
| 1009 |
| 1010 // If the version has a different embedded worker, assume the message is about |
| 1011 // a detached worker and ignore. |
| 1012 if (version->embedded_worker()->embedded_worker_id() != embedded_worker_id) |
| 1013 return; |
| 1014 |
| 1015 // A process for the worker must be equal to a process for the provider host. |
| 1016 if (version->embedded_worker()->process_id() != provider_host->process_id()) { |
| 1017 // Temporary debugging for https://crbug.com/668633 |
| 1018 base::debug::ScopedCrashKey scope_worker_pid( |
| 1019 "swdh_set_hosted_version_worker_pid", |
| 1020 base::IntToString(version->embedded_worker()->process_id())); |
| 1021 base::debug::ScopedCrashKey scope_provider_host_pid( |
| 1022 "swdh_set_hosted_version_host_pid", |
| 1023 base::IntToString(provider_host->process_id())); |
| 1024 if (version->embedded_worker()->process_id() != |
| 1025 ChildProcessHost::kInvalidUniqueID) { |
| 1026 base::debug::ScopedCrashKey scope_is_new_process( |
| 1027 "swdh_set_hosted_version_is_new_process", |
| 1028 version->embedded_worker()->is_new_process() ? "true" : "false"); |
| 1029 } |
| 1030 base::debug::ScopedCrashKey scope_worker_restart_count( |
| 1031 "swdh_set_hosted_version_restart_count", |
| 1032 base::IntToString(version->embedded_worker()->restart_count())); |
| 1033 bad_message::ReceivedBadMessage( |
| 1034 this, bad_message::SWDH_SET_HOSTED_VERSION_PROCESS_MISMATCH); |
| 1035 return; |
| 1036 } |
| 1037 |
| 1038 provider_host->SetHostedVersion(version); |
| 1039 |
| 1040 // Retrieve the registration associated with |version|. The registration |
| 1041 // must be alive because the version keeps it during starting worker. |
| 1042 ServiceWorkerRegistration* registration = |
| 1043 GetContext()->GetLiveRegistration(version->registration_id()); |
| 1044 DCHECK(registration); |
| 1045 |
| 1046 // Set the document URL to the script url in order to allow |
| 1047 // register/unregister/getRegistration on ServiceWorkerGlobalScope. |
| 1048 provider_host->SetDocumentUrl(version->script_url()); |
| 1049 |
| 1050 ServiceWorkerRegistrationObjectInfo info; |
| 1051 ServiceWorkerVersionAttributes attrs; |
| 1052 GetRegistrationObjectInfoAndVersionAttributes(provider_host->AsWeakPtr(), |
| 1053 registration, &info, &attrs); |
| 1054 |
| 1055 Send(new ServiceWorkerMsg_AssociateRegistration(kDocumentMainThreadId, |
| 1056 provider_id, info, attrs)); |
| 1057 } |
| 1058 |
| 983 template <typename SourceInfo> | 1059 template <typename SourceInfo> |
| 984 void ServiceWorkerDispatcherHost::DispatchExtendableMessageEventInternal( | 1060 void ServiceWorkerDispatcherHost::DispatchExtendableMessageEventInternal( |
| 985 scoped_refptr<ServiceWorkerVersion> worker, | 1061 scoped_refptr<ServiceWorkerVersion> worker, |
| 986 const base::string16& message, | 1062 const base::string16& message, |
| 987 const url::Origin& source_origin, | 1063 const url::Origin& source_origin, |
| 988 const std::vector<MessagePort>& sent_message_ports, | 1064 const std::vector<MessagePort>& sent_message_ports, |
| 989 const base::Optional<base::TimeDelta>& timeout, | 1065 const base::Optional<base::TimeDelta>& timeout, |
| 990 const StatusCallback& callback, | 1066 const StatusCallback& callback, |
| 991 const SourceInfo& source_info) { | 1067 const SourceInfo& source_info) { |
| 992 if (!source_info.IsValid()) { | 1068 if (!source_info.IsValid()) { |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1089 handle->registration()->id() == registration_id) { | 1165 handle->registration()->id() == registration_id) { |
| 1090 return handle; | 1166 return handle; |
| 1091 } | 1167 } |
| 1092 } | 1168 } |
| 1093 return nullptr; | 1169 return nullptr; |
| 1094 } | 1170 } |
| 1095 | 1171 |
| 1096 void ServiceWorkerDispatcherHost::GetRegistrationObjectInfoAndVersionAttributes( | 1172 void ServiceWorkerDispatcherHost::GetRegistrationObjectInfoAndVersionAttributes( |
| 1097 base::WeakPtr<ServiceWorkerProviderHost> provider_host, | 1173 base::WeakPtr<ServiceWorkerProviderHost> provider_host, |
| 1098 ServiceWorkerRegistration* registration, | 1174 ServiceWorkerRegistration* registration, |
| 1099 ServiceWorkerRegistrationObjectInfo* out_info, | 1175 ServiceWorkerRegistrationObjectInfo* info, |
| 1100 ServiceWorkerVersionAttributes* out_attrs) { | 1176 ServiceWorkerVersionAttributes* attrs) { |
| 1101 ServiceWorkerRegistrationHandle* handle = | 1177 ServiceWorkerRegistrationHandle* handle = |
| 1102 GetOrCreateRegistrationHandle(provider_host, registration); | 1178 GetOrCreateRegistrationHandle(provider_host, registration); |
| 1103 *out_info = handle->GetObjectInfo(); | 1179 *info = handle->GetObjectInfo(); |
| 1104 | 1180 |
| 1105 out_attrs->installing = provider_host->GetOrCreateServiceWorkerHandle( | 1181 attrs->installing = provider_host->GetOrCreateServiceWorkerHandle( |
| 1106 registration->installing_version()); | 1182 registration->installing_version()); |
| 1107 out_attrs->waiting = provider_host->GetOrCreateServiceWorkerHandle( | 1183 attrs->waiting = provider_host->GetOrCreateServiceWorkerHandle( |
| 1108 registration->waiting_version()); | 1184 registration->waiting_version()); |
| 1109 out_attrs->active = provider_host->GetOrCreateServiceWorkerHandle( | 1185 attrs->active = provider_host->GetOrCreateServiceWorkerHandle( |
| 1110 registration->active_version()); | 1186 registration->active_version()); |
| 1111 } | 1187 } |
| 1112 | 1188 |
| 1113 void ServiceWorkerDispatcherHost::RegistrationComplete( | 1189 void ServiceWorkerDispatcherHost::RegistrationComplete( |
| 1114 int thread_id, | 1190 int thread_id, |
| 1115 int provider_id, | 1191 int provider_id, |
| 1116 int request_id, | 1192 int request_id, |
| 1117 ServiceWorkerStatusCode status, | 1193 ServiceWorkerStatusCode status, |
| 1118 const std::string& status_message, | 1194 const std::string& status_message, |
| 1119 int64_t registration_id) { | 1195 int64_t registration_id) { |
| (...skipping 367 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1487 if (!handle) { | 1563 if (!handle) { |
| 1488 bad_message::ReceivedBadMessage(this, | 1564 bad_message::ReceivedBadMessage(this, |
| 1489 bad_message::SWDH_TERMINATE_BAD_HANDLE); | 1565 bad_message::SWDH_TERMINATE_BAD_HANDLE); |
| 1490 return; | 1566 return; |
| 1491 } | 1567 } |
| 1492 handle->version()->StopWorker( | 1568 handle->version()->StopWorker( |
| 1493 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); | 1569 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); |
| 1494 } | 1570 } |
| 1495 | 1571 |
| 1496 } // namespace content | 1572 } // namespace content |
| OLD | NEW |