Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "content/browser/loader/mojo_async_resource_handler.h" | |
| 6 | |
| 7 #include <utility> | |
| 8 | |
| 9 #include "base/command_line.h" | |
| 10 #include "base/containers/hash_tables.h" | |
| 11 #include "base/logging.h" | |
| 12 #include "base/macros.h" | |
| 13 #include "base/metrics/histogram_macros.h" | |
| 14 #include "base/strings/string_number_conversions.h" | |
| 15 #include "base/time/time.h" | |
| 16 #include "content/browser/loader/netlog_observer.h" | |
| 17 #include "content/browser/loader/resource_dispatcher_host_impl.h" | |
| 18 #include "content/browser/loader/resource_message_filter.h" | |
| 19 #include "content/browser/loader/resource_request_info_impl.h" | |
| 20 #include "content/common/resource_request_completion_status.h" | |
| 21 #include "content/public/browser/resource_dispatcher_host_delegate.h" | |
| 22 #include "content/public/common/resource_response.h" | |
| 23 #include "net/base/io_buffer.h" | |
| 24 #include "net/base/load_flags.h" | |
| 25 #include "net/log/net_log.h" | |
| 26 #include "net/url_request/redirect_info.h" | |
| 27 | |
| 28 namespace content { | |
| 29 namespace { | |
| 30 | |
| 31 int maxAllocationSize = 1024 * 32; | |
|
mmenke
2016/05/25 16:17:20
max_allocation_size, or better, g_max_allocation_s
yhirano
2016/05/26 15:42:44
Thanks, done.
| |
| 32 | |
| 33 void GetNumericArg(const std::string& name, int* result) { | |
| 34 const std::string& value = | |
| 35 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(name); | |
| 36 if (!value.empty()) | |
| 37 base::StringToInt(value, result); | |
| 38 } | |
| 39 | |
| 40 void InitializeResourceBufferConstants() { | |
| 41 static bool did_init = false; | |
| 42 if (did_init) | |
| 43 return; | |
| 44 did_init = true; | |
| 45 | |
| 46 GetNumericArg("resource-buffer-max-allocation-size", &maxAllocationSize); | |
|
mmenke
2016/05/25 16:17:20
Wow..I was going to complain about this being weir
| |
| 47 } | |
| 48 | |
| 49 } // namespace | |
| 50 | |
| 51 MojoAsyncResourceHandler::MojoAsyncResourceHandler( | |
| 52 net::URLRequest* request, | |
| 53 ResourceDispatcherHostImpl* rdh, | |
| 54 std::unique_ptr<mojom::URLLoader> url_loader, | |
| 55 mojom::URLLoaderClientPtr url_loader_client) | |
| 56 : ResourceHandler(request), | |
| 57 rdh_(rdh), | |
| 58 url_loader_(std::move(url_loader)), | |
| 59 url_loader_client_(std::move(url_loader_client)) { | |
| 60 DCHECK(url_loader_); | |
| 61 DCHECK(url_loader_client_); | |
| 62 InitializeResourceBufferConstants(); | |
| 63 } | |
| 64 | |
| 65 MojoAsyncResourceHandler::~MojoAsyncResourceHandler() { | |
| 66 if (has_checked_for_sufficient_resources_) | |
| 67 rdh_->FinishedWithResourcesForRequest(request()); | |
| 68 } | |
| 69 | |
| 70 void MojoAsyncResourceHandler::OnFollowRedirect(int request_id) { | |
| 71 NOTREACHED(); | |
| 72 } | |
| 73 | |
| 74 bool MojoAsyncResourceHandler::OnRequestRedirected( | |
| 75 const net::RedirectInfo& redirect_info, | |
| 76 ResourceResponse* response, | |
| 77 bool* defer) { | |
| 78 // Not implemented. | |
| 79 return false; | |
| 80 } | |
| 81 | |
| 82 bool MojoAsyncResourceHandler::OnResponseStarted(ResourceResponse* response, | |
| 83 bool* defer) { | |
| 84 // For changes to the main frame, inform the renderer of the new URL's | |
| 85 // per-host settings before the request actually commits. This way the | |
| 86 // renderer will be able to set these precisely at the time the | |
| 87 // request commits, avoiding the possibility of e.g. zooming the old content | |
| 88 // or of having to layout the new content twice. | |
| 89 | |
| 90 response_started_ticks_ = base::TimeTicks::Now(); | |
| 91 | |
| 92 const ResourceRequestInfoImpl* info = GetRequestInfo(); | |
| 93 if (!info->filter()) | |
| 94 return false; | |
| 95 | |
| 96 if (rdh_->delegate()) { | |
| 97 rdh_->delegate()->OnResponseStarted(request(), info->GetContext(), response, | |
| 98 info->filter()); | |
| 99 } | |
| 100 | |
| 101 NetLogObserver::PopulateResponseInfo(request(), response); | |
| 102 | |
| 103 // If the parent handler downloaded the resource to a file, grant the child | |
| 104 // read permissions on it. | |
| 105 if (!response->head.download_file_path.empty()) { | |
| 106 rdh_->RegisterDownloadedTempFile(info->GetChildID(), info->GetRequestID(), | |
| 107 response->head.download_file_path); | |
| 108 } | |
| 109 | |
| 110 response->head.request_start = request()->creation_time(); | |
| 111 response->head.response_start = base::TimeTicks::Now(); | |
| 112 sent_received_response_message_ = true; | |
| 113 url_loader_client_->OnReceiveResponse(response->head); | |
| 114 return true; | |
| 115 } | |
| 116 | |
| 117 bool MojoAsyncResourceHandler::OnWillStart(const GURL& url, bool* defer) { | |
| 118 return true; | |
| 119 } | |
| 120 | |
| 121 bool MojoAsyncResourceHandler::OnBeforeNetworkStart(const GURL& url, | |
| 122 bool* defer) { | |
| 123 return true; | |
| 124 } | |
| 125 | |
| 126 bool MojoAsyncResourceHandler::OnWillRead(scoped_refptr<net::IOBuffer>* buf, | |
| 127 int* buf_size, | |
| 128 int min_size) { | |
| 129 DCHECK_EQ(-1, min_size); | |
| 130 | |
| 131 if (!CheckForSufficientResource()) | |
| 132 return false; | |
| 133 | |
| 134 void* buffer = nullptr; | |
| 135 uint32_t available = 0; | |
| 136 if (!writer_.is_valid()) { | |
| 137 MojoCreateDataPipeOptions options; | |
| 138 options.struct_size = sizeof(MojoCreateDataPipeOptions); | |
| 139 options.flags = MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE; | |
| 140 options.element_num_bytes = 1; | |
| 141 options.capacity_num_bytes = maxAllocationSize; | |
| 142 mojo::DataPipe data_pipe(options); | |
| 143 | |
| 144 writer_ = std::move(data_pipe.producer_handle); | |
| 145 ResourceMessageFilter* filter = GetRequestInfo()->filter(); | |
| 146 if (filter) { | |
| 147 url_loader_client_->OnStartLoadingResponseBody( | |
| 148 std::move(data_pipe.consumer_handle)); | |
| 149 } | |
| 150 } | |
| 151 if (!writer_.is_valid()) { | |
| 152 controller()->CancelWithError(net::ERR_FAILED); | |
| 153 return false; | |
| 154 } | |
| 155 | |
| 156 MojoResult result = mojo::BeginWriteDataRaw( | |
| 157 writer_.get(), &buffer, &available, MOJO_WRITE_DATA_FLAG_NONE); | |
| 158 // SHOULD_WAIT should be handled in OnReadCompleted. | |
| 159 if (result == MOJO_RESULT_OK) { | |
| 160 *buf = new net::WrappedIOBuffer(static_cast<const char*>(buffer)); | |
| 161 *buf_size = available; | |
| 162 return true; | |
| 163 } | |
| 164 controller()->CancelWithError(net::ERR_FAILED); | |
| 165 return false; | |
| 166 } | |
| 167 | |
| 168 bool MojoAsyncResourceHandler::OnReadCompleted(int bytes_read, bool* defer) { | |
| 169 DCHECK_GE(bytes_read, 0); | |
| 170 | |
| 171 if (!bytes_read) | |
| 172 return true; | |
| 173 | |
| 174 MojoResult result = mojo::EndWriteDataRaw(writer_.get(), bytes_read); | |
| 175 if (result != MOJO_RESULT_OK) | |
| 176 return false; | |
| 177 void* buffer = nullptr; | |
| 178 uint32_t available = 0; | |
| 179 // To see if the handle is still writable. | |
| 180 result = mojo::BeginWriteDataRaw(writer_.get(), &buffer, &available, | |
| 181 MOJO_WRITE_DATA_FLAG_NONE); | |
| 182 if (result == MOJO_RESULT_SHOULD_WAIT || | |
| 183 (result == MOJO_RESULT_OK && available == 0)) { | |
| 184 *defer = did_defer_ = true; | |
| 185 OnDefer(); | |
| 186 handle_watcher_.Start(writer_.get(), MOJO_HANDLE_SIGNAL_WRITABLE, | |
| 187 MOJO_DEADLINE_INDEFINITE, | |
| 188 base::Bind(&MojoAsyncResourceHandler::OnWritable, | |
| 189 base::Unretained(this))); | |
| 190 } | |
| 191 if (result == MOJO_RESULT_OK) | |
| 192 mojo::EndWriteDataRaw(writer_.get(), 0); | |
| 193 return true; | |
| 194 } | |
| 195 | |
| 196 void MojoAsyncResourceHandler::OnDataDownloaded(int bytes_downloaded) { | |
| 197 // Not implemented. | |
|
kinuko
2016/05/26 08:43:12
NOTIMPLEMENTED() ?
yhirano
2016/05/26 15:42:44
I think it's not good to output error messages.
| |
| 198 } | |
| 199 | |
| 200 void MojoAsyncResourceHandler::OnResponseCompleted( | |
| 201 const net::URLRequestStatus& status, | |
| 202 const std::string& security_info, | |
| 203 bool* defer) { | |
| 204 const ResourceRequestInfoImpl* info = GetRequestInfo(); | |
| 205 if (!info->filter()) | |
| 206 return; | |
| 207 | |
| 208 // TODO(gavinp): Remove this CHECK when we figure out the cause of | |
| 209 // http://crbug.com/124680 . This check mirrors closely check in | |
| 210 // WebURLLoaderImpl::OnCompletedRequest that routes this message to a WebCore | |
| 211 // ResourceHandleInternal which asserts on its state and crashes. By crashing | |
| 212 // when the message is sent, we should get better crash reports. | |
| 213 CHECK(status.status() != net::URLRequestStatus::SUCCESS || | |
| 214 sent_received_response_message_); | |
|
kinuko
2016/05/26 08:43:12
Not entirely sure if we should mirror these CHECK
yhirano
2016/05/26 15:42:44
Deleted.
| |
| 215 | |
| 216 int error_code = status.error(); | |
| 217 bool was_ignored_by_handler = info->WasIgnoredByHandler(); | |
| 218 | |
| 219 DCHECK(status.status() != net::URLRequestStatus::IO_PENDING); | |
| 220 // If this check fails, then we're in an inconsistent state because all | |
| 221 // requests ignored by the handler should be canceled (which should result in | |
| 222 // the ERR_ABORTED error code). | |
| 223 DCHECK(!was_ignored_by_handler || error_code == net::ERR_ABORTED); | |
| 224 | |
| 225 // TODO(mkosiba): Fix up cases where we create a URLRequestStatus | |
| 226 // with a status() != SUCCESS and an error_code() == net::OK. | |
| 227 if (status.status() == net::URLRequestStatus::CANCELED && | |
| 228 error_code == net::OK) { | |
| 229 error_code = net::ERR_ABORTED; | |
| 230 } else if (status.status() == net::URLRequestStatus::FAILED && | |
| 231 error_code == net::OK) { | |
| 232 error_code = net::ERR_FAILED; | |
| 233 } | |
| 234 | |
| 235 ResourceRequestCompletionStatus request_complete_data; | |
| 236 request_complete_data.error_code = error_code; | |
| 237 request_complete_data.was_ignored_by_handler = was_ignored_by_handler; | |
| 238 request_complete_data.exists_in_cache = request()->response_info().was_cached; | |
| 239 request_complete_data.security_info = security_info; | |
| 240 request_complete_data.completion_time = base::TimeTicks::Now(); | |
| 241 request_complete_data.encoded_data_length = | |
| 242 request()->GetTotalReceivedBytes(); | |
| 243 | |
| 244 url_loader_client_->OnComplete(request_complete_data); | |
| 245 | |
| 246 if (status.is_success()) | |
| 247 RecordHistogram(); | |
| 248 } | |
| 249 | |
| 250 void MojoAsyncResourceHandler::ResumeIfDeferred() { | |
| 251 if (did_defer_) { | |
| 252 did_defer_ = false; | |
| 253 request()->LogUnblocked(); | |
| 254 controller()->Resume(); | |
| 255 } | |
| 256 } | |
| 257 | |
| 258 void MojoAsyncResourceHandler::OnDefer() { | |
| 259 request()->LogBlockedBy("MojoAsyncResourceHandler"); | |
| 260 } | |
| 261 | |
| 262 bool MojoAsyncResourceHandler::CheckForSufficientResource() { | |
| 263 if (has_checked_for_sufficient_resources_) | |
| 264 return true; | |
| 265 has_checked_for_sufficient_resources_ = true; | |
| 266 | |
| 267 if (rdh_->HasSufficientResourcesForRequest(request())) | |
| 268 return true; | |
| 269 | |
| 270 controller()->CancelWithError(net::ERR_INSUFFICIENT_RESOURCES); | |
| 271 return false; | |
| 272 } | |
| 273 | |
| 274 void MojoAsyncResourceHandler::RecordHistogram() { | |
| 275 int64_t elapsed_time = | |
| 276 (base::TimeTicks::Now() - response_started_ticks_).InMicroseconds(); | |
| 277 int64_t encoded_length = request()->GetTotalReceivedBytes(); | |
| 278 if (encoded_length < 2 * 1024) { | |
| 279 // The resource was smaller than the smallest required buffer size. | |
| 280 UMA_HISTOGRAM_CUSTOM_COUNTS("Net.ResourceLoader.ResponseStartToEnd.LT_2kB", | |
| 281 elapsed_time, 1, 100000, 100); | |
| 282 } else if (encoded_length < 32 * 1024) { | |
| 283 // The resource was smaller than single chunk. | |
| 284 UMA_HISTOGRAM_CUSTOM_COUNTS("Net.ResourceLoader.ResponseStartToEnd.LT_32kB", | |
| 285 elapsed_time, 1, 100000, 100); | |
| 286 } else if (encoded_length < 512 * 1024) { | |
| 287 // The resource was smaller than single chunk. | |
| 288 UMA_HISTOGRAM_CUSTOM_COUNTS( | |
| 289 "Net.ResourceLoader.ResponseStartToEnd.LT_512kB", elapsed_time, 1, | |
| 290 100000, 100); | |
| 291 } else { | |
| 292 UMA_HISTOGRAM_CUSTOM_COUNTS( | |
| 293 "Net.ResourceLoader.ResponseStartToEnd.Over_512kB", elapsed_time, 1, | |
| 294 100000, 100); | |
|
kinuko
2016/05/26 08:43:12
We should probably use different histogram names f
yhirano
2016/05/26 15:42:44
Done.
| |
| 295 } | |
| 296 } | |
| 297 | |
| 298 void MojoAsyncResourceHandler::OnWritable(MojoResult unused) { | |
| 299 ResumeIfDeferred(); | |
| 300 } | |
| 301 | |
| 302 } // namespace content | |
| OLD | NEW |