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_context_core.h" | 5 #include "content/browser/service_worker/service_worker_context_core.h" |
6 | 6 |
7 #include <limits> | 7 #include <limits> |
8 #include <set> | 8 #include <set> |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
11 #include "base/barrier_closure.h" | 11 #include "base/barrier_closure.h" |
12 #include "base/bind.h" | 12 #include "base/bind.h" |
13 #include "base/bind_helpers.h" | 13 #include "base/bind_helpers.h" |
14 #include "base/files/file_path.h" | 14 #include "base/files/file_path.h" |
15 #include "base/location.h" | 15 #include "base/location.h" |
16 #include "base/macros.h" | 16 #include "base/macros.h" |
17 #include "base/memory/ptr_util.h" | 17 #include "base/memory/ptr_util.h" |
18 #include "base/single_thread_task_runner.h" | 18 #include "base/single_thread_task_runner.h" |
19 #include "base/strings/string_util.h" | 19 #include "base/strings/string_util.h" |
20 #include "base/threading/thread_task_runner_handle.h" | 20 #include "base/threading/thread_task_runner_handle.h" |
21 #include "content/browser/frame_host/render_frame_host_impl.h" | 21 #include "content/browser/frame_host/render_frame_host_impl.h" |
22 #include "content/browser/service_worker/embedded_worker_registry.h" | 22 #include "content/browser/service_worker/embedded_worker_registry.h" |
23 #include "content/browser/service_worker/embedded_worker_status.h" | 23 #include "content/browser/service_worker/embedded_worker_status.h" |
24 #include "content/browser/service_worker/service_worker_context_observer.h" | 24 #include "content/browser/service_worker/service_worker_context_observer.h" |
25 #include "content/browser/service_worker/service_worker_context_wrapper.h" | 25 #include "content/browser/service_worker/service_worker_context_wrapper.h" |
26 #include "content/browser/service_worker/service_worker_database_task_manager.h" | 26 #include "content/browser/service_worker/service_worker_database_task_manager.h" |
27 #include "content/browser/service_worker/service_worker_dispatcher_host.h" | |
27 #include "content/browser/service_worker/service_worker_info.h" | 28 #include "content/browser/service_worker/service_worker_info.h" |
28 #include "content/browser/service_worker/service_worker_job_coordinator.h" | 29 #include "content/browser/service_worker/service_worker_job_coordinator.h" |
29 #include "content/browser/service_worker/service_worker_process_manager.h" | 30 #include "content/browser/service_worker/service_worker_process_manager.h" |
30 #include "content/browser/service_worker/service_worker_provider_host.h" | 31 #include "content/browser/service_worker/service_worker_provider_host.h" |
31 #include "content/browser/service_worker/service_worker_register_job.h" | 32 #include "content/browser/service_worker/service_worker_register_job.h" |
32 #include "content/browser/service_worker/service_worker_registration.h" | 33 #include "content/browser/service_worker/service_worker_registration.h" |
33 #include "content/browser/service_worker/service_worker_storage.h" | 34 #include "content/browser/service_worker/service_worker_storage.h" |
34 #include "content/common/service_worker/service_worker_utils.h" | 35 #include "content/common/service_worker/service_worker_utils.h" |
35 #include "content/public/browser/browser_thread.h" | 36 #include "content/public/browser/browser_thread.h" |
36 #include "ipc/ipc_message.h" | 37 #include "ipc/ipc_message.h" |
(...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
213 | 214 |
214 ServiceWorkerContextCore::ServiceWorkerContextCore( | 215 ServiceWorkerContextCore::ServiceWorkerContextCore( |
215 const base::FilePath& path, | 216 const base::FilePath& path, |
216 std::unique_ptr<ServiceWorkerDatabaseTaskManager> database_task_manager, | 217 std::unique_ptr<ServiceWorkerDatabaseTaskManager> database_task_manager, |
217 const scoped_refptr<base::SingleThreadTaskRunner>& disk_cache_thread, | 218 const scoped_refptr<base::SingleThreadTaskRunner>& disk_cache_thread, |
218 storage::QuotaManagerProxy* quota_manager_proxy, | 219 storage::QuotaManagerProxy* quota_manager_proxy, |
219 storage::SpecialStoragePolicy* special_storage_policy, | 220 storage::SpecialStoragePolicy* special_storage_policy, |
220 base::ObserverListThreadSafe<ServiceWorkerContextObserver>* observer_list, | 221 base::ObserverListThreadSafe<ServiceWorkerContextObserver>* observer_list, |
221 ServiceWorkerContextWrapper* wrapper) | 222 ServiceWorkerContextWrapper* wrapper) |
222 : wrapper_(wrapper), | 223 : wrapper_(wrapper), |
223 providers_(new ProcessToProviderMap), | 224 dispatcher_hosts_( |
224 provider_by_uuid_(new ProviderByClientUUIDMap), | 225 base::MakeUnique<IDMap<ServiceWorkerDispatcherHost*>>()), |
226 providers_(base::MakeUnique<ProcessToProviderMap>()), | |
227 provider_by_uuid_(base::MakeUnique<ProviderByClientUUIDMap>()), | |
225 force_update_on_page_load_(false), | 228 force_update_on_page_load_(false), |
226 next_handle_id_(0), | 229 next_handle_id_(0), |
227 next_registration_handle_id_(0), | 230 next_registration_handle_id_(0), |
228 was_service_worker_registered_(false), | 231 was_service_worker_registered_(false), |
229 observer_list_(observer_list), | 232 observer_list_(observer_list), |
230 weak_factory_(this) { | 233 weak_factory_(this) { |
231 // These get a WeakPtr from weak_factory_, so must be set after weak_factory_ | 234 // These get a WeakPtr from weak_factory_, so must be set after weak_factory_ |
232 // is initialized. | 235 // is initialized. |
233 storage_ = ServiceWorkerStorage::Create( | 236 storage_ = ServiceWorkerStorage::Create( |
234 path, AsWeakPtr(), std::move(database_task_manager), disk_cache_thread, | 237 path, AsWeakPtr(), std::move(database_task_manager), disk_cache_thread, |
235 quota_manager_proxy, special_storage_policy); | 238 quota_manager_proxy, special_storage_policy); |
236 embedded_worker_registry_ = EmbeddedWorkerRegistry::Create(AsWeakPtr()); | 239 embedded_worker_registry_ = EmbeddedWorkerRegistry::Create(AsWeakPtr()); |
237 job_coordinator_.reset(new ServiceWorkerJobCoordinator(AsWeakPtr())); | 240 job_coordinator_.reset(new ServiceWorkerJobCoordinator(AsWeakPtr())); |
238 } | 241 } |
239 | 242 |
240 ServiceWorkerContextCore::ServiceWorkerContextCore( | 243 ServiceWorkerContextCore::ServiceWorkerContextCore( |
241 ServiceWorkerContextCore* old_context, | 244 ServiceWorkerContextCore* old_context, |
242 ServiceWorkerContextWrapper* wrapper) | 245 ServiceWorkerContextWrapper* wrapper) |
243 : wrapper_(wrapper), | 246 : wrapper_(wrapper), |
247 dispatcher_hosts_(old_context->dispatcher_hosts_.release()), | |
dcheng
2017/02/14 08:47:46
Nit: std::move instead of release()
shimazu
2017/02/15 02:24:18
Done.
| |
244 providers_(old_context->providers_.release()), | 248 providers_(old_context->providers_.release()), |
245 provider_by_uuid_(old_context->provider_by_uuid_.release()), | 249 provider_by_uuid_(old_context->provider_by_uuid_.release()), |
246 next_handle_id_(old_context->next_handle_id_), | 250 next_handle_id_(old_context->next_handle_id_), |
247 next_registration_handle_id_(old_context->next_registration_handle_id_), | 251 next_registration_handle_id_(old_context->next_registration_handle_id_), |
248 was_service_worker_registered_( | 252 was_service_worker_registered_( |
249 old_context->was_service_worker_registered_), | 253 old_context->was_service_worker_registered_), |
250 observer_list_(old_context->observer_list_), | 254 observer_list_(old_context->observer_list_), |
251 weak_factory_(this) { | 255 weak_factory_(this) { |
252 // These get a WeakPtr from weak_factory_, so must be set after weak_factory_ | 256 // These get a WeakPtr from weak_factory_, so must be set after weak_factory_ |
253 // is initialized. | 257 // is initialized. |
254 storage_ = ServiceWorkerStorage::Create(AsWeakPtr(), old_context->storage()); | 258 storage_ = ServiceWorkerStorage::Create(AsWeakPtr(), old_context->storage()); |
255 embedded_worker_registry_ = EmbeddedWorkerRegistry::Create( | 259 embedded_worker_registry_ = EmbeddedWorkerRegistry::Create( |
256 AsWeakPtr(), | 260 AsWeakPtr(), |
257 old_context->embedded_worker_registry()); | 261 old_context->embedded_worker_registry()); |
258 job_coordinator_.reset(new ServiceWorkerJobCoordinator(AsWeakPtr())); | 262 job_coordinator_.reset(new ServiceWorkerJobCoordinator(AsWeakPtr())); |
259 } | 263 } |
260 | 264 |
261 ServiceWorkerContextCore::~ServiceWorkerContextCore() { | 265 ServiceWorkerContextCore::~ServiceWorkerContextCore() { |
262 DCHECK(storage_); | 266 DCHECK(storage_); |
263 for (VersionMap::iterator it = live_versions_.begin(); | 267 for (VersionMap::iterator it = live_versions_.begin(); |
264 it != live_versions_.end(); | 268 it != live_versions_.end(); |
265 ++it) { | 269 ++it) { |
266 it->second->RemoveListener(this); | 270 it->second->RemoveListener(this); |
267 } | 271 } |
268 weak_factory_.InvalidateWeakPtrs(); | 272 weak_factory_.InvalidateWeakPtrs(); |
269 } | 273 } |
270 | 274 |
271 ServiceWorkerProviderHost* ServiceWorkerContextCore::GetProviderHost( | 275 void ServiceWorkerContextCore::AddDispatcherHost( |
272 int process_id, int provider_id) { | 276 int process_id, |
273 ProviderMap* map = GetProviderMapForProcess(process_id); | 277 content::ServiceWorkerDispatcherHost* dispatcher_host) { |
274 if (!map) | 278 DCHECK(!dispatcher_hosts_->Lookup(process_id)); |
275 return NULL; | 279 dispatcher_hosts_->AddWithID(dispatcher_host, process_id); |
dcheng
2017/02/14 08:47:46
I wonder: do we need to use IDMap if we always hav
shimazu
2017/02/15 02:24:18
I assume it's just historical thing. Updated.
I'll
| |
276 return map->Lookup(provider_id); | 280 } |
281 | |
282 ServiceWorkerDispatcherHost* ServiceWorkerContextCore::GetDispatcherHost( | |
283 int process_id) { | |
284 return dispatcher_hosts_->Lookup(process_id); | |
285 } | |
286 | |
287 void ServiceWorkerContextCore::RemoveDispatcherHost(int process_id) { | |
288 DCHECK(dispatcher_hosts_->Lookup(process_id)); | |
289 RemoveAllProviderHostsForProcess(process_id); | |
290 embedded_worker_registry_->RemoveProcess(process_id); | |
291 dispatcher_hosts_->Remove(process_id); | |
277 } | 292 } |
278 | 293 |
279 void ServiceWorkerContextCore::AddProviderHost( | 294 void ServiceWorkerContextCore::AddProviderHost( |
280 std::unique_ptr<ServiceWorkerProviderHost> host) { | 295 std::unique_ptr<ServiceWorkerProviderHost> host) { |
281 int process_id = host->process_id(); | 296 int process_id = host->process_id(); |
282 int provider_id = host->provider_id(); | 297 int provider_id = host->provider_id(); |
283 ProviderMap* map = GetProviderMapForProcess(process_id); | 298 ProviderMap* map = GetProviderMapForProcess(process_id); |
284 if (!map) { | 299 if (!map) { |
285 providers_->AddWithID(base::MakeUnique<ProviderMap>(), process_id); | 300 providers_->AddWithID(base::MakeUnique<ProviderMap>(), process_id); |
286 map = GetProviderMapForProcess(process_id); | 301 map = GetProviderMapForProcess(process_id); |
287 } | 302 } |
288 map->AddWithID(std::move(host), provider_id); | 303 map->AddWithID(std::move(host), provider_id); |
289 } | 304 } |
290 | 305 |
306 ServiceWorkerProviderHost* ServiceWorkerContextCore::GetProviderHost( | |
307 int process_id, | |
308 int provider_id) { | |
309 ProviderMap* map = GetProviderMapForProcess(process_id); | |
310 if (!map) | |
311 return nullptr; | |
312 return map->Lookup(provider_id); | |
313 } | |
314 | |
291 void ServiceWorkerContextCore::RemoveProviderHost( | 315 void ServiceWorkerContextCore::RemoveProviderHost( |
292 int process_id, int provider_id) { | 316 int process_id, int provider_id) { |
293 ProviderMap* map = GetProviderMapForProcess(process_id); | 317 ProviderMap* map = GetProviderMapForProcess(process_id); |
294 DCHECK(map); | 318 DCHECK(map); |
295 map->Remove(provider_id); | 319 map->Remove(provider_id); |
296 } | 320 } |
297 | 321 |
298 void ServiceWorkerContextCore::RemoveAllProviderHostsForProcess( | 322 void ServiceWorkerContextCore::RemoveAllProviderHostsForProcess( |
299 int process_id) { | 323 int process_id) { |
300 if (providers_->Lookup(process_id)) | 324 if (providers_->Lookup(process_id)) |
(...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
498 if (status == SERVICE_WORKER_OK && observer_list_.get()) { | 522 if (status == SERVICE_WORKER_OK && observer_list_.get()) { |
499 observer_list_->Notify(FROM_HERE, | 523 observer_list_->Notify(FROM_HERE, |
500 &ServiceWorkerContextObserver::OnRegistrationDeleted, | 524 &ServiceWorkerContextObserver::OnRegistrationDeleted, |
501 registration_id, pattern); | 525 registration_id, pattern); |
502 } | 526 } |
503 } | 527 } |
504 | 528 |
505 ServiceWorkerRegistration* ServiceWorkerContextCore::GetLiveRegistration( | 529 ServiceWorkerRegistration* ServiceWorkerContextCore::GetLiveRegistration( |
506 int64_t id) { | 530 int64_t id) { |
507 RegistrationsMap::iterator it = live_registrations_.find(id); | 531 RegistrationsMap::iterator it = live_registrations_.find(id); |
508 return (it != live_registrations_.end()) ? it->second : NULL; | 532 return (it != live_registrations_.end()) ? it->second : nullptr; |
509 } | 533 } |
510 | 534 |
511 void ServiceWorkerContextCore::AddLiveRegistration( | 535 void ServiceWorkerContextCore::AddLiveRegistration( |
512 ServiceWorkerRegistration* registration) { | 536 ServiceWorkerRegistration* registration) { |
513 // TODO(nhiroki): Change CHECK to DCHECK after https://crbug.com/619294 is | 537 // TODO(nhiroki): Change CHECK to DCHECK after https://crbug.com/619294 is |
514 // fixed. | 538 // fixed. |
515 CHECK(!GetLiveRegistration(registration->id())); | 539 CHECK(!GetLiveRegistration(registration->id())); |
516 live_registrations_[registration->id()] = registration; | 540 live_registrations_[registration->id()] = registration; |
517 if (observer_list_.get()) { | 541 if (observer_list_.get()) { |
518 observer_list_->Notify(FROM_HERE, | 542 observer_list_->Notify(FROM_HERE, |
519 &ServiceWorkerContextObserver::OnNewLiveRegistration, | 543 &ServiceWorkerContextObserver::OnNewLiveRegistration, |
520 registration->id(), registration->pattern()); | 544 registration->id(), registration->pattern()); |
521 } | 545 } |
522 } | 546 } |
523 | 547 |
524 void ServiceWorkerContextCore::RemoveLiveRegistration(int64_t id) { | 548 void ServiceWorkerContextCore::RemoveLiveRegistration(int64_t id) { |
525 live_registrations_.erase(id); | 549 live_registrations_.erase(id); |
526 } | 550 } |
527 | 551 |
528 ServiceWorkerVersion* ServiceWorkerContextCore::GetLiveVersion(int64_t id) { | 552 ServiceWorkerVersion* ServiceWorkerContextCore::GetLiveVersion(int64_t id) { |
529 VersionMap::iterator it = live_versions_.find(id); | 553 VersionMap::iterator it = live_versions_.find(id); |
530 return (it != live_versions_.end()) ? it->second : NULL; | 554 return (it != live_versions_.end()) ? it->second : nullptr; |
531 } | 555 } |
532 | 556 |
533 // PlzNavigate | 557 // PlzNavigate |
534 void ServiceWorkerContextCore::AddNavigationHandleCore( | 558 void ServiceWorkerContextCore::AddNavigationHandleCore( |
535 int service_worker_provider_id, | 559 int service_worker_provider_id, |
536 ServiceWorkerNavigationHandleCore* handle) { | 560 ServiceWorkerNavigationHandleCore* handle) { |
537 auto result = navigation_handle_cores_map_.insert( | 561 auto result = navigation_handle_cores_map_.insert( |
538 std::pair<int, ServiceWorkerNavigationHandleCore*>( | 562 std::pair<int, ServiceWorkerNavigationHandleCore*>( |
539 service_worker_provider_id, handle)); | 563 service_worker_provider_id, handle)); |
540 DCHECK(result.second) | 564 DCHECK(result.second) |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
629 job_coordinator_->AbortAll(); | 653 job_coordinator_->AbortAll(); |
630 storage_->DeleteAndStartOver(callback); | 654 storage_->DeleteAndStartOver(callback); |
631 } | 655 } |
632 | 656 |
633 std::unique_ptr<ServiceWorkerProviderHost> | 657 std::unique_ptr<ServiceWorkerProviderHost> |
634 ServiceWorkerContextCore::TransferProviderHostOut(int process_id, | 658 ServiceWorkerContextCore::TransferProviderHostOut(int process_id, |
635 int provider_id) { | 659 int provider_id) { |
636 ProviderMap* map = GetProviderMapForProcess(process_id); | 660 ProviderMap* map = GetProviderMapForProcess(process_id); |
637 ServiceWorkerProviderHost* transferee = map->Lookup(provider_id); | 661 ServiceWorkerProviderHost* transferee = map->Lookup(provider_id); |
638 std::unique_ptr<ServiceWorkerProviderHost> replacement = | 662 std::unique_ptr<ServiceWorkerProviderHost> replacement = |
639 base::MakeUnique<ServiceWorkerProviderHost>( | 663 transferee->PrepareForCrossSiteTransfer(); |
640 process_id, transferee->frame_id(), provider_id, | |
641 transferee->provider_type(), | |
642 transferee->is_parent_frame_secure() | |
643 ? ServiceWorkerProviderHost::FrameSecurityLevel::SECURE | |
644 : ServiceWorkerProviderHost::FrameSecurityLevel::INSECURE, | |
645 AsWeakPtr(), transferee->dispatcher_host()); | |
646 transferee->PrepareForCrossSiteTransfer(); | |
647 return map->Replace(provider_id, std::move(replacement)); | 664 return map->Replace(provider_id, std::move(replacement)); |
648 } | 665 } |
649 | 666 |
650 void ServiceWorkerContextCore::TransferProviderHostIn( | 667 void ServiceWorkerContextCore::TransferProviderHostIn( |
651 int new_process_id, | 668 int new_process_id, |
652 int new_provider_id, | 669 int new_provider_id, |
653 std::unique_ptr<ServiceWorkerProviderHost> transferee) { | 670 std::unique_ptr<ServiceWorkerProviderHost> transferee) { |
654 ProviderMap* map = GetProviderMapForProcess(new_process_id); | 671 ProviderMap* map = GetProviderMapForProcess(new_process_id); |
655 ServiceWorkerProviderHost* temp = map->Lookup(new_provider_id); | 672 ServiceWorkerProviderHost* temp = map->Lookup(new_provider_id); |
656 if (!temp) | 673 if (!temp) |
(...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
864 } | 881 } |
865 | 882 |
866 void ServiceWorkerContextCore::OnRegistrationFinishedForCheckHasServiceWorker( | 883 void ServiceWorkerContextCore::OnRegistrationFinishedForCheckHasServiceWorker( |
867 const ServiceWorkerContext::CheckHasServiceWorkerCallback callback, | 884 const ServiceWorkerContext::CheckHasServiceWorkerCallback callback, |
868 scoped_refptr<ServiceWorkerRegistration> registration) { | 885 scoped_refptr<ServiceWorkerRegistration> registration) { |
869 callback.Run(registration->active_version() || | 886 callback.Run(registration->active_version() || |
870 registration->waiting_version()); | 887 registration->waiting_version()); |
871 } | 888 } |
872 | 889 |
873 } // namespace content | 890 } // namespace content |
OLD | NEW |