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

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

Issue 2668603003: Make ResourceHandler::OnWillRead able to complete asynchronously. (Closed)
Patch Set: Fix merge (x2) Created 3 years, 9 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/mock_resource_loader.h" 5 #include "content/browser/loader/mock_resource_loader.h"
6 6
7 #include <memory> 7 #include <memory>
8 8
9 #include "base/memory/ptr_util.h" 9 #include "base/memory/ptr_util.h"
10 #include "base/memory/ref_counted.h" 10 #include "base/memory/ref_counted.h"
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
91 if (status_ == Status::CALLING_HANDLER) 91 if (status_ == Status::CALLING_HANDLER)
92 status_ = Status::CALLBACK_PENDING; 92 status_ = Status::CALLBACK_PENDING;
93 return status_; 93 return status_;
94 } 94 }
95 95
96 MockResourceLoader::Status MockResourceLoader::OnWillRead() { 96 MockResourceLoader::Status MockResourceLoader::OnWillRead() {
97 EXPECT_FALSE(weak_factory_.HasWeakPtrs()); 97 EXPECT_FALSE(weak_factory_.HasWeakPtrs());
98 EXPECT_EQ(Status::IDLE, status_); 98 EXPECT_EQ(Status::IDLE, status_);
99 99
100 status_ = Status::CALLING_HANDLER; 100 status_ = Status::CALLING_HANDLER;
101 bool result = 101 waiting_on_buffer_ = true;
102 resource_handler_->OnWillRead(&io_buffer_, &io_buffer_size_); 102 resource_handler_->OnWillRead(
103 &io_buffer_, &io_buffer_size_,
104 base::MakeUnique<TestResourceController>(weak_factory_.GetWeakPtr()));
105 if (status_ == Status::CALLING_HANDLER) {
106 // Shouldn't update |io_buffer_| or |io_buffer_size_| yet if Resume()
107 // hasn't yet been called.
108 EXPECT_FALSE(io_buffer_);
109 EXPECT_EQ(0, io_buffer_size_);
103 110
104 // The second case isn't really allowed, but a number of classes do it 111 status_ = Status::CALLBACK_PENDING;
105 // anyways.
106 EXPECT_TRUE(status_ == Status::CALLING_HANDLER ||
107 (result == false && status_ == Status::CANCELED));
108 if (!result) {
109 // In the case of double-cancels, keep the old error code.
110 if (status_ != Status::CANCELED)
111 error_code_ = net::ERR_ABORTED;
112 EXPECT_EQ(0, io_buffer_size_);
113 EXPECT_FALSE(io_buffer_);
114 status_ = Status::CANCELED;
115 } else {
116 EXPECT_LT(0, io_buffer_size_);
117 EXPECT_TRUE(io_buffer_);
118 status_ = Status::IDLE;
119 } 112 }
113
120 return status_; 114 return status_;
121 }; 115 };
122 116
123 MockResourceLoader::Status MockResourceLoader::OnReadCompleted( 117 MockResourceLoader::Status MockResourceLoader::OnReadCompleted(
124 base::StringPiece bytes) { 118 base::StringPiece bytes) {
125 EXPECT_FALSE(weak_factory_.HasWeakPtrs()); 119 EXPECT_FALSE(weak_factory_.HasWeakPtrs());
126 EXPECT_EQ(Status::IDLE, status_); 120 EXPECT_EQ(Status::IDLE, status_);
127 EXPECT_LE(bytes.size(), static_cast<size_t>(io_buffer_size_)); 121 EXPECT_LE(bytes.size(), static_cast<size_t>(io_buffer_size_));
128 122
129 status_ = Status::CALLING_HANDLER; 123 status_ = Status::CALLING_HANDLER;
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
189 EXPECT_TRUE(status_ == Status::IDLE || status_ == Status::CANCELED); 183 EXPECT_TRUE(status_ == Status::IDLE || status_ == Status::CANCELED);
190 } 184 }
191 185
192 void MockResourceLoader::OutOfBandCancel(int error_code, bool tell_renderer) { 186 void MockResourceLoader::OutOfBandCancel(int error_code, bool tell_renderer) {
193 // Shouldn't be called in-band. 187 // Shouldn't be called in-band.
194 EXPECT_NE(Status::CALLING_HANDLER, status_); 188 EXPECT_NE(Status::CALLING_HANDLER, status_);
195 189
196 status_ = Status::CANCELED; 190 status_ = Status::CANCELED;
197 canceled_out_of_band_ = true; 191 canceled_out_of_band_ = true;
198 192
193 // If OnWillRead was deferred, no longer waiting on a buffer.
194 waiting_on_buffer_ = false;
195
199 // To mimic real behavior, keep old error, in the case of double-cancel. 196 // To mimic real behavior, keep old error, in the case of double-cancel.
200 if (error_code_ == net::OK) 197 if (error_code_ == net::OK)
201 error_code_ = error_code; 198 error_code_ = error_code;
202 } 199 }
203 200
204 void MockResourceLoader::OnCancel(int error_code) { 201 void MockResourceLoader::OnCancel(int error_code) {
205 // It's currently allowed to be canceled in-band after being cancelled 202 // It's currently allowed to be canceled in-band after being cancelled
206 // out-of-band, so do nothing, unless the status is no longer CANCELED, which 203 // out-of-band, so do nothing, unless the status is no longer CANCELED, which
207 // which case, OnResponseCompleted has already been called, and cancels aren't 204 // which case, OnResponseCompleted has already been called, and cancels aren't
208 // expected then. 205 // expected then.
209 // TODO(mmenke): Make CancelOutOfBand synchronously destroy the 206 // TODO(mmenke): Make CancelOutOfBand synchronously destroy the
210 // ResourceLoader. 207 // ResourceLoader.
211 if (canceled_out_of_band_ && status_ == Status::CANCELED) 208 if (canceled_out_of_band_ && status_ == Status::CANCELED)
212 return; 209 return;
213 210
211 // Shouldn't update |io_buffer_| or |io_buffer_size_| on cancel.
212 if (waiting_on_buffer_) {
213 EXPECT_FALSE(io_buffer_);
214 EXPECT_EQ(0, io_buffer_size_);
215 waiting_on_buffer_ = false;
216 }
217
214 EXPECT_LT(error_code, 0); 218 EXPECT_LT(error_code, 0);
215 EXPECT_TRUE(status_ == Status::CALLBACK_PENDING || 219 EXPECT_TRUE(status_ == Status::CALLBACK_PENDING ||
216 status_ == Status::CALLING_HANDLER); 220 status_ == Status::CALLING_HANDLER);
217 221
218 status_ = Status::CANCELED; 222 status_ = Status::CANCELED;
219 error_code_ = error_code; 223 error_code_ = error_code;
220 if (canceled_or_idle_run_loop_) 224 if (canceled_or_idle_run_loop_)
221 canceled_or_idle_run_loop_->Quit(); 225 canceled_or_idle_run_loop_->Quit();
222 } 226 }
223 227
224 void MockResourceLoader::OnResume() { 228 void MockResourceLoader::OnResume() {
229 if (waiting_on_buffer_) {
230 EXPECT_TRUE(io_buffer_);
231 EXPECT_LT(0, io_buffer_size_);
232
233 waiting_on_buffer_ = false;
234 }
235
236 // Shouldn't update |io_buffer_| or |io_buffer_size_| on cancel.
225 EXPECT_TRUE(status_ == Status::CALLBACK_PENDING || 237 EXPECT_TRUE(status_ == Status::CALLBACK_PENDING ||
226 status_ == Status::CALLING_HANDLER); 238 status_ == Status::CALLING_HANDLER);
227 239
228 status_ = Status::IDLE; 240 status_ = Status::IDLE;
229 if (canceled_or_idle_run_loop_) 241 if (canceled_or_idle_run_loop_)
230 canceled_or_idle_run_loop_->Quit(); 242 canceled_or_idle_run_loop_->Quit();
231 } 243 }
232 244
233 } // namespace content 245 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698