Supervised user whitelists: Cleanup
[chromium-blink-merge.git] / content / browser / service_worker / service_worker_context_request_handler.cc
blob3b91496a08efcb0e2dc60f2976af964e21099b6c
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"
18 namespace content {
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,
26 provider_host,
27 blob_storage_context,
28 resource_type),
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_)
41 return NULL;
43 // We currently have no use case for hijacking a redirected request.
44 if (request->url_chain().size() > 1)
45 return NULL;
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) {
54 return NULL;
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)
64 return NULL;
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 version_->force_bypass_cache_for_scripts()) {
73 extra_load_flags = net::LOAD_BYPASS_CACHE;
76 return new ServiceWorkerWriteToCacheJob(request,
77 network_delegate,
78 resource_type_,
79 context_,
80 version_.get(),
81 extra_load_flags,
82 response_id);
85 int64 response_id = kInvalidServiceWorkerResponseId;
86 if (ShouldReadFromScriptCache(request->url(), &response_id)) {
87 return new ServiceWorkerReadFromCacheJob(
88 request, network_delegate, context_, version_, response_id);
91 // NULL means use the network.
92 return NULL;
95 void ServiceWorkerContextRequestHandler::GetExtraResponseInfo(
96 bool* was_fetched_via_service_worker,
97 bool* was_fallback_required_by_service_worker,
98 GURL* original_url_via_service_worker,
99 blink::WebServiceWorkerResponseType* response_type_via_service_worker,
100 base::TimeTicks* fetch_start_time,
101 base::TimeTicks* fetch_ready_time,
102 base::TimeTicks* fetch_end_time) const {
103 *was_fetched_via_service_worker = false;
104 *was_fallback_required_by_service_worker = false;
105 *original_url_via_service_worker = GURL();
106 *response_type_via_service_worker =
107 blink::WebServiceWorkerResponseTypeDefault;
110 bool ServiceWorkerContextRequestHandler::ShouldAddToScriptCache(
111 const GURL& url) {
112 // We only write imports that occur during the initial eval.
113 if (version_->status() != ServiceWorkerVersion::NEW &&
114 version_->status() != ServiceWorkerVersion::INSTALLING) {
115 return false;
117 return version_->script_cache_map()->LookupResourceId(url) ==
118 kInvalidServiceWorkerResponseId;
121 bool ServiceWorkerContextRequestHandler::ShouldReadFromScriptCache(
122 const GURL& url, int64* response_id_out) {
123 // We don't read from the script cache until the version is INSTALLED.
124 if (version_->status() == ServiceWorkerVersion::NEW ||
125 version_->status() == ServiceWorkerVersion::INSTALLING)
126 return false;
127 *response_id_out = version_->script_cache_map()->LookupResourceId(url);
128 return *response_id_out != kInvalidServiceWorkerResponseId;
131 } // namespace content