Index: content/browser/loader/intercepting_resource_handler.cc |
diff --git a/content/browser/loader/intercepting_resource_handler.cc b/content/browser/loader/intercepting_resource_handler.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..f98dac8ee2fa97b4691aff6dbc0c35cc0a2abc45 |
--- /dev/null |
+++ b/content/browser/loader/intercepting_resource_handler.cc |
@@ -0,0 +1,142 @@ |
+// 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/intercepting_resource_handler.h" |
+ |
+#include <utility> |
+ |
+#include "base/logging.h" |
+#include "base/strings/string_util.h" |
+#include "content/public/common/resource_response.h" |
+#include "net/base/io_buffer.h" |
+ |
+namespace content { |
+ |
+InterceptingResourceHandler::InterceptingResourceHandler( |
+ std::unique_ptr<ResourceHandler> next_handler, |
+ net::URLRequest* request) |
+ : LayeredResourceHandler(request, std::move(next_handler)), |
+ state_(State::STARTING) {} |
+ |
+InterceptingResourceHandler::~InterceptingResourceHandler() {} |
+ |
+void InterceptingResourceHandler::UseNewHandler( |
+ std::unique_ptr<ResourceHandler> new_handler, |
+ const std::string& payload_for_old_handler) { |
+ new_handler_ = std::move(new_handler); |
+ new_handler_->SetController(controller()); |
+ payload_for_old_handler_ = payload_for_old_handler; |
+} |
+ |
+bool InterceptingResourceHandler::OnResponseStarted(ResourceResponse* response, |
+ bool* defer) { |
+ // If there's no need to switch handlers, just start acting as a blind |
+ // pass-through ResourceHandler. |
+ if (!new_handler_) { |
+ state_ = State::DONE; |
+ return next_handler_->OnResponseStarted(response, defer); |
+ } |
+ |
+ // Otherwise, switch handlers. First, inform the original ResourceHandler |
+ // that this will be handled entirely by the new ResourceHandler. |
+ // TODO(clamy): We will probably need to check the return values of these for |
+ // PlzNavigate. |
+ bool defer_ignored = false; |
+ next_handler_->OnResponseStarted(response, &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); |
+ |
+ // Send the payload to the old handler. |
+ SendPayloadToOldHandler(); |
+ |
+ // This is now handled entirely within the new ResourceHandler, so just reset |
+ // the original ResourceHandler. |
+ next_handler_ = std::move(new_handler_); |
+ |
+ state_ = read_buffer_ ? State::WAITING_FOR_BUFFER_COPY : State::DONE; |
+ |
+ return next_handler_->OnResponseStarted(response, defer); |
+} |
+ |
+bool InterceptingResourceHandler::OnWillRead(scoped_refptr<net::IOBuffer>* buf, |
+ int* buf_size, |
+ int min_size) { |
+ if (state_ == State::DONE) |
+ 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 InterceptingResourceHandler::OnReadCompleted(int bytes_read, bool* defer) { |
+ if (state_ == State::DONE) |
+ return next_handler_->OnReadCompleted(bytes_read, defer); |
+ |
+ DCHECK_EQ(State::WAITING_FOR_BUFFER_COPY, state_); |
+ state_ = State::DONE; |
+ |
+ // Copy the data in the read buffer to the new ResourceHandler. |
+ 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); |
+ read_buffer_ = nullptr; |
+ |
+ return next_handler_->OnReadCompleted(bytes_read, defer); |
+} |
+ |
+void InterceptingResourceHandler::SendPayloadToOldHandler() { |
+ bool defer_ignored = false; |
+ if (payload_for_old_handler_.empty()) { |
+ // If there is no payload, just finalize the request on the old handler. |
+ net::URLRequestStatus status(net::URLRequestStatus::CANCELED, |
+ net::ERR_ABORTED); |
+ next_handler_->OnResponseCompleted(status, std::string(), &defer_ignored); |
+ DCHECK(!defer_ignored); |
+ return; |
+ } |
+ |
+ // Pass the payload to the old handler. The IOBuffer which is created in |
+ // OnWillRead can contain data that needs to be passed to the new handler, so |
+ // it cannot be used to store the payload for the old handler. Instead, |
+ // complete the read operation that may have created the IOBuffer. The old |
+ // handler will release it, but this InterceptingResourceHandler keeps a |
+ // pointer to the IOBuffer, so it won't actually be reset. This will allow |
+ // the old handler to create a new buffer to store the payload. |
mmenke
2016/07/19 17:43:45
This seems like a problem. There doesn't seem to
clamy
2016/07/25 16:58:25
I'm now making a copy of the buffer before sending
|
+ if (read_buffer_) { |
+ next_handler_->OnReadCompleted(0, &defer_ignored); |
+ DCHECK(!defer_ignored); |
+ } |
+ |
+ // Get a new buffer and use it to send |payload_for_old_handler_| to the |
+ // original ResourceHandler. |
+ 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); |
+ |
+ // Finalize the request. |
+ net::URLRequestStatus status(net::URLRequestStatus::SUCCESS, 0); |
+ next_handler_->OnResponseCompleted(status, std::string(), &defer_ignored); |
+ DCHECK(!defer_ignored); |
+} |
+ |
+} // namespace content |