Battery Status API: add UMA logging for Linux.
[chromium-blink-merge.git] / content / browser / service_worker / service_worker_context_request_handler.cc
blobc30598539d4ddeba4a460e11b92a3c7d62cf15e4
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_context_core.h"
8 #include "content/browser/service_worker/service_worker_provider_host.h"
9 #include "content/browser/service_worker/service_worker_read_from_cache_job.h"
10 #include "content/browser/service_worker/service_worker_storage.h"
11 #include "content/browser/service_worker/service_worker_version.h"
12 #include "content/browser/service_worker/service_worker_write_to_cache_job.h"
13 #include "net/base/load_flags.h"
14 #include "net/url_request/url_request.h"
16 namespace content {
18 ServiceWorkerContextRequestHandler::ServiceWorkerContextRequestHandler(
19 base::WeakPtr<ServiceWorkerContextCore> context,
20 base::WeakPtr<ServiceWorkerProviderHost> provider_host,
21 base::WeakPtr<storage::BlobStorageContext> blob_storage_context,
22 ResourceType resource_type)
23 : ServiceWorkerRequestHandler(context,
24 provider_host,
25 blob_storage_context,
26 resource_type),
27 version_(provider_host_->running_hosted_version()) {
28 DCHECK(provider_host_->IsHostToRunningServiceWorker());
31 ServiceWorkerContextRequestHandler::~ServiceWorkerContextRequestHandler() {
34 net::URLRequestJob* ServiceWorkerContextRequestHandler::MaybeCreateJob(
35 net::URLRequest* request,
36 net::NetworkDelegate* network_delegate) {
37 if (!provider_host_ || !version_.get() || !context_)
38 return NULL;
40 // We currently have no use case for hijacking a redirected request.
41 if (request->url_chain().size() > 1)
42 return NULL;
44 // We only use the script cache for main script loading and
45 // importScripts(), even if a cached script is xhr'd, we don't
46 // retrieve it from the script cache.
47 // TODO(michaeln): Get the desired behavior clarified in the spec,
48 // and make tweak the behavior here to match.
49 if (resource_type_ != RESOURCE_TYPE_SERVICE_WORKER &&
50 resource_type_ != RESOURCE_TYPE_SCRIPT) {
51 return NULL;
54 if (ShouldAddToScriptCache(request->url())) {
55 ServiceWorkerRegistration* registration =
56 context_->GetLiveRegistration(version_->registration_id());
57 DCHECK(registration); // We're registering or updating so must be there.
59 int64 response_id = context_->storage()->NewResourceId();
60 if (response_id == kInvalidServiceWorkerResponseId)
61 return NULL;
63 // Bypass the browser cache for initial installs and update
64 // checks after 24 hours have passed.
65 int extra_load_flags = 0;
66 base::TimeDelta time_since_last_check =
67 base::Time::Now() - registration->last_update_check();
68 if (time_since_last_check > base::TimeDelta::FromHours(24))
69 extra_load_flags = net::LOAD_BYPASS_CACHE;
71 return new ServiceWorkerWriteToCacheJob(request,
72 network_delegate,
73 resource_type_,
74 context_,
75 version_.get(),
76 extra_load_flags,
77 response_id);
80 int64 response_id = kInvalidServiceWorkerResponseId;
81 if (ShouldReadFromScriptCache(request->url(), &response_id)) {
82 return new ServiceWorkerReadFromCacheJob(
83 request, network_delegate, context_, response_id);
86 // NULL means use the network.
87 return NULL;
90 void ServiceWorkerContextRequestHandler::GetExtraResponseInfo(
91 bool* was_fetched_via_service_worker,
92 GURL* original_url_via_service_worker) const {
93 *was_fetched_via_service_worker = false;
94 *original_url_via_service_worker = GURL();
97 bool ServiceWorkerContextRequestHandler::ShouldAddToScriptCache(
98 const GURL& url) {
99 // We only write imports that occur during the initial eval.
100 if (version_->status() != ServiceWorkerVersion::NEW &&
101 version_->status() != ServiceWorkerVersion::INSTALLING) {
102 return false;
104 return version_->script_cache_map()->Lookup(url) ==
105 kInvalidServiceWorkerResponseId;
108 bool ServiceWorkerContextRequestHandler::ShouldReadFromScriptCache(
109 const GURL& url, int64* response_id_out) {
110 // We don't read from the script cache until the version is INSTALLED.
111 if (version_->status() == ServiceWorkerVersion::NEW ||
112 version_->status() == ServiceWorkerVersion::INSTALLING)
113 return false;
114 *response_id_out = version_->script_cache_map()->Lookup(url);
115 return *response_id_out != kInvalidServiceWorkerResponseId;
118 } // namespace content