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 } else { | |
147 // Close the pipe to the network loader as we are delivering a fallback | |
148 // response to the client. | |
149 loader_client_proxy_binding_.Close(); | |
150 } | |
151 } | |
152 | |
153 void AppCacheURLLoaderJob::OnReceiveRedirect( | |
154 const net::RedirectInfo& redirect_info, | |
155 const ResourceResponseHead& response_head) { | |
156 url_loader_request_instance_->set_response(response_head); | |
157 // The MaybeLoadFallbackForRedirect() call below can pass a fallback | |
158 // response to us. Reset the delivery_type_ to ensure that we can | |
159 // receive it | |
160 delivery_type_ = AWAITING_DELIVERY_ORDERS; | |
161 if (!sub_resource_handler_->MaybeLoadFallbackForRedirect( | |
162 nullptr, redirect_info.new_url)) { | |
163 client_info_->OnReceiveRedirect(redirect_info, response_head); | |
164 } else { | |
165 // Close the pipe to the network loader as we are delivering a fallback | |
166 // response to the client. | |
167 loader_client_proxy_binding_.Close(); | |
168 } | |
169 } | |
170 | |
171 void AppCacheURLLoaderJob::OnDataDownloaded(int64_t data_len, | |
172 int64_t encoded_data_len) { | |
173 client_info_->OnDataDownloaded(data_len, encoded_data_len); | |
174 } | |
175 | |
176 void AppCacheURLLoaderJob::OnUploadProgress( | |
177 int64_t current_position, | |
178 int64_t total_size, | |
179 OnUploadProgressCallback ack_callback) { | |
180 client_info_->OnUploadProgress(current_position, total_size, | |
181 std::move(ack_callback)); | |
182 } | |
183 | |
184 void AppCacheURLLoaderJob::OnReceiveCachedMetadata( | |
185 const std::vector<uint8_t>& data) { | |
186 client_info_->OnReceiveCachedMetadata(data); | |
187 } | |
188 | |
189 void AppCacheURLLoaderJob::OnTransferSizeUpdated(int32_t transfer_size_diff) { | |
190 client_info_->OnTransferSizeUpdated(transfer_size_diff); | |
191 } | |
192 | |
193 void AppCacheURLLoaderJob::OnStartLoadingResponseBody( | |
194 mojo::ScopedDataPipeConsumerHandle body) { | |
195 client_info_->OnStartLoadingResponseBody(std::move(body)); | |
196 } | |
197 | |
198 void AppCacheURLLoaderJob::OnComplete( | |
199 const ResourceRequestCompletionStatus& status) { | |
200 delivery_type_ = AWAITING_DELIVERY_ORDERS; | |
201 if (!sub_resource_handler_->MaybeLoadFallbackForResponse(nullptr)) { | |
202 client_info_->OnComplete(status); | |
203 } else { | |
204 // Close the pipe to the network loader as we are delivering a fallback | |
205 // response to the client. | |
206 loader_client_proxy_binding_.Close(); | |
michaeln
2017/07/18 19:19:22
This turns off the network loaders delegate callba
ananta
2017/07/18 19:52:40
Thanks. Done. I moved this to a function Disconnec
| |
207 } | |
208 } | |
209 | |
133 void AppCacheURLLoaderJob::SetSubresourceLoadInfo( | 210 void AppCacheURLLoaderJob::SetSubresourceLoadInfo( |
134 std::unique_ptr<SubresourceLoadInfo> subresource_load_info, | 211 std::unique_ptr<SubresourceLoadInfo> subresource_load_info, |
135 URLLoaderFactoryGetter* default_url_loader) { | 212 URLLoaderFactoryGetter* default_url_loader) { |
136 subresource_load_info_ = std::move(subresource_load_info); | 213 subresource_load_info_ = std::move(subresource_load_info); |
137 | 214 |
138 associated_binding_.reset(new mojo::AssociatedBinding<mojom::URLLoader>( | 215 associated_binding_.reset(new mojo::AssociatedBinding<mojom::URLLoader>( |
139 this, std::move(subresource_load_info_->url_loader_request))); | 216 this, std::move(subresource_load_info_->url_loader_request))); |
140 associated_binding_->set_connection_error_handler(base::Bind( | 217 associated_binding_->set_connection_error_handler(base::Bind( |
141 &AppCacheURLLoaderJob::OnConnectionError, StaticAsWeakPtr(this))); | 218 &AppCacheURLLoaderJob::OnConnectionError, StaticAsWeakPtr(this))); |
142 | 219 |
143 client_info_ = std::move(subresource_load_info_->client); | 220 client_info_ = std::move(subresource_load_info_->client); |
144 default_url_loader_factory_getter_ = default_url_loader; | 221 default_url_loader_factory_getter_ = default_url_loader; |
145 } | 222 } |
146 | 223 |
147 void AppCacheURLLoaderJob::Start(mojom::URLLoaderRequest request, | 224 void AppCacheURLLoaderJob::Start(mojom::URLLoaderRequest request, |
148 mojom::URLLoaderClientPtr client) { | 225 mojom::URLLoaderClientPtr client) { |
149 DCHECK(!binding_.is_bound()); | 226 DCHECK(!binding_.is_bound()); |
150 binding_.Bind(std::move(request)); | 227 binding_.Bind(std::move(request)); |
151 | 228 |
152 binding_.set_connection_error_handler(base::Bind( | 229 binding_.set_connection_error_handler(base::Bind( |
153 &AppCacheURLLoaderJob::OnConnectionError, StaticAsWeakPtr(this))); | 230 &AppCacheURLLoaderJob::OnConnectionError, StaticAsWeakPtr(this))); |
154 | 231 |
155 client_info_ = std::move(client); | 232 client_info_ = std::move(client); |
156 | 233 |
157 // Send the cached AppCacheResponse if any. | 234 // Send the cached AppCacheResponse if any. |
158 if (info_.get()) | 235 if (info_.get()) |
159 SendResponseInfo(); | 236 SendResponseInfo(); |
160 } | 237 } |
161 | 238 |
162 AppCacheURLLoaderJob::AppCacheURLLoaderJob(const ResourceRequest& request, | 239 AppCacheURLLoaderJob::AppCacheURLLoaderJob( |
163 AppCacheStorage* storage) | 240 const ResourceRequest& request, |
241 AppCacheURLLoaderRequest* url_loader_request, | |
michaeln
2017/07/18 19:19:22
Calling this 'url_loader_request' is misleading, i
ananta
2017/07/18 19:52:40
Makes sense. The names are confusing. Done.
| |
242 AppCacheStorage* storage) | |
164 : request_(request), | 243 : request_(request), |
165 storage_(storage->GetWeakPtr()), | 244 storage_(storage->GetWeakPtr()), |
166 start_time_tick_(base::TimeTicks::Now()), | 245 start_time_tick_(base::TimeTicks::Now()), |
167 cache_id_(kAppCacheNoCacheId), | 246 cache_id_(kAppCacheNoCacheId), |
168 is_fallback_(false), | 247 is_fallback_(false), |
169 binding_(this), | 248 binding_(this), |
170 writable_handle_watcher_(FROM_HERE, | 249 writable_handle_watcher_(FROM_HERE, |
171 mojo::SimpleWatcher::ArmingPolicy::MANUAL) {} | 250 mojo::SimpleWatcher::ArmingPolicy::MANUAL), |
251 loader_client_proxy_binding_(this), | |
252 url_loader_request_instance_(url_loader_request) {} | |
172 | 253 |
173 void AppCacheURLLoaderJob::OnResponseInfoLoaded( | 254 void AppCacheURLLoaderJob::OnResponseInfoLoaded( |
174 AppCacheResponseInfo* response_info, | 255 AppCacheResponseInfo* response_info, |
175 int64_t response_id) { | 256 int64_t response_id) { |
176 DCHECK(IsDeliveringAppCacheResponse()); | 257 DCHECK(IsDeliveringAppCacheResponse()); |
177 | 258 |
178 if (!storage_.get()) { | 259 if (!storage_.get()) { |
179 DeliverErrorResponse(); | 260 DeliverErrorResponse(); |
180 return; | 261 return; |
181 } | 262 } |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
286 : info_->response_data_size(); | 367 : info_->response_data_size(); |
287 | 368 |
288 response_head.connection_info = http_info->connection_info; | 369 response_head.connection_info = http_info->connection_info; |
289 response_head.socket_address = http_info->socket_address; | 370 response_head.socket_address = http_info->socket_address; |
290 response_head.was_fetched_via_spdy = http_info->was_fetched_via_spdy; | 371 response_head.was_fetched_via_spdy = http_info->was_fetched_via_spdy; |
291 response_head.was_alpn_negotiated = http_info->was_alpn_negotiated; | 372 response_head.was_alpn_negotiated = http_info->was_alpn_negotiated; |
292 response_head.alpn_negotiated_protocol = http_info->alpn_negotiated_protocol; | 373 response_head.alpn_negotiated_protocol = http_info->alpn_negotiated_protocol; |
293 | 374 |
294 response_head.load_timing = load_timing_info_; | 375 response_head.load_timing = load_timing_info_; |
295 | 376 |
377 url_loader_request_instance_->set_response(response_head); | |
378 | |
296 client_info_->OnReceiveResponse(response_head, http_info->ssl_info, | 379 client_info_->OnReceiveResponse(response_head, http_info->ssl_info, |
297 mojom::DownloadedTempFilePtr()); | 380 mojom::DownloadedTempFilePtr()); |
298 | 381 |
299 client_info_->OnStartLoadingResponseBody( | 382 client_info_->OnStartLoadingResponseBody( |
300 std::move(data_pipe_.consumer_handle)); | 383 std::move(data_pipe_.consumer_handle)); |
301 } | 384 } |
302 | 385 |
303 void AppCacheURLLoaderJob::ReadMore() { | 386 void AppCacheURLLoaderJob::ReadMore() { |
304 DCHECK(!pending_write_.get()); | 387 DCHECK(!pending_write_.get()); |
305 | 388 |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
359 request_complete_data.completion_time = base::TimeTicks::Now(); | 442 request_complete_data.completion_time = base::TimeTicks::Now(); |
360 request_complete_data.encoded_body_length = | 443 request_complete_data.encoded_body_length = |
361 is_range_request() ? range_response_info_->headers->GetContentLength() | 444 is_range_request() ? range_response_info_->headers->GetContentLength() |
362 : info_->response_data_size(); | 445 : info_->response_data_size(); |
363 request_complete_data.decoded_body_length = | 446 request_complete_data.decoded_body_length = |
364 request_complete_data.encoded_body_length; | 447 request_complete_data.encoded_body_length; |
365 } | 448 } |
366 client_info_->OnComplete(request_complete_data); | 449 client_info_->OnComplete(request_complete_data); |
367 } | 450 } |
368 | 451 |
452 mojom::URLLoaderClientPtr AppCacheURLLoaderJob::CreateLoaderClientProxy() { | |
michaeln
2017/07/18 19:19:22
Consider moving the body of this method directly i
ananta
2017/07/18 19:52:40
Thanks. done.
| |
453 mojom::URLLoaderClientPtr client_ptr; | |
454 loader_client_proxy_binding_.Bind(mojo::MakeRequest(&client_ptr)); | |
455 return client_ptr; | |
456 } | |
457 | |
369 } // namespace content | 458 } // namespace content |
OLD | NEW |