[Storage] Blob Storage Refactoring pt 1:
[chromium-blink-merge.git] / content / browser / service_worker / service_worker_provider_host.h
blob03cb0c638d04a2e688e2a9d0843d0bac1efd9da2
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_
8 #include <set>
9 #include <vector>
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/request_context_frame_type.h"
17 #include "content/public/common/request_context_type.h"
18 #include "content/public/common/resource_type.h"
20 namespace IPC {
21 class Sender;
24 namespace storage {
25 class BlobStorageContext;
28 namespace content {
30 class ResourceRequestBody;
31 class ServiceWorkerContextCore;
32 class ServiceWorkerDispatcherHost;
33 class ServiceWorkerRequestHandler;
34 class ServiceWorkerVersion;
36 // This class is the browser-process representation of a service worker
37 // provider. There is a provider per document and the lifetime of this
38 // object is tied to the lifetime of its document in the renderer process.
39 // This class holds service worker state that is scoped to an individual
40 // document.
42 // Note this class can also host a running service worker, in which
43 // case it will observe resource loads made directly by the service worker.
44 class CONTENT_EXPORT ServiceWorkerProviderHost
45 : public NON_EXPORTED_BASE(ServiceWorkerRegistration::Listener),
46 public base::SupportsWeakPtr<ServiceWorkerProviderHost> {
47 public:
48 typedef base::Callback<void(bool)> FocusCallback;
50 ServiceWorkerProviderHost(int render_process_id,
51 int render_frame_id,
52 int provider_id,
53 base::WeakPtr<ServiceWorkerContextCore> context,
54 ServiceWorkerDispatcherHost* dispatcher_host);
55 virtual ~ServiceWorkerProviderHost();
57 int process_id() const { return render_process_id_; }
58 int provider_id() const { return provider_id_; }
59 int frame_id() const { return render_frame_id_; }
61 bool IsHostToRunningServiceWorker() {
62 return running_hosted_version_.get() != NULL;
65 ServiceWorkerVersion* controlling_version() const {
66 return controlling_version_.get();
68 ServiceWorkerVersion* active_version() const {
69 return associated_registration_.get() ?
70 associated_registration_->active_version() : NULL;
72 ServiceWorkerVersion* waiting_version() const {
73 return associated_registration_.get() ?
74 associated_registration_->waiting_version() : NULL;
76 ServiceWorkerVersion* installing_version() const {
77 return associated_registration_.get() ?
78 associated_registration_->installing_version() : NULL;
81 ServiceWorkerRegistration* associated_registration() const {
82 return associated_registration_.get();
85 // The running version, if any, that this provider is providing resource
86 // loads for.
87 ServiceWorkerVersion* running_hosted_version() const {
88 return running_hosted_version_.get();
91 void SetDocumentUrl(const GURL& url);
92 const GURL& document_url() const { return document_url_; }
94 void SetTopmostFrameUrl(const GURL& url);
95 const GURL& topmost_frame_url() const { return topmost_frame_url_; }
97 // Associates to |registration| to listen for its version change events.
98 void AssociateRegistration(ServiceWorkerRegistration* registration);
100 // Clears the associated registration and stop listening to it.
101 void DisassociateRegistration();
103 // Returns false if the version is not in the expected STARTING in our
104 // process state. That would be indicative of a bad IPC message.
105 bool SetHostedVersionId(int64 versions_id);
107 // Returns a handler for a request, the handler may return NULL if
108 // the request doesn't require special handling.
109 scoped_ptr<ServiceWorkerRequestHandler> CreateRequestHandler(
110 FetchRequestMode request_mode,
111 FetchCredentialsMode credentials_mode,
112 ResourceType resource_type,
113 RequestContextType request_context_type,
114 RequestContextFrameType frame_type,
115 base::WeakPtr<storage::BlobStorageContext> blob_storage_context,
116 scoped_refptr<ResourceRequestBody> body);
118 // Creates a ServiceWorkerHandle to retain |version| and returns a
119 // ServiceWorkerInfo with a newly created handle ID. The handle is held in
120 // the dispatcher host until its ref-count becomes zero.
121 ServiceWorkerObjectInfo CreateAndRegisterServiceWorkerHandle(
122 ServiceWorkerVersion* version);
124 // Returns true if |registration| can be associated with this provider.
125 bool CanAssociateRegistration(ServiceWorkerRegistration* registration);
127 // For use by the ServiceWorkerControlleeRequestHandler to disallow
128 // new registration association while a navigation is occurring and
129 // an existing registration is being looked for.
130 void SetAllowAssociation(bool allow) { allow_association_ = allow; }
132 // Returns true if the context referred to by this host (i.e. |context_|) is
133 // still alive.
134 bool IsContextAlive();
136 // Dispatches message event to the document.
137 void PostMessage(const base::string16& message,
138 const std::vector<int>& sent_message_port_ids);
140 // Activates the WebContents associated with
141 // { render_process_id_, render_frame_id_ }.
142 // Runs the |callback| with the result in parameter describing whether the
143 // focusing action was successful.
144 void Focus(const FocusCallback& callback);
146 // Asks the renderer to send back the document information.
147 void GetClientInfo(int embedded_worker_id, int request_id);
149 // Adds reference of this host's process to the |pattern|, the reference will
150 // be removed in destructor.
151 void AddScopedProcessReferenceToPattern(const GURL& pattern);
153 // Methods to support cross site navigations.
154 void PrepareForCrossSiteTransfer();
155 void CompleteCrossSiteTransfer(
156 int new_process_id,
157 int new_frame_id,
158 int new_provider_id,
159 ServiceWorkerDispatcherHost* dispatcher_host);
160 ServiceWorkerDispatcherHost* dispatcher_host() const {
161 return dispatcher_host_;
164 // Called from ServiceWorkerRegistrationHandle.
165 void SendUpdateFoundMessage(
166 const ServiceWorkerRegistrationObjectInfo& object_info);
167 void SendSetVersionAttributesMessage(
168 int registration_handle_id,
169 ChangedVersionAttributesMask changed_mask,
170 ServiceWorkerVersion* installing_version,
171 ServiceWorkerVersion* waiting_version,
172 ServiceWorkerVersion* active_version);
174 // Called from ServiceWorkerHandle.
175 void SendServiceWorkerStateChangedMessage(
176 int worker_handle_id,
177 blink::WebServiceWorkerState state);
179 private:
180 friend class ServiceWorkerProviderHostTest;
181 friend class ServiceWorkerWriteToCacheJobTest;
182 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerContextRequestHandlerTest,
183 UpdateBefore24Hours);
184 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerContextRequestHandlerTest,
185 UpdateAfter24Hours);
187 // ServiceWorkerRegistration::Listener overrides.
188 void OnRegistrationFailed(ServiceWorkerRegistration* registration) override;
189 void OnSkippedWaiting(ServiceWorkerRegistration* registration) override;
191 // Sets the controller version field to |version| or if |version| is NULL,
192 // clears the field.
193 void SetControllerVersionAttribute(ServiceWorkerVersion* version);
195 void SendAssociateRegistrationMessage();
197 // Increase/decrease this host's process reference for |pattern|.
198 void IncreaseProcessReference(const GURL& pattern);
199 void DecreaseProcessReference(const GURL& pattern);
201 int render_process_id_;
202 int render_frame_id_;
203 int provider_id_;
204 GURL document_url_;
205 GURL topmost_frame_url_;
207 std::vector<GURL> associated_patterns_;
208 scoped_refptr<ServiceWorkerRegistration> associated_registration_;
210 scoped_refptr<ServiceWorkerVersion> controlling_version_;
211 scoped_refptr<ServiceWorkerVersion> running_hosted_version_;
212 base::WeakPtr<ServiceWorkerContextCore> context_;
213 ServiceWorkerDispatcherHost* dispatcher_host_;
214 bool allow_association_;
216 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerProviderHost);
219 } // namespace content
221 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_PROVIDER_HOST_H_