Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/child/service_worker/service_worker_network_provider.h" | 5 #include "content/child/service_worker/service_worker_network_provider.h" |
| 6 | 6 |
| 7 #include "base/atomic_sequence_num.h" | 7 #include "base/atomic_sequence_num.h" |
| 8 #include "base/command_line.h" | |
| 8 #include "content/child/child_thread_impl.h" | 9 #include "content/child/child_thread_impl.h" |
| 9 #include "content/child/service_worker/service_worker_provider_context.h" | 10 #include "content/child/service_worker/service_worker_provider_context.h" |
| 10 #include "content/common/service_worker/service_worker_messages.h" | 11 #include "content/common/service_worker/service_worker_messages.h" |
| 12 #include "content/common/service_worker/service_worker_utils.h" | |
| 13 #include "content/public/common/content_switches.h" | |
| 11 | 14 |
| 12 namespace content { | 15 namespace content { |
| 13 | 16 |
| 14 namespace { | 17 namespace { |
| 15 | 18 |
| 16 const char kUserDataKey[] = "SWProviderKey"; | 19 const char kUserDataKey[] = "SWProviderKey"; |
| 17 | 20 |
| 18 // Must be unique in the child process. | 21 // Must be unique in the child process. |
| 19 int GetNextProviderId() { | 22 int GetNextProviderId() { |
| 20 static base::StaticAtomicSequenceNumber sequence; | 23 static base::StaticAtomicSequenceNumber sequence; |
| 21 return sequence.GetNext(); // We start at zero. | 24 return sequence.GetNext(); // We start at zero. |
| 22 } | 25 } |
| 23 | 26 |
| 24 // When the provider is for a sandboxed iframe we use | 27 // When the provider is for a sandboxed iframe we use |
| 25 // kInvalidServiceWorkerProviderId as the provider type and we don't create | 28 // kInvalidServiceWorkerProviderId as the provider type and we don't create |
| 26 // ServiceWorkerProviderContext and ServiceWorkerProviderHost. | 29 // ServiceWorkerProviderContext and ServiceWorkerProviderHost. |
| 27 int GenerateProviderIdForType(const ServiceWorkerProviderType provider_type) { | 30 int GenerateProviderIdForType(const ServiceWorkerProviderType provider_type) { |
| 28 if (provider_type == SERVICE_WORKER_PROVIDER_FOR_SANDBOXED_FRAME) | 31 if (provider_type == SERVICE_WORKER_PROVIDER_FOR_SANDBOXED_FRAME) |
| 29 return kInvalidServiceWorkerProviderId; | 32 return kInvalidServiceWorkerProviderId; |
|
michaeln
2015/10/01 01:42:16
This early return prevent the creation of a 'host'
Fabrice (no longer in Chrome)
2015/10/01 18:29:56
This is now taken care of by checking the flags in
| |
| 30 return GetNextProviderId(); | 33 return GetNextProviderId(); |
| 31 } | 34 } |
| 32 | 35 |
| 33 } // namespace | 36 } // namespace |
| 34 | 37 |
| 35 void ServiceWorkerNetworkProvider::AttachToDocumentState( | 38 void ServiceWorkerNetworkProvider::AttachToDocumentState( |
| 36 base::SupportsUserData* datasource_userdata, | 39 base::SupportsUserData* datasource_userdata, |
| 37 scoped_ptr<ServiceWorkerNetworkProvider> network_provider) { | 40 scoped_ptr<ServiceWorkerNetworkProvider> network_provider) { |
| 38 datasource_userdata->SetUserData(&kUserDataKey, network_provider.release()); | 41 datasource_userdata->SetUserData(&kUserDataKey, network_provider.release()); |
| 39 } | 42 } |
| 40 | 43 |
| 41 ServiceWorkerNetworkProvider* ServiceWorkerNetworkProvider::FromDocumentState( | 44 ServiceWorkerNetworkProvider* ServiceWorkerNetworkProvider::FromDocumentState( |
| 42 base::SupportsUserData* datasource_userdata) { | 45 base::SupportsUserData* datasource_userdata) { |
| 43 return static_cast<ServiceWorkerNetworkProvider*>( | 46 return static_cast<ServiceWorkerNetworkProvider*>( |
| 44 datasource_userdata->GetUserData(&kUserDataKey)); | 47 datasource_userdata->GetUserData(&kUserDataKey)); |
| 45 } | 48 } |
| 46 | 49 |
| 47 ServiceWorkerNetworkProvider::ServiceWorkerNetworkProvider( | 50 ServiceWorkerNetworkProvider::ServiceWorkerNetworkProvider( |
| 48 int route_id, | 51 int route_id, |
| 52 ServiceWorkerProviderType provider_type, | |
| 53 int browser_provider_id) { | |
| 54 provider_id_ = kInvalidServiceWorkerProviderId; | |
| 55 if (ServiceWorkerUtils::IsBrowserAssignedProviderId(browser_provider_id)) { | |
| 56 // PlzNavigate | |
| 57 CHECK(base::CommandLine::ForCurrentProcess()->HasSwitch( | |
| 58 switches::kEnableBrowserSideNavigation)); | |
| 59 DCHECK_NE(SERVICE_WORKER_PROVIDER_FOR_SANDBOXED_FRAME, provider_type); | |
| 60 // This is a navigation. The ServiceWorkerProviderHost has already been | |
| 61 // initialized, copy the provider_id value. | |
| 62 provider_id_ = browser_provider_id; | |
| 63 } else { | |
| 64 // Initialize a new provider ID in all other cases. | |
| 65 DCHECK_EQ(kInvalidServiceWorkerProviderId, browser_provider_id); | |
| 66 provider_id_ = GenerateProviderIdForType(provider_type); | |
| 67 } | |
| 68 if (provider_id_ == kInvalidServiceWorkerProviderId) | |
| 69 return; | |
| 70 | |
| 71 context_ = new ServiceWorkerProviderContext(provider_id_); | |
| 72 if (!ChildThreadImpl::current()) | |
| 73 return; // May be null in some tests. | |
| 74 | |
| 75 ChildThreadImpl::current()->Send(new ServiceWorkerHostMsg_ProviderCreated( | |
| 76 provider_id_, route_id, provider_type)); | |
| 77 } | |
| 78 | |
| 79 ServiceWorkerNetworkProvider::ServiceWorkerNetworkProvider( | |
| 80 int route_id, | |
| 49 ServiceWorkerProviderType provider_type) | 81 ServiceWorkerProviderType provider_type) |
| 50 : provider_id_(GenerateProviderIdForType(provider_type)) { | 82 : ServiceWorkerNetworkProvider(route_id, |
| 51 if (provider_id_ == kInvalidServiceWorkerProviderId) | 83 provider_type, |
| 52 return; | 84 kInvalidServiceWorkerProviderId) {} |
| 53 context_ = new ServiceWorkerProviderContext(provider_id_); | |
| 54 if (!ChildThreadImpl::current()) | |
| 55 return; // May be null in some tests. | |
| 56 ChildThreadImpl::current()->Send(new ServiceWorkerHostMsg_ProviderCreated( | |
| 57 provider_id_, route_id, provider_type)); | |
| 58 } | |
| 59 | 85 |
| 60 ServiceWorkerNetworkProvider::~ServiceWorkerNetworkProvider() { | 86 ServiceWorkerNetworkProvider::~ServiceWorkerNetworkProvider() { |
| 61 if (provider_id_ == kInvalidServiceWorkerProviderId) | 87 if (provider_id_ == kInvalidServiceWorkerProviderId) |
| 62 return; | 88 return; |
| 63 if (!ChildThreadImpl::current()) | 89 if (!ChildThreadImpl::current()) |
| 64 return; // May be null in some tests. | 90 return; // May be null in some tests. |
| 65 ChildThreadImpl::current()->Send( | 91 ChildThreadImpl::current()->Send( |
| 66 new ServiceWorkerHostMsg_ProviderDestroyed(provider_id_)); | 92 new ServiceWorkerHostMsg_ProviderDestroyed(provider_id_)); |
| 67 } | 93 } |
| 68 | 94 |
| 69 void ServiceWorkerNetworkProvider::SetServiceWorkerVersionId( | 95 void ServiceWorkerNetworkProvider::SetServiceWorkerVersionId( |
| 70 int64 version_id) { | 96 int64 version_id) { |
| 71 DCHECK_NE(kInvalidServiceWorkerProviderId, provider_id_); | 97 DCHECK_NE(kInvalidServiceWorkerProviderId, provider_id_); |
| 72 if (!ChildThreadImpl::current()) | 98 if (!ChildThreadImpl::current()) |
| 73 return; // May be null in some tests. | 99 return; // May be null in some tests. |
| 74 ChildThreadImpl::current()->Send( | 100 ChildThreadImpl::current()->Send( |
| 75 new ServiceWorkerHostMsg_SetVersionId(provider_id_, version_id)); | 101 new ServiceWorkerHostMsg_SetVersionId(provider_id_, version_id)); |
| 76 } | 102 } |
| 77 | 103 |
| 78 bool ServiceWorkerNetworkProvider::IsControlledByServiceWorker() const { | 104 bool ServiceWorkerNetworkProvider::IsControlledByServiceWorker() const { |
| 79 return context() && context()->controller(); | 105 return context() && context()->controller(); |
| 80 } | 106 } |
| 81 | 107 |
| 82 } // namespace content | 108 } // namespace content |
| OLD | NEW |