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"
27 static const int kDocumentMainThreadId
= 0;
31 void FocusOnUIThread(int render_process_id
,
33 const ServiceWorkerProviderHost::FocusCallback
& callback
) {
34 WebContents
* web_contents
= WebContents::FromRenderFrameHost(
35 RenderFrameHost::FromID(render_process_id
, render_frame_id
));
39 if (web_contents
&& web_contents
->GetDelegate()) {
41 web_contents
->GetDelegate()->ActivateContents(web_contents
);
44 BrowserThread::PostTask(BrowserThread::IO
, FROM_HERE
,
45 base::Bind(callback
, result
));
48 } // anonymous namespace
50 ServiceWorkerProviderHost::ServiceWorkerProviderHost(
51 int render_process_id
, int render_frame_id
, int provider_id
,
52 base::WeakPtr
<ServiceWorkerContextCore
> context
,
53 ServiceWorkerDispatcherHost
* dispatcher_host
)
54 : render_process_id_(render_process_id
),
55 render_frame_id_(render_frame_id
),
56 provider_id_(provider_id
),
58 dispatcher_host_(dispatcher_host
),
59 allow_association_(true) {
60 DCHECK_NE(ChildProcessHost::kInvalidUniqueID
, render_process_id_
);
63 ServiceWorkerProviderHost::~ServiceWorkerProviderHost() {
64 // Clear docurl so the deferred activation of a waiting worker
65 // won't associate the new version with a provider being destroyed.
66 document_url_
= GURL();
67 if (controlling_version_
.get())
68 controlling_version_
->RemoveControllee(this);
69 if (associated_registration_
.get()) {
70 DecreaseProcessReference(associated_registration_
->pattern());
71 associated_registration_
->RemoveListener(this);
74 for (const GURL
& pattern
: associated_patterns_
)
75 DecreaseProcessReference(pattern
);
78 void ServiceWorkerProviderHost::OnRegistrationFailed(
79 ServiceWorkerRegistration
* registration
) {
80 DCHECK_EQ(associated_registration_
.get(), registration
);
81 DisassociateRegistration();
84 void ServiceWorkerProviderHost::OnSkippedWaiting(
85 ServiceWorkerRegistration
* registration
) {
86 DCHECK_EQ(associated_registration_
.get(), registration
);
87 ServiceWorkerVersion
* active_version
= registration
->active_version();
88 DCHECK_EQ(active_version
->status(), ServiceWorkerVersion::ACTIVATING
);
89 SetControllerVersionAttribute(active_version
);
92 void ServiceWorkerProviderHost::SetDocumentUrl(const GURL
& url
) {
93 DCHECK(!url
.has_ref());
97 void ServiceWorkerProviderHost::SetTopmostFrameUrl(const GURL
& url
) {
98 topmost_frame_url_
= url
;
101 void ServiceWorkerProviderHost::SetControllerVersionAttribute(
102 ServiceWorkerVersion
* version
) {
103 if (version
== controlling_version_
.get())
106 scoped_refptr
<ServiceWorkerVersion
> previous_version
= controlling_version_
;
107 controlling_version_
= version
;
109 version
->AddControllee(this);
110 if (previous_version
.get())
111 previous_version
->RemoveControllee(this);
113 if (!dispatcher_host_
)
114 return; // Could be NULL in some tests.
116 bool should_notify_controllerchange
=
117 previous_version
&& version
&& version
->skip_waiting();
119 dispatcher_host_
->Send(new ServiceWorkerMsg_SetControllerServiceWorker(
120 kDocumentMainThreadId
, provider_id(), CreateHandleAndPass(version
),
121 should_notify_controllerchange
));
124 bool ServiceWorkerProviderHost::SetHostedVersionId(int64 version_id
) {
126 return true; // System is shutting down.
127 if (active_version())
128 return false; // Unexpected bad message.
130 ServiceWorkerVersion
* live_version
= context_
->GetLiveVersion(version_id
);
132 return true; // Was deleted before it got started.
134 ServiceWorkerVersionInfo info
= live_version
->GetInfo();
135 if (info
.running_status
!= ServiceWorkerVersion::STARTING
||
136 info
.process_id
!= render_process_id_
) {
137 // If we aren't trying to start this version in our process
138 // something is amiss.
142 running_hosted_version_
= live_version
;
146 void ServiceWorkerProviderHost::AssociateRegistration(
147 ServiceWorkerRegistration
* registration
) {
148 DCHECK(CanAssociateRegistration(registration
));
149 if (associated_registration_
.get())
150 DecreaseProcessReference(associated_registration_
->pattern());
151 IncreaseProcessReference(registration
->pattern());
153 associated_registration_
= registration
;
154 associated_registration_
->AddListener(this);
155 SendAssociateRegistrationMessage();
156 SetControllerVersionAttribute(registration
->active_version());
159 void ServiceWorkerProviderHost::DisassociateRegistration() {
160 if (!associated_registration_
.get())
162 DecreaseProcessReference(associated_registration_
->pattern());
163 associated_registration_
->RemoveListener(this);
164 associated_registration_
= NULL
;
165 SetControllerVersionAttribute(NULL
);
167 if (dispatcher_host_
) {
168 dispatcher_host_
->Send(new ServiceWorkerMsg_DisassociateRegistration(
169 kDocumentMainThreadId
, provider_id()));
173 scoped_ptr
<ServiceWorkerRequestHandler
>
174 ServiceWorkerProviderHost::CreateRequestHandler(
175 FetchRequestMode request_mode
,
176 FetchCredentialsMode credentials_mode
,
177 ResourceType resource_type
,
178 RequestContextType request_context_type
,
179 RequestContextFrameType frame_type
,
180 base::WeakPtr
<storage::BlobStorageContext
> blob_storage_context
,
181 scoped_refptr
<ResourceRequestBody
> body
) {
182 if (IsHostToRunningServiceWorker()) {
183 return scoped_ptr
<ServiceWorkerRequestHandler
>(
184 new ServiceWorkerContextRequestHandler(
185 context_
, AsWeakPtr(), blob_storage_context
, resource_type
));
187 if (ServiceWorkerUtils::IsMainResourceType(resource_type
) ||
188 controlling_version()) {
189 return scoped_ptr
<ServiceWorkerRequestHandler
>(
190 new ServiceWorkerControlleeRequestHandler(context_
,
192 blob_storage_context
,
196 request_context_type
,
200 return scoped_ptr
<ServiceWorkerRequestHandler
>();
203 bool ServiceWorkerProviderHost::CanAssociateRegistration(
204 ServiceWorkerRegistration
* registration
) {
207 if (running_hosted_version_
.get())
209 if (!registration
|| associated_registration_
.get() || !allow_association_
)
214 void ServiceWorkerProviderHost::PostMessage(
215 const base::string16
& message
,
216 const std::vector
<int>& sent_message_port_ids
) {
217 if (!dispatcher_host_
)
218 return; // Could be NULL in some tests.
220 std::vector
<int> new_routing_ids
;
221 dispatcher_host_
->message_port_message_filter()->
222 UpdateMessagePortsWithNewRoutes(sent_message_port_ids
,
225 dispatcher_host_
->Send(
226 new ServiceWorkerMsg_MessageToDocument(
227 kDocumentMainThreadId
, provider_id(),
229 sent_message_port_ids
,
233 void ServiceWorkerProviderHost::Focus(const FocusCallback
& callback
) {
234 BrowserThread::PostTask(
235 BrowserThread::UI
, FROM_HERE
,
236 base::Bind(&FocusOnUIThread
,
242 void ServiceWorkerProviderHost::GetClientInfo(
243 int embedded_worker_id
,
245 dispatcher_host_
->Send(new ServiceWorkerMsg_GetClientInfo(
246 kDocumentMainThreadId
, embedded_worker_id
, request_id
, provider_id()));
249 void ServiceWorkerProviderHost::AddScopedProcessReferenceToPattern(
250 const GURL
& pattern
) {
251 associated_patterns_
.push_back(pattern
);
252 IncreaseProcessReference(pattern
);
255 void ServiceWorkerProviderHost::PrepareForCrossSiteTransfer() {
256 DCHECK_NE(ChildProcessHost::kInvalidUniqueID
, render_process_id_
);
258 for (const GURL
& pattern
: associated_patterns_
)
259 DecreaseProcessReference(pattern
);
261 if (associated_registration_
.get())
262 DecreaseProcessReference(associated_registration_
->pattern());
264 render_process_id_
= ChildProcessHost::kInvalidUniqueID
;
265 render_frame_id_
= MSG_ROUTING_NONE
;
266 provider_id_
= kInvalidServiceWorkerProviderId
;
267 dispatcher_host_
= nullptr;
270 void ServiceWorkerProviderHost::CompleteCrossSiteTransfer(
274 ServiceWorkerDispatcherHost
* new_dispatcher_host
) {
275 DCHECK_EQ(ChildProcessHost::kInvalidUniqueID
, render_process_id_
);
276 DCHECK_NE(ChildProcessHost::kInvalidUniqueID
, new_process_id
);
278 render_process_id_
= new_process_id
;
279 render_frame_id_
= new_frame_id
;
280 provider_id_
= new_provider_id
;
281 dispatcher_host_
= new_dispatcher_host
;
283 for (const GURL
& pattern
: associated_patterns_
)
284 IncreaseProcessReference(pattern
);
286 if (associated_registration_
.get()) {
287 IncreaseProcessReference(associated_registration_
->pattern());
288 SendAssociateRegistrationMessage();
289 if (dispatcher_host_
&& associated_registration_
->active_version()) {
290 dispatcher_host_
->Send(new ServiceWorkerMsg_SetControllerServiceWorker(
291 kDocumentMainThreadId
, provider_id(),
292 CreateHandleAndPass(associated_registration_
->active_version()),
293 false /* shouldNotifyControllerChange */));
298 void ServiceWorkerProviderHost::SendAssociateRegistrationMessage() {
299 if (!dispatcher_host_
)
302 ServiceWorkerRegistrationHandle
* handle
=
303 dispatcher_host_
->GetOrCreateRegistrationHandle(
304 provider_id(), associated_registration_
.get());
306 ServiceWorkerVersionAttributes attrs
;
307 attrs
.installing
= handle
->CreateServiceWorkerHandleAndPass(
308 associated_registration_
->installing_version());
309 attrs
.waiting
= handle
->CreateServiceWorkerHandleAndPass(
310 associated_registration_
->waiting_version());
311 attrs
.active
= handle
->CreateServiceWorkerHandleAndPass(
312 associated_registration_
->active_version());
314 dispatcher_host_
->Send(new ServiceWorkerMsg_AssociateRegistration(
315 kDocumentMainThreadId
, provider_id(), handle
->GetObjectInfo(), attrs
));
318 ServiceWorkerObjectInfo
ServiceWorkerProviderHost::CreateHandleAndPass(
319 ServiceWorkerVersion
* version
) {
320 ServiceWorkerObjectInfo info
;
321 if (context_
&& version
) {
322 scoped_ptr
<ServiceWorkerHandle
> handle
=
323 ServiceWorkerHandle::Create(context_
,
325 kDocumentMainThreadId
,
328 info
= handle
->GetObjectInfo();
329 dispatcher_host_
->RegisterServiceWorkerHandle(handle
.Pass());
334 void ServiceWorkerProviderHost::IncreaseProcessReference(
335 const GURL
& pattern
) {
336 if (context_
&& context_
->process_manager()) {
337 context_
->process_manager()->AddProcessReferenceToPattern(
338 pattern
, render_process_id_
);
342 void ServiceWorkerProviderHost::DecreaseProcessReference(
343 const GURL
& pattern
) {
344 if (context_
&& context_
->process_manager()) {
345 context_
->process_manager()->RemoveProcessReferenceFromPattern(
346 pattern
, render_process_id_
);
350 bool ServiceWorkerProviderHost::IsContextAlive() {
351 return context_
!= NULL
;
354 } // namespace content