1 // Copyright 2014 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_context_request_handler.h"
7 #include "content/browser/service_worker/service_worker_provider_host.h"
8 #include "content/browser/service_worker/service_worker_read_from_cache_job.h"
9 #include "content/browser/service_worker/service_worker_version.h"
10 #include "net/url_request/url_request.h"
14 ServiceWorkerContextRequestHandler::ServiceWorkerContextRequestHandler(
15 base::WeakPtr
<ServiceWorkerContextCore
> context
,
16 base::WeakPtr
<ServiceWorkerProviderHost
> provider_host
,
17 ResourceType::Type resource_type
)
18 : ServiceWorkerRequestHandler(context
, provider_host
, resource_type
),
19 version_(provider_host_
->running_hosted_version()) {
20 DCHECK(provider_host_
->IsHostToRunningServiceWorker());
23 ServiceWorkerContextRequestHandler::~ServiceWorkerContextRequestHandler() {
26 net::URLRequestJob
* ServiceWorkerContextRequestHandler::MaybeCreateJob(
27 net::URLRequest
* request
,
28 net::NetworkDelegate
* network_delegate
) {
29 if (!provider_host_
|| !version_
)
32 // We only use the script cache for main script loading and
33 // importScripts(), even if a cached script is xhr'd, we don't
34 // retrieve it from the script cache.
35 // TODO(michaeln): Get the desired behavior clarified in the spec,
36 // and make tweak the behavior here to match.
37 if (resource_type_
!= ResourceType::SERVICE_WORKER
&&
38 resource_type_
!= ResourceType::SCRIPT
) {
42 if (ShouldAddToScriptCache(request
->url())) {
44 // TODO(michaeln): return new ServiceWorkerWriteToCacheJob();
47 int64 response_id
= kInvalidServiceWorkerResponseId
;
48 if (ShouldReadFromScriptCache(request
->url(), &response_id
)) {
49 return new ServiceWorkerReadFromCacheJob(
50 request
, network_delegate
, context_
, response_id
);
53 // NULL means use the network.
57 bool ServiceWorkerContextRequestHandler::ShouldAddToScriptCache(
59 // TODO(michaeln): Ensure the transition to INSTALLING can't
60 // happen prior to the initial eval completion.
61 if (version_
->status() != ServiceWorkerVersion::NEW
)
63 return version_
->LookupInScriptCache(url
) == kInvalidServiceWorkerResponseId
;
66 bool ServiceWorkerContextRequestHandler::ShouldReadFromScriptCache(
67 const GURL
& url
, int64
* response_id_out
) {
68 // We don't read from the script cache until the version is INSTALLED.
69 if (version_
->status() == ServiceWorkerVersion::NEW
||
70 version_
->status() == ServiceWorkerVersion::INSTALLING
)
72 *response_id_out
= version_
->LookupInScriptCache(url
);
73 return *response_id_out
!= kInvalidServiceWorkerResponseId
;
76 } // namespace content