Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(370)

Side by Side Diff: content/browser/loader/mojo_async_resource_handler.cc

Issue 2574143003: Implement upload progress handling in Mojo loading (Closed)
Patch Set: +comment Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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/loader/mojo_async_resource_handler.h" 5 #include "content/browser/loader/mojo_async_resource_handler.h"
6 6
7 #include <algorithm>
7 #include <utility> 8 #include <utility>
8 #include <vector> 9 #include <vector>
9 10
10 #include "base/bind.h" 11 #include "base/bind.h"
11 #include "base/command_line.h" 12 #include "base/command_line.h"
12 #include "base/containers/hash_tables.h" 13 #include "base/location.h"
13 #include "base/logging.h" 14 #include "base/logging.h"
14 #include "base/macros.h" 15 #include "base/macros.h"
16 #include "base/memory/ptr_util.h"
15 #include "base/strings/string_number_conversions.h" 17 #include "base/strings/string_number_conversions.h"
16 #include "base/time/time.h" 18 #include "base/time/time.h"
17 #include "content/browser/loader/downloaded_temp_file_impl.h" 19 #include "content/browser/loader/downloaded_temp_file_impl.h"
18 #include "content/browser/loader/netlog_observer.h" 20 #include "content/browser/loader/netlog_observer.h"
19 #include "content/browser/loader/resource_controller.h" 21 #include "content/browser/loader/resource_controller.h"
20 #include "content/browser/loader/resource_dispatcher_host_impl.h" 22 #include "content/browser/loader/resource_dispatcher_host_impl.h"
21 #include "content/browser/loader/resource_request_info_impl.h" 23 #include "content/browser/loader/resource_request_info_impl.h"
24 #include "content/browser/loader/upload_progress_tracker.h"
22 #include "content/common/resource_request_completion_status.h" 25 #include "content/common/resource_request_completion_status.h"
23 #include "content/public/browser/global_request_id.h" 26 #include "content/public/browser/global_request_id.h"
24 #include "content/public/browser/resource_dispatcher_host_delegate.h" 27 #include "content/public/browser/resource_dispatcher_host_delegate.h"
25 #include "content/public/common/resource_response.h" 28 #include "content/public/common/resource_response.h"
26 #include "mojo/public/c/system/data_pipe.h" 29 #include "mojo/public/c/system/data_pipe.h"
27 #include "mojo/public/cpp/bindings/message.h" 30 #include "mojo/public/cpp/bindings/message.h"
28 #include "net/base/io_buffer.h" 31 #include "net/base/io_buffer.h"
29 #include "net/base/load_flags.h"
30 #include "net/base/mime_sniffer.h" 32 #include "net/base/mime_sniffer.h"
31 #include "net/url_request/redirect_info.h" 33 #include "net/url_request/redirect_info.h"
32 34
33 namespace content { 35 namespace content {
34 namespace { 36 namespace {
35 37
36 int g_allocation_size = MojoAsyncResourceHandler::kDefaultAllocationSize; 38 int g_allocation_size = MojoAsyncResourceHandler::kDefaultAllocationSize;
37 39
38 // MimeTypeResourceHandler *implicitly* requires that the buffer size 40 // MimeTypeResourceHandler *implicitly* requires that the buffer size
39 // returned from OnWillRead should be larger than certain size. 41 // returned from OnWillRead should be larger than certain size.
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after
160 // TODO(davidben): Is it necessary to pass the new first party URL for 162 // TODO(davidben): Is it necessary to pass the new first party URL for
161 // cookies? The only case where it can change is top-level navigation requests 163 // cookies? The only case where it can change is top-level navigation requests
162 // and hopefully those will eventually all be owned by the browser. It's 164 // and hopefully those will eventually all be owned by the browser. It's
163 // possible this is still needed while renderer-owned ones exist. 165 // possible this is still needed while renderer-owned ones exist.
164 url_loader_client_->OnReceiveRedirect(redirect_info, response->head); 166 url_loader_client_->OnReceiveRedirect(redirect_info, response->head);
165 return true; 167 return true;
166 } 168 }
167 169
168 bool MojoAsyncResourceHandler::OnResponseStarted(ResourceResponse* response, 170 bool MojoAsyncResourceHandler::OnResponseStarted(ResourceResponse* response,
169 bool* defer) { 171 bool* defer) {
172 if (upload_progress_tracker_) {
173 upload_progress_tracker_->OnUploadCompleted();
174 upload_progress_tracker_ = nullptr;
175 }
176
170 const ResourceRequestInfoImpl* info = GetRequestInfo(); 177 const ResourceRequestInfoImpl* info = GetRequestInfo();
171
172 if (rdh_->delegate()) { 178 if (rdh_->delegate()) {
173 rdh_->delegate()->OnResponseStarted(request(), info->GetContext(), 179 rdh_->delegate()->OnResponseStarted(request(), info->GetContext(),
174 response); 180 response);
175 } 181 }
176 182
177 NetLogObserver::PopulateResponseInfo(request(), response); 183 NetLogObserver::PopulateResponseInfo(request(), response);
178 response->head.encoded_data_length = request()->raw_header_size(); 184 response->head.encoded_data_length = request()->raw_header_size();
179 reported_total_received_bytes_ = response->head.encoded_data_length; 185 reported_total_received_bytes_ = response->head.encoded_data_length;
180 186
181 response->head.request_start = request()->creation_time(); 187 response->head.request_start = request()->creation_time();
(...skipping 15 matching lines...) Expand all
197 if (metadata) { 203 if (metadata) {
198 const uint8_t* data = reinterpret_cast<const uint8_t*>(metadata->data()); 204 const uint8_t* data = reinterpret_cast<const uint8_t*>(metadata->data());
199 205
200 url_loader_client_->OnReceiveCachedMetadata( 206 url_loader_client_->OnReceiveCachedMetadata(
201 std::vector<uint8_t>(data, data + metadata->size())); 207 std::vector<uint8_t>(data, data + metadata->size()));
202 } 208 }
203 return true; 209 return true;
204 } 210 }
205 211
206 bool MojoAsyncResourceHandler::OnWillStart(const GURL& url, bool* defer) { 212 bool MojoAsyncResourceHandler::OnWillStart(const GURL& url, bool* defer) {
213 if (GetRequestInfo()->is_upload_progress_enabled() &&
214 request()->has_upload()) {
215 upload_progress_tracker_ = CreateUploadProgressTracker(
216 FROM_HERE,
217 base::BindRepeating(&MojoAsyncResourceHandler::SendUploadProgress,
218 base::Unretained(this)));
219 }
220
207 return true; 221 return true;
208 } 222 }
209 223
210 bool MojoAsyncResourceHandler::OnWillRead(scoped_refptr<net::IOBuffer>* buf, 224 bool MojoAsyncResourceHandler::OnWillRead(scoped_refptr<net::IOBuffer>* buf,
211 int* buf_size, 225 int* buf_size,
212 int min_size) { 226 int min_size) {
213 DCHECK_EQ(-1, min_size); 227 DCHECK_EQ(-1, min_size);
214 228
215 if (!CheckForSufficientResource()) 229 if (!CheckForSufficientResource())
216 return false; 230 return false;
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after
349 } 363 }
350 364
351 net::IOBufferWithSize* MojoAsyncResourceHandler::GetResponseMetadata( 365 net::IOBufferWithSize* MojoAsyncResourceHandler::GetResponseMetadata(
352 net::URLRequest* request) { 366 net::URLRequest* request) {
353 return request->response_info().metadata.get(); 367 return request->response_info().metadata.get();
354 } 368 }
355 369
356 void MojoAsyncResourceHandler::OnResponseCompleted( 370 void MojoAsyncResourceHandler::OnResponseCompleted(
357 const net::URLRequestStatus& status, 371 const net::URLRequestStatus& status,
358 bool* defer) { 372 bool* defer) {
373 // Ensure sending the final upload progress message here, since
374 // OnResponseCompleted can be called without OnResponseStarted on cancellation
375 // or error cases.
376 if (upload_progress_tracker_) {
377 upload_progress_tracker_->OnUploadCompleted();
378 upload_progress_tracker_ = nullptr;
379 }
380
359 shared_writer_ = nullptr; 381 shared_writer_ = nullptr;
360 buffer_ = nullptr; 382 buffer_ = nullptr;
361 handle_watcher_.Cancel(); 383 handle_watcher_.Cancel();
362 384
363 const ResourceRequestInfoImpl* info = GetRequestInfo(); 385 const ResourceRequestInfoImpl* info = GetRequestInfo();
364 386
365 // TODO(gavinp): Remove this CHECK when we figure out the cause of 387 // TODO(gavinp): Remove this CHECK when we figure out the cause of
366 // http://crbug.com/124680 . This check mirrors closely check in 388 // http://crbug.com/124680 . This check mirrors closely check in
367 // WebURLLoaderImpl::OnCompletedRequest that routes this message to a WebCore 389 // WebURLLoaderImpl::OnCompletedRequest that routes this message to a WebCore
368 // ResourceHandleInternal which asserts on its state and crashes. By crashing 390 // ResourceHandleInternal which asserts on its state and crashes. By crashing
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after
490 total_received_bytes - reported_total_received_bytes_; 512 total_received_bytes - reported_total_received_bytes_;
491 reported_total_received_bytes_ = total_received_bytes; 513 reported_total_received_bytes_ = total_received_bytes;
492 DCHECK_LE(0, bytes_to_report); 514 DCHECK_LE(0, bytes_to_report);
493 return bytes_to_report; 515 return bytes_to_report;
494 } 516 }
495 517
496 void MojoAsyncResourceHandler::ReportBadMessage(const std::string& error) { 518 void MojoAsyncResourceHandler::ReportBadMessage(const std::string& error) {
497 mojo::ReportBadMessage(error); 519 mojo::ReportBadMessage(error);
498 } 520 }
499 521
522 std::unique_ptr<UploadProgressTracker>
523 MojoAsyncResourceHandler::CreateUploadProgressTracker(
524 const tracked_objects::Location& from_here,
525 UploadProgressTracker::UploadProgressReportCallback callback) {
526 return base::MakeUnique<UploadProgressTracker>(from_here, std::move(callback),
527 request());
528 }
529
500 void MojoAsyncResourceHandler::OnTransfer( 530 void MojoAsyncResourceHandler::OnTransfer(
501 mojom::URLLoaderAssociatedRequest mojo_request, 531 mojom::URLLoaderAssociatedRequest mojo_request,
502 mojom::URLLoaderClientAssociatedPtr url_loader_client) { 532 mojom::URLLoaderClientAssociatedPtr url_loader_client) {
503 binding_.Unbind(); 533 binding_.Unbind();
504 binding_.Bind(std::move(mojo_request)); 534 binding_.Bind(std::move(mojo_request));
505 binding_.set_connection_error_handler( 535 binding_.set_connection_error_handler(
506 base::Bind(&MojoAsyncResourceHandler::Cancel, base::Unretained(this))); 536 base::Bind(&MojoAsyncResourceHandler::Cancel, base::Unretained(this)));
507 url_loader_client_ = std::move(url_loader_client); 537 url_loader_client_ = std::move(url_loader_client);
508 } 538 }
509 539
540 void MojoAsyncResourceHandler::SendUploadProgress(
541 const net::UploadProgress& progress) {
542 url_loader_client_->OnUploadProgress(
543 progress.position(), progress.size(),
544 base::Bind(&MojoAsyncResourceHandler::OnUploadProgressACK,
545 weak_factory_.GetWeakPtr()));
546 }
547
548 void MojoAsyncResourceHandler::OnUploadProgressACK() {
549 if (upload_progress_tracker_)
550 upload_progress_tracker_->OnAckReceived();
551 }
552
510 } // namespace content 553 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/loader/mojo_async_resource_handler.h ('k') | content/browser/loader/mojo_async_resource_handler_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698