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 | 6 |
7 #include "base/strings/string_number_conversions.h" | 7 #include "base/strings/string_number_conversions.h" |
8 #include "content/browser/appcache/appcache_histograms.h" | 8 #include "content/browser/appcache/appcache_histograms.h" |
9 #include "content/browser/appcache/appcache_subresource_url_factory.h" | 9 #include "content/browser/appcache/appcache_subresource_url_factory.h" |
10 #include "content/browser/appcache/appcache_url_loader_request.h" | |
10 #include "content/browser/url_loader_factory_getter.h" | 11 #include "content/browser/url_loader_factory_getter.h" |
11 #include "content/common/net_adapters.h" | 12 #include "content/common/net_adapters.h" |
12 #include "content/public/common/resource_type.h" | 13 #include "content/public/common/resource_type.h" |
13 #include "net/http/http_status_code.h" | 14 #include "net/http/http_status_code.h" |
14 | 15 |
15 namespace content { | 16 namespace content { |
16 | 17 |
17 SubresourceLoadInfo::SubresourceLoadInfo() | 18 SubresourceLoadInfo::SubresourceLoadInfo() |
18 : routing_id(-1), request_id(-1), options(0) {} | 19 : routing_id(-1), request_id(-1), options(0) {} |
19 | 20 |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
75 // need to inform the loader callback that we are not going to handle this | 76 // need to inform the loader callback that we are not going to handle this |
76 // request. The loader callback is valid only for navigation requests. | 77 // request. The loader callback is valid only for navigation requests. |
77 std::move(main_resource_loader_callback_).Run(StartLoaderCallback()); | 78 std::move(main_resource_loader_callback_).Run(StartLoaderCallback()); |
78 } else { | 79 } else { |
79 default_url_loader_factory_getter_->GetNetworkFactory() | 80 default_url_loader_factory_getter_->GetNetworkFactory() |
80 ->get() | 81 ->get() |
81 ->CreateLoaderAndStart( | 82 ->CreateLoaderAndStart( |
82 mojo::MakeRequest(&network_loader_request_), | 83 mojo::MakeRequest(&network_loader_request_), |
83 subresource_load_info_->routing_id, | 84 subresource_load_info_->routing_id, |
84 subresource_load_info_->request_id, subresource_load_info_->options, | 85 subresource_load_info_->request_id, subresource_load_info_->options, |
85 subresource_load_info_->request, std::move(client_info_), | 86 subresource_load_info_->request, CreateLoaderClientProxy(), |
86 subresource_load_info_->traffic_annotation); | 87 subresource_load_info_->traffic_annotation); |
87 } | 88 } |
88 } | 89 } |
89 | 90 |
90 void AppCacheURLLoaderJob::DeliverErrorResponse() { | 91 void AppCacheURLLoaderJob::DeliverErrorResponse() { |
91 delivery_type_ = ERROR_DELIVERY; | 92 delivery_type_ = ERROR_DELIVERY; |
92 | 93 |
93 // We expect the URLLoaderClient pointer to be valid at this point. | 94 // We expect the URLLoaderClient pointer to be valid at this point. |
94 DCHECK(client_info_); | 95 DCHECK(client_info_); |
95 | 96 |
(...skipping 27 matching lines...) Expand all Loading... | |
123 if (network_loader_request_) | 124 if (network_loader_request_) |
124 network_loader_request_->FollowRedirect(); | 125 network_loader_request_->FollowRedirect(); |
125 } | 126 } |
126 | 127 |
127 void AppCacheURLLoaderJob::SetPriority(net::RequestPriority priority, | 128 void AppCacheURLLoaderJob::SetPriority(net::RequestPriority priority, |
128 int32_t intra_priority_value) { | 129 int32_t intra_priority_value) { |
129 if (network_loader_request_) | 130 if (network_loader_request_) |
130 network_loader_request_->SetPriority(priority, intra_priority_value); | 131 network_loader_request_->SetPriority(priority, intra_priority_value); |
131 } | 132 } |
132 | 133 |
134 void AppCacheURLLoaderJob::OnReceiveResponse( | |
135 const ResourceResponseHead& response_head, | |
136 const base::Optional<net::SSLInfo>& ssl_info, | |
137 mojom::DownloadedTempFilePtr downloaded_file) { | |
138 url_loader_request_instance_->set_response(response_head); | |
139 // The MaybeLoadFallbackForResponse() call below can pass a fallback | |
140 // response to us. Reset the delivery_type_ to ensure that we can | |
141 // receive it | |
142 delivery_type_ = AWAITING_DELIVERY_ORDERS; | |
143 if (!sub_resource_handler_->MaybeLoadFallbackForResponse(nullptr)) { | |
144 client_info_->OnReceiveResponse(response_head, ssl_info, | |
145 std::move(downloaded_file)); | |
146 } | |
147 } | |
148 | |
149 void AppCacheURLLoaderJob::OnReceiveRedirect( | |
150 const net::RedirectInfo& redirect_info, | |
151 const ResourceResponseHead& response_head) { | |
152 url_loader_request_instance_->set_response(response_head); | |
153 // The MaybeLoadFallbackForRedirect() call below can pass a fallback | |
154 // response to us. Reset the delivery_type_ to ensure that we can | |
155 // receive it | |
156 delivery_type_ = AWAITING_DELIVERY_ORDERS; | |
157 if (!sub_resource_handler_->MaybeLoadFallbackForRedirect( | |
158 nullptr, redirect_info.new_url)) { | |
159 client_info_->OnReceiveRedirect(redirect_info, response_head); | |
160 } | |
161 } | |
162 | |
163 void AppCacheURLLoaderJob::OnDataDownloaded(int64_t data_len, | |
164 int64_t encoded_data_len) { | |
165 client_info_->OnDataDownloaded(data_len, encoded_data_len); | |
166 } | |
167 | |
168 void AppCacheURLLoaderJob::OnUploadProgress( | |
169 int64_t current_position, | |
170 int64_t total_size, | |
171 OnUploadProgressCallback ack_callback) { | |
172 client_info_->OnUploadProgress(current_position, total_size, | |
173 std::move(ack_callback)); | |
174 } | |
175 | |
176 void AppCacheURLLoaderJob::OnReceiveCachedMetadata( | |
177 const std::vector<uint8_t>& data) { | |
178 client_info_->OnReceiveCachedMetadata(data); | |
179 } | |
180 | |
181 void AppCacheURLLoaderJob::OnTransferSizeUpdated(int32_t transfer_size_diff) { | |
182 client_info_->OnTransferSizeUpdated(transfer_size_diff); | |
183 } | |
184 | |
185 void AppCacheURLLoaderJob::OnStartLoadingResponseBody( | |
186 mojo::ScopedDataPipeConsumerHandle body) { | |
187 client_info_->OnStartLoadingResponseBody(std::move(body)); | |
188 } | |
189 | |
190 void AppCacheURLLoaderJob::OnComplete( | |
191 const ResourceRequestCompletionStatus& status) { | |
192 delivery_type_ = AWAITING_DELIVERY_ORDERS; | |
193 if (!sub_resource_handler_->MaybeLoadFallbackForResponse(nullptr)) | |
michaeln
2017/07/17 21:04:55
is this one really needed, does the code in OnRece
ananta
2017/07/18 00:19:03
It is needed. We don't receive the OnReceiveRespon
michaeln
2017/07/18 01:40:36
What happens to the network_loader when we do invo
ananta
2017/07/18 02:06:49
We should not be receiving those calls as they are
ananta
2017/07/18 02:11:39
Thanks for catching this. I was only testing the c
| |
194 client_info_->OnComplete(status); | |
195 } | |
196 | |
133 void AppCacheURLLoaderJob::SetSubresourceLoadInfo( | 197 void AppCacheURLLoaderJob::SetSubresourceLoadInfo( |
134 std::unique_ptr<SubresourceLoadInfo> subresource_load_info, | 198 std::unique_ptr<SubresourceLoadInfo> subresource_load_info, |
135 URLLoaderFactoryGetter* default_url_loader) { | 199 URLLoaderFactoryGetter* default_url_loader) { |
136 subresource_load_info_ = std::move(subresource_load_info); | 200 subresource_load_info_ = std::move(subresource_load_info); |
137 | 201 |
138 associated_binding_.reset(new mojo::AssociatedBinding<mojom::URLLoader>( | 202 associated_binding_.reset(new mojo::AssociatedBinding<mojom::URLLoader>( |
139 this, std::move(subresource_load_info_->url_loader_request))); | 203 this, std::move(subresource_load_info_->url_loader_request))); |
140 associated_binding_->set_connection_error_handler(base::Bind( | 204 associated_binding_->set_connection_error_handler(base::Bind( |
141 &AppCacheURLLoaderJob::OnConnectionError, StaticAsWeakPtr(this))); | 205 &AppCacheURLLoaderJob::OnConnectionError, StaticAsWeakPtr(this))); |
142 | 206 |
143 client_info_ = std::move(subresource_load_info_->client); | 207 client_info_ = std::move(subresource_load_info_->client); |
144 default_url_loader_factory_getter_ = default_url_loader; | 208 default_url_loader_factory_getter_ = default_url_loader; |
145 } | 209 } |
146 | 210 |
147 void AppCacheURLLoaderJob::Start(mojom::URLLoaderRequest request, | 211 void AppCacheURLLoaderJob::Start(mojom::URLLoaderRequest request, |
148 mojom::URLLoaderClientPtr client) { | 212 mojom::URLLoaderClientPtr client) { |
149 DCHECK(!binding_.is_bound()); | 213 DCHECK(!binding_.is_bound()); |
150 binding_.Bind(std::move(request)); | 214 binding_.Bind(std::move(request)); |
151 | 215 |
152 binding_.set_connection_error_handler(base::Bind( | 216 binding_.set_connection_error_handler(base::Bind( |
153 &AppCacheURLLoaderJob::OnConnectionError, StaticAsWeakPtr(this))); | 217 &AppCacheURLLoaderJob::OnConnectionError, StaticAsWeakPtr(this))); |
154 | 218 |
155 client_info_ = std::move(client); | 219 client_info_ = std::move(client); |
156 | 220 |
157 // Send the cached AppCacheResponse if any. | 221 // Send the cached AppCacheResponse if any. |
158 if (info_.get()) | 222 if (info_.get()) |
159 SendResponseInfo(); | 223 SendResponseInfo(); |
160 } | 224 } |
161 | 225 |
162 AppCacheURLLoaderJob::AppCacheURLLoaderJob(const ResourceRequest& request, | 226 AppCacheURLLoaderJob::AppCacheURLLoaderJob( |
163 AppCacheStorage* storage) | 227 const ResourceRequest& request, |
228 AppCacheURLLoaderRequest* url_loader_request, | |
229 AppCacheStorage* storage) | |
164 : request_(request), | 230 : request_(request), |
165 storage_(storage->GetWeakPtr()), | 231 storage_(storage->GetWeakPtr()), |
166 start_time_tick_(base::TimeTicks::Now()), | 232 start_time_tick_(base::TimeTicks::Now()), |
167 cache_id_(kAppCacheNoCacheId), | 233 cache_id_(kAppCacheNoCacheId), |
168 is_fallback_(false), | 234 is_fallback_(false), |
169 binding_(this), | 235 binding_(this), |
170 writable_handle_watcher_(FROM_HERE, | 236 writable_handle_watcher_(FROM_HERE, |
171 mojo::SimpleWatcher::ArmingPolicy::MANUAL) {} | 237 mojo::SimpleWatcher::ArmingPolicy::MANUAL), |
238 loader_client_proxy_binding_(this), | |
239 url_loader_request_instance_{url_loader_request} {} | |
michaeln
2017/07/17 21:04:55
are the brackets needed, can we use parens here?
ananta
2017/07/18 00:19:03
eek. typo. Fixed
| |
172 | 240 |
173 void AppCacheURLLoaderJob::OnResponseInfoLoaded( | 241 void AppCacheURLLoaderJob::OnResponseInfoLoaded( |
174 AppCacheResponseInfo* response_info, | 242 AppCacheResponseInfo* response_info, |
175 int64_t response_id) { | 243 int64_t response_id) { |
176 DCHECK(IsDeliveringAppCacheResponse()); | 244 DCHECK(IsDeliveringAppCacheResponse()); |
177 | 245 |
178 if (!storage_.get()) { | 246 if (!storage_.get()) { |
179 DeliverErrorResponse(); | 247 DeliverErrorResponse(); |
180 return; | 248 return; |
181 } | 249 } |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
246 manifest_url_.GetOrigin()); | 314 manifest_url_.GetOrigin()); |
247 return; | 315 return; |
248 } | 316 } |
249 | 317 |
250 uint32_t bytes_written = static_cast<uint32_t>(result); | 318 uint32_t bytes_written = static_cast<uint32_t>(result); |
251 response_body_stream_ = pending_write_->Complete(bytes_written); | 319 response_body_stream_ = pending_write_->Complete(bytes_written); |
252 pending_write_ = nullptr; | 320 pending_write_ = nullptr; |
253 ReadMore(); | 321 ReadMore(); |
254 } | 322 } |
255 | 323 |
256 void AppCacheURLLoaderJob::OnConnectionError() { | 324 void AppCacheURLLoaderJob::OnConnectionError() { |
michaeln
2017/07/17 21:04:55
I think this method is being used for two differen
ananta
2017/07/18 00:19:03
Thanks for noticing this. I removed the bind call
| |
257 if (storage_.get()) | 325 if (storage_.get()) |
258 storage_->CancelDelegateCallbacks(this); | 326 storage_->CancelDelegateCallbacks(this); |
259 base::ThreadTaskRunnerHandle::Get()->DeleteSoon(FROM_HERE, this); | 327 base::ThreadTaskRunnerHandle::Get()->DeleteSoon(FROM_HERE, this); |
260 } | 328 } |
261 | 329 |
262 void AppCacheURLLoaderJob::SendResponseInfo() { | 330 void AppCacheURLLoaderJob::SendResponseInfo() { |
263 DCHECK(client_info_); | 331 DCHECK(client_info_); |
264 // If this is null it means the response information was sent to the client. | 332 // If this is null it means the response information was sent to the client. |
265 if (!data_pipe_.consumer_handle.is_valid()) | 333 if (!data_pipe_.consumer_handle.is_valid()) |
266 return; | 334 return; |
(...skipping 19 matching lines...) Expand all Loading... | |
286 : info_->response_data_size(); | 354 : info_->response_data_size(); |
287 | 355 |
288 response_head.connection_info = http_info->connection_info; | 356 response_head.connection_info = http_info->connection_info; |
289 response_head.socket_address = http_info->socket_address; | 357 response_head.socket_address = http_info->socket_address; |
290 response_head.was_fetched_via_spdy = http_info->was_fetched_via_spdy; | 358 response_head.was_fetched_via_spdy = http_info->was_fetched_via_spdy; |
291 response_head.was_alpn_negotiated = http_info->was_alpn_negotiated; | 359 response_head.was_alpn_negotiated = http_info->was_alpn_negotiated; |
292 response_head.alpn_negotiated_protocol = http_info->alpn_negotiated_protocol; | 360 response_head.alpn_negotiated_protocol = http_info->alpn_negotiated_protocol; |
293 | 361 |
294 response_head.load_timing = load_timing_info_; | 362 response_head.load_timing = load_timing_info_; |
295 | 363 |
364 url_loader_request_instance_->set_response(response_head); | |
365 | |
296 client_info_->OnReceiveResponse(response_head, http_info->ssl_info, | 366 client_info_->OnReceiveResponse(response_head, http_info->ssl_info, |
297 mojom::DownloadedTempFilePtr()); | 367 mojom::DownloadedTempFilePtr()); |
298 | 368 |
299 client_info_->OnStartLoadingResponseBody( | 369 client_info_->OnStartLoadingResponseBody( |
300 std::move(data_pipe_.consumer_handle)); | 370 std::move(data_pipe_.consumer_handle)); |
301 } | 371 } |
302 | 372 |
303 void AppCacheURLLoaderJob::ReadMore() { | 373 void AppCacheURLLoaderJob::ReadMore() { |
304 DCHECK(!pending_write_.get()); | 374 DCHECK(!pending_write_.get()); |
305 | 375 |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
359 request_complete_data.completion_time = base::TimeTicks::Now(); | 429 request_complete_data.completion_time = base::TimeTicks::Now(); |
360 request_complete_data.encoded_body_length = | 430 request_complete_data.encoded_body_length = |
361 is_range_request() ? range_response_info_->headers->GetContentLength() | 431 is_range_request() ? range_response_info_->headers->GetContentLength() |
362 : info_->response_data_size(); | 432 : info_->response_data_size(); |
363 request_complete_data.decoded_body_length = | 433 request_complete_data.decoded_body_length = |
364 request_complete_data.encoded_body_length; | 434 request_complete_data.encoded_body_length; |
365 } | 435 } |
366 client_info_->OnComplete(request_complete_data); | 436 client_info_->OnComplete(request_complete_data); |
367 } | 437 } |
368 | 438 |
439 mojom::URLLoaderClientPtr AppCacheURLLoaderJob::CreateLoaderClientProxy() { | |
440 mojom::URLLoaderClientPtr client_ptr; | |
441 loader_client_proxy_binding_.Bind(mojo::MakeRequest(&client_ptr)); | |
442 | |
443 loader_client_proxy_binding_.set_connection_error_handler(base::Bind( | |
444 &AppCacheURLLoaderJob::OnConnectionError, StaticAsWeakPtr(this))); | |
445 | |
446 return client_ptr; | |
447 } | |
448 | |
369 } // namespace content | 449 } // namespace content |
OLD | NEW |