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 // We currently have no use case for hijacking a redirected request. |
| 36 if (request->url_chain().size() > 1) |
30 return NULL; | 37 return NULL; |
31 | 38 |
32 // We only use the script cache for main script loading and | 39 // We only use the script cache for main script loading and |
33 // importScripts(), even if a cached script is xhr'd, we don't | 40 // importScripts(), even if a cached script is xhr'd, we don't |
34 // retrieve it from the script cache. | 41 // retrieve it from the script cache. |
35 // TODO(michaeln): Get the desired behavior clarified in the spec, | 42 // TODO(michaeln): Get the desired behavior clarified in the spec, |
36 // and make tweak the behavior here to match. | 43 // and make tweak the behavior here to match. |
37 if (resource_type_ != ResourceType::SERVICE_WORKER && | 44 if (resource_type_ != ResourceType::SERVICE_WORKER && |
38 resource_type_ != ResourceType::SCRIPT) { | 45 resource_type_ != ResourceType::SCRIPT) { |
39 return NULL; | 46 return NULL; |
40 } | 47 } |
41 | 48 |
42 if (ShouldAddToScriptCache(request->url())) { | 49 if (ShouldAddToScriptCache(request->url())) { |
43 return NULL; | 50 int64 response_id = context_->storage()->NewResourceId(); |
44 // TODO(michaeln): return new ServiceWorkerWriteToCacheJob(); | 51 if (response_id == kInvalidServiceWorkerResponseId) |
| 52 return NULL; |
| 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 // We only write imports that occur during the initial eval. |
60 // happen prior to the initial eval completion. | |
61 if (version_->status() != ServiceWorkerVersion::NEW) | 70 if (version_->status() != ServiceWorkerVersion::NEW) |
62 return false; | 71 return false; |
63 return version_->script_cache_map()->Lookup(url) == | 72 return version_->script_cache_map()->Lookup(url) == |
64 kInvalidServiceWorkerResponseId; | 73 kInvalidServiceWorkerResponseId; |
65 } | 74 } |
66 | 75 |
67 bool ServiceWorkerContextRequestHandler::ShouldReadFromScriptCache( | 76 bool ServiceWorkerContextRequestHandler::ShouldReadFromScriptCache( |
68 const GURL& url, int64* response_id_out) { | 77 const GURL& url, int64* response_id_out) { |
69 // 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. |
70 if (version_->status() == ServiceWorkerVersion::NEW || | 79 if (version_->status() == ServiceWorkerVersion::NEW || |
71 version_->status() == ServiceWorkerVersion::INSTALLING) | 80 version_->status() == ServiceWorkerVersion::INSTALLING) |
72 return false; | 81 return false; |
73 *response_id_out = version_->script_cache_map()->Lookup(url); | 82 *response_id_out = version_->script_cache_map()->Lookup(url); |
74 return *response_id_out != kInvalidServiceWorkerResponseId; | 83 return *response_id_out != kInvalidServiceWorkerResponseId; |
75 } | 84 } |
76 | 85 |
77 } // namespace content | 86 } // namespace content |
OLD | NEW |