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_) |
33 return NULL; | |
34 | |
35 if (request->url_chain().size() > 1) | |
30 return NULL; | 36 return NULL; |
kinuko
2014/05/08 08:00:24
Is this to abort (fall back to net) redirected req
michaeln
2014/05/08 22:27:24
This is to always send redirected requests thru th
| |
31 | 37 |
32 // We only use the script cache for main script loading and | 38 // We only use the script cache for main script loading and |
33 // importScripts(), even if a cached script is xhr'd, we don't | 39 // importScripts(), even if a cached script is xhr'd, we don't |
34 // retrieve it from the script cache. | 40 // retrieve it from the script cache. |
35 // TODO(michaeln): Get the desired behavior clarified in the spec, | 41 // TODO(michaeln): Get the desired behavior clarified in the spec, |
36 // and make tweak the behavior here to match. | 42 // and make tweak the behavior here to match. |
37 if (resource_type_ != ResourceType::SERVICE_WORKER && | 43 if (resource_type_ != ResourceType::SERVICE_WORKER && |
38 resource_type_ != ResourceType::SCRIPT) { | 44 resource_type_ != ResourceType::SCRIPT) { |
39 return NULL; | 45 return NULL; |
40 } | 46 } |
41 | 47 |
42 if (ShouldAddToScriptCache(request->url())) { | 48 if (ShouldAddToScriptCache(request->url())) { |
43 return NULL; | 49 int64 response_id = context_->storage()->NewResourceId(); |
44 // TODO(michaeln): return new ServiceWorkerWriteToCacheJob(); | 50 if (response_id == kInvalidServiceWorkerResponseId) |
51 return NULL; | |
52 version_->AddToScriptCache(request->url(), response_id); | |
53 return new ServiceWorkerWriteToCacheJob( | |
54 request, network_delegate, context_, version_, response_id); | |
45 } | 55 } |
46 | 56 |
47 int64 response_id = kInvalidServiceWorkerResponseId; | 57 int64 response_id = kInvalidServiceWorkerResponseId; |
48 if (ShouldReadFromScriptCache(request->url(), &response_id)) { | 58 if (ShouldReadFromScriptCache(request->url(), &response_id)) { |
49 return new ServiceWorkerReadFromCacheJob( | 59 return new ServiceWorkerReadFromCacheJob( |
50 request, network_delegate, context_, response_id); | 60 request, network_delegate, context_, response_id); |
51 } | 61 } |
52 | 62 |
53 // NULL means use the network. | 63 // NULL means use the network. |
54 return NULL; | 64 return NULL; |
55 } | 65 } |
56 | 66 |
57 bool ServiceWorkerContextRequestHandler::ShouldAddToScriptCache( | 67 bool ServiceWorkerContextRequestHandler::ShouldAddToScriptCache( |
58 const GURL& url) { | 68 const GURL& url) { |
59 // TODO(michaeln): Ensure the transition to INSTALLING can't | 69 // TODO(michaeln): Stop caching imports after the initial eval. |
60 // happen prior to the initial eval completion. | 70 if (version_->status() != ServiceWorkerVersion::NEW && |
61 if (version_->status() != ServiceWorkerVersion::NEW) | 71 version_->status() != ServiceWorkerVersion::INSTALLING) |
62 return false; | 72 return false; |
63 return version_->LookupInScriptCache(url) == kInvalidServiceWorkerResponseId; | 73 return version_->LookupInScriptCache(url) == kInvalidServiceWorkerResponseId; |
64 } | 74 } |
65 | 75 |
66 bool ServiceWorkerContextRequestHandler::ShouldReadFromScriptCache( | 76 bool ServiceWorkerContextRequestHandler::ShouldReadFromScriptCache( |
67 const GURL& url, int64* response_id_out) { | 77 const GURL& url, int64* response_id_out) { |
68 // We don't read from the script cache until the version is INSTALLED. | 78 // We don't read from the script cache until the version is INSTALLED. |
69 if (version_->status() == ServiceWorkerVersion::NEW || | 79 if (version_->status() == ServiceWorkerVersion::NEW || |
70 version_->status() == ServiceWorkerVersion::INSTALLING) | 80 version_->status() == ServiceWorkerVersion::INSTALLING) |
71 return false; | 81 return false; |
72 *response_id_out = version_->LookupInScriptCache(url); | 82 *response_id_out = version_->LookupInScriptCache(url); |
73 return *response_id_out != kInvalidServiceWorkerResponseId; | 83 return *response_id_out != kInvalidServiceWorkerResponseId; |
74 } | 84 } |
75 | 85 |
76 } // namespace content | 86 } // namespace content |
OLD | NEW |