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/intercepting_resource_handler.h" |
| 6 |
| 7 #include <utility> |
| 8 |
| 9 #include "base/logging.h" |
| 10 #include "base/strings/string_util.h" |
| 11 #include "content/public/common/resource_response.h" |
| 12 #include "net/base/io_buffer.h" |
| 13 |
| 14 namespace content { |
| 15 |
| 16 InterceptingResourceHandler::InterceptingResourceHandler( |
| 17 std::unique_ptr<ResourceHandler> next_handler, |
| 18 net::URLRequest* request) |
| 19 : LayeredResourceHandler(request, std::move(next_handler)), |
| 20 state_(State::STARTING), |
| 21 first_read_buffer_size_(0) {} |
| 22 |
| 23 InterceptingResourceHandler::~InterceptingResourceHandler() {} |
| 24 |
| 25 void InterceptingResourceHandler::UseNewHandler( |
| 26 std::unique_ptr<ResourceHandler> new_handler, |
| 27 const std::string& payload_for_old_handler) { |
| 28 new_handler_ = std::move(new_handler); |
| 29 new_handler_->SetController(controller()); |
| 30 payload_for_old_handler_ = payload_for_old_handler; |
| 31 } |
| 32 |
| 33 bool InterceptingResourceHandler::OnResponseStarted(ResourceResponse* response, |
| 34 bool* defer) { |
| 35 // If there's no need to switch handlers, just start acting as a blind |
| 36 // pass-through ResourceHandler. |
| 37 if (!new_handler_) { |
| 38 state_ = State::DONE; |
| 39 first_read_buffer_ = nullptr; |
| 40 return next_handler_->OnResponseStarted(response, defer); |
| 41 } |
| 42 |
| 43 // Otherwise, switch handlers. First, inform the original ResourceHandler |
| 44 // that this will be handled entirely by the new ResourceHandler. |
| 45 // TODO(clamy): We will probably need to check the return values of these for |
| 46 // PlzNavigate. |
| 47 bool defer_ignored = false; |
| 48 next_handler_->OnResponseStarted(response, &defer_ignored); |
| 49 |
| 50 // Although deferring OnResponseStarted is legal, the only downstream handler |
| 51 // which does so is CrossSiteResourceHandler. Cross-site transitions should |
| 52 // not trigger when switching handlers. |
| 53 DCHECK(!defer_ignored); |
| 54 |
| 55 // Make a copy of the data in the first read buffer. This data should be |
| 56 // passed to the alternate ResourceHandler and not to to the current |
| 57 // ResourceHandler. |
| 58 if (first_read_buffer_) { |
| 59 first_read_buffer_copy_ = new net::IOBuffer(first_read_buffer_size_); |
| 60 memcpy(first_read_buffer_copy_->data(), first_read_buffer_->data(), |
| 61 first_read_buffer_size_); |
| 62 } |
| 63 |
| 64 // Send the payload to the old handler. |
| 65 SendPayloadToOldHandler(); |
| 66 first_read_buffer_ = nullptr; |
| 67 |
| 68 // This is now handled entirely within the new ResourceHandler, so just reset |
| 69 // the original ResourceHandler. |
| 70 next_handler_ = std::move(new_handler_); |
| 71 |
| 72 state_ = |
| 73 first_read_buffer_copy_ ? State::WAITING_FOR_BUFFER_COPY : State::DONE; |
| 74 |
| 75 return next_handler_->OnResponseStarted(response, defer); |
| 76 } |
| 77 |
| 78 bool InterceptingResourceHandler::OnWillRead(scoped_refptr<net::IOBuffer>* buf, |
| 79 int* buf_size, |
| 80 int min_size) { |
| 81 if (state_ == State::DONE) |
| 82 return next_handler_->OnWillRead(buf, buf_size, min_size); |
| 83 |
| 84 DCHECK_EQ(State::STARTING, state_); |
| 85 DCHECK_EQ(-1, min_size); |
| 86 |
| 87 if (!next_handler_->OnWillRead(buf, buf_size, min_size)) |
| 88 return false; |
| 89 |
| 90 first_read_buffer_ = *buf; |
| 91 first_read_buffer_size_ = *buf_size; |
| 92 return true; |
| 93 } |
| 94 |
| 95 bool InterceptingResourceHandler::OnReadCompleted(int bytes_read, bool* defer) { |
| 96 if (state_ == State::DONE) |
| 97 return next_handler_->OnReadCompleted(bytes_read, defer); |
| 98 |
| 99 DCHECK_EQ(State::WAITING_FOR_BUFFER_COPY, state_); |
| 100 state_ = State::DONE; |
| 101 |
| 102 // Copy the data from the first read to the new ResourceHandler. |
| 103 scoped_refptr<net::IOBuffer> buf; |
| 104 int buf_len = 0; |
| 105 if (!next_handler_->OnWillRead(&buf, &buf_len, bytes_read)) |
| 106 return false; |
| 107 |
| 108 CHECK((buf_len >= bytes_read) && (bytes_read >= 0)); |
| 109 CHECK_GE(first_read_buffer_size_, static_cast<size_t>(bytes_read)); |
| 110 memcpy(buf->data(), first_read_buffer_copy_->data(), bytes_read); |
| 111 |
| 112 first_read_buffer_copy_ = nullptr; |
| 113 |
| 114 return next_handler_->OnReadCompleted(bytes_read, defer); |
| 115 } |
| 116 |
| 117 void InterceptingResourceHandler::SendPayloadToOldHandler() { |
| 118 bool defer_ignored = false; |
| 119 if (payload_for_old_handler_.empty()) { |
| 120 // If there is no payload, just finalize the request on the old handler. |
| 121 net::URLRequestStatus status(net::URLRequestStatus::CANCELED, |
| 122 net::ERR_ABORTED); |
| 123 next_handler_->OnResponseCompleted(status, std::string(), &defer_ignored); |
| 124 DCHECK(!defer_ignored); |
| 125 return; |
| 126 } |
| 127 |
| 128 // Ensure the old ResourceHandler has a buffer that can store the payload. |
| 129 scoped_refptr<net::IOBuffer> buf; |
| 130 int size = 0; |
| 131 if (first_read_buffer_) { |
| 132 // The first read buffer can be reused. The data inside it has been copied |
| 133 // before calling this function, so it can safely be overriden. |
| 134 buf = first_read_buffer_; |
| 135 size = first_read_buffer_size_; |
| 136 } |
| 137 |
| 138 // If there is no first read buffer, ask the old ResourceHandler to create a |
| 139 // buffer that can contain payload. |
| 140 if (!buf) |
| 141 next_handler_->OnWillRead(&buf, &size, -1); |
| 142 |
| 143 CHECK(buf); |
| 144 CHECK_GE(size, static_cast<int>(payload_for_old_handler_.length())); |
| 145 memcpy(buf->data(), payload_for_old_handler_.c_str(), |
| 146 payload_for_old_handler_.length()); |
| 147 next_handler_->OnReadCompleted(payload_for_old_handler_.length(), |
| 148 &defer_ignored); |
| 149 DCHECK(!defer_ignored); |
| 150 |
| 151 // Finalize the request. |
| 152 net::URLRequestStatus status(net::URLRequestStatus::SUCCESS, 0); |
| 153 next_handler_->OnResponseCompleted(status, std::string(), &defer_ignored); |
| 154 DCHECK(!defer_ignored); |
| 155 } |
| 156 |
| 157 } // namespace content |
OLD | NEW |