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

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

Issue 2668603003: Make ResourceHandler::OnWillRead able to complete asynchronously. (Closed)
Patch Set: Response to comments Created 3 years, 9 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 <algorithm>
8 #include <utility> 8 #include <utility>
9 #include <vector> 9 #include <vector>
10 10
(...skipping 211 matching lines...) Expand 10 before | Expand all | Expand 10 after
222 request()->has_upload()) { 222 request()->has_upload()) {
223 upload_progress_tracker_ = CreateUploadProgressTracker( 223 upload_progress_tracker_ = CreateUploadProgressTracker(
224 FROM_HERE, 224 FROM_HERE,
225 base::BindRepeating(&MojoAsyncResourceHandler::SendUploadProgress, 225 base::BindRepeating(&MojoAsyncResourceHandler::SendUploadProgress,
226 base::Unretained(this))); 226 base::Unretained(this)));
227 } 227 }
228 228
229 controller->Resume(); 229 controller->Resume();
230 } 230 }
231 231
232 bool MojoAsyncResourceHandler::OnWillRead(scoped_refptr<net::IOBuffer>* buf, 232 void MojoAsyncResourceHandler::OnWillRead(
233 int* buf_size) { 233 scoped_refptr<net::IOBuffer>* buf,
234 // TODO(mmenke): Cancel with net::ERR_INSUFFICIENT_RESOURCES instead. 234 int* buf_size,
235 if (!CheckForSufficientResource()) 235 std::unique_ptr<ResourceController> controller) {
236 return false; 236 if (!CheckForSufficientResource()) {
237 controller->CancelWithError(net::ERR_INSUFFICIENT_RESOURCES);
238 return;
239 }
237 240
238 if (!shared_writer_) { 241 if (!shared_writer_) {
239 MojoCreateDataPipeOptions options; 242 MojoCreateDataPipeOptions options;
240 options.struct_size = sizeof(MojoCreateDataPipeOptions); 243 options.struct_size = sizeof(MojoCreateDataPipeOptions);
241 options.flags = MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE; 244 options.flags = MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE;
242 options.element_num_bytes = 1; 245 options.element_num_bytes = 1;
243 options.capacity_num_bytes = g_allocation_size; 246 options.capacity_num_bytes = g_allocation_size;
244 mojo::DataPipe data_pipe(options); 247 mojo::DataPipe data_pipe(options);
245 248
246 DCHECK(data_pipe.producer_handle.is_valid()); 249 DCHECK(data_pipe.producer_handle.is_valid());
247 DCHECK(data_pipe.consumer_handle.is_valid()); 250 DCHECK(data_pipe.consumer_handle.is_valid());
248 251
249 response_body_consumer_handle_ = std::move(data_pipe.consumer_handle); 252 response_body_consumer_handle_ = std::move(data_pipe.consumer_handle);
250 shared_writer_ = new SharedWriter(std::move(data_pipe.producer_handle)); 253 shared_writer_ = new SharedWriter(std::move(data_pipe.producer_handle));
251 handle_watcher_.Start(shared_writer_->writer(), MOJO_HANDLE_SIGNAL_WRITABLE, 254 handle_watcher_.Start(shared_writer_->writer(), MOJO_HANDLE_SIGNAL_WRITABLE,
252 base::Bind(&MojoAsyncResourceHandler::OnWritable, 255 base::Bind(&MojoAsyncResourceHandler::OnWritable,
253 base::Unretained(this))); 256 base::Unretained(this)));
254 257
255 bool defer = false; 258 bool defer = false;
256 scoped_refptr<net::IOBufferWithSize> buffer; 259 scoped_refptr<net::IOBufferWithSize> buffer;
257 if (!AllocateWriterIOBuffer(&buffer, &defer)) 260 if (!AllocateWriterIOBuffer(&buffer, &defer)) {
258 return false; 261 controller->CancelWithError(net::ERR_INSUFFICIENT_RESOURCES);
262 return;
263 }
259 if (!defer) { 264 if (!defer) {
260 if (static_cast<size_t>(buffer->size()) >= kMinAllocationSize) { 265 if (static_cast<size_t>(buffer->size()) >= kMinAllocationSize) {
261 *buf = buffer_ = buffer; 266 *buf = buffer_ = buffer;
262 *buf_size = buffer_->size(); 267 *buf_size = buffer_->size();
263 return true; 268 controller->Resume();
269 return;
264 } 270 }
265 271
266 // The allocated buffer is too small. 272 // The allocated buffer is too small.
267 if (EndWrite(0) != MOJO_RESULT_OK) 273 if (EndWrite(0) != MOJO_RESULT_OK) {
268 return false; 274 controller->CancelWithError(net::ERR_INSUFFICIENT_RESOURCES);
275 return;
276 }
269 } 277 }
270 DCHECK(!is_using_io_buffer_not_from_writer_); 278 DCHECK(!is_using_io_buffer_not_from_writer_);
271 is_using_io_buffer_not_from_writer_ = true; 279 is_using_io_buffer_not_from_writer_ = true;
272 buffer_ = new net::IOBufferWithSize(kMinAllocationSize); 280 buffer_ = new net::IOBufferWithSize(kMinAllocationSize);
273 } 281 }
274 282
275 DCHECK_EQ(0u, buffer_offset_); 283 DCHECK_EQ(0u, buffer_offset_);
276 *buf = buffer_; 284 *buf = buffer_;
277 *buf_size = buffer_->size(); 285 *buf_size = buffer_->size();
278 return true; 286 controller->Resume();
279 } 287 }
280 288
281 void MojoAsyncResourceHandler::OnReadCompleted( 289 void MojoAsyncResourceHandler::OnReadCompleted(
282 int bytes_read, 290 int bytes_read,
283 std::unique_ptr<ResourceController> controller) { 291 std::unique_ptr<ResourceController> controller) {
284 DCHECK(!has_controller()); 292 DCHECK(!has_controller());
285 DCHECK_GE(bytes_read, 0); 293 DCHECK_GE(bytes_read, 0);
286 DCHECK(buffer_); 294 DCHECK(buffer_);
287 295
288 if (!bytes_read) { 296 if (!bytes_read) {
(...skipping 287 matching lines...) Expand 10 before | Expand all | Expand 10 after
576 base::Bind(&MojoAsyncResourceHandler::OnUploadProgressACK, 584 base::Bind(&MojoAsyncResourceHandler::OnUploadProgressACK,
577 weak_factory_.GetWeakPtr())); 585 weak_factory_.GetWeakPtr()));
578 } 586 }
579 587
580 void MojoAsyncResourceHandler::OnUploadProgressACK() { 588 void MojoAsyncResourceHandler::OnUploadProgressACK() {
581 if (upload_progress_tracker_) 589 if (upload_progress_tracker_)
582 upload_progress_tracker_->OnAckReceived(); 590 upload_progress_tracker_->OnAckReceived();
583 } 591 }
584 592
585 } // namespace content 593 } // 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