OLD | NEW |
---|---|
1 // Copyright (c) 2017 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2017 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/appcache/appcache_url_loader_job.h" | 5 #include "content/browser/appcache/appcache_url_loader_job.h" |
6 #include "content/browser/appcache/appcache_entry.h" | 6 #include "content/browser/appcache/appcache_histograms.h" |
7 #include "content/browser/url_loader_factory_getter.h" | |
8 #include "content/public/common/resource_type.h" | |
9 #include "net/base/io_buffer.h" | |
7 | 10 |
8 namespace content { | 11 namespace content { |
9 | 12 |
10 AppCacheURLLoaderJob::~AppCacheURLLoaderJob() {} | 13 AppCacheURLLoaderJob::~AppCacheURLLoaderJob() {} |
11 | 14 |
12 void AppCacheURLLoaderJob::Kill() {} | 15 void AppCacheURLLoaderJob::Kill() {} |
13 | 16 |
14 bool AppCacheURLLoaderJob::IsStarted() const { | 17 bool AppCacheURLLoaderJob::IsStarted() const { |
15 return false; | 18 return delivery_type_ != AWAITING_DELIVERY_ORDERS; |
16 } | |
17 | |
18 bool AppCacheURLLoaderJob::IsWaiting() const { | |
19 return false; | |
20 } | |
21 | |
22 bool AppCacheURLLoaderJob::IsDeliveringAppCacheResponse() const { | |
23 return false; | |
24 } | |
25 | |
26 bool AppCacheURLLoaderJob::IsDeliveringNetworkResponse() const { | |
27 return false; | |
28 } | |
29 | |
30 bool AppCacheURLLoaderJob::IsDeliveringErrorResponse() const { | |
31 return false; | |
32 } | |
33 | |
34 bool AppCacheURLLoaderJob::IsCacheEntryNotFound() const { | |
35 return false; | |
36 } | 19 } |
37 | 20 |
38 void AppCacheURLLoaderJob::DeliverAppCachedResponse(const GURL& manifest_url, | 21 void AppCacheURLLoaderJob::DeliverAppCachedResponse(const GURL& manifest_url, |
39 int64_t cache_id, | 22 int64_t cache_id, |
40 const AppCacheEntry& entry, | 23 const AppCacheEntry& entry, |
41 bool is_fallback) {} | 24 bool is_fallback) { |
42 | 25 delivery_type_ = APPCACHED_DELIVERY; |
43 void AppCacheURLLoaderJob::DeliverNetworkResponse() {} | 26 |
44 | 27 AppCacheHistograms::AddAppCacheJobStartDelaySample(base::TimeTicks::Now() - |
45 void AppCacheURLLoaderJob::DeliverErrorResponse() {} | 28 start_time_tick_); |
29 | |
30 manifest_url_ = manifest_url; | |
31 cache_id_ = cache_id; | |
32 entry_ = entry; | |
33 is_fallback_ = is_fallback; | |
34 | |
35 storage_->LoadResponseInfo(manifest_url_, entry_.response_id(), this); | |
36 } | |
37 | |
38 void AppCacheURLLoaderJob::DeliverNetworkResponse() { | |
39 delivery_type_ = NETWORK_DELIVERY; | |
40 | |
41 AppCacheHistograms::AddNetworkJobStartDelaySample(base::TimeTicks::Now() - | |
42 start_time_tick_); | |
43 | |
44 url_loader_factory_getter_->GetNetworkFactory()->get()->CreateLoaderAndStart( | |
45 mojo::MakeRequest(&url_loader_network_), routing_id_, request_id_, | |
46 mojom::kURLLoadOptionSendSSLInfo, request_, std::move(client_info_)); | |
47 } | |
48 | |
49 void AppCacheURLLoaderJob::DeliverErrorResponse() { | |
50 delivery_type_ = ERROR_DELIVERY; | |
51 | |
52 AppCacheHistograms::AddErrorJobStartDelaySample(base::TimeTicks::Now() - | |
53 start_time_tick_); | |
54 // TODO(ananta) | |
55 // Add support to send out an error response. | |
56 } | |
46 | 57 |
47 const GURL& AppCacheURLLoaderJob::GetURL() const { | 58 const GURL& AppCacheURLLoaderJob::GetURL() const { |
48 return url_; | 59 return request_.url; |
49 } | 60 } |
50 | 61 |
51 AppCacheURLLoaderJob::AppCacheURLLoaderJob() {} | 62 AppCacheURLLoaderJob* AppCacheURLLoaderJob::AsURLLoaderJob() { |
63 return this; | |
64 } | |
65 | |
66 void AppCacheURLLoaderJob::FollowRedirect() { | |
67 if (url_loader_network_) | |
68 url_loader_network_->FollowRedirect(); | |
69 } | |
70 | |
71 void AppCacheURLLoaderJob::SetPriority(net::RequestPriority priority, | |
72 int32_t intra_priority_value) { | |
73 NOTREACHED() << "We don't support SetPriority()"; | |
74 } | |
75 | |
76 void AppCacheURLLoaderJob::BindEndpoints( | |
77 const ResourceRequest& request, | |
78 mojom::URLLoaderAssociatedRequest url_loader_request, | |
79 int32_t routing_id, | |
80 int32_t request_id, | |
81 mojom::URLLoaderClientPtrInfo client_info) { | |
82 request_ = request; | |
83 | |
kinuko
2017/06/06 08:07:30
DCHECK(!binding_); ?
ananta
2017/06/06 20:49:16
Not needed now.
| |
84 binding_.reset(new mojo::AssociatedBinding<mojom::URLLoader>( | |
85 this, std::move(url_loader_request))); | |
kinuko
2017/06/06 08:07:30
nit: I think we can also do something like:
bind
ananta
2017/06/06 20:49:15
Done.
| |
86 binding_->set_connection_error_handler(base::Bind( | |
87 &AppCacheURLLoaderJob::OnConnectionError, base::Unretained(this))); | |
88 | |
89 routing_id_ = routing_id; | |
90 request_id_ = request_id; | |
91 | |
92 client_info_.Bind(std::move(client_info)); | |
93 | |
94 // Send the cached AppCacheResponse if any. | |
95 if (info_.get()) | |
96 SendResponseInfo(); | |
97 } | |
98 | |
99 void AppCacheURLLoaderJob::SetURLLoaderFactoryGetter( | |
100 URLLoaderFactoryGetter* url_loader_factory_getter) { | |
101 url_loader_factory_getter_ = url_loader_factory_getter; | |
102 } | |
103 | |
104 AppCacheURLLoaderJob::AppCacheURLLoaderJob(const ResourceRequest& request, | |
105 AppCacheStorage* storage) | |
106 : request_(request), | |
107 storage_(storage), | |
108 start_time_tick_(base::TimeTicks::Now()), | |
109 cache_id_(kAppCacheNoCacheId), | |
110 is_fallback_(false), | |
111 buffer_(nullptr), | |
112 routing_id_(-1), | |
113 request_id_(-1) {} | |
114 | |
115 void AppCacheURLLoaderJob::OnResponseInfoLoaded( | |
116 AppCacheResponseInfo* response_info, | |
117 int64_t response_id) { | |
118 DCHECK(IsDeliveringAppCacheResponse()); | |
119 | |
120 if (response_info) { | |
121 info_ = response_info; | |
122 reader_.reset( | |
123 storage_->CreateResponseReader(manifest_url_, entry_.response_id())); | |
124 | |
125 // TODO(ananta) | |
126 // Handle range requests. | |
127 | |
128 buffer_ = | |
129 new net::IOBuffer(static_cast<size_t>(info_->response_data_size())); | |
130 reader_->ReadData(buffer_.get(), info_->response_data_size(), | |
131 base::Bind(&AppCacheURLLoaderJob::OnReadComplete, | |
132 base::Unretained(this))); | |
133 if (client_info_) | |
134 SendResponseInfo(); | |
135 } else { | |
136 // Error case here. We fallback to the network. | |
137 DeliverNetworkResponse(); | |
138 AppCacheHistograms::CountResponseRetrieval( | |
139 false, IsResourceTypeFrame(request_.resource_type), | |
140 manifest_url_.GetOrigin()); | |
141 | |
142 cache_entry_not_found_ = true; | |
143 } | |
144 } | |
145 | |
146 void AppCacheURLLoaderJob::OnCacheLoaded(AppCache* cache, int64_t cache_id) { | |
147 NOTREACHED() << "Unhandled at the moment."; | |
148 } | |
149 | |
150 void AppCacheURLLoaderJob::OnReadComplete(int result) { | |
151 DLOG(WARNING) << "AppCache read completed with result: " << result; | |
152 | |
153 bool is_main_resource = IsResourceTypeFrame(request_.resource_type); | |
154 | |
155 if (result == 0) { | |
156 AppCacheHistograms::CountResponseRetrieval(true, is_main_resource, | |
157 manifest_url_.GetOrigin()); | |
158 } else if (result < 0) { | |
159 // Error case here. We fallback to the network. | |
160 DeliverNetworkResponse(); | |
161 AppCacheHistograms::CountResponseRetrieval(false, is_main_resource, | |
162 manifest_url_.GetOrigin()); | |
163 return; | |
164 } | |
165 | |
166 if (result > 0) { | |
167 uint32_t bytes_written = static_cast<uint32_t>(result); | |
168 // TODO(ananta) | |
169 // Use the IO buffer net adapter to efficiently pass the data over to the | |
170 // cliet. | |
171 mojo::WriteDataRaw(data_pipe_.producer_handle.get(), buffer_->data(), | |
172 &bytes_written, MOJO_WRITE_DATA_FLAG_NONE); | |
173 reader_->ReadData(buffer_.get(), info_->response_data_size(), | |
174 base::Bind(&AppCacheURLLoaderJob::OnReadComplete, | |
175 base::Unretained(this))); | |
176 } | |
177 } | |
178 | |
179 void AppCacheURLLoaderJob::OnConnectionError() { | |
180 delete this; | |
181 } | |
182 | |
183 void AppCacheURLLoaderJob::SendResponseInfo() { | |
184 DCHECK(client_info_); | |
185 | |
186 const net::HttpResponseInfo* http_info = info_->http_response_info(); | |
187 | |
188 ResourceResponseHead response_head; | |
189 response_head.headers = new net::HttpResponseHeaders(""); | |
190 | |
191 std::string name; | |
192 std::string value; | |
193 | |
194 for (size_t it = 0; | |
195 http_info->headers->EnumerateHeaderLines(&it, &name, &value);) { | |
196 response_head.headers->AddHeader(name + ": " + value); | |
197 } | |
198 | |
199 // TODO(ananta) | |
200 // Copy more fields. | |
201 http_info->headers->GetMimeType(&response_head.mime_type); | |
202 http_info->headers->GetCharset(&response_head.charset); | |
203 | |
204 response_head.request_time = http_info->request_time; | |
205 response_head.response_time = http_info->response_time; | |
206 response_head.content_length = info_->response_data_size(); | |
207 | |
208 client_info_->OnReceiveResponse(response_head, http_info->ssl_info, | |
209 mojom::DownloadedTempFilePtr()); | |
210 | |
211 client_info_->OnStartLoadingResponseBody( | |
212 std::move(data_pipe_.consumer_handle)); | |
213 } | |
52 | 214 |
53 } // namespace content | 215 } // namespace content |
OLD | NEW |