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

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

Issue 2005273002: Move MimeTypeResourceHandler before ThrottlingResourceHandler (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fixed gypi error Created 4 years, 6 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/replacing_resource_handler.cc
diff --git a/content/browser/loader/replacing_resource_handler.cc b/content/browser/loader/replacing_resource_handler.cc
new file mode 100644
index 0000000000000000000000000000000000000000..d1ecbf132b0a287e73d3beb682f45654c1014981
--- /dev/null
+++ b/content/browser/loader/replacing_resource_handler.cc
@@ -0,0 +1,317 @@
+// Copyright 2016 The Chromium Authors. All rights reserved.
mmenke 2016/06/09 18:09:29 Weird that git isn't picking up that this file was
clamy 2016/06/21 16:14:15 Done.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "content/browser/loader/replacing_resource_handler.h"
+
+#include <utility>
+#include <vector>
+
+#include "base/bind.h"
+#include "base/location.h"
+#include "base/logging.h"
+#include "base/metrics/histogram_macros.h"
+#include "base/single_thread_task_runner.h"
+#include "base/strings/string_util.h"
+#include "base/threading/thread_task_runner_handle.h"
+#include "components/mime_util/mime_util.h"
+#include "content/browser/download/download_resource_handler.h"
+#include "content/browser/download/download_stats.h"
+#include "content/browser/loader/resource_dispatcher_host_impl.h"
+#include "content/browser/loader/resource_request_info_impl.h"
+#include "content/browser/loader/stream_resource_handler.h"
+#include "content/public/browser/content_browser_client.h"
+#include "content/public/browser/download_item.h"
+#include "content/public/browser/download_save_info.h"
+#include "content/public/browser/download_url_parameters.h"
+#include "content/public/browser/plugin_service.h"
+#include "content/public/browser/resource_context.h"
+#include "content/public/browser/resource_dispatcher_host_delegate.h"
+#include "content/public/common/resource_response.h"
+#include "content/public/common/webplugininfo.h"
+#include "net/base/io_buffer.h"
+#include "net/base/mime_sniffer.h"
mattm 2016/06/09 22:55:32 not used here?
clamy 2016/06/21 16:14:15 Done.
+#include "net/base/mime_util.h"
+#include "net/http/http_content_disposition.h"
+#include "net/http/http_response_headers.h"
+
+namespace content {
+
+ReplacingResourceHandler::ReplacingResourceHandler(
+ std::unique_ptr<ResourceHandler> next_handler,
+ ResourceDispatcherHostImpl* host,
+ PluginService* plugin_service,
+ net::URLRequest* request)
+ : LayeredResourceHandler(request, std::move(next_handler)),
+ state_(State::STARTING),
+ host_(host),
+#if defined(ENABLE_PLUGINS)
+ plugin_service_(plugin_service),
+#endif
+ switched_handler_(false),
+ must_download_(false),
+ must_download_is_set_(false),
+ bytes_read_(0),
+ weak_ptr_factory_(this) {
+}
+
+ReplacingResourceHandler::~ReplacingResourceHandler() {}
+
+bool ReplacingResourceHandler::OnResponseStarted(ResourceResponse* response,
+ bool* defer) {
+ if (response->head.headers.get() &&
+ response->head.headers->response_code() == 304) {
+ return true;
mmenke 2016/06/09 18:09:29 BUG: This should be passed through to the next ha
clamy 2016/06/21 16:14:15 Done.
+ }
+
+ response_ = response;
+
+ state_ = State::WAITING_FOR_NEW_HANDLER;
+
+ return MakeHandlerChoice(defer);
+}
+
+bool ReplacingResourceHandler::OnWillRead(scoped_refptr<net::IOBuffer>* buf,
+ int* buf_size,
+ int min_size) {
+ if (state_ == State::CHOICE_MADE)
+ return next_handler_->OnWillRead(buf, buf_size, min_size);
+
+ DCHECK_EQ(State::STARTING, state_);
+ DCHECK_EQ(-1, min_size);
+
+ if (!next_handler_->OnWillRead(buf, buf_size, min_size))
+ return false;
+
+ read_buffer_ = *buf;
+ return true;
+}
+
+bool ReplacingResourceHandler::OnReadCompleted(int bytes_read, bool* defer) {
+ if (state_ == State::CHOICE_MADE)
+ return next_handler_->OnReadCompleted(bytes_read, defer);
+
+ DCHECK(state_ == State::WAITING_FOR_BUFFER_COPY);
mmenke 2016/06/09 18:09:29 DCHECK_EQ
clamy 2016/06/21 16:14:15 Done.
+ bytes_read_ += bytes_read;
+ if (!CopyReadBufferToNextHandler())
+ return false;
+
+ return next_handler_->OnReadCompleted(bytes_read, defer);
+}
+
+bool ReplacingResourceHandler::MakeHandlerChoice(bool* defer) {
+ if (!SelectNextHandler(defer))
+ return false;
+
+ if (*defer)
+ return true;
+
+ if (!next_handler_->OnResponseStarted(response_.get(), defer))
+ return false;
+
+ state_ = read_buffer_ && switched_handler_ ? State::WAITING_FOR_BUFFER_COPY
+ : State::CHOICE_MADE;
+ return true;
+}
+
+bool ReplacingResourceHandler::SelectPluginHandler(bool* defer,
+ bool* handled_by_plugin) {
+ *handled_by_plugin = false;
+#if defined(ENABLE_PLUGINS)
+ ResourceRequestInfoImpl* info = GetRequestInfo();
+ bool allow_wildcard = false;
+ bool stale;
+ WebPluginInfo plugin;
+ bool has_plugin = plugin_service_->GetPluginInfo(
+ info->GetChildID(), info->GetRenderFrameID(), info->GetContext(),
+ request()->url(), GURL(), response_->head.mime_type, allow_wildcard,
+ &stale, &plugin, NULL);
+
+ if (stale) {
+ // Refresh the plugins asynchronously.
+ plugin_service_->GetPlugins(
+ base::Bind(&ReplacingResourceHandler::OnPluginsLoaded,
+ weak_ptr_factory_.GetWeakPtr()));
+ request()->LogBlockedBy("ReplacingResourceHandler");
+ *defer = true;
+ return true;
+ }
+
+ if (has_plugin && plugin.type != WebPluginInfo::PLUGIN_TYPE_BROWSER_PLUGIN) {
+ *handled_by_plugin = true;
+ return true;
+ }
+
+ // Attempt to intercept the request as a stream.
+ base::FilePath plugin_path;
+ if (has_plugin)
+ plugin_path = plugin.path;
+ std::string payload;
+ std::unique_ptr<ResourceHandler> handler(host_->MaybeInterceptAsStream(
+ plugin_path, request(), response_.get(), &payload));
+ if (handler) {
+ *handled_by_plugin = true;
+ return UseAlternateNextHandler(std::move(handler), payload);
+ }
+#endif
+ return true;
+}
+
+bool ReplacingResourceHandler::SelectNextHandler(bool* defer) {
+ DCHECK(!response_->head.mime_type.empty());
+
+ ResourceRequestInfoImpl* info = GetRequestInfo();
+ const std::string& mime_type = response_->head.mime_type;
+
+ // https://crbug.com/568184 - Temporary hack to track servers that aren't
+ // setting Content-Disposition when sending x-x509-user-cert and expecting
+ // the browser to automatically install certificates; this is being
+ // deprecated and will be removed upon full <keygen> removal.
+ if (mime_type == "application/x-x509-user-cert") {
+ UMA_HISTOGRAM_BOOLEAN(
+ "UserCert.ContentDisposition",
+ response_->head.headers->HasHeader("Content-Disposition"));
+ }
+
+ // Allow requests for object/embed tags to be intercepted as streams.
+ if (info->GetResourceType() == content::RESOURCE_TYPE_OBJECT) {
+ DCHECK(!info->allow_download());
+
+ bool handled_by_plugin;
+ if (!SelectPluginHandler(defer, &handled_by_plugin))
+ return false;
+ if (handled_by_plugin || *defer)
+ return true;
+ }
+
+ if (!info->allow_download())
+ return true;
+
+ // info->allow_download() == true implies
+ // info->GetResourceType() == RESOURCE_TYPE_MAIN_FRAME or
+ // info->GetResourceType() == RESOURCE_TYPE_SUB_FRAME.
+ DCHECK(info->GetResourceType() == RESOURCE_TYPE_MAIN_FRAME ||
+ info->GetResourceType() == RESOURCE_TYPE_SUB_FRAME);
+
+ bool must_download = MustDownload();
+ if (!must_download) {
+ if (mime_util::IsSupportedMimeType(mime_type))
+ return true;
+
+ bool handled_by_plugin;
+ if (!SelectPluginHandler(defer, &handled_by_plugin))
+ return false;
+ if (handled_by_plugin || *defer)
+ return true;
+ }
+
+ // Install download handler
+ info->set_is_download(true);
+ std::unique_ptr<ResourceHandler> handler(
+ host_->CreateResourceHandlerForDownload(request(),
+ true, // is_content_initiated
+ must_download));
+ return UseAlternateNextHandler(std::move(handler), std::string());
+}
+
+bool ReplacingResourceHandler::UseAlternateNextHandler(
+ std::unique_ptr<ResourceHandler> new_handler,
+ const std::string& payload_for_old_handler) {
+ if (response_->head.headers.get() && // Can be NULL if FTP.
+ response_->head.headers->response_code() / 100 != 2) {
+ // The response code indicates that this is an error page, but we don't
+ // know how to display the content. We follow Firefox here and show our
+ // own error page instead of triggering a download.
+ // TODO(abarth): We should abstract the response_code test, but this kind
+ // of check is scattered throughout our codebase.
+ request()->CancelWithError(net::ERR_INVALID_RESPONSE);
+ return false;
+ }
+
+ // Inform the original ResourceHandler that this will be handled entirely by
+ // the new ResourceHandler.
+ // TODO(darin): We should probably check the return values of these.
+ bool defer_ignored = false;
+ next_handler_->OnResponseStarted(response_.get(), &defer_ignored);
+ // Although deferring OnResponseStarted is legal, the only downstream handler
+ // which does so is CrossSiteResourceHandler. Cross-site transitions should
+ // not trigger when switching handlers.
+ DCHECK(!defer_ignored);
+ if (payload_for_old_handler.empty()) {
+ net::URLRequestStatus status(net::URLRequestStatus::CANCELED,
+ net::ERR_ABORTED);
+ next_handler_->OnResponseCompleted(status, std::string(), &defer_ignored);
+ DCHECK(!defer_ignored);
+ } else {
+ scoped_refptr<net::IOBuffer> buf;
+ int size = 0;
+ next_handler_->OnWillRead(&buf, &size, -1);
+ CHECK_GE(size, static_cast<int>(payload_for_old_handler.length()));
+ memcpy(buf->data(), payload_for_old_handler.c_str(),
+ payload_for_old_handler.length());
+ next_handler_->OnReadCompleted(payload_for_old_handler.length(),
+ &defer_ignored);
+ DCHECK(!defer_ignored);
+
+ net::URLRequestStatus status(net::URLRequestStatus::SUCCESS, 0);
+ next_handler_->OnResponseCompleted(status, std::string(), &defer_ignored);
+ DCHECK(!defer_ignored);
+ }
+
+ // This is handled entirely within the new ResourceHandler, so just reset the
+ // original ResourceHandler.
+ next_handler_ = std::move(new_handler);
+ next_handler_->SetController(controller());
+
+ switched_handler_ = true;
+
+ return true;
+}
+
+bool ReplacingResourceHandler::MustDownload() {
+ if (must_download_is_set_)
+ return must_download_;
+
+ must_download_is_set_ = true;
+
+ std::string disposition;
+ request()->GetResponseHeaderByName("content-disposition", &disposition);
+ if (!disposition.empty() &&
+ net::HttpContentDisposition(disposition, std::string()).is_attachment()) {
+ must_download_ = true;
+ } else if (host_->delegate() &&
+ host_->delegate()->ShouldForceDownloadResource(
+ request()->url(), response_->head.mime_type)) {
+ must_download_ = true;
+ } else {
+ must_download_ = false;
+ }
+
+ return must_download_;
+}
+
+void ReplacingResourceHandler::OnPluginsLoaded(
+ const std::vector<WebPluginInfo>& plugins) {
+ request()->LogUnblocked();
+ bool defer = false;
+ if (!MakeHandlerChoice(&defer)) {
+ controller()->Cancel();
+ } else if (!defer) {
+ controller()->Resume();
+ }
+}
+
+bool ReplacingResourceHandler::CopyReadBufferToNextHandler() {
+ DCHECK(read_buffer_.get());
+ scoped_refptr<net::IOBuffer> buf;
+ int buf_len = 0;
+ if (!next_handler_->OnWillRead(&buf, &buf_len, bytes_read_))
+ return false;
+
+ CHECK((buf_len >= bytes_read_) && (bytes_read_ >= 0));
+ memcpy(buf->data(), read_buffer_->data(), bytes_read_);
+ state_ = State::CHOICE_MADE;
+ return true;
+}
+
+} // namespace content

Powered by Google App Engine
This is Rietveld 408576698