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