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 "base/time/time.h"
8 #include "content/browser/service_worker/service_worker_context_core.h"
9 #include "content/browser/service_worker/service_worker_provider_host.h"
10 #include "content/browser/service_worker/service_worker_read_from_cache_job.h"
11 #include "content/browser/service_worker/service_worker_storage.h"
12 #include "content/browser/service_worker/service_worker_version.h"
13 #include "content/browser/service_worker/service_worker_write_to_cache_job.h"
14 #include "content/public/browser/resource_context.h"
15 #include "net/base/load_flags.h"
16 #include "net/url_request/url_request.h"
20 ServiceWorkerContextRequestHandler::ServiceWorkerContextRequestHandler(
21 base::WeakPtr
<ServiceWorkerContextCore
> context
,
22 base::WeakPtr
<ServiceWorkerProviderHost
> provider_host
,
23 base::WeakPtr
<storage::BlobStorageContext
> blob_storage_context
,
24 ResourceType resource_type
)
25 : ServiceWorkerRequestHandler(context
,
29 version_(provider_host_
->running_hosted_version()) {
30 DCHECK(provider_host_
->IsHostToRunningServiceWorker());
33 ServiceWorkerContextRequestHandler::~ServiceWorkerContextRequestHandler() {
36 net::URLRequestJob
* ServiceWorkerContextRequestHandler::MaybeCreateJob(
37 net::URLRequest
* request
,
38 net::NetworkDelegate
* network_delegate
,
39 ResourceContext
* resource_context
) {
40 if (!provider_host_
|| !version_
.get() || !context_
)
43 // We currently have no use case for hijacking a redirected request.
44 if (request
->url_chain().size() > 1)
47 // We only use the script cache for main script loading and
48 // importScripts(), even if a cached script is xhr'd, we don't
49 // retrieve it from the script cache.
50 // TODO(michaeln): Get the desired behavior clarified in the spec,
51 // and make tweak the behavior here to match.
52 if (resource_type_
!= RESOURCE_TYPE_SERVICE_WORKER
&&
53 resource_type_
!= RESOURCE_TYPE_SCRIPT
) {
57 if (ShouldAddToScriptCache(request
->url())) {
58 ServiceWorkerRegistration
* registration
=
59 context_
->GetLiveRegistration(version_
->registration_id());
60 DCHECK(registration
); // We're registering or updating so must be there.
62 int64 response_id
= context_
->storage()->NewResourceId();
63 if (response_id
== kInvalidServiceWorkerResponseId
)
66 // Bypass the browser cache for initial installs and update
67 // checks after 24 hours have passed.
68 int extra_load_flags
= 0;
69 base::TimeDelta time_since_last_check
=
70 base::Time::Now() - registration
->last_update_check();
71 if (time_since_last_check
> base::TimeDelta::FromHours(24))
72 extra_load_flags
= net::LOAD_BYPASS_CACHE
;
74 return new ServiceWorkerWriteToCacheJob(request
,
83 int64 response_id
= kInvalidServiceWorkerResponseId
;
84 if (ShouldReadFromScriptCache(request
->url(), &response_id
)) {
85 return new ServiceWorkerReadFromCacheJob(
86 request
, network_delegate
, context_
, version_
, response_id
);
89 // NULL means use the network.
93 void ServiceWorkerContextRequestHandler::GetExtraResponseInfo(
94 bool* was_fetched_via_service_worker
,
95 bool* was_fallback_required_by_service_worker
,
96 GURL
* original_url_via_service_worker
,
97 blink::WebServiceWorkerResponseType
* response_type_via_service_worker
,
98 base::TimeTicks
* fetch_start_time
,
99 base::TimeTicks
* fetch_ready_time
,
100 base::TimeTicks
* fetch_end_time
) const {
101 *was_fetched_via_service_worker
= false;
102 *was_fallback_required_by_service_worker
= false;
103 *original_url_via_service_worker
= GURL();
104 *response_type_via_service_worker
=
105 blink::WebServiceWorkerResponseTypeDefault
;
108 bool ServiceWorkerContextRequestHandler::ShouldAddToScriptCache(
110 // We only write imports that occur during the initial eval.
111 if (version_
->status() != ServiceWorkerVersion::NEW
&&
112 version_
->status() != ServiceWorkerVersion::INSTALLING
) {
115 return version_
->script_cache_map()->LookupResourceId(url
) ==
116 kInvalidServiceWorkerResponseId
;
119 bool ServiceWorkerContextRequestHandler::ShouldReadFromScriptCache(
120 const GURL
& url
, int64
* response_id_out
) {
121 // We don't read from the script cache until the version is INSTALLED.
122 if (version_
->status() == ServiceWorkerVersion::NEW
||
123 version_
->status() == ServiceWorkerVersion::INSTALLING
)
125 *response_id_out
= version_
->script_cache_map()->LookupResourceId(url
);
126 return *response_id_out
!= kInvalidServiceWorkerResponseId
;
129 } // namespace content