OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/browser/service_worker/service_worker_context_request_handler. h" | 5 #include "content/browser/service_worker/service_worker_context_request_handler. h" |
6 | 6 |
7 #include "content/browser/service_worker/service_worker_context_core.h" | |
7 #include "content/browser/service_worker/service_worker_provider_host.h" | 8 #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_read_from_cache_job.h" |
10 #include "content/browser/service_worker/service_worker_storage.h" | |
9 #include "content/browser/service_worker/service_worker_version.h" | 11 #include "content/browser/service_worker/service_worker_version.h" |
12 #include "content/browser/service_worker/service_worker_write_to_cache_job.h" | |
10 #include "net/url_request/url_request.h" | 13 #include "net/url_request/url_request.h" |
11 | 14 |
12 namespace content { | 15 namespace content { |
13 | 16 |
14 ServiceWorkerContextRequestHandler::ServiceWorkerContextRequestHandler( | 17 ServiceWorkerContextRequestHandler::ServiceWorkerContextRequestHandler( |
15 base::WeakPtr<ServiceWorkerContextCore> context, | 18 base::WeakPtr<ServiceWorkerContextCore> context, |
16 base::WeakPtr<ServiceWorkerProviderHost> provider_host, | 19 base::WeakPtr<ServiceWorkerProviderHost> provider_host, |
17 ResourceType::Type resource_type) | 20 ResourceType::Type resource_type) |
18 : ServiceWorkerRequestHandler(context, provider_host, resource_type), | 21 : ServiceWorkerRequestHandler(context, provider_host, resource_type), |
19 version_(provider_host_->running_hosted_version()) { | 22 version_(provider_host_->running_hosted_version()) { |
20 DCHECK(provider_host_->IsHostToRunningServiceWorker()); | 23 DCHECK(provider_host_->IsHostToRunningServiceWorker()); |
21 } | 24 } |
22 | 25 |
23 ServiceWorkerContextRequestHandler::~ServiceWorkerContextRequestHandler() { | 26 ServiceWorkerContextRequestHandler::~ServiceWorkerContextRequestHandler() { |
24 } | 27 } |
25 | 28 |
26 net::URLRequestJob* ServiceWorkerContextRequestHandler::MaybeCreateJob( | 29 net::URLRequestJob* ServiceWorkerContextRequestHandler::MaybeCreateJob( |
27 net::URLRequest* request, | 30 net::URLRequest* request, |
28 net::NetworkDelegate* network_delegate) { | 31 net::NetworkDelegate* network_delegate) { |
29 if (!provider_host_ || !version_) | 32 if (!provider_host_ || !version_ || !context_) |
30 return NULL; | 33 return NULL; |
31 | 34 |
michaeln
2014/05/07 05:46:05
todo: bail out if method != GET
| |
32 // We only use the script cache for main script loading and | 35 // We only use the script cache for main script loading and |
33 // importScripts(), even if a cached script is xhr'd, we don't | 36 // importScripts(), even if a cached script is xhr'd, we don't |
34 // retrieve it from the script cache. | 37 // retrieve it from the script cache. |
35 // TODO(michaeln): Get the desired behavior clarified in the spec, | 38 // TODO(michaeln): Get the desired behavior clarified in the spec, |
36 // and make tweak the behavior here to match. | 39 // and make tweak the behavior here to match. |
37 if (resource_type_ != ResourceType::SERVICE_WORKER && | 40 if (resource_type_ != ResourceType::SERVICE_WORKER && |
38 resource_type_ != ResourceType::SCRIPT) { | 41 resource_type_ != ResourceType::SCRIPT) { |
39 return NULL; | 42 return NULL; |
40 } | 43 } |
41 | 44 |
42 if (ShouldAddToScriptCache(request->url())) { | 45 if (ShouldAddToScriptCache(request->url())) { |
43 return NULL; | 46 int64 response_id = context_->storage()->NewResourceId(); |
44 // TODO(michaeln): return new ServiceWorkerWriteToCacheJob(); | 47 if (response_id == kInvalidServiceWorkerResponseId) |
48 return NULL; | |
49 version_->AddToScriptCache(request->url(), response_id); | |
50 return new ServiceWorkerWriteToCacheJob( | |
51 request, network_delegate, context_, version_, response_id); | |
45 } | 52 } |
46 | 53 |
47 int64 response_id = kInvalidServiceWorkerResponseId; | 54 int64 response_id = kInvalidServiceWorkerResponseId; |
48 if (ShouldReadFromScriptCache(request->url(), &response_id)) { | 55 if (ShouldReadFromScriptCache(request->url(), &response_id)) { |
49 return new ServiceWorkerReadFromCacheJob( | 56 return new ServiceWorkerReadFromCacheJob( |
50 request, network_delegate, context_, response_id); | 57 request, network_delegate, context_, response_id); |
51 } | 58 } |
52 | 59 |
53 // NULL means use the network. | 60 // NULL means use the network. |
54 return NULL; | 61 return NULL; |
(...skipping 12 matching lines...) Expand all Loading... | |
67 const GURL& url, int64* response_id_out) { | 74 const GURL& url, int64* response_id_out) { |
68 // We don't read from the script cache until the version is INSTALLED. | 75 // We don't read from the script cache until the version is INSTALLED. |
69 if (version_->status() == ServiceWorkerVersion::NEW || | 76 if (version_->status() == ServiceWorkerVersion::NEW || |
70 version_->status() == ServiceWorkerVersion::INSTALLING) | 77 version_->status() == ServiceWorkerVersion::INSTALLING) |
71 return false; | 78 return false; |
72 *response_id_out = version_->LookupInScriptCache(url); | 79 *response_id_out = version_->LookupInScriptCache(url); |
73 return *response_id_out != kInvalidServiceWorkerResponseId; | 80 return *response_id_out != kInvalidServiceWorkerResponseId; |
74 } | 81 } |
75 | 82 |
76 } // namespace content | 83 } // namespace content |
OLD | NEW |