| OLD | NEW |
| 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" |
| 11 #include "content/browser/loader/resource_controller.h" | 11 #include "content/browser/loader/resource_controller.h" |
| 12 #include "content/browser/loader/resource_handler.h" | 12 #include "content/browser/loader/resource_handler.h" |
| 13 #include "content/public/common/resource_response.h" | 13 #include "content/public/common/resource_response.h" |
| 14 #include "net/base/io_buffer.h" | 14 #include "net/base/io_buffer.h" |
| 15 #include "net/url_request/url_request_status.h" | 15 #include "net/url_request/url_request_status.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
| 17 | 17 |
| 18 namespace content { | 18 namespace content { |
| 19 | 19 |
| 20 class MockResourceLoader::TestResourceController : public ResourceController { |
| 21 public: |
| 22 explicit TestResourceController(base::WeakPtr<MockResourceLoader> mock_loader) |
| 23 : mock_loader_(mock_loader) {} |
| 24 ~TestResourceController() override {} |
| 25 |
| 26 void Resume() override { mock_loader_->OnResume(); } |
| 27 |
| 28 void Cancel() override { CancelWithError(net::ERR_ABORTED); } |
| 29 |
| 30 void CancelAndIgnore() override { |
| 31 ADD_FAILURE() << "Unexpected CancelAndIgnore call."; |
| 32 Cancel(); |
| 33 } |
| 34 |
| 35 void CancelWithError(int error_code) override { |
| 36 mock_loader_->OnCancel(error_code); |
| 37 } |
| 38 |
| 39 base::WeakPtr<MockResourceLoader> mock_loader_; |
| 40 }; |
| 41 |
| 20 MockResourceLoader::MockResourceLoader(ResourceHandler* resource_handler) | 42 MockResourceLoader::MockResourceLoader(ResourceHandler* resource_handler) |
| 21 : resource_handler_(resource_handler) { | 43 : resource_handler_(resource_handler), weak_factory_(this) { |
| 22 resource_handler_->SetController(this); | 44 resource_handler_->SetDelegate(this); |
| 23 } | 45 } |
| 24 | 46 |
| 25 MockResourceLoader::~MockResourceLoader() {} | 47 MockResourceLoader::~MockResourceLoader() {} |
| 26 | 48 |
| 27 MockResourceLoader::Status MockResourceLoader::OnWillStart(const GURL& url) { | 49 MockResourceLoader::Status MockResourceLoader::OnWillStart(const GURL& url) { |
| 50 EXPECT_FALSE(weak_factory_.HasWeakPtrs()); |
| 28 EXPECT_EQ(Status::IDLE, status_); | 51 EXPECT_EQ(Status::IDLE, status_); |
| 52 |
| 29 status_ = Status::CALLING_HANDLER; | 53 status_ = Status::CALLING_HANDLER; |
| 30 | 54 resource_handler_->OnWillStart(url, base::MakeUnique<TestResourceController>( |
| 31 bool defer = false; | 55 weak_factory_.GetWeakPtr())); |
| 32 bool result = resource_handler_->OnWillStart(url, &defer); | 56 if (status_ == Status::CALLING_HANDLER) |
| 33 // The second case isn't really allowed, but a number of classes do it | |
| 34 // anyways. | |
| 35 EXPECT_TRUE(status_ == Status::CALLING_HANDLER || | |
| 36 (result == false && status_ == Status::CANCELED)); | |
| 37 if (!result) { | |
| 38 // In the case of double-cancels, keep the old error code. | |
| 39 // TODO(mmenke): Once there are no more double cancel cases, remove this | |
| 40 // code. | |
| 41 if (status_ != Status::CANCELED) | |
| 42 error_code_ = net::ERR_ABORTED; | |
| 43 status_ = Status::CANCELED; | |
| 44 } else if (defer) { | |
| 45 status_ = Status::CALLBACK_PENDING; | 57 status_ = Status::CALLBACK_PENDING; |
| 46 } else { | |
| 47 status_ = Status::IDLE; | |
| 48 } | |
| 49 return status_; | 58 return status_; |
| 50 } | 59 } |
| 51 | 60 |
| 52 MockResourceLoader::Status MockResourceLoader::OnRequestRedirected( | 61 MockResourceLoader::Status MockResourceLoader::OnRequestRedirected( |
| 53 const net::RedirectInfo& redirect_info, | 62 const net::RedirectInfo& redirect_info, |
| 54 scoped_refptr<ResourceResponse> response) { | 63 scoped_refptr<ResourceResponse> response) { |
| 64 EXPECT_FALSE(weak_factory_.HasWeakPtrs()); |
| 55 EXPECT_EQ(Status::IDLE, status_); | 65 EXPECT_EQ(Status::IDLE, status_); |
| 66 |
| 56 status_ = Status::CALLING_HANDLER; | 67 status_ = Status::CALLING_HANDLER; |
| 57 | |
| 58 bool defer = false; | |
| 59 // Note that |this| does not hold onto |response|, to match ResourceLoader's | 68 // Note that |this| does not hold onto |response|, to match ResourceLoader's |
| 60 // behavior. If |resource_handler_| wants to use |response| asynchronously, it | 69 // behavior. If |resource_handler_| wants to use |response| asynchronously, it |
| 61 // needs to hold onto its own pointer to it. | 70 // needs to hold onto its own pointer to it. |
| 62 bool result = resource_handler_->OnRequestRedirected(redirect_info, | 71 resource_handler_->OnRequestRedirected( |
| 63 response.get(), &defer); | 72 redirect_info, response.get(), |
| 64 // The second case isn't really allowed, but a number of classes do it | 73 base::MakeUnique<TestResourceController>(weak_factory_.GetWeakPtr())); |
| 65 // anyways. | 74 if (status_ == Status::CALLING_HANDLER) |
| 66 EXPECT_TRUE(status_ == Status::CALLING_HANDLER || | |
| 67 (result == false && status_ == Status::CANCELED)); | |
| 68 if (!result) { | |
| 69 // In the case of double-cancels, keep the old error code. | |
| 70 if (status_ != Status::CANCELED) | |
| 71 error_code_ = net::ERR_ABORTED; | |
| 72 status_ = Status::CANCELED; | |
| 73 } else if (defer) { | |
| 74 status_ = Status::CALLBACK_PENDING; | 75 status_ = Status::CALLBACK_PENDING; |
| 75 } else { | |
| 76 status_ = Status::IDLE; | |
| 77 } | |
| 78 return status_; | 76 return status_; |
| 79 } | 77 } |
| 80 | 78 |
| 81 MockResourceLoader::Status MockResourceLoader::OnResponseStarted( | 79 MockResourceLoader::Status MockResourceLoader::OnResponseStarted( |
| 82 scoped_refptr<ResourceResponse> response) { | 80 scoped_refptr<ResourceResponse> response) { |
| 81 EXPECT_FALSE(weak_factory_.HasWeakPtrs()); |
| 83 EXPECT_EQ(Status::IDLE, status_); | 82 EXPECT_EQ(Status::IDLE, status_); |
| 83 |
| 84 status_ = Status::CALLING_HANDLER; | 84 status_ = Status::CALLING_HANDLER; |
| 85 | |
| 86 bool defer = false; | |
| 87 // Note that |this| does not hold onto |response|, to match ResourceLoader's | 85 // Note that |this| does not hold onto |response|, to match ResourceLoader's |
| 88 // behavior. If |resource_handler_| wants to use |response| asynchronously, it | 86 // behavior. If |resource_handler_| wants to use |response| asynchronously, it |
| 89 // needs to hold onto its own pointer to it. | 87 // needs to hold onto its own pointer to it. |
| 90 bool result = resource_handler_->OnResponseStarted(response.get(), &defer); | 88 resource_handler_->OnResponseStarted( |
| 91 // The second case isn't really allowed, but a number of classes do it | 89 response.get(), |
| 92 // anyways. | 90 base::MakeUnique<TestResourceController>(weak_factory_.GetWeakPtr())); |
| 93 EXPECT_TRUE(status_ == Status::CALLING_HANDLER || | 91 if (status_ == Status::CALLING_HANDLER) |
| 94 (result == false && status_ == Status::CANCELED)); | |
| 95 if (!result) { | |
| 96 // In the case of double-cancels, keep the old error code. | |
| 97 if (status_ != Status::CANCELED) | |
| 98 error_code_ = net::ERR_ABORTED; | |
| 99 status_ = Status::CANCELED; | |
| 100 } else if (defer) { | |
| 101 status_ = Status::CALLBACK_PENDING; | 92 status_ = Status::CALLBACK_PENDING; |
| 102 } else { | |
| 103 status_ = Status::IDLE; | |
| 104 } | |
| 105 return status_; | 93 return status_; |
| 106 } | 94 } |
| 107 | 95 |
| 108 MockResourceLoader::Status MockResourceLoader::OnWillRead(int min_size) { | 96 MockResourceLoader::Status MockResourceLoader::OnWillRead(int min_size) { |
| 97 EXPECT_FALSE(weak_factory_.HasWeakPtrs()); |
| 109 EXPECT_EQ(Status::IDLE, status_); | 98 EXPECT_EQ(Status::IDLE, status_); |
| 110 EXPECT_FALSE(io_buffer_); | |
| 111 EXPECT_EQ(0, io_buffer_size_); | |
| 112 | 99 |
| 113 status_ = Status::CALLING_HANDLER; | 100 status_ = Status::CALLING_HANDLER; |
| 114 | |
| 115 bool result = | 101 bool result = |
| 116 resource_handler_->OnWillRead(&io_buffer_, &io_buffer_size_, min_size); | 102 resource_handler_->OnWillRead(&io_buffer_, &io_buffer_size_, min_size); |
| 103 |
| 117 // The second case isn't really allowed, but a number of classes do it | 104 // The second case isn't really allowed, but a number of classes do it |
| 118 // anyways. | 105 // anyways. |
| 119 EXPECT_TRUE(status_ == Status::CALLING_HANDLER || | 106 EXPECT_TRUE(status_ == Status::CALLING_HANDLER || |
| 120 (result == false && status_ == Status::CANCELED)); | 107 (result == false && status_ == Status::CANCELED)); |
| 121 if (!result) { | 108 if (!result) { |
| 122 // In the case of double-cancels, keep the old error code. | 109 // In the case of double-cancels, keep the old error code. |
| 123 if (status_ != Status::CANCELED) | 110 if (status_ != Status::CANCELED) |
| 124 error_code_ = net::ERR_ABORTED; | 111 error_code_ = net::ERR_ABORTED; |
| 125 EXPECT_EQ(0, io_buffer_size_); | 112 EXPECT_EQ(0, io_buffer_size_); |
| 126 EXPECT_FALSE(io_buffer_); | 113 EXPECT_FALSE(io_buffer_); |
| 127 status_ = Status::CANCELED; | 114 status_ = Status::CANCELED; |
| 128 } else { | 115 } else { |
| 129 EXPECT_LE(min_size, io_buffer_size_); | 116 EXPECT_LE(min_size, io_buffer_size_); |
| 130 EXPECT_LT(0, io_buffer_size_); | 117 EXPECT_LT(0, io_buffer_size_); |
| 131 EXPECT_TRUE(io_buffer_); | 118 EXPECT_TRUE(io_buffer_); |
| 132 status_ = Status::IDLE; | 119 status_ = Status::IDLE; |
| 133 } | 120 } |
| 134 return status_; | 121 return status_; |
| 135 }; | 122 }; |
| 136 | 123 |
| 137 MockResourceLoader::Status MockResourceLoader::OnReadCompleted( | 124 MockResourceLoader::Status MockResourceLoader::OnReadCompleted( |
| 138 base::StringPiece bytes) { | 125 base::StringPiece bytes) { |
| 126 EXPECT_FALSE(weak_factory_.HasWeakPtrs()); |
| 139 EXPECT_EQ(Status::IDLE, status_); | 127 EXPECT_EQ(Status::IDLE, status_); |
| 140 EXPECT_LE(bytes.size(), static_cast<size_t>(io_buffer_size_)); | 128 EXPECT_LE(bytes.size(), static_cast<size_t>(io_buffer_size_)); |
| 141 | 129 |
| 142 status_ = Status::CALLING_HANDLER; | 130 status_ = Status::CALLING_HANDLER; |
| 143 std::copy(bytes.begin(), bytes.end(), io_buffer_->data()); | 131 std::copy(bytes.begin(), bytes.end(), io_buffer_->data()); |
| 144 io_buffer_ = nullptr; | 132 io_buffer_ = nullptr; |
| 145 io_buffer_size_ = 0; | 133 io_buffer_size_ = 0; |
| 146 status_ = Status::CALLING_HANDLER; | 134 resource_handler_->OnReadCompleted( |
| 147 | 135 bytes.size(), |
| 148 bool defer = false; | 136 base::MakeUnique<TestResourceController>(weak_factory_.GetWeakPtr())); |
| 149 bool result = resource_handler_->OnReadCompleted(bytes.size(), &defer); | 137 if (status_ == Status::CALLING_HANDLER) |
| 150 // The second case isn't really allowed, but a number of classes do it | |
| 151 // anyways. | |
| 152 EXPECT_TRUE(status_ == Status::CALLING_HANDLER || | |
| 153 (result == false && status_ == Status::CANCELED)); | |
| 154 if (!result) { | |
| 155 // In the case of double-cancels, keep the old error code. | |
| 156 if (status_ != Status::CANCELED) | |
| 157 error_code_ = net::ERR_ABORTED; | |
| 158 status_ = Status::CANCELED; | |
| 159 } else if (defer) { | |
| 160 status_ = Status::CALLBACK_PENDING; | 138 status_ = Status::CALLBACK_PENDING; |
| 161 } else { | |
| 162 status_ = Status::IDLE; | |
| 163 } | |
| 164 return status_; | 139 return status_; |
| 165 } | 140 } |
| 166 | 141 |
| 167 MockResourceLoader::Status MockResourceLoader::OnResponseCompleted( | 142 MockResourceLoader::Status MockResourceLoader::OnResponseCompleted( |
| 168 const net::URLRequestStatus& status) { | 143 const net::URLRequestStatus& status) { |
| 144 EXPECT_FALSE(weak_factory_.HasWeakPtrs()); |
| 169 // This should only happen while the ResourceLoader is idle or the request was | 145 // This should only happen while the ResourceLoader is idle or the request was |
| 170 // canceled. | 146 // canceled. |
| 171 EXPECT_TRUE(status_ == Status::IDLE || | 147 EXPECT_TRUE(status_ == Status::IDLE || |
| 172 (!status.is_success() && status_ == Status::CANCELED && | 148 (!status.is_success() && status_ == Status::CANCELED && |
| 173 error_code_ == status.error())); | 149 error_code_ == status.error())); |
| 174 | 150 |
| 175 io_buffer_ = nullptr; | 151 io_buffer_ = nullptr; |
| 176 io_buffer_size_ = 0; | 152 io_buffer_size_ = 0; |
| 177 status_ = Status::CALLING_HANDLER; | 153 status_ = Status::CALLING_HANDLER; |
| 178 | 154 resource_handler_->OnResponseCompleted( |
| 179 bool defer = false; | 155 status, |
| 180 resource_handler_->OnResponseCompleted(status, &defer); | 156 base::MakeUnique<TestResourceController>(weak_factory_.GetWeakPtr())); |
| 181 EXPECT_EQ(Status::CALLING_HANDLER, status_); | 157 if (status_ == Status::CALLING_HANDLER) |
| 182 if (defer) { | |
| 183 status_ = Status::CALLBACK_PENDING; | 158 status_ = Status::CALLBACK_PENDING; |
| 184 } else { | 159 EXPECT_NE(Status::CANCELED, status_); |
| 185 status_ = Status::IDLE; | |
| 186 } | |
| 187 return status_; | 160 return status_; |
| 188 } | 161 } |
| 189 | 162 |
| 190 MockResourceLoader::Status | 163 MockResourceLoader::Status |
| 191 MockResourceLoader::OnResponseCompletedFromExternalOutOfBandCancel( | 164 MockResourceLoader::OnResponseCompletedFromExternalOutOfBandCancel( |
| 192 const net::URLRequestStatus& url_request_status) { | 165 const net::URLRequestStatus& url_request_status) { |
| 193 // This can happen at any point, except from a recursive call from | 166 // This can happen at any point, except from a recursive call from |
| 194 // ResourceHandler. | 167 // ResourceHandler. |
| 195 EXPECT_NE(Status::CALLING_HANDLER, status_); | 168 EXPECT_NE(Status::CALLING_HANDLER, status_); |
| 196 | 169 |
| 197 io_buffer_ = nullptr; | 170 io_buffer_ = nullptr; |
| 198 io_buffer_size_ = 0; | 171 io_buffer_size_ = 0; |
| 199 status_ = Status::CALLING_HANDLER; | 172 status_ = Status::CALLING_HANDLER; |
| 200 | 173 |
| 201 bool defer = false; | 174 resource_handler_->OnResponseCompleted( |
| 202 resource_handler_->OnResponseCompleted(url_request_status, &defer); | 175 url_request_status, |
| 203 EXPECT_EQ(Status::CALLING_HANDLER, status_); | 176 base::MakeUnique<TestResourceController>(weak_factory_.GetWeakPtr())); |
| 204 if (defer) { | 177 if (status_ == Status::CALLING_HANDLER) |
| 205 status_ = Status::CALLBACK_PENDING; | 178 status_ = Status::CALLBACK_PENDING; |
| 206 } else { | 179 EXPECT_NE(Status::CANCELED, status_); |
| 207 status_ = Status::IDLE; | |
| 208 } | |
| 209 return status_; | 180 return status_; |
| 210 } | 181 } |
| 211 | 182 |
| 212 void MockResourceLoader::WaitUntilIdleOrCanceled() { | 183 void MockResourceLoader::WaitUntilIdleOrCanceled() { |
| 213 if (status_ == Status::IDLE || status_ == Status::CANCELED) | 184 if (status_ == Status::IDLE || status_ == Status::CANCELED) |
| 214 return; | 185 return; |
| 215 EXPECT_FALSE(canceled_or_idle_run_loop_); | 186 EXPECT_FALSE(canceled_or_idle_run_loop_); |
| 216 canceled_or_idle_run_loop_.reset(new base::RunLoop()); | 187 canceled_or_idle_run_loop_.reset(new base::RunLoop()); |
| 217 canceled_or_idle_run_loop_->Run(); | 188 canceled_or_idle_run_loop_->Run(); |
| 218 canceled_or_idle_run_loop_.reset(); | 189 canceled_or_idle_run_loop_.reset(); |
| 219 EXPECT_TRUE(status_ == Status::IDLE || status_ == Status::CANCELED); | 190 EXPECT_TRUE(status_ == Status::IDLE || status_ == Status::CANCELED); |
| 220 } | 191 } |
| 221 | 192 |
| 222 void MockResourceLoader::Cancel() { | 193 void MockResourceLoader::OutOfBandCancel(int error_code, bool tell_renderer) { |
| 223 CancelWithError(net::ERR_ABORTED); | 194 // Shouldn't be called in-band. |
| 195 EXPECT_NE(Status::CALLING_HANDLER, status_); |
| 196 |
| 197 status_ = Status::CANCELED; |
| 198 canceled_out_of_band_ = true; |
| 199 |
| 200 // To mimic real behavior, keep old error, in the case of double-cancel. |
| 201 if (error_code_ == net::OK) |
| 202 error_code_ = error_code; |
| 224 } | 203 } |
| 225 | 204 |
| 226 void MockResourceLoader::CancelAndIgnore() { | 205 void MockResourceLoader::OnCancel(int error_code) { |
| 227 NOTREACHED(); | 206 // It's currently allowed to be canceled in-band after being cancelled |
| 228 } | 207 // out-of-band, so do nothing, unless the status is no longer CANCELED, which |
| 229 | 208 // which case, OnResponseCompleted has already been called, and cancels aren't |
| 230 void MockResourceLoader::CancelWithError(int error_code) { | 209 // expected then. |
| 231 EXPECT_LT(error_code, 0); | 210 // TODO(mmenke): Make CancelOutOfBand synchronously destroy the |
| 232 // Ignore double cancels. Classes shouldn't be doing this, as | 211 // ResourceLoader. |
| 233 // ResourceLoader doesn't really support it, but they do anywways. :( | 212 if (canceled_out_of_band_ && status_ == Status::CANCELED) |
| 234 // TODO(mmenke): Remove this check. | |
| 235 if (error_code_ != net::OK) | |
| 236 return; | 213 return; |
| 237 | 214 |
| 238 // ResourceLoader really expects canceled not to be called synchronously, but | 215 EXPECT_LT(error_code, 0); |
| 239 // a lot of code does it, so allow it. | |
| 240 // TODO(mmenke): Remove CALLING_HANDLER. | |
| 241 EXPECT_TRUE(status_ == Status::CALLBACK_PENDING || | 216 EXPECT_TRUE(status_ == Status::CALLBACK_PENDING || |
| 242 status_ == Status::CALLING_HANDLER || status_ == Status::IDLE); | 217 status_ == Status::CALLING_HANDLER); |
| 218 |
| 243 status_ = Status::CANCELED; | 219 status_ = Status::CANCELED; |
| 244 error_code_ = error_code; | 220 error_code_ = error_code; |
| 245 if (canceled_or_idle_run_loop_) | 221 if (canceled_or_idle_run_loop_) |
| 246 canceled_or_idle_run_loop_->Quit(); | 222 canceled_or_idle_run_loop_->Quit(); |
| 247 } | 223 } |
| 248 | 224 |
| 249 void MockResourceLoader::Resume() { | 225 void MockResourceLoader::OnResume() { |
| 250 EXPECT_EQ(Status::CALLBACK_PENDING, status_); | 226 EXPECT_TRUE(status_ == Status::CALLBACK_PENDING || |
| 227 status_ == Status::CALLING_HANDLER); |
| 228 |
| 251 status_ = Status::IDLE; | 229 status_ = Status::IDLE; |
| 252 if (canceled_or_idle_run_loop_) | 230 if (canceled_or_idle_run_loop_) |
| 253 canceled_or_idle_run_loop_->Quit(); | 231 canceled_or_idle_run_loop_->Quit(); |
| 254 } | 232 } |
| 255 | 233 |
| 256 } // namespace content | 234 } // namespace content |
| OLD | NEW |