| OLD | NEW |
| 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 <algorithm> |
| 8 #include <utility> | 8 #include <utility> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 114 | 114 |
| 115 MojoAsyncResourceHandler::MojoAsyncResourceHandler( | 115 MojoAsyncResourceHandler::MojoAsyncResourceHandler( |
| 116 net::URLRequest* request, | 116 net::URLRequest* request, |
| 117 ResourceDispatcherHostImpl* rdh, | 117 ResourceDispatcherHostImpl* rdh, |
| 118 mojom::URLLoaderAssociatedRequest mojo_request, | 118 mojom::URLLoaderAssociatedRequest mojo_request, |
| 119 mojom::URLLoaderClientPtr url_loader_client, | 119 mojom::URLLoaderClientPtr url_loader_client, |
| 120 ResourceType resource_type) | 120 ResourceType resource_type) |
| 121 : ResourceHandler(request), | 121 : ResourceHandler(request), |
| 122 rdh_(rdh), | 122 rdh_(rdh), |
| 123 binding_(this, std::move(mojo_request)), | 123 binding_(this, std::move(mojo_request)), |
| 124 handle_watcher_(FROM_HERE), | 124 handle_watcher_(FROM_HERE, mojo::SimpleWatcher::ArmingPolicy::MANUAL), |
| 125 url_loader_client_(std::move(url_loader_client)), | 125 url_loader_client_(std::move(url_loader_client)), |
| 126 weak_factory_(this) { | 126 weak_factory_(this) { |
| 127 DCHECK(url_loader_client_); | 127 DCHECK(url_loader_client_); |
| 128 InitializeResourceBufferConstants(); | 128 InitializeResourceBufferConstants(); |
| 129 // This unretained pointer is safe, because |binding_| is owned by |this| and | 129 // This unretained pointer is safe, because |binding_| is owned by |this| and |
| 130 // the callback will never be called after |this| is destroyed. | 130 // the callback will never be called after |this| is destroyed. |
| 131 binding_.set_connection_error_handler( | 131 binding_.set_connection_error_handler( |
| 132 base::Bind(&MojoAsyncResourceHandler::Cancel, base::Unretained(this))); | 132 base::Bind(&MojoAsyncResourceHandler::Cancel, base::Unretained(this))); |
| 133 | 133 |
| 134 if (IsResourceTypeFrame(resource_type)) { | 134 if (IsResourceTypeFrame(resource_type)) { |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 238 options.flags = MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE; | 238 options.flags = MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE; |
| 239 options.element_num_bytes = 1; | 239 options.element_num_bytes = 1; |
| 240 options.capacity_num_bytes = g_allocation_size; | 240 options.capacity_num_bytes = g_allocation_size; |
| 241 mojo::DataPipe data_pipe(options); | 241 mojo::DataPipe data_pipe(options); |
| 242 | 242 |
| 243 DCHECK(data_pipe.producer_handle.is_valid()); | 243 DCHECK(data_pipe.producer_handle.is_valid()); |
| 244 DCHECK(data_pipe.consumer_handle.is_valid()); | 244 DCHECK(data_pipe.consumer_handle.is_valid()); |
| 245 | 245 |
| 246 response_body_consumer_handle_ = std::move(data_pipe.consumer_handle); | 246 response_body_consumer_handle_ = std::move(data_pipe.consumer_handle); |
| 247 shared_writer_ = new SharedWriter(std::move(data_pipe.producer_handle)); | 247 shared_writer_ = new SharedWriter(std::move(data_pipe.producer_handle)); |
| 248 handle_watcher_.Start(shared_writer_->writer(), MOJO_HANDLE_SIGNAL_WRITABLE, | 248 handle_watcher_.Watch(shared_writer_->writer(), MOJO_HANDLE_SIGNAL_WRITABLE, |
| 249 base::Bind(&MojoAsyncResourceHandler::OnWritable, | 249 base::Bind(&MojoAsyncResourceHandler::OnWritable, |
| 250 base::Unretained(this))); | 250 base::Unretained(this))); |
| 251 handle_watcher_.ArmOrNotify(); |
| 251 | 252 |
| 252 bool defer = false; | 253 bool defer = false; |
| 253 scoped_refptr<net::IOBufferWithSize> buffer; | 254 scoped_refptr<net::IOBufferWithSize> buffer; |
| 254 if (!AllocateWriterIOBuffer(&buffer, &defer)) { | 255 if (!AllocateWriterIOBuffer(&buffer, &defer)) { |
| 255 controller->CancelWithError(net::ERR_INSUFFICIENT_RESOURCES); | 256 controller->CancelWithError(net::ERR_INSUFFICIENT_RESOURCES); |
| 256 return; | 257 return; |
| 257 } | 258 } |
| 258 if (!defer) { | 259 if (!defer) { |
| 259 if (static_cast<size_t>(buffer->size()) >= kMinAllocationSize) { | 260 if (static_cast<size_t>(buffer->size()) >= kMinAllocationSize) { |
| 260 *buf = buffer_ = buffer; | 261 *buf = buffer_ = buffer; |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 381 void MojoAsyncResourceHandler::SetAllocationSizeForTesting(size_t size) { | 382 void MojoAsyncResourceHandler::SetAllocationSizeForTesting(size_t size) { |
| 382 g_allocation_size = size; | 383 g_allocation_size = size; |
| 383 } | 384 } |
| 384 | 385 |
| 385 MojoResult MojoAsyncResourceHandler::BeginWrite(void** data, | 386 MojoResult MojoAsyncResourceHandler::BeginWrite(void** data, |
| 386 uint32_t* available) { | 387 uint32_t* available) { |
| 387 MojoResult result = mojo::BeginWriteDataRaw( | 388 MojoResult result = mojo::BeginWriteDataRaw( |
| 388 shared_writer_->writer(), data, available, MOJO_WRITE_DATA_FLAG_NONE); | 389 shared_writer_->writer(), data, available, MOJO_WRITE_DATA_FLAG_NONE); |
| 389 if (result == MOJO_RESULT_OK) | 390 if (result == MOJO_RESULT_OK) |
| 390 *available = std::min(*available, static_cast<uint32_t>(kMaxChunkSize)); | 391 *available = std::min(*available, static_cast<uint32_t>(kMaxChunkSize)); |
| 392 else if (result == MOJO_RESULT_SHOULD_WAIT) |
| 393 handle_watcher_.ArmOrNotify(); |
| 391 return result; | 394 return result; |
| 392 } | 395 } |
| 393 | 396 |
| 394 MojoResult MojoAsyncResourceHandler::EndWrite(uint32_t written) { | 397 MojoResult MojoAsyncResourceHandler::EndWrite(uint32_t written) { |
| 395 return mojo::EndWriteDataRaw(shared_writer_->writer(), written); | 398 MojoResult result = mojo::EndWriteDataRaw(shared_writer_->writer(), written); |
| 399 if (result == MOJO_RESULT_OK) |
| 400 handle_watcher_.ArmOrNotify(); |
| 401 return result; |
| 396 } | 402 } |
| 397 | 403 |
| 398 net::IOBufferWithSize* MojoAsyncResourceHandler::GetResponseMetadata( | 404 net::IOBufferWithSize* MojoAsyncResourceHandler::GetResponseMetadata( |
| 399 net::URLRequest* request) { | 405 net::URLRequest* request) { |
| 400 return request->response_info().metadata.get(); | 406 return request->response_info().metadata.get(); |
| 401 } | 407 } |
| 402 | 408 |
| 403 void MojoAsyncResourceHandler::OnResponseCompleted( | 409 void MojoAsyncResourceHandler::OnResponseCompleted( |
| 404 const net::URLRequestStatus& status, | 410 const net::URLRequestStatus& status, |
| 405 std::unique_ptr<ResourceController> controller) { | 411 std::unique_ptr<ResourceController> controller) { |
| (...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 578 base::Bind(&MojoAsyncResourceHandler::OnUploadProgressACK, | 584 base::Bind(&MojoAsyncResourceHandler::OnUploadProgressACK, |
| 579 weak_factory_.GetWeakPtr())); | 585 weak_factory_.GetWeakPtr())); |
| 580 } | 586 } |
| 581 | 587 |
| 582 void MojoAsyncResourceHandler::OnUploadProgressACK() { | 588 void MojoAsyncResourceHandler::OnUploadProgressACK() { |
| 583 if (upload_progress_tracker_) | 589 if (upload_progress_tracker_) |
| 584 upload_progress_tracker_->OnAckReceived(); | 590 upload_progress_tracker_->OnAckReceived(); |
| 585 } | 591 } |
| 586 | 592 |
| 587 } // namespace content | 593 } // namespace content |
| OLD | NEW |