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 <utility> | 7 #include <utility> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
124 | 124 |
125 GetRequestInfo()->set_on_transfer(base::Bind( | 125 GetRequestInfo()->set_on_transfer(base::Bind( |
126 &MojoAsyncResourceHandler::OnTransfer, weak_factory_.GetWeakPtr())); | 126 &MojoAsyncResourceHandler::OnTransfer, weak_factory_.GetWeakPtr())); |
127 } | 127 } |
128 | 128 |
129 MojoAsyncResourceHandler::~MojoAsyncResourceHandler() { | 129 MojoAsyncResourceHandler::~MojoAsyncResourceHandler() { |
130 if (has_checked_for_sufficient_resources_) | 130 if (has_checked_for_sufficient_resources_) |
131 rdh_->FinishedWithResourcesForRequest(request()); | 131 rdh_->FinishedWithResourcesForRequest(request()); |
132 } | 132 } |
133 | 133 |
134 bool MojoAsyncResourceHandler::OnRequestRedirected( | 134 void MojoAsyncResourceHandler::OnRequestRedirected( |
135 const net::RedirectInfo& redirect_info, | 135 const net::RedirectInfo& redirect_info, |
136 ResourceResponse* response, | 136 ResourceResponse* response, |
137 bool* defer) { | 137 std::unique_ptr<ResourceController> controller) { |
138 // Unlike OnResponseStarted, OnRequestRedirected will NOT be preceded by | 138 // Unlike OnResponseStarted, OnRequestRedirected will NOT be preceded by |
139 // OnWillRead. | 139 // OnWillRead. |
| 140 DCHECK(!has_controller()); |
140 DCHECK(!shared_writer_); | 141 DCHECK(!shared_writer_); |
141 | 142 |
142 *defer = true; | |
143 request()->LogBlockedBy("MojoAsyncResourceHandler"); | 143 request()->LogBlockedBy("MojoAsyncResourceHandler"); |
| 144 HoldController(std::move(controller)); |
144 did_defer_on_redirect_ = true; | 145 did_defer_on_redirect_ = true; |
145 | 146 |
146 NetLogObserver::PopulateResponseInfo(request(), response); | 147 NetLogObserver::PopulateResponseInfo(request(), response); |
147 response->head.encoded_data_length = request()->GetTotalReceivedBytes(); | 148 response->head.encoded_data_length = request()->GetTotalReceivedBytes(); |
148 response->head.request_start = request()->creation_time(); | 149 response->head.request_start = request()->creation_time(); |
149 response->head.response_start = base::TimeTicks::Now(); | 150 response->head.response_start = base::TimeTicks::Now(); |
150 // TODO(davidben): Is it necessary to pass the new first party URL for | 151 // TODO(davidben): Is it necessary to pass the new first party URL for |
151 // cookies? The only case where it can change is top-level navigation requests | 152 // cookies? The only case where it can change is top-level navigation requests |
152 // and hopefully those will eventually all be owned by the browser. It's | 153 // and hopefully those will eventually all be owned by the browser. It's |
153 // possible this is still needed while renderer-owned ones exist. | 154 // possible this is still needed while renderer-owned ones exist. |
154 url_loader_client_->OnReceiveRedirect(redirect_info, response->head); | 155 url_loader_client_->OnReceiveRedirect(redirect_info, response->head); |
155 return true; | |
156 } | 156 } |
157 | 157 |
158 bool MojoAsyncResourceHandler::OnResponseStarted(ResourceResponse* response, | 158 void MojoAsyncResourceHandler::OnResponseStarted( |
159 bool* defer) { | 159 ResourceResponse* response, |
| 160 std::unique_ptr<ResourceController> controller) { |
| 161 DCHECK(!has_controller()); |
| 162 |
160 const ResourceRequestInfoImpl* info = GetRequestInfo(); | 163 const ResourceRequestInfoImpl* info = GetRequestInfo(); |
161 | 164 |
162 if (rdh_->delegate()) { | 165 if (rdh_->delegate()) { |
163 rdh_->delegate()->OnResponseStarted(request(), info->GetContext(), | 166 rdh_->delegate()->OnResponseStarted(request(), info->GetContext(), |
164 response); | 167 response); |
165 } | 168 } |
166 | 169 |
167 NetLogObserver::PopulateResponseInfo(request(), response); | 170 NetLogObserver::PopulateResponseInfo(request(), response); |
168 response->head.encoded_data_length = request()->raw_header_size(); | 171 response->head.encoded_data_length = request()->raw_header_size(); |
169 reported_total_received_bytes_ = response->head.encoded_data_length; | 172 reported_total_received_bytes_ = response->head.encoded_data_length; |
(...skipping 13 matching lines...) Expand all Loading... |
183 url_loader_client_->OnReceiveResponse(response->head, | 186 url_loader_client_->OnReceiveResponse(response->head, |
184 std::move(downloaded_file_ptr)); | 187 std::move(downloaded_file_ptr)); |
185 | 188 |
186 net::IOBufferWithSize* metadata = GetResponseMetadata(request()); | 189 net::IOBufferWithSize* metadata = GetResponseMetadata(request()); |
187 if (metadata) { | 190 if (metadata) { |
188 const uint8_t* data = reinterpret_cast<const uint8_t*>(metadata->data()); | 191 const uint8_t* data = reinterpret_cast<const uint8_t*>(metadata->data()); |
189 | 192 |
190 url_loader_client_->OnReceiveCachedMetadata( | 193 url_loader_client_->OnReceiveCachedMetadata( |
191 std::vector<uint8_t>(data, data + metadata->size())); | 194 std::vector<uint8_t>(data, data + metadata->size())); |
192 } | 195 } |
193 return true; | 196 |
| 197 controller->Resume(); |
194 } | 198 } |
195 | 199 |
196 bool MojoAsyncResourceHandler::OnWillStart(const GURL& url, bool* defer) { | 200 void MojoAsyncResourceHandler::OnWillStart( |
197 return true; | 201 const GURL& url, |
| 202 std::unique_ptr<ResourceController> controller) { |
| 203 controller->Resume(); |
198 } | 204 } |
199 | 205 |
200 bool MojoAsyncResourceHandler::OnWillRead(scoped_refptr<net::IOBuffer>* buf, | 206 bool MojoAsyncResourceHandler::OnWillRead(scoped_refptr<net::IOBuffer>* buf, |
201 int* buf_size, | 207 int* buf_size, |
202 int min_size) { | 208 int min_size) { |
203 DCHECK_EQ(-1, min_size); | 209 DCHECK_EQ(-1, min_size); |
204 | 210 |
| 211 // TODO(mmenke): Cancel with net::ERR_INSUFFICIENT_RESOURCES instead. |
205 if (!CheckForSufficientResource()) | 212 if (!CheckForSufficientResource()) |
206 return false; | 213 return false; |
207 | 214 |
208 if (!shared_writer_) { | 215 if (!shared_writer_) { |
209 MojoCreateDataPipeOptions options; | 216 MojoCreateDataPipeOptions options; |
210 options.struct_size = sizeof(MojoCreateDataPipeOptions); | 217 options.struct_size = sizeof(MojoCreateDataPipeOptions); |
211 options.flags = MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE; | 218 options.flags = MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE; |
212 options.element_num_bytes = 1; | 219 options.element_num_bytes = 1; |
213 options.capacity_num_bytes = g_allocation_size; | 220 options.capacity_num_bytes = g_allocation_size; |
214 mojo::DataPipe data_pipe(options); | 221 mojo::DataPipe data_pipe(options); |
(...skipping 27 matching lines...) Expand all Loading... |
242 is_using_io_buffer_not_from_writer_ = true; | 249 is_using_io_buffer_not_from_writer_ = true; |
243 buffer_ = new net::IOBufferWithSize(kMinAllocationSize); | 250 buffer_ = new net::IOBufferWithSize(kMinAllocationSize); |
244 } | 251 } |
245 | 252 |
246 DCHECK_EQ(0u, buffer_offset_); | 253 DCHECK_EQ(0u, buffer_offset_); |
247 *buf = buffer_; | 254 *buf = buffer_; |
248 *buf_size = buffer_->size(); | 255 *buf_size = buffer_->size(); |
249 return true; | 256 return true; |
250 } | 257 } |
251 | 258 |
252 bool MojoAsyncResourceHandler::OnReadCompleted(int bytes_read, bool* defer) { | 259 void MojoAsyncResourceHandler::OnReadCompleted( |
| 260 int bytes_read, |
| 261 std::unique_ptr<ResourceController> controller) { |
| 262 DCHECK(!has_controller()); |
253 DCHECK_GE(bytes_read, 0); | 263 DCHECK_GE(bytes_read, 0); |
254 DCHECK(buffer_); | 264 DCHECK(buffer_); |
255 | 265 |
256 if (!bytes_read) | 266 if (!bytes_read) { |
257 return true; | 267 controller->Resume(); |
| 268 return; |
| 269 } |
258 | 270 |
259 const ResourceRequestInfoImpl* info = GetRequestInfo(); | 271 const ResourceRequestInfoImpl* info = GetRequestInfo(); |
260 if (info->ShouldReportRawHeaders()) { | 272 if (info->ShouldReportRawHeaders()) { |
261 auto transfer_size_diff = CalculateRecentlyReceivedBytes(); | 273 auto transfer_size_diff = CalculateRecentlyReceivedBytes(); |
262 if (transfer_size_diff > 0) | 274 if (transfer_size_diff > 0) |
263 url_loader_client_->OnTransferSizeUpdated(transfer_size_diff); | 275 url_loader_client_->OnTransferSizeUpdated(transfer_size_diff); |
264 } | 276 } |
265 | 277 |
266 if (is_using_io_buffer_not_from_writer_) { | 278 if (is_using_io_buffer_not_from_writer_) { |
267 // Couldn't allocate a buffer on the data pipe in OnWillRead. | 279 // Couldn't allocate a buffer on the data pipe in OnWillRead. |
268 DCHECK_EQ(0u, buffer_bytes_read_); | 280 DCHECK_EQ(0u, buffer_bytes_read_); |
269 buffer_bytes_read_ = bytes_read; | 281 buffer_bytes_read_ = bytes_read; |
270 if (!CopyReadDataToDataPipe(defer)) | 282 bool defer = false; |
271 return false; | 283 if (!CopyReadDataToDataPipe(&defer)) { |
272 if (*defer) { | 284 controller->Cancel(); |
| 285 return; |
| 286 } |
| 287 if (defer) { |
273 request()->LogBlockedBy("MojoAsyncResourceHandler"); | 288 request()->LogBlockedBy("MojoAsyncResourceHandler"); |
274 did_defer_on_writing_ = true; | 289 did_defer_on_writing_ = true; |
| 290 HoldController(std::move(controller)); |
| 291 return; |
275 } | 292 } |
276 return true; | 293 controller->Resume(); |
| 294 return; |
277 } | 295 } |
278 | 296 |
279 if (EndWrite(bytes_read) != MOJO_RESULT_OK) | 297 if (EndWrite(bytes_read) != MOJO_RESULT_OK) { |
280 return false; | 298 controller->Cancel(); |
| 299 return; |
| 300 } |
281 // Allocate a buffer for the next OnWillRead call here, because OnWillRead | 301 // Allocate a buffer for the next OnWillRead call here, because OnWillRead |
282 // doesn't have |defer| parameter. | 302 // doesn't have |defer| parameter. |
283 if (!AllocateWriterIOBuffer(&buffer_, defer)) | 303 bool defer = false; |
284 return false; | 304 if (!AllocateWriterIOBuffer(&buffer_, &defer)) { |
285 if (*defer) { | 305 controller->Cancel(); |
| 306 return; |
| 307 } |
| 308 if (defer) { |
286 request()->LogBlockedBy("MojoAsyncResourceHandler"); | 309 request()->LogBlockedBy("MojoAsyncResourceHandler"); |
287 did_defer_on_writing_ = true; | 310 did_defer_on_writing_ = true; |
| 311 HoldController(std::move(controller)); |
| 312 return; |
288 } | 313 } |
289 return true; | 314 |
| 315 controller->Resume(); |
290 } | 316 } |
291 | 317 |
292 void MojoAsyncResourceHandler::OnDataDownloaded(int bytes_downloaded) { | 318 void MojoAsyncResourceHandler::OnDataDownloaded(int bytes_downloaded) { |
293 url_loader_client_->OnDataDownloaded(bytes_downloaded, | 319 url_loader_client_->OnDataDownloaded(bytes_downloaded, |
294 CalculateRecentlyReceivedBytes()); | 320 CalculateRecentlyReceivedBytes()); |
295 } | 321 } |
296 | 322 |
297 void MojoAsyncResourceHandler::FollowRedirect() { | 323 void MojoAsyncResourceHandler::FollowRedirect() { |
298 if (!request()->status().is_success()) { | 324 if (!request()->status().is_success()) { |
299 DVLOG(1) << "FollowRedirect for invalid request"; | 325 DVLOG(1) << "FollowRedirect for invalid request"; |
300 return; | 326 return; |
301 } | 327 } |
302 if (!did_defer_on_redirect_) { | 328 if (!did_defer_on_redirect_) { |
303 DVLOG(1) << "Malformed FollowRedirect request"; | 329 DVLOG(1) << "Malformed FollowRedirect request"; |
304 ReportBadMessage("Malformed FollowRedirect request"); | 330 ReportBadMessage("Malformed FollowRedirect request"); |
305 return; | 331 return; |
306 } | 332 } |
307 | 333 |
308 DCHECK(!did_defer_on_writing_); | 334 DCHECK(!did_defer_on_writing_); |
309 did_defer_on_redirect_ = false; | 335 did_defer_on_redirect_ = false; |
310 request()->LogUnblocked(); | 336 request()->LogUnblocked(); |
311 controller()->Resume(); | 337 Resume(); |
312 } | 338 } |
313 | 339 |
314 void MojoAsyncResourceHandler::OnWritableForTesting() { | 340 void MojoAsyncResourceHandler::OnWritableForTesting() { |
315 OnWritable(MOJO_RESULT_OK); | 341 OnWritable(MOJO_RESULT_OK); |
316 } | 342 } |
317 | 343 |
318 void MojoAsyncResourceHandler::SetAllocationSizeForTesting(size_t size) { | 344 void MojoAsyncResourceHandler::SetAllocationSizeForTesting(size_t size) { |
319 g_allocation_size = size; | 345 g_allocation_size = size; |
320 } | 346 } |
321 | 347 |
(...skipping 10 matching lines...) Expand all Loading... |
332 return mojo::EndWriteDataRaw(shared_writer_->writer(), written); | 358 return mojo::EndWriteDataRaw(shared_writer_->writer(), written); |
333 } | 359 } |
334 | 360 |
335 net::IOBufferWithSize* MojoAsyncResourceHandler::GetResponseMetadata( | 361 net::IOBufferWithSize* MojoAsyncResourceHandler::GetResponseMetadata( |
336 net::URLRequest* request) { | 362 net::URLRequest* request) { |
337 return request->response_info().metadata.get(); | 363 return request->response_info().metadata.get(); |
338 } | 364 } |
339 | 365 |
340 void MojoAsyncResourceHandler::OnResponseCompleted( | 366 void MojoAsyncResourceHandler::OnResponseCompleted( |
341 const net::URLRequestStatus& status, | 367 const net::URLRequestStatus& status, |
342 bool* defer) { | 368 std::unique_ptr<ResourceController> controller) { |
343 shared_writer_ = nullptr; | 369 shared_writer_ = nullptr; |
344 buffer_ = nullptr; | 370 buffer_ = nullptr; |
345 handle_watcher_.Cancel(); | 371 handle_watcher_.Cancel(); |
346 | 372 |
347 const ResourceRequestInfoImpl* info = GetRequestInfo(); | 373 const ResourceRequestInfoImpl* info = GetRequestInfo(); |
348 | 374 |
349 // TODO(gavinp): Remove this CHECK when we figure out the cause of | 375 // TODO(gavinp): Remove this CHECK when we figure out the cause of |
350 // http://crbug.com/124680 . This check mirrors closely check in | 376 // http://crbug.com/124680 . This check mirrors closely check in |
351 // WebURLLoaderImpl::OnCompletedRequest that routes this message to a WebCore | 377 // WebURLLoaderImpl::OnCompletedRequest that routes this message to a WebCore |
352 // ResourceHandleInternal which asserts on its state and crashes. By crashing | 378 // ResourceHandleInternal which asserts on its state and crashes. By crashing |
(...skipping 13 matching lines...) Expand all Loading... |
366 ResourceRequestCompletionStatus request_complete_data; | 392 ResourceRequestCompletionStatus request_complete_data; |
367 request_complete_data.error_code = error_code; | 393 request_complete_data.error_code = error_code; |
368 request_complete_data.was_ignored_by_handler = was_ignored_by_handler; | 394 request_complete_data.was_ignored_by_handler = was_ignored_by_handler; |
369 request_complete_data.exists_in_cache = request()->response_info().was_cached; | 395 request_complete_data.exists_in_cache = request()->response_info().was_cached; |
370 request_complete_data.completion_time = base::TimeTicks::Now(); | 396 request_complete_data.completion_time = base::TimeTicks::Now(); |
371 request_complete_data.encoded_data_length = | 397 request_complete_data.encoded_data_length = |
372 request()->GetTotalReceivedBytes(); | 398 request()->GetTotalReceivedBytes(); |
373 request_complete_data.encoded_body_length = request()->GetRawBodyBytes(); | 399 request_complete_data.encoded_body_length = request()->GetRawBodyBytes(); |
374 | 400 |
375 url_loader_client_->OnComplete(request_complete_data); | 401 url_loader_client_->OnComplete(request_complete_data); |
| 402 controller->Resume(); |
376 } | 403 } |
377 | 404 |
378 bool MojoAsyncResourceHandler::CopyReadDataToDataPipe(bool* defer) { | 405 bool MojoAsyncResourceHandler::CopyReadDataToDataPipe(bool* defer) { |
379 while (true) { | 406 while (true) { |
380 scoped_refptr<net::IOBufferWithSize> dest; | 407 scoped_refptr<net::IOBufferWithSize> dest; |
381 if (!AllocateWriterIOBuffer(&dest, defer)) | 408 if (!AllocateWriterIOBuffer(&dest, defer)) |
382 return false; | 409 return false; |
383 if (*defer) | 410 if (*defer) |
384 return true; | 411 return true; |
385 if (buffer_bytes_read_ == 0) { | 412 if (buffer_bytes_read_ == 0) { |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
421 } | 448 } |
422 | 449 |
423 bool MojoAsyncResourceHandler::CheckForSufficientResource() { | 450 bool MojoAsyncResourceHandler::CheckForSufficientResource() { |
424 if (has_checked_for_sufficient_resources_) | 451 if (has_checked_for_sufficient_resources_) |
425 return true; | 452 return true; |
426 has_checked_for_sufficient_resources_ = true; | 453 has_checked_for_sufficient_resources_ = true; |
427 | 454 |
428 if (rdh_->HasSufficientResourcesForRequest(request())) | 455 if (rdh_->HasSufficientResourcesForRequest(request())) |
429 return true; | 456 return true; |
430 | 457 |
431 controller()->CancelWithError(net::ERR_INSUFFICIENT_RESOURCES); | |
432 return false; | 458 return false; |
433 } | 459 } |
434 | 460 |
435 void MojoAsyncResourceHandler::OnWritable(MojoResult result) { | 461 void MojoAsyncResourceHandler::OnWritable(MojoResult result) { |
436 if (!did_defer_on_writing_) | 462 if (!did_defer_on_writing_) |
437 return; | 463 return; |
| 464 DCHECK(has_controller()); |
438 DCHECK(!did_defer_on_redirect_); | 465 DCHECK(!did_defer_on_redirect_); |
439 did_defer_on_writing_ = false; | 466 did_defer_on_writing_ = false; |
440 | 467 |
441 if (is_using_io_buffer_not_from_writer_) { | 468 if (is_using_io_buffer_not_from_writer_) { |
442 // |buffer_| is set to a net::IOBufferWithSize. Write the buffer contents | 469 // |buffer_| is set to a net::IOBufferWithSize. Write the buffer contents |
443 // to the data pipe. | 470 // to the data pipe. |
444 DCHECK_GT(buffer_bytes_read_, 0u); | 471 DCHECK_GT(buffer_bytes_read_, 0u); |
445 if (!CopyReadDataToDataPipe(&did_defer_on_writing_)) { | 472 if (!CopyReadDataToDataPipe(&did_defer_on_writing_)) { |
446 controller()->CancelWithError(net::ERR_FAILED); | 473 CancelWithError(net::ERR_FAILED); |
447 return; | 474 return; |
448 } | 475 } |
449 } else { | 476 } else { |
450 // Allocate a buffer for the next OnWillRead call here. | 477 // Allocate a buffer for the next OnWillRead call here. |
451 if (!AllocateWriterIOBuffer(&buffer_, &did_defer_on_writing_)) { | 478 if (!AllocateWriterIOBuffer(&buffer_, &did_defer_on_writing_)) { |
452 controller()->CancelWithError(net::ERR_FAILED); | 479 CancelWithError(net::ERR_FAILED); |
453 return; | 480 return; |
454 } | 481 } |
455 } | 482 } |
456 | 483 |
457 if (did_defer_on_writing_) { | 484 if (did_defer_on_writing_) { |
458 // Continue waiting. | 485 // Continue waiting. |
459 return; | 486 return; |
460 } | 487 } |
461 request()->LogUnblocked(); | 488 request()->LogUnblocked(); |
462 controller()->Resume(); | 489 Resume(); |
463 } | 490 } |
464 | 491 |
465 void MojoAsyncResourceHandler::Cancel() { | 492 void MojoAsyncResourceHandler::Cancel() { |
466 const ResourceRequestInfoImpl* info = GetRequestInfo(); | 493 const ResourceRequestInfoImpl* info = GetRequestInfo(); |
467 ResourceDispatcherHostImpl::Get()->CancelRequestFromRenderer( | 494 ResourceDispatcherHostImpl::Get()->CancelRequestFromRenderer( |
468 GlobalRequestID(info->GetChildID(), info->GetRequestID())); | 495 GlobalRequestID(info->GetChildID(), info->GetRequestID())); |
469 } | 496 } |
470 | 497 |
471 int64_t MojoAsyncResourceHandler::CalculateRecentlyReceivedBytes() { | 498 int64_t MojoAsyncResourceHandler::CalculateRecentlyReceivedBytes() { |
472 int64_t total_received_bytes = request()->GetTotalReceivedBytes(); | 499 int64_t total_received_bytes = request()->GetTotalReceivedBytes(); |
(...skipping 12 matching lines...) Expand all Loading... |
485 mojom::URLLoaderAssociatedRequest mojo_request, | 512 mojom::URLLoaderAssociatedRequest mojo_request, |
486 mojom::URLLoaderClientAssociatedPtr url_loader_client) { | 513 mojom::URLLoaderClientAssociatedPtr url_loader_client) { |
487 binding_.Unbind(); | 514 binding_.Unbind(); |
488 binding_.Bind(std::move(mojo_request)); | 515 binding_.Bind(std::move(mojo_request)); |
489 binding_.set_connection_error_handler( | 516 binding_.set_connection_error_handler( |
490 base::Bind(&MojoAsyncResourceHandler::Cancel, base::Unretained(this))); | 517 base::Bind(&MojoAsyncResourceHandler::Cancel, base::Unretained(this))); |
491 url_loader_client_ = std::move(url_loader_client); | 518 url_loader_client_ = std::move(url_loader_client); |
492 } | 519 } |
493 | 520 |
494 } // namespace content | 521 } // namespace content |
OLD | NEW |