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

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

Issue 2005273002: Move MimeTypeResourceHandler before ThrottlingResourceHandler (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressed comments Created 4 years, 5 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/mime_sniffing_resource_handler.cc
diff --git a/content/browser/loader/mime_type_resource_handler.cc b/content/browser/loader/mime_sniffing_resource_handler.cc
similarity index 64%
rename from content/browser/loader/mime_type_resource_handler.cc
rename to content/browser/loader/mime_sniffing_resource_handler.cc
index ab4fca3a8dbdc4780fb66e01878e11c1a5aa31eb..0a0eb92936ebbaa317536b59d975d83ddda3d6e6 100644
--- a/content/browser/loader/mime_type_resource_handler.cc
+++ b/content/browser/loader/mime_sniffing_resource_handler.cc
@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-#include "content/browser/loader/mime_type_resource_handler.h"
+#include "content/browser/loader/mime_sniffing_resource_handler.h"
#include <utility>
#include <vector>
@@ -17,6 +17,7 @@
#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/intercepting_resource_handler.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"
@@ -32,7 +33,6 @@
#include "net/base/io_buffer.h"
#include "net/base/mime_sniffer.h"
#include "net/base/mime_util.h"
-#include "net/base/net_errors.h"
#include "net/http/http_content_disposition.h"
#include "net/http/http_response_headers.h"
#include "net/url_request/url_request.h"
@@ -53,9 +53,7 @@ const char kDefaultAcceptHeader[] = "*/*";
class DependentIOBuffer : public net::WrappedIOBuffer {
public:
DependentIOBuffer(net::IOBuffer* buf, int offset)
- : net::WrappedIOBuffer(buf->data() + offset),
- buf_(buf) {
- }
+ : net::WrappedIOBuffer(buf->data() + offset), buf_(buf) {}
private:
~DependentIOBuffer() override {}
@@ -65,10 +63,11 @@ class DependentIOBuffer : public net::WrappedIOBuffer {
} // namespace
-MimeTypeResourceHandler::MimeTypeResourceHandler(
+MimeSniffingResourceHandler::MimeSniffingResourceHandler(
std::unique_ptr<ResourceHandler> next_handler,
ResourceDispatcherHostImpl* host,
PluginService* plugin_service,
+ InterceptingResourceHandler* intercepting_handler,
net::URLRequest* request)
: LayeredResourceHandler(request, std::move(next_handler)),
state_(STATE_STARTING),
@@ -76,59 +75,28 @@ MimeTypeResourceHandler::MimeTypeResourceHandler(
#if defined(ENABLE_PLUGINS)
plugin_service_(plugin_service),
#endif
- read_buffer_size_(0),
- bytes_read_(0),
must_download_(false),
must_download_is_set_(false),
+ read_buffer_size_(0),
+ bytes_read_(0),
+ intercepting_handler_(intercepting_handler),
weak_ptr_factory_(this) {
}
-MimeTypeResourceHandler::~MimeTypeResourceHandler() {
-}
+MimeSniffingResourceHandler::~MimeSniffingResourceHandler() {}
-void MimeTypeResourceHandler::SetController(ResourceController* controller) {
+void MimeSniffingResourceHandler::SetController(
+ ResourceController* controller) {
ResourceHandler::SetController(controller);
- // Downstream handlers see us as their ResourceController, which allows us to
- // consume part or all of the resource response, and then later replay it to
- // downstream handler.
+ // Downstream handlers see the MimeSniffingResourceHandler as their
+ // ResourceController, which allows it to consume part or all of the resource
+ // response, and then later replay it to downstream handler.
DCHECK(next_handler_.get());
next_handler_->SetController(this);
}
-bool MimeTypeResourceHandler::OnResponseStarted(ResourceResponse* response,
- bool* defer) {
- response_ = response;
-
- // A 304 response should not contain a Content-Type header (RFC 7232 section
- // 4.1). The following code may incorrectly attempt to add a Content-Type to
- // the response, and so must be skipped for 304 responses.
- if (!(response_->head.headers.get() &&
- response_->head.headers->response_code() == 304)) {
- if (ShouldSniffContent()) {
- state_ = STATE_BUFFERING;
- return true;
- }
-
- if (response_->head.mime_type.empty()) {
- // Ugg. The server told us not to sniff the content but didn't give us
- // a mime type. What's a browser to do? Turns out, we're supposed to
- // treat the response as "text/plain". This is the most secure option.
- response_->head.mime_type.assign("text/plain");
- }
-
- // Treat feed types as text/plain.
- if (response_->head.mime_type == "application/rss+xml" ||
- response_->head.mime_type == "application/atom+xml") {
- response_->head.mime_type.assign("text/plain");
- }
- }
-
- state_ = STATE_PROCESSING;
- return ProcessResponse(defer);
-}
-
-bool MimeTypeResourceHandler::OnWillStart(const GURL& url, bool* defer) {
+bool MimeSniffingResourceHandler::OnWillStart(const GURL& url, bool* defer) {
const char* accept_value = nullptr;
switch (GetRequestInfo()->GetResourceType()) {
case RESOURCE_TYPE_MAIN_FRAME:
@@ -168,9 +136,41 @@ bool MimeTypeResourceHandler::OnWillStart(const GURL& url, bool* defer) {
return next_handler_->OnWillStart(url, defer);
}
-bool MimeTypeResourceHandler::OnWillRead(scoped_refptr<net::IOBuffer>* buf,
- int* buf_size,
- int min_size) {
+bool MimeSniffingResourceHandler::OnResponseStarted(ResourceResponse* response,
+ bool* defer) {
+ DCHECK_EQ(STATE_STARTING, state_);
+ response_ = response;
+
+ state_ = STATE_BUFFERING;
+
+ // A 304 response should not contain a Content-Type header (RFC 7232 section
+ // 4.1). The following code may incorrectly attempt to add a Content-Type to
+ // the response, and so must be skipped for 304 responses.
+ if (!(response_->head.headers.get() &&
+ response_->head.headers->response_code() == 304)) {
+ if (ShouldSniffContent())
+ return true;
+
+ if (response_->head.mime_type.empty()) {
+ // Ugg. The server told us not to sniff the content but didn't give us a
+ // mime type. What's a browser to do? Turns out, we're supposed to
+ // treat the response as "text/plain". This is the most secure option.
+ response_->head.mime_type.assign("text/plain");
+ }
+
+ // Treat feed types as text/plain.
+ if (response_->head.mime_type == "application/rss+xml" ||
+ response_->head.mime_type == "application/atom+xml") {
+ response_->head.mime_type.assign("text/plain");
+ }
+ }
+
+ return ProcessReplay(defer);
+}
+
+bool MimeSniffingResourceHandler::OnWillRead(scoped_refptr<net::IOBuffer>* buf,
+ int* buf_size,
+ int min_size) {
if (state_ == STATE_STREAMING)
return next_handler_->OnWillRead(buf, buf_size, min_size);
@@ -191,21 +191,32 @@ bool MimeTypeResourceHandler::OnWillRead(scoped_refptr<net::IOBuffer>* buf,
return true;
}
-bool MimeTypeResourceHandler::OnReadCompleted(int bytes_read, bool* defer) {
+bool MimeSniffingResourceHandler::OnReadCompleted(int bytes_read, bool* defer) {
if (state_ == STATE_STREAMING)
return next_handler_->OnReadCompleted(bytes_read, defer);
DCHECK_EQ(state_, STATE_BUFFERING);
bytes_read_ += bytes_read;
- if (!DetermineMimeType() && (bytes_read > 0))
- return true; // Needs more data, so keep buffering.
+ const std::string& type_hint = response_->head.mime_type;
+
+ std::string new_type;
+ bool made_final_decision =
+ net::SniffMimeType(read_buffer_->data(), bytes_read_, request()->url(),
+ type_hint, &new_type);
+
+ // SniffMimeType() returns false if there is not enough data to determine
+ // the mime type. However, even if it returns false, it returns a new type
+ // that is probably better than the current one.
+ response_->head.mime_type.assign(new_type);
+
+ if (!made_final_decision && (bytes_read > 0))
+ return true;
- state_ = STATE_PROCESSING;
- return ProcessResponse(defer);
+ return ProcessReplay(defer);
}
-void MimeTypeResourceHandler::OnResponseCompleted(
+void MimeSniffingResourceHandler::OnResponseCompleted(
const net::URLRequestStatus& status,
const std::string& security_info,
bool* defer) {
@@ -216,66 +227,106 @@ void MimeTypeResourceHandler::OnResponseCompleted(
next_handler_->OnResponseCompleted(status, security_info, defer);
}
-void MimeTypeResourceHandler::Resume() {
- switch (state_) {
- case STATE_BUFFERING:
- case STATE_PROCESSING:
- NOTREACHED();
- break;
- case STATE_REPLAYING:
- base::ThreadTaskRunnerHandle::Get()->PostTask(
- FROM_HERE,
- base::Bind(&MimeTypeResourceHandler::CallReplayReadCompleted,
- weak_ptr_factory_.GetWeakPtr()));
- break;
- case STATE_STARTING:
- case STATE_STREAMING:
- controller()->Resume();
- break;
+void MimeSniffingResourceHandler::Resume() {
+ // If no information is currently being transmitted to downstream handlers,
+ // they should not attempt to resume the request.
+ if (state_ == STATE_BUFFERING) {
+ NOTREACHED();
+ return;
+ }
+
+ // If the BufferingHandler is acting as a pass-through handler, just ask the
+ // upwards ResourceController to resume the request.
+ if (state_ == STATE_STARTING || state_ == STATE_STREAMING) {
+ controller()->Resume();
+ return;
}
+
+ // Otherwise proceed with the replay of the response. If it is successful,
+ // it will resume the request.
+ ProceedWithReplay();
}
-void MimeTypeResourceHandler::Cancel() {
+void MimeSniffingResourceHandler::Cancel() {
controller()->Cancel();
}
-void MimeTypeResourceHandler::CancelAndIgnore() {
+void MimeSniffingResourceHandler::CancelAndIgnore() {
controller()->CancelAndIgnore();
}
-void MimeTypeResourceHandler::CancelWithError(int error_code) {
+void MimeSniffingResourceHandler::CancelWithError(int error_code) {
controller()->CancelWithError(error_code);
}
-bool MimeTypeResourceHandler::ProcessResponse(bool* defer) {
- DCHECK_EQ(STATE_PROCESSING, state_);
-
- // TODO(darin): Stop special-casing 304 responses.
- if (!(response_->head.headers.get() &&
- response_->head.headers->response_code() == 304)) {
- if (!SelectNextHandler(defer))
- return false;
- if (*defer)
- return true;
+void MimeSniffingResourceHandler::ProceedWithReplay() {
+ bool defer = false;
+ if (!ProcessReplay(&defer)) {
+ Cancel();
+ } else if (!defer) {
+ DCHECK_EQ(STATE_STREAMING, state_);
+ controller()->Resume();
}
+}
- state_ = STATE_REPLAYING;
+bool MimeSniffingResourceHandler::ProcessReplay(bool* defer) {
+ bool return_value = true;
+ while (!*defer && return_value && state_ != STATE_STREAMING) {
+ switch (state_) {
+ case STATE_BUFFERING:
+ return_value = MaybeCheckForInterception(defer);
+ break;
+ case STATE_INTERCEPTION_CHECK_DONE:
+ return_value = ReplayResponseReceived(defer);
+ break;
+ case STATE_REPLAYING_RESPONSE_RECEIVED:
+ return_value = ReplayReadCompleted(defer);
+ break;
+ default:
+ NOTREACHED();
+ break;
+ }
+ }
+ return return_value;
+}
- if (!next_handler_->OnResponseStarted(response_.get(), defer))
+bool MimeSniffingResourceHandler::MaybeCheckForInterception(bool* defer) {
+ DCHECK_EQ(STATE_BUFFERING, state_);
+ // If a request that can be intercepted failed the check for interception
+ // step, it should be canceled.
+ if (!CheckForInterception(defer))
return false;
- if (!read_buffer_.get()) {
- state_ = STATE_STREAMING;
- return true;
- }
-
if (!*defer)
- return ReplayReadCompleted(defer);
+ state_ = STATE_INTERCEPTION_CHECK_DONE;
return true;
}
-bool MimeTypeResourceHandler::ShouldSniffContent() {
+bool MimeSniffingResourceHandler::ReplayResponseReceived(bool* defer) {
+ DCHECK_EQ(STATE_INTERCEPTION_CHECK_DONE, state_);
+ state_ = STATE_REPLAYING_RESPONSE_RECEIVED;
+ return next_handler_->OnResponseStarted(response_.get(), defer);
+}
+
+bool MimeSniffingResourceHandler::ReplayReadCompleted(bool* defer) {
+ DCHECK_EQ(STATE_REPLAYING_RESPONSE_RECEIVED, state_);
+
+ state_ = STATE_STREAMING;
+
+ if (!read_buffer_.get())
+ return true;
+
+ bool result = next_handler_->OnReadCompleted(bytes_read_, defer);
+
+ read_buffer_ = NULL;
+ read_buffer_size_ = 0;
+ bytes_read_ = 0;
+
+ return result;
+}
+
+bool MimeSniffingResourceHandler::ShouldSniffContent() {
const std::string& mime_type = response_->head.mime_type;
std::string content_type_options;
@@ -298,68 +349,10 @@ bool MimeTypeResourceHandler::ShouldSniffContent() {
return false;
}
-bool MimeTypeResourceHandler::DetermineMimeType() {
- DCHECK_EQ(STATE_BUFFERING, state_);
-
- const std::string& type_hint = response_->head.mime_type;
-
- std::string new_type;
- bool made_final_decision =
- net::SniffMimeType(read_buffer_->data(), bytes_read_, request()->url(),
- type_hint, &new_type);
-
- // SniffMimeType() returns false if there is not enough data to determine
- // the mime type. However, even if it returns false, it returns a new type
- // that is probably better than the current one.
- response_->head.mime_type.assign(new_type);
-
- return made_final_decision;
-}
-
-bool MimeTypeResourceHandler::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(&MimeTypeResourceHandler::OnPluginsLoaded,
- weak_ptr_factory_.GetWeakPtr()));
- request()->LogBlockedBy("MimeTypeResourceHandler");
- *defer = true;
+bool MimeSniffingResourceHandler::CheckForInterception(bool* defer) {
+ if (!CanBeIntercepted())
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 MimeTypeResourceHandler::SelectNextHandler(bool* defer) {
DCHECK(!response_->head.mime_type.empty());
ResourceRequestInfoImpl* info = GetRequestInfo();
@@ -380,7 +373,7 @@ bool MimeTypeResourceHandler::SelectNextHandler(bool* defer) {
DCHECK(!info->allow_download());
bool handled_by_plugin;
- if (!SelectPluginHandler(defer, &handled_by_plugin))
+ if (!CheckForPluginHandler(defer, &handled_by_plugin))
return false;
if (handled_by_plugin || *defer)
return true;
@@ -401,101 +394,98 @@ bool MimeTypeResourceHandler::SelectNextHandler(bool* defer) {
return true;
bool handled_by_plugin;
- if (!SelectPluginHandler(defer, &handled_by_plugin))
+ if (!CheckForPluginHandler(defer, &handled_by_plugin))
return false;
if (handled_by_plugin || *defer)
return true;
}
- // Install download handler
+ // This is request is a download,
+
+ if (!CheckResponseIsNotProvisional())
+ return false;
+
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());
+ intercepting_handler_->UseNewHandler(std::move(handler), std::string());
+ return true;
}
-bool MimeTypeResourceHandler::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);
+bool MimeSniffingResourceHandler::CheckForPluginHandler(
+ 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);
- net::URLRequestStatus status(net::URLRequestStatus::SUCCESS, 0);
- next_handler_->OnResponseCompleted(status, std::string(), &defer_ignored);
- DCHECK(!defer_ignored);
+ if (stale) {
+ // Refresh the plugins asynchronously.
+ plugin_service_->GetPlugins(
+ base::Bind(&MimeSniffingResourceHandler::OnPluginsLoaded,
+ weak_ptr_factory_.GetWeakPtr()));
+ request()->LogBlockedBy("MimeSniffingResourceHandler");
+ *defer = true;
+ return true;
}
- // This is handled entirely within the new ResourceHandler, so just reset the
- // original ResourceHandler.
- next_handler_ = std::move(new_handler);
- next_handler_->SetController(this);
+ if (has_plugin && plugin.type != WebPluginInfo::PLUGIN_TYPE_BROWSER_PLUGIN) {
+ *handled_by_plugin = true;
+ return true;
+ }
- return CopyReadBufferToNextHandler();
+ // 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) {
+ if (!CheckResponseIsNotProvisional())
+ return false;
+ *handled_by_plugin = true;
+ intercepting_handler_->UseNewHandler(std::move(handler), payload);
+ }
+#endif
+ return true;
}
-bool MimeTypeResourceHandler::ReplayReadCompleted(bool* defer) {
- DCHECK(read_buffer_.get());
-
- bool result = next_handler_->OnReadCompleted(bytes_read_, defer);
-
- read_buffer_ = NULL;
- read_buffer_size_ = 0;
- bytes_read_ = 0;
-
- state_ = STATE_STREAMING;
+bool MimeSniffingResourceHandler::CanBeIntercepted() {
+ if (response_->head.headers.get() &&
+ response_->head.headers->response_code() == 304) {
+ return false;
+ }
- return result;
+ return true;
}
-void MimeTypeResourceHandler::CallReplayReadCompleted() {
- bool defer = false;
- if (!ReplayReadCompleted(&defer)) {
- controller()->Cancel();
- } else if (!defer) {
- state_ = STATE_STREAMING;
- controller()->Resume();
+bool MimeSniffingResourceHandler::CheckResponseIsNotProvisional() {
+ if (!response_->head.headers.get() ||
+ response_->head.headers->response_code() / 100 == 2) {
+ return true;
}
+
+ // 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 intercepting the request as a stream or 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;
}
-bool MimeTypeResourceHandler::MustDownload() {
+bool MimeSniffingResourceHandler::MustDownload() {
if (must_download_is_set_)
return must_download_;
@@ -517,29 +507,12 @@ bool MimeTypeResourceHandler::MustDownload() {
return must_download_;
}
-bool MimeTypeResourceHandler::CopyReadBufferToNextHandler() {
- if (!read_buffer_.get())
- return true;
-
- 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_);
- return true;
-}
-
-void MimeTypeResourceHandler::OnPluginsLoaded(
+void MimeSniffingResourceHandler::OnPluginsLoaded(
const std::vector<WebPluginInfo>& plugins) {
+ // No longer blocking on the plugins being loaded.
request()->LogUnblocked();
- bool defer = false;
- if (!ProcessResponse(&defer)) {
- controller()->Cancel();
- } else if (!defer) {
- controller()->Resume();
- }
+ if (state_ == STATE_BUFFERING)
+ ProceedWithReplay();
}
} // namespace content

Powered by Google App Engine
This is Rietveld 408576698