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

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

Issue 2574143003: Implement upload progress handling in Mojo loading (Closed)
Patch Set: s/OnMesnsageReceived/Dispatch/ Created 3 years, 11 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
11 #include "base/bind.h"
10 #include "base/command_line.h" 12 #include "base/command_line.h"
11 #include "base/containers/hash_tables.h" 13 #include "base/location.h"
12 #include "base/logging.h" 14 #include "base/logging.h"
13 #include "base/macros.h" 15 #include "base/macros.h"
16 #include "base/memory/ptr_util.h"
14 #include "base/strings/string_number_conversions.h" 17 #include "base/strings/string_number_conversions.h"
15 #include "base/time/time.h" 18 #include "base/time/time.h"
16 #include "content/browser/loader/downloaded_temp_file_impl.h" 19 #include "content/browser/loader/downloaded_temp_file_impl.h"
17 #include "content/browser/loader/netlog_observer.h" 20 #include "content/browser/loader/netlog_observer.h"
18 #include "content/browser/loader/resource_controller.h" 21 #include "content/browser/loader/resource_controller.h"
19 #include "content/browser/loader/resource_dispatcher_host_impl.h" 22 #include "content/browser/loader/resource_dispatcher_host_impl.h"
20 #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"
21 #include "content/common/resource_request_completion_status.h" 25 #include "content/common/resource_request_completion_status.h"
22 #include "content/public/browser/global_request_id.h" 26 #include "content/public/browser/global_request_id.h"
23 #include "content/public/browser/resource_dispatcher_host_delegate.h" 27 #include "content/public/browser/resource_dispatcher_host_delegate.h"
24 #include "content/public/common/resource_response.h" 28 #include "content/public/common/resource_response.h"
25 #include "mojo/public/c/system/data_pipe.h" 29 #include "mojo/public/c/system/data_pipe.h"
26 #include "mojo/public/cpp/bindings/message.h" 30 #include "mojo/public/cpp/bindings/message.h"
27 #include "net/base/io_buffer.h" 31 #include "net/base/io_buffer.h"
28 #include "net/base/load_flags.h"
29 #include "net/base/mime_sniffer.h" 32 #include "net/base/mime_sniffer.h"
30 #include "net/url_request/redirect_info.h" 33 #include "net/url_request/redirect_info.h"
31 34
32 namespace content { 35 namespace content {
33 namespace { 36 namespace {
34 37
35 int g_allocation_size = MojoAsyncResourceHandler::kDefaultAllocationSize; 38 int g_allocation_size = MojoAsyncResourceHandler::kDefaultAllocationSize;
36 39
37 // MimeTypeResourceHandler *implicitly* requires that the buffer size 40 // MimeTypeResourceHandler *implicitly* requires that the buffer size
38 // returned from OnWillRead should be larger than certain size. 41 // returned from OnWillRead should be larger than certain size.
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after
149 // TODO(davidben): Is it necessary to pass the new first party URL for 152 // TODO(davidben): Is it necessary to pass the new first party URL for
150 // cookies? The only case where it can change is top-level navigation requests 153 // cookies? The only case where it can change is top-level navigation requests
151 // and hopefully those will eventually all be owned by the browser. It's 154 // and hopefully those will eventually all be owned by the browser. It's
152 // possible this is still needed while renderer-owned ones exist. 155 // possible this is still needed while renderer-owned ones exist.
153 url_loader_client_->OnReceiveRedirect(redirect_info, response->head); 156 url_loader_client_->OnReceiveRedirect(redirect_info, response->head);
154 return true; 157 return true;
155 } 158 }
156 159
157 bool MojoAsyncResourceHandler::OnResponseStarted(ResourceResponse* response, 160 bool MojoAsyncResourceHandler::OnResponseStarted(ResourceResponse* response,
158 bool* defer) { 161 bool* defer) {
162 if (upload_progress_tracker_) {
163 upload_progress_tracker_->OnUploadCompleted();
164 upload_progress_tracker_ = nullptr;
165 }
166
159 const ResourceRequestInfoImpl* info = GetRequestInfo(); 167 const ResourceRequestInfoImpl* info = GetRequestInfo();
160
161 if (rdh_->delegate()) { 168 if (rdh_->delegate()) {
162 rdh_->delegate()->OnResponseStarted(request(), info->GetContext(), 169 rdh_->delegate()->OnResponseStarted(request(), info->GetContext(),
163 response); 170 response);
164 } 171 }
165 172
166 NetLogObserver::PopulateResponseInfo(request(), response); 173 NetLogObserver::PopulateResponseInfo(request(), response);
167 response->head.encoded_data_length = request()->raw_header_size(); 174 response->head.encoded_data_length = request()->raw_header_size();
168 reported_total_received_bytes_ = response->head.encoded_data_length; 175 reported_total_received_bytes_ = response->head.encoded_data_length;
169 176
170 response->head.request_start = request()->creation_time(); 177 response->head.request_start = request()->creation_time();
(...skipping 15 matching lines...) Expand all
186 if (metadata) { 193 if (metadata) {
187 const uint8_t* data = reinterpret_cast<const uint8_t*>(metadata->data()); 194 const uint8_t* data = reinterpret_cast<const uint8_t*>(metadata->data());
188 195
189 url_loader_client_->OnReceiveCachedMetadata( 196 url_loader_client_->OnReceiveCachedMetadata(
190 std::vector<uint8_t>(data, data + metadata->size())); 197 std::vector<uint8_t>(data, data + metadata->size()));
191 } 198 }
192 return true; 199 return true;
193 } 200 }
194 201
195 bool MojoAsyncResourceHandler::OnWillStart(const GURL& url, bool* defer) { 202 bool MojoAsyncResourceHandler::OnWillStart(const GURL& url, bool* defer) {
203 if (GetRequestInfo()->is_upload_progress_enabled() &&
204 request()->has_upload()) {
205 upload_progress_tracker_ = CreateUploadProgressTracker(
206 FROM_HERE,
207 base::BindRepeating(&MojoAsyncResourceHandler::SendUploadProgress,
208 base::Unretained(this)));
209 }
210
196 return true; 211 return true;
197 } 212 }
198 213
199 bool MojoAsyncResourceHandler::OnWillRead(scoped_refptr<net::IOBuffer>* buf, 214 bool MojoAsyncResourceHandler::OnWillRead(scoped_refptr<net::IOBuffer>* buf,
200 int* buf_size, 215 int* buf_size,
201 int min_size) { 216 int min_size) {
202 DCHECK_EQ(-1, min_size); 217 DCHECK_EQ(-1, min_size);
203 218
204 if (!CheckForSufficientResource()) 219 if (!CheckForSufficientResource())
205 return false; 220 return false;
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after
338 } 353 }
339 354
340 net::IOBufferWithSize* MojoAsyncResourceHandler::GetResponseMetadata( 355 net::IOBufferWithSize* MojoAsyncResourceHandler::GetResponseMetadata(
341 net::URLRequest* request) { 356 net::URLRequest* request) {
342 return request->response_info().metadata.get(); 357 return request->response_info().metadata.get();
343 } 358 }
344 359
345 void MojoAsyncResourceHandler::OnResponseCompleted( 360 void MojoAsyncResourceHandler::OnResponseCompleted(
346 const net::URLRequestStatus& status, 361 const net::URLRequestStatus& status,
347 bool* defer) { 362 bool* defer) {
363 if (upload_progress_tracker_) {
364 upload_progress_tracker_->OnUploadCompleted();
365 upload_progress_tracker_ = nullptr;
dcheng 2017/01/24 10:38:58 This might be a dumb question, but why do we need
tzik 2017/01/24 12:04:59 Yes, we skip it on cancellation and error cases. A
366 }
367
348 shared_writer_ = nullptr; 368 shared_writer_ = nullptr;
349 buffer_ = nullptr; 369 buffer_ = nullptr;
350 handle_watcher_.Cancel(); 370 handle_watcher_.Cancel();
351 371
352 const ResourceRequestInfoImpl* info = GetRequestInfo(); 372 const ResourceRequestInfoImpl* info = GetRequestInfo();
353 373
354 // TODO(gavinp): Remove this CHECK when we figure out the cause of 374 // TODO(gavinp): Remove this CHECK when we figure out the cause of
355 // http://crbug.com/124680 . This check mirrors closely check in 375 // http://crbug.com/124680 . This check mirrors closely check in
356 // WebURLLoaderImpl::OnCompletedRequest that routes this message to a WebCore 376 // WebURLLoaderImpl::OnCompletedRequest that routes this message to a WebCore
357 // ResourceHandleInternal which asserts on its state and crashes. By crashing 377 // ResourceHandleInternal which asserts on its state and crashes. By crashing
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after
479 total_received_bytes - reported_total_received_bytes_; 499 total_received_bytes - reported_total_received_bytes_;
480 reported_total_received_bytes_ = total_received_bytes; 500 reported_total_received_bytes_ = total_received_bytes;
481 DCHECK_LE(0, bytes_to_report); 501 DCHECK_LE(0, bytes_to_report);
482 return bytes_to_report; 502 return bytes_to_report;
483 } 503 }
484 504
485 void MojoAsyncResourceHandler::ReportBadMessage(const std::string& error) { 505 void MojoAsyncResourceHandler::ReportBadMessage(const std::string& error) {
486 mojo::ReportBadMessage(error); 506 mojo::ReportBadMessage(error);
487 } 507 }
488 508
509 std::unique_ptr<UploadProgressTracker>
510 MojoAsyncResourceHandler::CreateUploadProgressTracker(
511 const tracked_objects::Location& from_here,
512 UploadProgressTracker::UploadProgressReportCallback callback) {
513 return base::MakeUnique<UploadProgressTracker>(from_here, std::move(callback),
514 request());
515 }
516
489 void MojoAsyncResourceHandler::OnTransfer( 517 void MojoAsyncResourceHandler::OnTransfer(
490 mojom::URLLoaderAssociatedRequest mojo_request, 518 mojom::URLLoaderAssociatedRequest mojo_request,
491 mojom::URLLoaderClientAssociatedPtr url_loader_client) { 519 mojom::URLLoaderClientAssociatedPtr url_loader_client) {
492 binding_.Unbind(); 520 binding_.Unbind();
493 binding_.Bind(std::move(mojo_request)); 521 binding_.Bind(std::move(mojo_request));
494 binding_.set_connection_error_handler( 522 binding_.set_connection_error_handler(
495 base::Bind(&MojoAsyncResourceHandler::Cancel, base::Unretained(this))); 523 base::Bind(&MojoAsyncResourceHandler::Cancel, base::Unretained(this)));
496 url_loader_client_ = std::move(url_loader_client); 524 url_loader_client_ = std::move(url_loader_client);
497 } 525 }
498 526
527 void MojoAsyncResourceHandler::SendUploadProgress(
528 const net::UploadProgress& progress) {
529 url_loader_client_->OnUploadProgress(
530 progress.position(), progress.size(),
531 base::Bind(&MojoAsyncResourceHandler::OnUploadProgressACK,
532 weak_factory_.GetWeakPtr()));
533 }
534
535 void MojoAsyncResourceHandler::OnUploadProgressACK() {
536 if (upload_progress_tracker_)
537 upload_progress_tracker_->OnAckReceived();
538 }
539
499 } // namespace content 540 } // 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