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.
5 #include "content/child/service_worker/service_worker_network_provider.h"
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"
16 const char kUserDataKey
[] = "SWProviderKey";
18 // Must be unique in the child process.
19 int GetNextProviderId() {
20 static base::StaticAtomicSequenceNumber sequence
;
21 return sequence
.GetNext(); // We start at zero.
26 void ServiceWorkerNetworkProvider::AttachToDocumentState(
27 base::SupportsUserData
* datasource_userdata
,
28 scoped_ptr
<ServiceWorkerNetworkProvider
> network_provider
) {
29 datasource_userdata
->SetUserData(&kUserDataKey
, network_provider
.release());
32 ServiceWorkerNetworkProvider
* ServiceWorkerNetworkProvider::FromDocumentState(
33 base::SupportsUserData
* datasource_userdata
) {
34 return static_cast<ServiceWorkerNetworkProvider
*>(
35 datasource_userdata
->GetUserData(&kUserDataKey
));
38 ServiceWorkerNetworkProvider::ServiceWorkerNetworkProvider(int render_frame_id
)
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_
, render_frame_id
));
47 ServiceWorkerNetworkProvider::~ServiceWorkerNetworkProvider() {
48 if (!ChildThread::current())
49 return; // May be null in some tests.
50 ChildThread::current()->Send(
51 new ServiceWorkerHostMsg_ProviderDestroyed(provider_id_
));
54 void ServiceWorkerNetworkProvider::SetServiceWorkerVersionId(
56 if (!ChildThread::current())
57 return; // May be null in some tests.
58 ChildThread::current()->Send(
59 new ServiceWorkerHostMsg_SetVersionId(provider_id_
, version_id
));
62 } // namespace content