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

Unified Diff: content/browser/loader/mojo_async_resource_handler.cc

Issue 1970693002: Use mojo for Chrome Loading, Part 1 (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 7 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 side-by-side diff with in-line comments
Download patch
Index: content/browser/loader/mojo_async_resource_handler.cc
diff --git a/content/browser/loader/mojo_async_resource_handler.cc b/content/browser/loader/mojo_async_resource_handler.cc
new file mode 100644
index 0000000000000000000000000000000000000000..6db9b4f6740b27a6ca7a9239fc7602de947e3e51
--- /dev/null
+++ b/content/browser/loader/mojo_async_resource_handler.cc
@@ -0,0 +1,302 @@
+// Copyright 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "content/browser/loader/mojo_async_resource_handler.h"
+
+#include <utility>
+
+#include "base/command_line.h"
+#include "base/containers/hash_tables.h"
+#include "base/logging.h"
+#include "base/macros.h"
+#include "base/metrics/histogram_macros.h"
+#include "base/strings/string_number_conversions.h"
+#include "base/time/time.h"
+#include "content/browser/loader/netlog_observer.h"
+#include "content/browser/loader/resource_dispatcher_host_impl.h"
+#include "content/browser/loader/resource_message_filter.h"
+#include "content/browser/loader/resource_request_info_impl.h"
+#include "content/common/resource_request_completion_status.h"
+#include "content/public/browser/resource_dispatcher_host_delegate.h"
+#include "content/public/common/resource_response.h"
+#include "net/base/io_buffer.h"
+#include "net/base/load_flags.h"
+#include "net/log/net_log.h"
+#include "net/url_request/redirect_info.h"
+
+namespace content {
+namespace {
+
+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.
+
+void GetNumericArg(const std::string& name, int* result) {
+ const std::string& value =
+ base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(name);
+ if (!value.empty())
+ base::StringToInt(value, result);
+}
+
+void InitializeResourceBufferConstants() {
+ static bool did_init = false;
+ if (did_init)
+ return;
+ did_init = true;
+
+ GetNumericArg("resource-buffer-max-allocation-size", &maxAllocationSize);
mmenke 2016/05/25 16:17:20 Wow..I was going to complain about this being weir
+}
+
+} // namespace
+
+MojoAsyncResourceHandler::MojoAsyncResourceHandler(
+ net::URLRequest* request,
+ ResourceDispatcherHostImpl* rdh,
+ std::unique_ptr<mojom::URLLoader> url_loader,
+ mojom::URLLoaderClientPtr url_loader_client)
+ : ResourceHandler(request),
+ rdh_(rdh),
+ url_loader_(std::move(url_loader)),
+ url_loader_client_(std::move(url_loader_client)) {
+ DCHECK(url_loader_);
+ DCHECK(url_loader_client_);
+ InitializeResourceBufferConstants();
+}
+
+MojoAsyncResourceHandler::~MojoAsyncResourceHandler() {
+ if (has_checked_for_sufficient_resources_)
+ rdh_->FinishedWithResourcesForRequest(request());
+}
+
+void MojoAsyncResourceHandler::OnFollowRedirect(int request_id) {
+ NOTREACHED();
+}
+
+bool MojoAsyncResourceHandler::OnRequestRedirected(
+ const net::RedirectInfo& redirect_info,
+ ResourceResponse* response,
+ bool* defer) {
+ // Not implemented.
+ return false;
+}
+
+bool MojoAsyncResourceHandler::OnResponseStarted(ResourceResponse* response,
+ bool* defer) {
+ // For changes to the main frame, inform the renderer of the new URL's
+ // per-host settings before the request actually commits. This way the
+ // renderer will be able to set these precisely at the time the
+ // request commits, avoiding the possibility of e.g. zooming the old content
+ // or of having to layout the new content twice.
+
+ response_started_ticks_ = base::TimeTicks::Now();
+
+ const ResourceRequestInfoImpl* info = GetRequestInfo();
+ if (!info->filter())
+ return false;
+
+ if (rdh_->delegate()) {
+ rdh_->delegate()->OnResponseStarted(request(), info->GetContext(), response,
+ info->filter());
+ }
+
+ NetLogObserver::PopulateResponseInfo(request(), response);
+
+ // If the parent handler downloaded the resource to a file, grant the child
+ // read permissions on it.
+ if (!response->head.download_file_path.empty()) {
+ rdh_->RegisterDownloadedTempFile(info->GetChildID(), info->GetRequestID(),
+ response->head.download_file_path);
+ }
+
+ response->head.request_start = request()->creation_time();
+ response->head.response_start = base::TimeTicks::Now();
+ sent_received_response_message_ = true;
+ url_loader_client_->OnReceiveResponse(response->head);
+ return true;
+}
+
+bool MojoAsyncResourceHandler::OnWillStart(const GURL& url, bool* defer) {
+ return true;
+}
+
+bool MojoAsyncResourceHandler::OnBeforeNetworkStart(const GURL& url,
+ bool* defer) {
+ return true;
+}
+
+bool MojoAsyncResourceHandler::OnWillRead(scoped_refptr<net::IOBuffer>* buf,
+ int* buf_size,
+ int min_size) {
+ DCHECK_EQ(-1, min_size);
+
+ if (!CheckForSufficientResource())
+ return false;
+
+ void* buffer = nullptr;
+ uint32_t available = 0;
+ if (!writer_.is_valid()) {
+ MojoCreateDataPipeOptions options;
+ options.struct_size = sizeof(MojoCreateDataPipeOptions);
+ options.flags = MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE;
+ options.element_num_bytes = 1;
+ options.capacity_num_bytes = maxAllocationSize;
+ mojo::DataPipe data_pipe(options);
+
+ writer_ = std::move(data_pipe.producer_handle);
+ ResourceMessageFilter* filter = GetRequestInfo()->filter();
+ if (filter) {
+ url_loader_client_->OnStartLoadingResponseBody(
+ std::move(data_pipe.consumer_handle));
+ }
+ }
+ if (!writer_.is_valid()) {
+ controller()->CancelWithError(net::ERR_FAILED);
+ return false;
+ }
+
+ MojoResult result = mojo::BeginWriteDataRaw(
+ writer_.get(), &buffer, &available, MOJO_WRITE_DATA_FLAG_NONE);
+ // SHOULD_WAIT should be handled in OnReadCompleted.
+ if (result == MOJO_RESULT_OK) {
+ *buf = new net::WrappedIOBuffer(static_cast<const char*>(buffer));
+ *buf_size = available;
+ return true;
+ }
+ controller()->CancelWithError(net::ERR_FAILED);
+ return false;
+}
+
+bool MojoAsyncResourceHandler::OnReadCompleted(int bytes_read, bool* defer) {
+ DCHECK_GE(bytes_read, 0);
+
+ if (!bytes_read)
+ return true;
+
+ MojoResult result = mojo::EndWriteDataRaw(writer_.get(), bytes_read);
+ if (result != MOJO_RESULT_OK)
+ return false;
+ void* buffer = nullptr;
+ uint32_t available = 0;
+ // To see if the handle is still writable.
+ result = mojo::BeginWriteDataRaw(writer_.get(), &buffer, &available,
+ MOJO_WRITE_DATA_FLAG_NONE);
+ if (result == MOJO_RESULT_SHOULD_WAIT ||
+ (result == MOJO_RESULT_OK && available == 0)) {
+ *defer = did_defer_ = true;
+ OnDefer();
+ handle_watcher_.Start(writer_.get(), MOJO_HANDLE_SIGNAL_WRITABLE,
+ MOJO_DEADLINE_INDEFINITE,
+ base::Bind(&MojoAsyncResourceHandler::OnWritable,
+ base::Unretained(this)));
+ }
+ if (result == MOJO_RESULT_OK)
+ mojo::EndWriteDataRaw(writer_.get(), 0);
+ return true;
+}
+
+void MojoAsyncResourceHandler::OnDataDownloaded(int bytes_downloaded) {
+ // 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.
+}
+
+void MojoAsyncResourceHandler::OnResponseCompleted(
+ const net::URLRequestStatus& status,
+ const std::string& security_info,
+ bool* defer) {
+ const ResourceRequestInfoImpl* info = GetRequestInfo();
+ if (!info->filter())
+ return;
+
+ // TODO(gavinp): Remove this CHECK when we figure out the cause of
+ // http://crbug.com/124680 . This check mirrors closely check in
+ // WebURLLoaderImpl::OnCompletedRequest that routes this message to a WebCore
+ // ResourceHandleInternal which asserts on its state and crashes. By crashing
+ // when the message is sent, we should get better crash reports.
+ CHECK(status.status() != net::URLRequestStatus::SUCCESS ||
+ 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.
+
+ int error_code = status.error();
+ bool was_ignored_by_handler = info->WasIgnoredByHandler();
+
+ DCHECK(status.status() != net::URLRequestStatus::IO_PENDING);
+ // If this check fails, then we're in an inconsistent state because all
+ // requests ignored by the handler should be canceled (which should result in
+ // the ERR_ABORTED error code).
+ DCHECK(!was_ignored_by_handler || error_code == net::ERR_ABORTED);
+
+ // TODO(mkosiba): Fix up cases where we create a URLRequestStatus
+ // with a status() != SUCCESS and an error_code() == net::OK.
+ if (status.status() == net::URLRequestStatus::CANCELED &&
+ error_code == net::OK) {
+ error_code = net::ERR_ABORTED;
+ } else if (status.status() == net::URLRequestStatus::FAILED &&
+ error_code == net::OK) {
+ error_code = net::ERR_FAILED;
+ }
+
+ ResourceRequestCompletionStatus request_complete_data;
+ request_complete_data.error_code = error_code;
+ request_complete_data.was_ignored_by_handler = was_ignored_by_handler;
+ request_complete_data.exists_in_cache = request()->response_info().was_cached;
+ request_complete_data.security_info = security_info;
+ request_complete_data.completion_time = base::TimeTicks::Now();
+ request_complete_data.encoded_data_length =
+ request()->GetTotalReceivedBytes();
+
+ url_loader_client_->OnComplete(request_complete_data);
+
+ if (status.is_success())
+ RecordHistogram();
+}
+
+void MojoAsyncResourceHandler::ResumeIfDeferred() {
+ if (did_defer_) {
+ did_defer_ = false;
+ request()->LogUnblocked();
+ controller()->Resume();
+ }
+}
+
+void MojoAsyncResourceHandler::OnDefer() {
+ request()->LogBlockedBy("MojoAsyncResourceHandler");
+}
+
+bool MojoAsyncResourceHandler::CheckForSufficientResource() {
+ if (has_checked_for_sufficient_resources_)
+ return true;
+ has_checked_for_sufficient_resources_ = true;
+
+ if (rdh_->HasSufficientResourcesForRequest(request()))
+ return true;
+
+ controller()->CancelWithError(net::ERR_INSUFFICIENT_RESOURCES);
+ return false;
+}
+
+void MojoAsyncResourceHandler::RecordHistogram() {
+ int64_t elapsed_time =
+ (base::TimeTicks::Now() - response_started_ticks_).InMicroseconds();
+ int64_t encoded_length = request()->GetTotalReceivedBytes();
+ if (encoded_length < 2 * 1024) {
+ // The resource was smaller than the smallest required buffer size.
+ UMA_HISTOGRAM_CUSTOM_COUNTS("Net.ResourceLoader.ResponseStartToEnd.LT_2kB",
+ elapsed_time, 1, 100000, 100);
+ } else if (encoded_length < 32 * 1024) {
+ // The resource was smaller than single chunk.
+ UMA_HISTOGRAM_CUSTOM_COUNTS("Net.ResourceLoader.ResponseStartToEnd.LT_32kB",
+ elapsed_time, 1, 100000, 100);
+ } else if (encoded_length < 512 * 1024) {
+ // The resource was smaller than single chunk.
+ UMA_HISTOGRAM_CUSTOM_COUNTS(
+ "Net.ResourceLoader.ResponseStartToEnd.LT_512kB", elapsed_time, 1,
+ 100000, 100);
+ } else {
+ UMA_HISTOGRAM_CUSTOM_COUNTS(
+ "Net.ResourceLoader.ResponseStartToEnd.Over_512kB", elapsed_time, 1,
+ 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.
+ }
+}
+
+void MojoAsyncResourceHandler::OnWritable(MojoResult unused) {
+ ResumeIfDeferred();
+}
+
+} // namespace content

Powered by Google App Engine
This is Rietveld 408576698