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

Side by Side Diff: content/browser/loader/intercepting_resource_handler.cc

Issue 2327463002: Relax ad-hoc assumptions on InterceptingResourceHandler (Closed)
Patch Set: fix Created 4 years, 2 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 unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "content/browser/loader/intercepting_resource_handler.h" 5 #include "content/browser/loader/intercepting_resource_handler.h"
6 6
7 #include "base/logging.h" 7 #include "base/logging.h"
8 #include "base/strings/string_util.h" 8 #include "base/strings/string_util.h"
9 #include "content/public/common/resource_response.h" 9 #include "content/public/common/resource_response.h"
10 #include "net/base/io_buffer.h" 10 #include "net/base/io_buffer.h"
11 11
12 namespace content { 12 namespace content {
13 13
14 InterceptingResourceHandler::InterceptingResourceHandler( 14 InterceptingResourceHandler::InterceptingResourceHandler(
15 std::unique_ptr<ResourceHandler> next_handler, 15 std::unique_ptr<ResourceHandler> next_handler,
16 net::URLRequest* request) 16 net::URLRequest* request)
17 : LayeredResourceHandler(request, std::move(next_handler)), 17 : LayeredResourceHandler(request, std::move(next_handler)) {
18 state_(State::STARTING), 18 next_handler_->SetController(this);
19 first_read_buffer_size_(0) {} 19 }
20 20
21 InterceptingResourceHandler::~InterceptingResourceHandler() {} 21 InterceptingResourceHandler::~InterceptingResourceHandler() {}
22 22
23 void InterceptingResourceHandler::SetController(
24 ResourceController* controller) {
25 if (state_ == State::PASS_THROUGH)
26 return LayeredResourceHandler::SetController(controller);
27 ResourceHandler::SetController(controller);
28 }
29
23 bool InterceptingResourceHandler::OnResponseStarted(ResourceResponse* response, 30 bool InterceptingResourceHandler::OnResponseStarted(ResourceResponse* response,
24 bool* defer) { 31 bool* defer) {
25 // If there's no need to switch handlers, just start acting as a blind 32 // If there's no need to switch handlers, just start acting as a blind
26 // pass-through ResourceHandler. 33 // pass-through ResourceHandler.
27 if (!new_handler_) { 34 if (!new_handler_) {
28 state_ = State::DONE; 35 state_ = State::PASS_THROUGH;
29 first_read_buffer_ = nullptr; 36 first_read_buffer_ = nullptr;
37 next_handler_->SetController(controller());
30 return next_handler_->OnResponseStarted(response, defer); 38 return next_handler_->OnResponseStarted(response, defer);
31 } 39 }
32 40
41 DCHECK_EQ(state_, State::STARTING);
33 // Otherwise, switch handlers. First, inform the original ResourceHandler 42 // Otherwise, switch handlers. First, inform the original ResourceHandler
34 // that this will be handled entirely by the new ResourceHandler. 43 // 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; 44 bool defer_ignored = false;
38 next_handler_->OnResponseStarted(response, &defer_ignored); 45 if (!next_handler_->OnResponseStarted(response, &defer_ignored))
46 return false;
39 47
40 // Although deferring OnResponseStarted is legal, the only downstream handler 48 // Although deferring OnResponseStarted is legal, the only downstream handler
41 // which does so is CrossSiteResourceHandler. Cross-site transitions should 49 // which does so is CrossSiteResourceHandler. Cross-site transitions should
42 // not trigger when switching handlers. 50 // not trigger when switching handlers.
43 DCHECK(!defer_ignored); 51 DCHECK(!defer_ignored);
44 52
45 // Make a copy of the data in the first read buffer. Despite not having been 53 if (!new_handler_->OnResponseStarted(response, defer))
46 // informed of any data being stored in first_read_buffer_, the 54 return false;
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 55
58 // Send the payload to the old handler. 56 if (*defer)
59 SendPayloadToOldHandler(); 57 state_ = State::NOTIFYING_ON_RESPONSE_STARTED_TO_NEW_HANDLER;
60 first_read_buffer_ = nullptr; 58 return true;
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 } 59 }
71 60
72 bool InterceptingResourceHandler::OnWillRead(scoped_refptr<net::IOBuffer>* buf, 61 bool InterceptingResourceHandler::OnWillRead(scoped_refptr<net::IOBuffer>* buf,
73 int* buf_size, 62 int* buf_size,
74 int min_size) { 63 int min_size) {
75 if (state_ == State::DONE) 64 if (state_ == State::PASS_THROUGH)
76 return next_handler_->OnWillRead(buf, buf_size, min_size); 65 return next_handler_->OnWillRead(buf, buf_size, min_size);
77 66
78 DCHECK_EQ(State::STARTING, state_); 67 DCHECK_EQ(State::STARTING, state_);
79 DCHECK_EQ(-1, min_size); 68 DCHECK_EQ(-1, min_size);
80 69
81 if (!next_handler_->OnWillRead(buf, buf_size, min_size)) 70 if (!next_handler_->OnWillRead(buf, buf_size, min_size))
82 return false; 71 return false;
83 72
84 first_read_buffer_ = *buf; 73 first_read_buffer_ = *buf;
85 first_read_buffer_size_ = *buf_size; 74 first_read_buffer_size_ = *buf_size;
86 return true; 75 return true;
87 } 76 }
88 77
89 bool InterceptingResourceHandler::OnReadCompleted(int bytes_read, bool* defer) { 78 bool InterceptingResourceHandler::OnReadCompleted(int bytes_read, bool* defer) {
90 DCHECK(bytes_read >= 0); 79 DCHECK_GE(bytes_read, 0);
91 if (state_ == State::DONE) 80 if (state_ == State::PASS_THROUGH)
92 return next_handler_->OnReadCompleted(bytes_read, defer); 81 return next_handler_->OnReadCompleted(bytes_read, defer);
93 82
94 DCHECK_EQ(State::WAITING_FOR_BUFFER_COPY, state_); 83 DCHECK_EQ(State::STARTING, state_);
Charlie Harrison 2016/09/29 22:32:58 It seems strange that we are STARTING at this poin
yhirano 2016/10/03 11:50:22 Makes sense, but I couldn't come up with a good na
95 state_ = State::DONE; 84 state_ = State::SENDING_BUFFER_TO_NEW_HANDLER;
85 first_read_buffer_bytes_read_ = bytes_read;
96 86
97 // Copy the data from the first read to the new ResourceHandler. 87 return SendFirstReadBufferToNewHandler(defer);
98 scoped_refptr<net::IOBuffer> buf; 88 }
99 int buf_len = 0;
100 if (!next_handler_->OnWillRead(&buf, &buf_len, bytes_read))
101 return false;
102 89
103 CHECK(buf_len >= bytes_read); 90 void InterceptingResourceHandler::OnResponseCompleted(
104 CHECK_GE(first_read_buffer_size_, static_cast<size_t>(bytes_read)); 91 const net::URLRequestStatus& status,
105 memcpy(buf->data(), first_read_buffer_copy_->data(), bytes_read); 92 bool* defer) {
93 switch (state_) {
94 case State::PASS_THROUGH:
95 LayeredResourceHandler::OnResponseCompleted(status, defer);
96 return;
97 case State::NOTIFYING_ON_RESPONSE_COMPLETED_TO_NEW_HANDLER:
98 case State::DONE:
99 NOTREACHED();
100 return;
101 default:
102 break;
103 }
106 104
107 first_read_buffer_copy_ = nullptr; 105 next_handler_->OnResponseCompleted(status, defer);
106 DCHECK(!*defer);
108 107
109 // TODO(clamy): Add a unit test to check that the deferral value is properly 108 completion_status_ = status;
110 // passed to the caller. 109 state_ = State::NOTIFYING_ON_RESPONSE_COMPLETED_TO_NEW_HANDLER;
111 return next_handler_->OnReadCompleted(bytes_read, defer); 110 NotifyOnResponseCompletedToNewHandler(defer);
112 } 111 }
113 112
114 void InterceptingResourceHandler::UseNewHandler( 113 void InterceptingResourceHandler::UseNewHandler(
115 std::unique_ptr<ResourceHandler> new_handler, 114 std::unique_ptr<ResourceHandler> new_handler,
116 const std::string& payload_for_old_handler) { 115 const std::string& payload_for_old_handler) {
117 new_handler_ = std::move(new_handler); 116 new_handler_ = std::move(new_handler);
118 new_handler_->SetController(controller()); 117 new_handler_->SetController(this);
119 payload_for_old_handler_ = payload_for_old_handler; 118 payload_for_old_handler_ = payload_for_old_handler;
120 } 119 }
121 120
122 void InterceptingResourceHandler::SendPayloadToOldHandler() { 121 void InterceptingResourceHandler::Cancel() {
122 controller()->Cancel();
123 }
124
125 void InterceptingResourceHandler::CancelAndIgnore() {
126 controller()->CancelAndIgnore();
127 }
128
129 void InterceptingResourceHandler::CancelWithError(int error_code) {
130 controller()->CancelWithError(error_code);
131 }
132
133 bool InterceptingResourceHandler::SendFirstReadBufferToNewHandler(bool* defer) {
134 DCHECK_EQ(state_, State::SENDING_BUFFER_TO_NEW_HANDLER);
135
Charlie Harrison 2016/09/29 22:32:58 DCHECK_EQ(first_read_buffer_bytes_written_, 0)?
yhirano 2016/10/03 11:50:22 No, it can be greater than zero when called from R
136 while (first_read_buffer_bytes_written_ < first_read_buffer_bytes_read_) {
137 scoped_refptr<net::IOBuffer> buf;
138 int size = 0;
139 if (!new_handler_->OnWillRead(&buf, &size, -1))
140 return false;
141 size = std::min(size, static_cast<int>(first_read_buffer_bytes_read_ -
142 first_read_buffer_bytes_written_));
143 memcpy(buf->data(),
144 first_read_buffer_->data() + first_read_buffer_bytes_written_, size);
145 if (!new_handler_->OnReadCompleted(size, defer))
146 return false;
147 first_read_buffer_bytes_written_ += size;
148 if (*defer)
149 return true;
150 }
151 state_ = State::SENDING_PAYLOAD_TO_OLD_HANDLER;
152 return SendPayloadToOldHandler(defer);
153 }
154
155 bool InterceptingResourceHandler::SendPayloadToOldHandler(bool* defer) {
Charlie Harrison 2016/09/29 22:32:58 Optional: Should this be a MaybeSendPayloadToOldHa
yhirano 2016/10/03 11:50:22 Hmm, I don't think we should append Maybe (weak op
156 DCHECK_EQ(State::SENDING_PAYLOAD_TO_OLD_HANDLER, state_);
157
158 while (payload_bytes_written_ < payload_for_old_handler_.size()) {
159 scoped_refptr<net::IOBuffer> buffer;
160 int size = 0;
161 if (first_read_buffer_) {
Charlie Harrison 2016/09/29 22:32:58 Can this first condition be pulled out of the loop
yhirano 2016/10/03 11:50:22 |first_read_buffer_| is null at the function top w
162 buffer = first_read_buffer_;
163 size = first_read_buffer_size_;
164
165 first_read_buffer_ = nullptr;
166 first_read_buffer_size_ = 0;
167 } else {
168 if (!next_handler_->OnWillRead(&buffer, &size, -1))
169 return false;
170 }
171
172 size = std::min(size, static_cast<int>(payload_for_old_handler_.size() -
173 payload_bytes_written_));
174 memcpy(buffer->data(),
175 payload_for_old_handler_.data() + payload_bytes_written_, size);
176 if (!next_handler_->OnReadCompleted(size, defer))
177 return false;
178 payload_bytes_written_ += size;
179 if (*defer)
180 return true;
181 }
123 bool defer_ignored = false; 182 bool defer_ignored = false;
183 net::URLRequestStatus status = {net::URLRequestStatus::SUCCESS, 0};
124 if (payload_for_old_handler_.empty()) { 184 if (payload_for_old_handler_.empty()) {
125 // If there is no payload, just finalize the request on the old handler. 185 // If there is no payload, just finalize the request on the old handler.
126 net::URLRequestStatus status(net::URLRequestStatus::CANCELED, 186 status = net::URLRequestStatus(net::URLRequestStatus::CANCELED,
127 net::ERR_ABORTED); 187 net::ERR_ABORTED);
128 next_handler_->OnResponseCompleted(status, &defer_ignored); 188 }
129 DCHECK(!defer_ignored); 189 next_handler_->OnResponseCompleted(status, &defer_ignored);
190 DCHECK(!defer_ignored);
191
192 state_ = State::PASS_THROUGH;
193 next_handler_ = std::move(new_handler_);
194 next_handler_->SetController(controller());
195 return true;
196 }
197
198 void InterceptingResourceHandler::Resume() {
199 bool defer = false;
200 switch (state_) {
201 case State::STARTING:
202 // Uninteresting Resume: just delegate to the original resource
203 // controller.
204 controller()->Resume();
205 return;
206 case State::PASS_THROUGH:
207 case State::DONE:
208 NOTREACHED();
209 break;
210 case State::NOTIFYING_ON_RESPONSE_STARTED_TO_NEW_HANDLER:
211 state_ = State::STARTING;
212 break;
213 case State::SENDING_BUFFER_TO_NEW_HANDLER:
214 if (!SendFirstReadBufferToNewHandler(&defer)) {
215 controller()->Cancel();
216 return;
217 }
218 break;
219 case State::SENDING_PAYLOAD_TO_OLD_HANDLER:
220 if (!SendPayloadToOldHandler(&defer)) {
221 controller()->Cancel();
222 return;
223 }
224 break;
225 case State::NOTIFYING_ON_RESPONSE_COMPLETED_TO_NEW_HANDLER:
226 state_ = State::DONE;
227 new_handler_ = nullptr;
228 break;
229 }
230 if (state_ == State::PASS_THROUGH || state_ == State::STARTING ||
231 state_ == State::DONE) {
232 DCHECK(!defer);
233 controller()->Resume();
234 } else {
235 DCHECK(defer);
236 }
237 }
238
239 void InterceptingResourceHandler::NotifyOnResponseCompletedToNewHandler(
240 bool* defer) {
241 if (!new_handler_) {
242 state_ = State::DONE;
130 return; 243 return;
131 } 244 }
245 new_handler_->OnResponseCompleted(completion_status_, defer);
246 if (*defer)
247 return;
132 248
133 // Ensure the old ResourceHandler has a buffer that can store the payload. 249 new_handler_ = nullptr;
134 scoped_refptr<net::IOBuffer> buf; 250 state_ = State::DONE;
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, &defer_ignored);
160 DCHECK(!defer_ignored);
161 } 251 }
162 252
163 } // namespace content 253 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698