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