Chromium Code Reviews| 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 #include <vector> | |
|
mmenke
2016/07/18 18:50:42
Not used
clamy
2016/07/19 14:24:54
Done.
| |
| 9 | |
| 10 #include "base/logging.h" | |
| 11 #include "base/strings/string_util.h" | |
| 12 #include "content/public/common/resource_response.h" | |
| 13 #include "net/base/io_buffer.h" | |
| 14 | |
| 15 namespace content { | |
| 16 | |
| 17 InterceptingResourceHandler::InterceptingResourceHandler( | |
| 18 std::unique_ptr<ResourceHandler> next_handler, | |
| 19 net::URLRequest* request) | |
| 20 : LayeredResourceHandler(request, std::move(next_handler)), | |
| 21 state_(State::STARTING), | |
| 22 bytes_read_(0) {} | |
| 23 | |
| 24 InterceptingResourceHandler::~InterceptingResourceHandler() {} | |
| 25 | |
| 26 void InterceptingResourceHandler::UseNewHandler( | |
| 27 std::unique_ptr<ResourceHandler> new_handler, | |
| 28 const std::string& payload_for_old_handler) { | |
| 29 new_handler_ = std::move(new_handler); | |
| 30 new_handler_->SetController(controller()); | |
| 31 payload_for_old_handler_ = payload_for_old_handler; | |
| 32 } | |
| 33 | |
| 34 bool InterceptingResourceHandler::OnResponseStarted(ResourceResponse* response, | |
| 35 bool* defer) { | |
| 36 // If there's no need to switch handlers, just start acting as a blind | |
| 37 // pass-through ResourceHandler. | |
| 38 if (!new_handler_) { | |
| 39 state_ = State::DONE; | |
| 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 // Although deferring OnResponseStarted is legal, the only downstream handler | |
| 50 // which does so is CrossSiteResourceHandler. Cross-site transitions should | |
| 51 // not trigger when switching handlers. | |
| 52 DCHECK(!defer_ignored); | |
| 53 | |
| 54 // Send the payload to the old handler. | |
| 55 SendPayloadToOldHandler(); | |
| 56 | |
| 57 // This is now handled entirely within the new ResourceHandler, so just reset | |
| 58 // the original ResourceHandler. | |
| 59 next_handler_ = std::move(new_handler_); | |
| 60 | |
| 61 state_ = read_buffer_ ? State::WAITING_FOR_BUFFER_COPY : State::DONE; | |
| 62 | |
| 63 return next_handler_->OnResponseStarted(response, defer); | |
| 64 } | |
| 65 | |
| 66 bool InterceptingResourceHandler::OnWillRead(scoped_refptr<net::IOBuffer>* buf, | |
| 67 int* buf_size, | |
| 68 int min_size) { | |
| 69 if (state_ == State::DONE) | |
| 70 return next_handler_->OnWillRead(buf, buf_size, min_size); | |
| 71 | |
| 72 DCHECK_EQ(State::STARTING, state_); | |
| 73 DCHECK_EQ(-1, min_size); | |
| 74 | |
| 75 if (!next_handler_->OnWillRead(buf, buf_size, min_size)) | |
| 76 return false; | |
| 77 | |
| 78 read_buffer_ = *buf; | |
| 79 return true; | |
| 80 } | |
| 81 | |
| 82 bool InterceptingResourceHandler::OnReadCompleted(int bytes_read, bool* defer) { | |
| 83 if (state_ == State::DONE) | |
| 84 return next_handler_->OnReadCompleted(bytes_read, defer); | |
| 85 | |
| 86 DCHECK_EQ(State::WAITING_FOR_BUFFER_COPY, state_); | |
| 87 bytes_read_ += bytes_read; | |
|
mmenke
2016/07/18 18:50:42
Seems like bytes_read_ isn't needed, given that th
clamy
2016/07/19 14:24:54
Done.
| |
| 88 if (!CopyReadBufferToNextHandler()) | |
| 89 return false; | |
| 90 | |
| 91 return next_handler_->OnReadCompleted(bytes_read, defer); | |
| 92 } | |
| 93 | |
| 94 void InterceptingResourceHandler::SendPayloadToOldHandler() { | |
| 95 bool defer_ignored = false; | |
| 96 if (payload_for_old_handler_.empty()) { | |
| 97 // If there is no payload, just finalize the request on the old handler. | |
| 98 net::URLRequestStatus status(net::URLRequestStatus::CANCELED, | |
| 99 net::ERR_ABORTED); | |
| 100 next_handler_->OnResponseCompleted(status, std::string(), &defer_ignored); | |
| 101 DCHECK(!defer_ignored); | |
| 102 return; | |
| 103 } | |
| 104 | |
| 105 // Complete the read operation that may have created the IOBuffer. Note that | |
| 106 // this InterceptingResourceHandler keeps a pointer to the IOBuffer, so it | |
| 107 // won't actually be reset, since the data inside it needs to be sent to the | |
| 108 // new next handler. | |
| 109 if (read_buffer_) { | |
| 110 next_handler_->OnReadCompleted(0, &defer_ignored); | |
| 111 DCHECK(!defer_ignored); | |
|
mmenke
2016/07/18 18:50:42
I don't understand this - why are we completing th
clamy
2016/07/19 14:24:54
Because we may already have read something in read
| |
| 112 } | |
| 113 | |
| 114 // Get a new buffer iand use it to send |payload_for_old_handler_| to the | |
|
mmenke
2016/07/18 18:50:42
nit: iand -> and
clamy
2016/07/19 14:24:54
Done.
| |
| 115 // next ResourceHandler. | |
|
mmenke
2016/07/18 18:50:42
"next ResourceHandler" is extremely confusing - th
clamy
2016/07/19 14:24:54
Done.
| |
| 116 scoped_refptr<net::IOBuffer> buf; | |
| 117 int size = 0; | |
| 118 next_handler_->OnWillRead(&buf, &size, -1); | |
| 119 CHECK_GE(size, static_cast<int>(payload_for_old_handler_.length())); | |
| 120 memcpy(buf->data(), payload_for_old_handler_.c_str(), | |
| 121 payload_for_old_handler_.length()); | |
| 122 next_handler_->OnReadCompleted(payload_for_old_handler_.length(), | |
| 123 &defer_ignored); | |
| 124 DCHECK(!defer_ignored); | |
| 125 | |
| 126 // Finalize the request. | |
| 127 net::URLRequestStatus status(net::URLRequestStatus::SUCCESS, 0); | |
| 128 next_handler_->OnResponseCompleted(status, std::string(), &defer_ignored); | |
| 129 DCHECK(!defer_ignored); | |
| 130 } | |
| 131 | |
| 132 bool InterceptingResourceHandler::CopyReadBufferToNextHandler() { | |
|
mmenke
2016/07/18 18:50:42
Suggest just inlining this. The method it's calle
clamy
2016/07/19 14:24:54
Done.
| |
| 133 DCHECK(read_buffer_.get()); | |
| 134 scoped_refptr<net::IOBuffer> buf; | |
| 135 int buf_len = 0; | |
| 136 if (!next_handler_->OnWillRead(&buf, &buf_len, bytes_read_)) | |
| 137 return false; | |
| 138 | |
| 139 CHECK((buf_len >= bytes_read_) && (bytes_read_ >= 0)); | |
| 140 memcpy(buf->data(), read_buffer_->data(), bytes_read_); | |
| 141 state_ = State::DONE; | |
|
mmenke
2016/07/18 18:50:42
Suppose it doesn't really matter what we do if OnW
clamy
2016/07/19 14:24:54
Done.
| |
| 142 read_buffer_ = nullptr; | |
| 143 return true; | |
| 144 } | |
| 145 | |
| 146 } // namespace content | |
| OLD | NEW |