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 #ifndef CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_PROVIDER_HOST_H_ | 5 #ifndef CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_PROVIDER_HOST_H_ |
6 #define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_PROVIDER_HOST_H_ | 6 #define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_PROVIDER_HOST_H_ |
7 | 7 |
8 #include <set> | 8 #include <set> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
12 #include "base/memory/weak_ptr.h" | 12 #include "base/memory/weak_ptr.h" |
| 13 #include "content/browser/service_worker/service_worker_registration.h" |
13 #include "content/common/content_export.h" | 14 #include "content/common/content_export.h" |
14 #include "content/common/service_worker/service_worker_types.h" | 15 #include "content/common/service_worker/service_worker_types.h" |
15 #include "content/public/common/resource_type.h" | 16 #include "content/public/common/resource_type.h" |
16 | 17 |
17 namespace IPC { | 18 namespace IPC { |
18 class Sender; | 19 class Sender; |
19 } | 20 } |
20 | 21 |
21 namespace webkit_blob { | 22 namespace webkit_blob { |
22 class BlobStorageContext; | 23 class BlobStorageContext; |
23 } | 24 } |
24 | 25 |
25 namespace content { | 26 namespace content { |
26 | 27 |
27 class ServiceWorkerContextCore; | 28 class ServiceWorkerContextCore; |
28 class ServiceWorkerDispatcherHost; | 29 class ServiceWorkerDispatcherHost; |
29 class ServiceWorkerRequestHandler; | 30 class ServiceWorkerRequestHandler; |
30 class ServiceWorkerVersion; | 31 class ServiceWorkerVersion; |
31 | 32 |
32 // This class is the browser-process representation of a service worker | 33 // This class is the browser-process representation of a service worker |
33 // provider. There is a provider per document and the lifetime of this | 34 // provider. There is a provider per document and the lifetime of this |
34 // object is tied to the lifetime of its document in the renderer process. | 35 // object is tied to the lifetime of its document in the renderer process. |
35 // This class holds service worker state that is scoped to an individual | 36 // This class holds service worker state that is scoped to an individual |
36 // document. | 37 // document. |
37 // | 38 // |
38 // Note this class can also host a running service worker, in which | 39 // Note this class can also host a running service worker, in which |
39 // case it will observe resource loads made directly by the service worker. | 40 // case it will observe resource loads made directly by the service worker. |
40 class CONTENT_EXPORT ServiceWorkerProviderHost | 41 class CONTENT_EXPORT ServiceWorkerProviderHost |
41 : public base::SupportsWeakPtr<ServiceWorkerProviderHost> { | 42 : public ServiceWorkerRegistration::Listener, |
| 43 public base::SupportsWeakPtr<ServiceWorkerProviderHost> { |
42 public: | 44 public: |
43 ServiceWorkerProviderHost(int process_id, | 45 ServiceWorkerProviderHost(int process_id, |
44 int provider_id, | 46 int provider_id, |
45 base::WeakPtr<ServiceWorkerContextCore> context, | 47 base::WeakPtr<ServiceWorkerContextCore> context, |
46 ServiceWorkerDispatcherHost* dispatcher_host); | 48 ServiceWorkerDispatcherHost* dispatcher_host); |
47 ~ServiceWorkerProviderHost(); | 49 virtual ~ServiceWorkerProviderHost(); |
48 | 50 |
49 int process_id() const { return process_id_; } | 51 int process_id() const { return process_id_; } |
50 int provider_id() const { return provider_id_; } | 52 int provider_id() const { return provider_id_; } |
51 | 53 |
52 bool IsHostToRunningServiceWorker() { | 54 bool IsHostToRunningServiceWorker() { |
53 return running_hosted_version_ != NULL; | 55 return running_hosted_version_ != NULL; |
54 } | 56 } |
55 | 57 |
56 // Getters for the navigator.serviceWorker attribute values. | 58 // Getters for the navigator.serviceWorker attribute values. |
57 ServiceWorkerVersion* controlling_version() const { | 59 ServiceWorkerVersion* controlling_version() const { |
(...skipping 11 matching lines...) Expand all Loading... |
69 | 71 |
70 // The running version, if any, that this provider is providing resource | 72 // The running version, if any, that this provider is providing resource |
71 // loads for. | 73 // loads for. |
72 ServiceWorkerVersion* running_hosted_version() const { | 74 ServiceWorkerVersion* running_hosted_version() const { |
73 return running_hosted_version_.get(); | 75 return running_hosted_version_.get(); |
74 } | 76 } |
75 | 77 |
76 void SetDocumentUrl(const GURL& url); | 78 void SetDocumentUrl(const GURL& url); |
77 const GURL& document_url() const { return document_url_; } | 79 const GURL& document_url() const { return document_url_; } |
78 | 80 |
79 // Associates |version| to the corresponding field or if |version| is NULL | 81 // Associates |version| to the controller field or if |version| is NULL clears |
80 // clears the field. | 82 // the field. |
81 void SetControllerVersion(ServiceWorkerVersion* version); | 83 void SetControllerVersion(ServiceWorkerVersion* version); |
82 void SetActiveVersion(ServiceWorkerVersion* version); | |
83 void SetWaitingVersion(ServiceWorkerVersion* version); | |
84 void SetInstallingVersion(ServiceWorkerVersion* version); | |
85 | 84 |
86 // If |version| is the installing, waiting, or active version of this | 85 // Associates to |registration| to listen for its version change events. |
87 // provider, the method will reset that field to NULL. | 86 void AssociateRegistration(ServiceWorkerRegistration* registration); |
88 void UnsetVersion(ServiceWorkerVersion* version); | 87 |
| 88 // Clears the associated registration and stop listening to it. |
| 89 void UnassociateRegistration(); |
89 | 90 |
90 // Returns false if the version is not in the expected STARTING in our | 91 // Returns false if the version is not in the expected STARTING in our |
91 // process state. That would be indicative of a bad IPC message. | 92 // process state. That would be indicative of a bad IPC message. |
92 bool SetHostedVersionId(int64 versions_id); | 93 bool SetHostedVersionId(int64 versions_id); |
93 | 94 |
94 // Returns a handler for a request, the handler may return NULL if | 95 // Returns a handler for a request, the handler may return NULL if |
95 // the request doesn't require special handling. | 96 // the request doesn't require special handling. |
96 scoped_ptr<ServiceWorkerRequestHandler> CreateRequestHandler( | 97 scoped_ptr<ServiceWorkerRequestHandler> CreateRequestHandler( |
97 ResourceType resource_type, | 98 ResourceType resource_type, |
98 base::WeakPtr<webkit_blob::BlobStorageContext> blob_storage_context); | 99 base::WeakPtr<webkit_blob::BlobStorageContext> blob_storage_context); |
99 | 100 |
100 // Returns true if |version| can be associated with this provider. | 101 // Returns true if |version| can be associated with this provider. |
101 bool CanAssociateVersion(ServiceWorkerVersion* version); | 102 bool CanAssociateVersion(ServiceWorkerVersion* version); |
102 | 103 |
| 104 // Returns true if |registration| can be associated with this provider. |
| 105 bool CanAssociateRegistration(ServiceWorkerRegistration* registration); |
| 106 |
103 // Returns true if the context referred to by this host (i.e. |context_|) is | 107 // Returns true if the context referred to by this host (i.e. |context_|) is |
104 // still alive. | 108 // still alive. |
105 bool IsContextAlive(); | 109 bool IsContextAlive(); |
106 | 110 |
107 // Dispatches message event to the document. | 111 // Dispatches message event to the document. |
108 void PostMessage(const base::string16& message, | 112 void PostMessage(const base::string16& message, |
109 const std::vector<int>& sent_message_port_ids); | 113 const std::vector<int>& sent_message_port_ids); |
110 | 114 |
111 private: | 115 private: |
| 116 friend class ServiceWorkerProviderHostTest; |
| 117 |
| 118 // ServiceWorkerRegistration::Listener overrides. |
| 119 virtual void OnVersionAttributesChanged( |
| 120 ServiceWorkerRegistration* registration, |
| 121 ChangedVersionAttributesMask changed_mask, |
| 122 const ServiceWorkerRegistrationInfo& info) OVERRIDE; |
| 123 virtual void OnRegistrationFailed( |
| 124 ServiceWorkerRegistration* registration) OVERRIDE; |
| 125 |
| 126 // Associates |version| to the corresponding field or if |version| is NULL |
| 127 // clears the field. |
| 128 void SetActiveVersion(ServiceWorkerVersion* version); |
| 129 void SetWaitingVersion(ServiceWorkerVersion* version); |
| 130 void SetInstallingVersion(ServiceWorkerVersion* version); |
| 131 |
| 132 // If |version| is the installing, waiting, or active version of this |
| 133 // provider, the method will reset that field to NULL. |
| 134 void UnsetVersion(ServiceWorkerVersion* version); |
| 135 |
112 // Creates a ServiceWorkerHandle to retain |version| and returns a | 136 // Creates a ServiceWorkerHandle to retain |version| and returns a |
113 // ServiceWorkerInfo with the handle ID to pass to the provider. The | 137 // ServiceWorkerInfo with the handle ID to pass to the provider. The |
114 // provider is responsible for releasing the handle. | 138 // provider is responsible for releasing the handle. |
115 ServiceWorkerObjectInfo CreateHandleAndPass(ServiceWorkerVersion* version); | 139 ServiceWorkerObjectInfo CreateHandleAndPass(ServiceWorkerVersion* version); |
116 | 140 |
117 const int process_id_; | 141 const int process_id_; |
118 const int provider_id_; | 142 const int provider_id_; |
119 GURL document_url_; | 143 GURL document_url_; |
120 | 144 |
| 145 scoped_refptr<ServiceWorkerRegistration> associated_registration_; |
| 146 |
121 scoped_refptr<ServiceWorkerVersion> controlling_version_; | 147 scoped_refptr<ServiceWorkerVersion> controlling_version_; |
122 scoped_refptr<ServiceWorkerVersion> active_version_; | 148 scoped_refptr<ServiceWorkerVersion> active_version_; |
123 scoped_refptr<ServiceWorkerVersion> waiting_version_; | 149 scoped_refptr<ServiceWorkerVersion> waiting_version_; |
124 scoped_refptr<ServiceWorkerVersion> installing_version_; | 150 scoped_refptr<ServiceWorkerVersion> installing_version_; |
125 scoped_refptr<ServiceWorkerVersion> running_hosted_version_; | 151 scoped_refptr<ServiceWorkerVersion> running_hosted_version_; |
126 base::WeakPtr<ServiceWorkerContextCore> context_; | 152 base::WeakPtr<ServiceWorkerContextCore> context_; |
127 ServiceWorkerDispatcherHost* dispatcher_host_; | 153 ServiceWorkerDispatcherHost* dispatcher_host_; |
128 | 154 |
129 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerProviderHost); | 155 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerProviderHost); |
130 }; | 156 }; |
131 | 157 |
132 } // namespace content | 158 } // namespace content |
133 | 159 |
134 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_PROVIDER_HOST_H_ | 160 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_PROVIDER_HOST_H_ |
OLD | NEW |