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 3 // found in the LICENSE file. 4 5 #include "content/child/service_worker/service_worker_network_provider.h" 6 7 #include "base/atomic_sequence_num.h" 8 #include "content/child/child_thread.h" 9 #include "content/child/service_worker/service_worker_provider_context.h" 10 #include "content/common/service_worker/service_worker_messages.h" 11 12 namespace content { 13 14 namespace { 15 16 const char kUserDataKey[] = "SWProviderKey"; 17 18 // Must be unique in the child process. GetNextProviderId()19int GetNextProviderId() { 20 static base::StaticAtomicSequenceNumber sequence; 21 return sequence.GetNext(); // We start at zero. 22 } 23 24 } // namespace 25 AttachToDocumentState(base::SupportsUserData * datasource_userdata,scoped_ptr<ServiceWorkerNetworkProvider> network_provider)26void ServiceWorkerNetworkProvider::AttachToDocumentState( 27 base::SupportsUserData* datasource_userdata, 28 scoped_ptr<ServiceWorkerNetworkProvider> network_provider) { 29 datasource_userdata->SetUserData(&kUserDataKey, network_provider.release()); 30 } 31 FromDocumentState(base::SupportsUserData * datasource_userdata)32ServiceWorkerNetworkProvider* ServiceWorkerNetworkProvider::FromDocumentState( 33 base::SupportsUserData* datasource_userdata) { 34 return static_cast<ServiceWorkerNetworkProvider*>( 35 datasource_userdata->GetUserData(&kUserDataKey)); 36 } 37 ServiceWorkerNetworkProvider()38ServiceWorkerNetworkProvider::ServiceWorkerNetworkProvider() 39 : provider_id_(GetNextProviderId()), 40 context_(new ServiceWorkerProviderContext(provider_id_)) { 41 if (!ChildThread::current()) 42 return; // May be null in some tests. 43 ChildThread::current()->Send( 44 new ServiceWorkerHostMsg_ProviderCreated(provider_id_)); 45 } 46 ~ServiceWorkerNetworkProvider()47ServiceWorkerNetworkProvider::~ServiceWorkerNetworkProvider() { 48 if (!ChildThread::current()) 49 return; // May be null in some tests. 50 ChildThread::current()->Send( 51 new ServiceWorkerHostMsg_ProviderDestroyed(provider_id_)); 52 } 53 SetServiceWorkerVersionId(int64 version_id)54void ServiceWorkerNetworkProvider::SetServiceWorkerVersionId( 55 int64 version_id) { 56 if (!ChildThread::current()) 57 return; // May be null in some tests. 58 ChildThread::current()->Send( 59 new ServiceWorkerHostMsg_SetVersionId(provider_id_, version_id)); 60 } 61 62 } // namespace content 63