| 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 | 8 |
| 9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 10 #include "base/containers/hash_tables.h" | 10 #include "base/containers/hash_tables.h" |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 118 // the callback will never be called after |this| is destroyed. | 118 // the callback will never be called after |this| is destroyed. |
| 119 binding_.set_connection_error_handler( | 119 binding_.set_connection_error_handler( |
| 120 base::Bind(&MojoAsyncResourceHandler::Cancel, base::Unretained(this))); | 120 base::Bind(&MojoAsyncResourceHandler::Cancel, base::Unretained(this))); |
| 121 } | 121 } |
| 122 | 122 |
| 123 MojoAsyncResourceHandler::~MojoAsyncResourceHandler() { | 123 MojoAsyncResourceHandler::~MojoAsyncResourceHandler() { |
| 124 if (has_checked_for_sufficient_resources_) | 124 if (has_checked_for_sufficient_resources_) |
| 125 rdh_->FinishedWithResourcesForRequest(request()); | 125 rdh_->FinishedWithResourcesForRequest(request()); |
| 126 } | 126 } |
| 127 | 127 |
| 128 bool MojoAsyncResourceHandler::OnRequestRedirected( | 128 void MojoAsyncResourceHandler::OnRequestRedirected( |
| 129 const net::RedirectInfo& redirect_info, | 129 const net::RedirectInfo& redirect_info, |
| 130 ResourceResponse* response, | 130 ResourceResponse* response, |
| 131 bool* defer) { | 131 bool* defer_or_cancel) { |
| 132 // Unlike OnResponseStarted, OnRequestRedirected will NOT be preceded by | 132 // Unlike OnResponseStarted, OnRequestRedirected will NOT be preceded by |
| 133 // OnWillRead. | 133 // OnWillRead. |
| 134 DCHECK(!shared_writer_); | 134 DCHECK(!shared_writer_); |
| 135 | 135 |
| 136 *defer = true; | 136 *defer_or_cancel = true; |
| 137 request()->LogBlockedBy("MojoAsyncResourceHandler"); | 137 request()->LogBlockedBy("MojoAsyncResourceHandler"); |
| 138 did_defer_on_redirect_ = true; | 138 did_defer_on_redirect_ = true; |
| 139 | 139 |
| 140 NetLogObserver::PopulateResponseInfo(request(), response); | 140 NetLogObserver::PopulateResponseInfo(request(), response); |
| 141 response->head.encoded_data_length = request()->GetTotalReceivedBytes(); | 141 response->head.encoded_data_length = request()->GetTotalReceivedBytes(); |
| 142 response->head.request_start = request()->creation_time(); | 142 response->head.request_start = request()->creation_time(); |
| 143 response->head.response_start = base::TimeTicks::Now(); | 143 response->head.response_start = base::TimeTicks::Now(); |
| 144 // TODO(davidben): Is it necessary to pass the new first party URL for | 144 // TODO(davidben): Is it necessary to pass the new first party URL for |
| 145 // cookies? The only case where it can change is top-level navigation requests | 145 // cookies? The only case where it can change is top-level navigation requests |
| 146 // and hopefully those will eventually all be owned by the browser. It's | 146 // and hopefully those will eventually all be owned by the browser. It's |
| 147 // possible this is still needed while renderer-owned ones exist. | 147 // possible this is still needed while renderer-owned ones exist. |
| 148 url_loader_client_->OnReceiveRedirect(redirect_info, response->head); | 148 url_loader_client_->OnReceiveRedirect(redirect_info, response->head); |
| 149 return true; | |
| 150 } | 149 } |
| 151 | 150 |
| 152 bool MojoAsyncResourceHandler::OnResponseStarted(ResourceResponse* response, | 151 void MojoAsyncResourceHandler::OnResponseStarted(ResourceResponse* response, |
| 153 bool* defer) { | 152 bool* defer_or_cancel) { |
| 154 const ResourceRequestInfoImpl* info = GetRequestInfo(); | 153 const ResourceRequestInfoImpl* info = GetRequestInfo(); |
| 155 | 154 |
| 156 if (rdh_->delegate()) { | 155 if (rdh_->delegate()) { |
| 157 rdh_->delegate()->OnResponseStarted(request(), info->GetContext(), | 156 rdh_->delegate()->OnResponseStarted(request(), info->GetContext(), |
| 158 response); | 157 response); |
| 159 } | 158 } |
| 160 | 159 |
| 161 NetLogObserver::PopulateResponseInfo(request(), response); | 160 NetLogObserver::PopulateResponseInfo(request(), response); |
| 162 | 161 |
| 163 response->head.request_start = request()->creation_time(); | 162 response->head.request_start = request()->creation_time(); |
| 164 response->head.response_start = base::TimeTicks::Now(); | 163 response->head.response_start = base::TimeTicks::Now(); |
| 165 sent_received_response_message_ = true; | 164 sent_received_response_message_ = true; |
| 166 url_loader_client_->OnReceiveResponse(response->head); | 165 url_loader_client_->OnReceiveResponse(response->head); |
| 167 return true; | |
| 168 } | 166 } |
| 169 | 167 |
| 170 bool MojoAsyncResourceHandler::OnWillStart(const GURL& url, bool* defer) { | 168 void MojoAsyncResourceHandler::OnWillStart(const GURL& url, |
| 171 return true; | 169 bool* defer_or_cancel) { |
| 170 return; |
| 172 } | 171 } |
| 173 | 172 |
| 173 // TODO(mmenke): The implementation of this method and OnReadComplete is very |
| 174 // unwieldy. Allow OnWillRead to complete asynchronously and clean things up. |
| 174 bool MojoAsyncResourceHandler::OnWillRead(scoped_refptr<net::IOBuffer>* buf, | 175 bool MojoAsyncResourceHandler::OnWillRead(scoped_refptr<net::IOBuffer>* buf, |
| 175 int* buf_size, | 176 int* buf_size, |
| 176 int min_size) { | 177 int min_size) { |
| 177 DCHECK_EQ(-1, min_size); | 178 DCHECK_EQ(-1, min_size); |
| 178 | 179 |
| 179 if (!CheckForSufficientResource()) | 180 if (!CheckForSufficientResource()) |
| 180 return false; | 181 return false; |
| 181 | 182 |
| 182 if (!shared_writer_) { | 183 if (!shared_writer_) { |
| 183 MojoCreateDataPipeOptions options; | 184 MojoCreateDataPipeOptions options; |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 216 is_using_io_buffer_not_from_writer_ = true; | 217 is_using_io_buffer_not_from_writer_ = true; |
| 217 buffer_ = new net::IOBufferWithSize(kMinAllocationSize); | 218 buffer_ = new net::IOBufferWithSize(kMinAllocationSize); |
| 218 } | 219 } |
| 219 | 220 |
| 220 DCHECK_EQ(0u, buffer_offset_); | 221 DCHECK_EQ(0u, buffer_offset_); |
| 221 *buf = buffer_; | 222 *buf = buffer_; |
| 222 *buf_size = buffer_->size(); | 223 *buf_size = buffer_->size(); |
| 223 return true; | 224 return true; |
| 224 } | 225 } |
| 225 | 226 |
| 226 bool MojoAsyncResourceHandler::OnReadCompleted(int bytes_read, bool* defer) { | 227 void MojoAsyncResourceHandler::OnReadCompleted(int bytes_read, |
| 228 bool* defer_or_cancel) { |
| 227 DCHECK_GE(bytes_read, 0); | 229 DCHECK_GE(bytes_read, 0); |
| 228 DCHECK(buffer_); | 230 DCHECK(buffer_); |
| 229 | 231 |
| 230 if (!bytes_read) | 232 if (!bytes_read) |
| 231 return true; | 233 return; |
| 232 | 234 |
| 233 if (is_using_io_buffer_not_from_writer_) { | 235 if (is_using_io_buffer_not_from_writer_) { |
| 234 // Couldn't allocate a buffer on the data pipe in OnWillRead. | 236 // Couldn't allocate a buffer on the data pipe in OnWillRead. |
| 235 DCHECK_EQ(0u, buffer_bytes_read_); | 237 DCHECK_EQ(0u, buffer_bytes_read_); |
| 236 buffer_bytes_read_ = bytes_read; | 238 buffer_bytes_read_ = bytes_read; |
| 237 if (!CopyReadDataToDataPipe(defer)) | 239 if (!CopyReadDataToDataPipe(defer_or_cancel)) { |
| 238 return false; | 240 controller()->Cancel(); |
| 239 if (*defer) { | 241 *defer_or_cancel = true; |
| 242 return; |
| 243 } |
| 244 if (*defer_or_cancel) { |
| 240 request()->LogBlockedBy("MojoAsyncResourceHandler"); | 245 request()->LogBlockedBy("MojoAsyncResourceHandler"); |
| 241 did_defer_on_writing_ = true; | 246 did_defer_on_writing_ = true; |
| 242 } | 247 } |
| 243 return true; | 248 return; |
| 244 } | 249 } |
| 245 | 250 |
| 246 if (EndWrite(bytes_read) != MOJO_RESULT_OK) | 251 if (EndWrite(bytes_read) != MOJO_RESULT_OK) { |
| 247 return false; | 252 controller()->Cancel(); |
| 253 *defer_or_cancel = true; |
| 254 return; |
| 255 } |
| 248 // Allocate a buffer for the next OnWillRead call here, because OnWillRead | 256 // Allocate a buffer for the next OnWillRead call here, because OnWillRead |
| 249 // doesn't have |defer| parameter. | 257 // doesn't have |defer| parameter. |
| 250 if (!AllocateWriterIOBuffer(&buffer_, defer)) | 258 if (!AllocateWriterIOBuffer(&buffer_, defer_or_cancel)) { |
| 251 return false; | 259 controller()->Cancel(); |
| 252 if (*defer) { | 260 *defer_or_cancel = true; |
| 261 return; |
| 262 } |
| 263 if (*defer_or_cancel) { |
| 253 request()->LogBlockedBy("MojoAsyncResourceHandler"); | 264 request()->LogBlockedBy("MojoAsyncResourceHandler"); |
| 254 did_defer_on_writing_ = true; | 265 did_defer_on_writing_ = true; |
| 255 } | 266 } |
| 256 return true; | |
| 257 } | 267 } |
| 258 | 268 |
| 259 void MojoAsyncResourceHandler::OnDataDownloaded(int bytes_downloaded) { | 269 void MojoAsyncResourceHandler::OnDataDownloaded(int bytes_downloaded) { |
| 260 int64_t total_received_bytes = request()->GetTotalReceivedBytes(); | 270 int64_t total_received_bytes = request()->GetTotalReceivedBytes(); |
| 261 int64_t bytes_to_report = | 271 int64_t bytes_to_report = |
| 262 total_received_bytes - reported_total_received_bytes_; | 272 total_received_bytes - reported_total_received_bytes_; |
| 263 reported_total_received_bytes_ = total_received_bytes; | 273 reported_total_received_bytes_ = total_received_bytes; |
| 264 DCHECK_LE(0, bytes_to_report); | 274 DCHECK_LE(0, bytes_to_report); |
| 265 | 275 |
| 266 url_loader_client_->OnDataDownloaded(bytes_downloaded, bytes_to_report); | 276 url_loader_client_->OnDataDownloaded(bytes_downloaded, bytes_to_report); |
| (...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 432 const ResourceRequestInfoImpl* info = GetRequestInfo(); | 442 const ResourceRequestInfoImpl* info = GetRequestInfo(); |
| 433 ResourceDispatcherHostImpl::Get()->CancelRequestFromRenderer( | 443 ResourceDispatcherHostImpl::Get()->CancelRequestFromRenderer( |
| 434 GlobalRequestID(info->GetChildID(), info->GetRequestID())); | 444 GlobalRequestID(info->GetChildID(), info->GetRequestID())); |
| 435 } | 445 } |
| 436 | 446 |
| 437 void MojoAsyncResourceHandler::ReportBadMessage(const std::string& error) { | 447 void MojoAsyncResourceHandler::ReportBadMessage(const std::string& error) { |
| 438 mojo::ReportBadMessage(error); | 448 mojo::ReportBadMessage(error); |
| 439 } | 449 } |
| 440 | 450 |
| 441 } // namespace content | 451 } // namespace content |
| OLD | NEW |