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
3 // found in the LICENSE file.
5 #ifndef CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_PROVIDER_HOST_H_
6 #define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_PROVIDER_HOST_H_
11 #include "base/memory/ref_counted.h"
12 #include "base/memory/weak_ptr.h"
13 #include "content/browser/service_worker/service_worker_registration.h"
14 #include "content/common/content_export.h"
15 #include "content/common/service_worker/service_worker_types.h"
16 #include "content/public/common/resource_type.h"
23 class BlobStorageContext
;
28 class ResourceRequestBody
;
29 class ServiceWorkerContextCore
;
30 class ServiceWorkerDispatcherHost
;
31 class ServiceWorkerRequestHandler
;
32 class ServiceWorkerVersion
;
34 // This class is the browser-process representation of a service worker
35 // provider. There is a provider per document and the lifetime of this
36 // object is tied to the lifetime of its document in the renderer process.
37 // This class holds service worker state that is scoped to an individual
40 // Note this class can also host a running service worker, in which
41 // case it will observe resource loads made directly by the service worker.
42 class CONTENT_EXPORT ServiceWorkerProviderHost
43 : public ServiceWorkerRegistration::Listener
,
44 public base::SupportsWeakPtr
<ServiceWorkerProviderHost
> {
46 ServiceWorkerProviderHost(int process_id
,
48 base::WeakPtr
<ServiceWorkerContextCore
> context
,
49 ServiceWorkerDispatcherHost
* dispatcher_host
);
50 virtual ~ServiceWorkerProviderHost();
52 int process_id() const { return process_id_
; }
53 int provider_id() const { return provider_id_
; }
55 bool IsHostToRunningServiceWorker() {
56 return running_hosted_version_
.get() != NULL
;
59 // Getters for the navigator.serviceWorker attribute values.
60 ServiceWorkerVersion
* controlling_version() const {
61 return controlling_version_
.get();
63 ServiceWorkerVersion
* active_version() const {
64 return active_version_
.get();
66 ServiceWorkerVersion
* waiting_version() const {
67 return waiting_version_
.get();
69 ServiceWorkerVersion
* installing_version() const {
70 return installing_version_
.get();
73 // The running version, if any, that this provider is providing resource
75 ServiceWorkerVersion
* running_hosted_version() const {
76 return running_hosted_version_
.get();
79 void SetDocumentUrl(const GURL
& url
);
80 const GURL
& document_url() const { return document_url_
; }
82 // Associates to |registration| to listen for its version change events.
83 void AssociateRegistration(ServiceWorkerRegistration
* registration
);
85 // Clears the associated registration and stop listening to it.
86 void UnassociateRegistration();
88 // Returns false if the version is not in the expected STARTING in our
89 // process state. That would be indicative of a bad IPC message.
90 bool SetHostedVersionId(int64 versions_id
);
92 // Returns a handler for a request, the handler may return NULL if
93 // the request doesn't require special handling.
94 scoped_ptr
<ServiceWorkerRequestHandler
> CreateRequestHandler(
95 ResourceType resource_type
,
96 base::WeakPtr
<storage::BlobStorageContext
> blob_storage_context
,
97 scoped_refptr
<ResourceRequestBody
> body
);
99 // Returns true if |registration| can be associated with this provider.
100 bool CanAssociateRegistration(ServiceWorkerRegistration
* registration
);
102 // Returns true if the context referred to by this host (i.e. |context_|) is
104 bool IsContextAlive();
106 // Dispatches message event to the document.
107 void PostMessage(const base::string16
& message
,
108 const std::vector
<int>& sent_message_port_ids
);
111 friend class ServiceWorkerProviderHostTest
;
112 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerContextRequestHandlerTest
,
113 UpdateBefore24Hours
);
114 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerContextRequestHandlerTest
,
117 // ServiceWorkerRegistration::Listener overrides.
118 virtual void OnVersionAttributesChanged(
119 ServiceWorkerRegistration
* registration
,
120 ChangedVersionAttributesMask changed_mask
,
121 const ServiceWorkerRegistrationInfo
& info
) OVERRIDE
;
122 virtual void OnRegistrationFailed(
123 ServiceWorkerRegistration
* registration
) OVERRIDE
;
125 // Sets the corresponding version field to the given version or if the given
126 // version is NULL, clears the field.
127 void SetVersionAttributes(
128 ServiceWorkerVersion
* installing_version
,
129 ServiceWorkerVersion
* waiting_version
,
130 ServiceWorkerVersion
* active_version
);
131 void SetVersionAttributesInternal(
132 ServiceWorkerVersion
* version
,
133 scoped_refptr
<ServiceWorkerVersion
>* data_member
);
135 // Sets the controller version field to |version| or if |version| is NULL,
137 void SetControllerVersionAttribute(ServiceWorkerVersion
* version
);
139 // Clears all version fields.
140 void ClearVersionAttributes();
142 // Creates a ServiceWorkerHandle to retain |version| and returns a
143 // ServiceWorkerInfo with the handle ID to pass to the provider. The
144 // provider is responsible for releasing the handle.
145 ServiceWorkerObjectInfo
CreateHandleAndPass(ServiceWorkerVersion
* version
);
147 const int process_id_
;
148 const int provider_id_
;
151 scoped_refptr
<ServiceWorkerRegistration
> associated_registration_
;
153 scoped_refptr
<ServiceWorkerVersion
> controlling_version_
;
154 scoped_refptr
<ServiceWorkerVersion
> active_version_
;
155 scoped_refptr
<ServiceWorkerVersion
> waiting_version_
;
156 scoped_refptr
<ServiceWorkerVersion
> installing_version_
;
157 scoped_refptr
<ServiceWorkerVersion
> running_hosted_version_
;
158 base::WeakPtr
<ServiceWorkerContextCore
> context_
;
159 ServiceWorkerDispatcherHost
* dispatcher_host_
;
161 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerProviderHost
);
164 } // namespace content
166 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_PROVIDER_HOST_H_