ServiceWorker: Send state change events via SWProviderHost
[chromium-blink-merge.git] / content / browser / service_worker / service_worker_provider_host.cc
blob12512a122eda99d33d3713f994ada6064a231219
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 #include "content/browser/service_worker/service_worker_provider_host.h"
7 #include "base/stl_util.h"
8 #include "content/browser/message_port_message_filter.h"
9 #include "content/browser/service_worker/service_worker_context_core.h"
10 #include "content/browser/service_worker/service_worker_context_request_handler.h"
11 #include "content/browser/service_worker/service_worker_controllee_request_handler.h"
12 #include "content/browser/service_worker/service_worker_dispatcher_host.h"
13 #include "content/browser/service_worker/service_worker_handle.h"
14 #include "content/browser/service_worker/service_worker_registration_handle.h"
15 #include "content/browser/service_worker/service_worker_utils.h"
16 #include "content/browser/service_worker/service_worker_version.h"
17 #include "content/common/resource_request_body.h"
18 #include "content/common/service_worker/service_worker_messages.h"
19 #include "content/common/service_worker/service_worker_types.h"
20 #include "content/public/browser/render_frame_host.h"
21 #include "content/public/browser/web_contents.h"
22 #include "content/public/browser/web_contents_delegate.h"
23 #include "content/public/common/child_process_host.h"
25 namespace content {
27 namespace {
29 void FocusOnUIThread(int render_process_id,
30 int render_frame_id,
31 const ServiceWorkerProviderHost::FocusCallback& callback) {
32 WebContents* web_contents = WebContents::FromRenderFrameHost(
33 RenderFrameHost::FromID(render_process_id, render_frame_id));
35 bool result = false;
37 if (web_contents && web_contents->GetDelegate()) {
38 result = true;
39 web_contents->GetDelegate()->ActivateContents(web_contents);
42 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
43 base::Bind(callback, result));
46 } // anonymous namespace
48 ServiceWorkerProviderHost::ServiceWorkerProviderHost(
49 int render_process_id, int render_frame_id, int provider_id,
50 base::WeakPtr<ServiceWorkerContextCore> context,
51 ServiceWorkerDispatcherHost* dispatcher_host)
52 : render_process_id_(render_process_id),
53 render_frame_id_(render_frame_id),
54 provider_id_(provider_id),
55 context_(context),
56 dispatcher_host_(dispatcher_host),
57 allow_association_(true) {
58 DCHECK_NE(ChildProcessHost::kInvalidUniqueID, render_process_id_);
61 ServiceWorkerProviderHost::~ServiceWorkerProviderHost() {
62 // Clear docurl so the deferred activation of a waiting worker
63 // won't associate the new version with a provider being destroyed.
64 document_url_ = GURL();
65 if (controlling_version_.get())
66 controlling_version_->RemoveControllee(this);
67 if (associated_registration_.get()) {
68 DecreaseProcessReference(associated_registration_->pattern());
69 associated_registration_->RemoveListener(this);
72 for (const GURL& pattern : associated_patterns_)
73 DecreaseProcessReference(pattern);
76 void ServiceWorkerProviderHost::OnRegistrationFailed(
77 ServiceWorkerRegistration* registration) {
78 DCHECK_EQ(associated_registration_.get(), registration);
79 DisassociateRegistration();
82 void ServiceWorkerProviderHost::OnSkippedWaiting(
83 ServiceWorkerRegistration* registration) {
84 DCHECK_EQ(associated_registration_.get(), registration);
85 // A client is "using" a registration if it is controlled by the active
86 // worker of the registration. skipWaiting doesn't cause a client to start
87 // using the registration.
88 if (!controlling_version_)
89 return;
90 ServiceWorkerVersion* active_version = registration->active_version();
91 DCHECK_EQ(active_version->status(), ServiceWorkerVersion::ACTIVATING);
92 SetControllerVersionAttribute(active_version);
95 void ServiceWorkerProviderHost::SetDocumentUrl(const GURL& url) {
96 DCHECK(!url.has_ref());
97 document_url_ = url;
100 void ServiceWorkerProviderHost::SetTopmostFrameUrl(const GURL& url) {
101 topmost_frame_url_ = url;
104 void ServiceWorkerProviderHost::SetControllerVersionAttribute(
105 ServiceWorkerVersion* version) {
106 if (version == controlling_version_.get())
107 return;
109 scoped_refptr<ServiceWorkerVersion> previous_version = controlling_version_;
110 controlling_version_ = version;
111 if (version)
112 version->AddControllee(this);
113 if (previous_version.get())
114 previous_version->RemoveControllee(this);
116 if (!dispatcher_host_)
117 return; // Could be NULL in some tests.
119 bool should_notify_controllerchange =
120 previous_version && version && version->skip_waiting();
122 dispatcher_host_->Send(new ServiceWorkerMsg_SetControllerServiceWorker(
123 kDocumentMainThreadId, provider_id(),
124 CreateAndRegisterServiceWorkerHandle(version),
125 should_notify_controllerchange));
128 bool ServiceWorkerProviderHost::SetHostedVersionId(int64 version_id) {
129 if (!context_)
130 return true; // System is shutting down.
131 if (active_version())
132 return false; // Unexpected bad message.
134 ServiceWorkerVersion* live_version = context_->GetLiveVersion(version_id);
135 if (!live_version)
136 return true; // Was deleted before it got started.
138 ServiceWorkerVersionInfo info = live_version->GetInfo();
139 if (info.running_status != ServiceWorkerVersion::STARTING ||
140 info.process_id != render_process_id_) {
141 // If we aren't trying to start this version in our process
142 // something is amiss.
143 return false;
146 running_hosted_version_ = live_version;
147 return true;
150 void ServiceWorkerProviderHost::AssociateRegistration(
151 ServiceWorkerRegistration* registration) {
152 DCHECK(CanAssociateRegistration(registration));
153 if (associated_registration_.get())
154 DecreaseProcessReference(associated_registration_->pattern());
155 IncreaseProcessReference(registration->pattern());
157 associated_registration_ = registration;
158 associated_registration_->AddListener(this);
159 SendAssociateRegistrationMessage();
160 SetControllerVersionAttribute(registration->active_version());
163 void ServiceWorkerProviderHost::DisassociateRegistration() {
164 if (!associated_registration_.get())
165 return;
166 DecreaseProcessReference(associated_registration_->pattern());
167 associated_registration_->RemoveListener(this);
168 associated_registration_ = NULL;
169 SetControllerVersionAttribute(NULL);
171 if (dispatcher_host_) {
172 dispatcher_host_->Send(new ServiceWorkerMsg_DisassociateRegistration(
173 kDocumentMainThreadId, provider_id()));
177 scoped_ptr<ServiceWorkerRequestHandler>
178 ServiceWorkerProviderHost::CreateRequestHandler(
179 FetchRequestMode request_mode,
180 FetchCredentialsMode credentials_mode,
181 ResourceType resource_type,
182 RequestContextType request_context_type,
183 RequestContextFrameType frame_type,
184 base::WeakPtr<storage::BlobStorageContext> blob_storage_context,
185 scoped_refptr<ResourceRequestBody> body) {
186 if (IsHostToRunningServiceWorker()) {
187 return scoped_ptr<ServiceWorkerRequestHandler>(
188 new ServiceWorkerContextRequestHandler(
189 context_, AsWeakPtr(), blob_storage_context, resource_type));
191 if (ServiceWorkerUtils::IsMainResourceType(resource_type) ||
192 controlling_version()) {
193 return scoped_ptr<ServiceWorkerRequestHandler>(
194 new ServiceWorkerControlleeRequestHandler(context_,
195 AsWeakPtr(),
196 blob_storage_context,
197 request_mode,
198 credentials_mode,
199 resource_type,
200 request_context_type,
201 frame_type,
202 body));
204 return scoped_ptr<ServiceWorkerRequestHandler>();
207 ServiceWorkerObjectInfo
208 ServiceWorkerProviderHost::CreateAndRegisterServiceWorkerHandle(
209 ServiceWorkerVersion* version) {
210 DCHECK(dispatcher_host_);
211 ServiceWorkerObjectInfo info;
212 if (context_ && version) {
213 scoped_ptr<ServiceWorkerHandle> handle =
214 ServiceWorkerHandle::Create(context_, AsWeakPtr(), version);
215 info = handle->GetObjectInfo();
216 dispatcher_host_->RegisterServiceWorkerHandle(handle.Pass());
218 return info;
221 bool ServiceWorkerProviderHost::CanAssociateRegistration(
222 ServiceWorkerRegistration* registration) {
223 if (!context_)
224 return false;
225 if (running_hosted_version_.get())
226 return false;
227 if (!registration || associated_registration_.get() || !allow_association_)
228 return false;
229 return true;
232 void ServiceWorkerProviderHost::PostMessage(
233 const base::string16& message,
234 const std::vector<int>& sent_message_port_ids) {
235 if (!dispatcher_host_)
236 return; // Could be NULL in some tests.
238 std::vector<int> new_routing_ids;
239 dispatcher_host_->message_port_message_filter()->
240 UpdateMessagePortsWithNewRoutes(sent_message_port_ids,
241 &new_routing_ids);
243 dispatcher_host_->Send(
244 new ServiceWorkerMsg_MessageToDocument(
245 kDocumentMainThreadId, provider_id(),
246 message,
247 sent_message_port_ids,
248 new_routing_ids));
251 void ServiceWorkerProviderHost::Focus(const FocusCallback& callback) {
252 BrowserThread::PostTask(
253 BrowserThread::UI, FROM_HERE,
254 base::Bind(&FocusOnUIThread,
255 render_process_id_,
256 render_frame_id_,
257 callback));
260 void ServiceWorkerProviderHost::GetClientInfo(
261 int embedded_worker_id,
262 int request_id) {
263 dispatcher_host_->Send(new ServiceWorkerMsg_GetClientInfo(
264 kDocumentMainThreadId, embedded_worker_id, request_id, provider_id()));
267 void ServiceWorkerProviderHost::AddScopedProcessReferenceToPattern(
268 const GURL& pattern) {
269 associated_patterns_.push_back(pattern);
270 IncreaseProcessReference(pattern);
273 void ServiceWorkerProviderHost::PrepareForCrossSiteTransfer() {
274 DCHECK_NE(ChildProcessHost::kInvalidUniqueID, render_process_id_);
276 for (const GURL& pattern : associated_patterns_)
277 DecreaseProcessReference(pattern);
279 if (associated_registration_.get()) {
280 DecreaseProcessReference(associated_registration_->pattern());
281 if (dispatcher_host_) {
282 dispatcher_host_->Send(new ServiceWorkerMsg_DisassociateRegistration(
283 kDocumentMainThreadId, provider_id()));
287 render_process_id_ = ChildProcessHost::kInvalidUniqueID;
288 render_frame_id_ = MSG_ROUTING_NONE;
289 provider_id_ = kInvalidServiceWorkerProviderId;
290 dispatcher_host_ = nullptr;
293 void ServiceWorkerProviderHost::CompleteCrossSiteTransfer(
294 int new_process_id,
295 int new_frame_id,
296 int new_provider_id,
297 ServiceWorkerDispatcherHost* new_dispatcher_host) {
298 DCHECK_EQ(ChildProcessHost::kInvalidUniqueID, render_process_id_);
299 DCHECK_NE(ChildProcessHost::kInvalidUniqueID, new_process_id);
301 render_process_id_ = new_process_id;
302 render_frame_id_ = new_frame_id;
303 provider_id_ = new_provider_id;
304 dispatcher_host_ = new_dispatcher_host;
306 for (const GURL& pattern : associated_patterns_)
307 IncreaseProcessReference(pattern);
309 if (associated_registration_.get()) {
310 IncreaseProcessReference(associated_registration_->pattern());
311 SendAssociateRegistrationMessage();
312 if (dispatcher_host_ && associated_registration_->active_version()) {
313 dispatcher_host_->Send(new ServiceWorkerMsg_SetControllerServiceWorker(
314 kDocumentMainThreadId, provider_id(),
315 CreateAndRegisterServiceWorkerHandle(
316 associated_registration_->active_version()),
317 false /* shouldNotifyControllerChange */));
322 void ServiceWorkerProviderHost::SendUpdateFoundMessage(
323 const ServiceWorkerRegistrationObjectInfo& object_info) {
324 if (!dispatcher_host_)
325 return; // Could be nullptr in some tests.
327 // TODO(nhiroki): Queue the message if a receiver's thread is not ready yet
328 // (http://crbug.com/437677).
329 dispatcher_host_->Send(new ServiceWorkerMsg_UpdateFound(
330 kDocumentMainThreadId, object_info));
333 void ServiceWorkerProviderHost::SendSetVersionAttributesMessage(
334 int registration_handle_id,
335 ChangedVersionAttributesMask changed_mask,
336 ServiceWorkerVersion* installing_version,
337 ServiceWorkerVersion* waiting_version,
338 ServiceWorkerVersion* active_version) {
339 if (!dispatcher_host_)
340 return; // Could be nullptr in some tests.
341 if (!changed_mask.changed())
342 return;
344 ServiceWorkerVersionAttributes attrs;
345 if (changed_mask.installing_changed())
346 attrs.installing = CreateAndRegisterServiceWorkerHandle(installing_version);
347 if (changed_mask.waiting_changed())
348 attrs.waiting = CreateAndRegisterServiceWorkerHandle(waiting_version);
349 if (changed_mask.active_changed())
350 attrs.active = CreateAndRegisterServiceWorkerHandle(active_version);
352 // TODO(nhiroki): Queue the message if a receiver's thread is not ready yet
353 // (http://crbug.com/437677).
354 dispatcher_host_->Send(new ServiceWorkerMsg_SetVersionAttributes(
355 kDocumentMainThreadId, provider_id_, registration_handle_id,
356 changed_mask.changed(), attrs));
359 void ServiceWorkerProviderHost::SendServiceWorkerStateChangedMessage(
360 int worker_handle_id,
361 blink::WebServiceWorkerState state) {
362 if (!dispatcher_host_)
363 return;
365 // TODO(nhiroki): Queue the message if a receiver's thread is not ready yet
366 // (http://crbug.com/437677).
367 dispatcher_host_->Send(new ServiceWorkerMsg_ServiceWorkerStateChanged(
368 kDocumentMainThreadId, worker_handle_id, state));
371 void ServiceWorkerProviderHost::SendAssociateRegistrationMessage() {
372 if (!dispatcher_host_)
373 return;
375 ServiceWorkerRegistrationHandle* handle =
376 dispatcher_host_->GetOrCreateRegistrationHandle(
377 AsWeakPtr(), associated_registration_.get());
379 ServiceWorkerVersionAttributes attrs;
380 attrs.installing = CreateAndRegisterServiceWorkerHandle(
381 associated_registration_->installing_version());
382 attrs.waiting = CreateAndRegisterServiceWorkerHandle(
383 associated_registration_->waiting_version());
384 attrs.active = CreateAndRegisterServiceWorkerHandle(
385 associated_registration_->active_version());
387 dispatcher_host_->Send(new ServiceWorkerMsg_AssociateRegistration(
388 kDocumentMainThreadId, provider_id(), handle->GetObjectInfo(), attrs));
391 void ServiceWorkerProviderHost::IncreaseProcessReference(
392 const GURL& pattern) {
393 if (context_ && context_->process_manager()) {
394 context_->process_manager()->AddProcessReferenceToPattern(
395 pattern, render_process_id_);
399 void ServiceWorkerProviderHost::DecreaseProcessReference(
400 const GURL& pattern) {
401 if (context_ && context_->process_manager()) {
402 context_->process_manager()->RemoveProcessReferenceFromPattern(
403 pattern, render_process_id_);
407 bool ServiceWorkerProviderHost::IsContextAlive() {
408 return context_ != NULL;
411 } // namespace content