| 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/intercepting_resource_handler.h" | 5 #include "content/browser/loader/intercepting_resource_handler.h" |
| 6 | 6 |
| 7 #include "base/location.h" |
| 7 #include "base/logging.h" | 8 #include "base/logging.h" |
| 8 #include "base/strings/string_util.h" | 9 #include "base/strings/string_util.h" |
| 10 #include "base/threading/thread_task_runner_handle.h" |
| 9 #include "content/public/common/resource_response.h" | 11 #include "content/public/common/resource_response.h" |
| 10 #include "net/base/io_buffer.h" | 12 #include "net/base/io_buffer.h" |
| 11 #include "net/url_request/url_request.h" | 13 #include "net/url_request/url_request.h" |
| 12 | 14 |
| 13 namespace content { | 15 namespace content { |
| 14 | 16 |
| 15 InterceptingResourceHandler::InterceptingResourceHandler( | 17 InterceptingResourceHandler::InterceptingResourceHandler( |
| 16 std::unique_ptr<ResourceHandler> next_handler, | 18 std::unique_ptr<ResourceHandler> next_handler, |
| 17 net::URLRequest* request) | 19 net::URLRequest* request) |
| 18 : LayeredResourceHandler(request, std::move(next_handler)) { | 20 : LayeredResourceHandler(request, std::move(next_handler)), |
| 21 weak_ptr_factory_(this) { |
| 19 next_handler_->SetController(this); | 22 next_handler_->SetController(this); |
| 20 } | 23 } |
| 21 | 24 |
| 22 InterceptingResourceHandler::~InterceptingResourceHandler() {} | 25 InterceptingResourceHandler::~InterceptingResourceHandler() {} |
| 23 | 26 |
| 24 void InterceptingResourceHandler::SetController( | 27 void InterceptingResourceHandler::SetController( |
| 25 ResourceController* controller) { | 28 ResourceController* controller) { |
| 26 if (state_ == State::PASS_THROUGH) | 29 if (state_ == State::PASS_THROUGH) |
| 27 return LayeredResourceHandler::SetController(controller); | 30 return LayeredResourceHandler::SetController(controller); |
| 28 ResourceHandler::SetController(controller); | 31 ResourceHandler::SetController(controller); |
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 142 } | 145 } |
| 143 | 146 |
| 144 void InterceptingResourceHandler::Resume() { | 147 void InterceptingResourceHandler::Resume() { |
| 145 DCHECK_NE(State::PASS_THROUGH, state_); | 148 DCHECK_NE(State::PASS_THROUGH, state_); |
| 146 if (state_ == State::STARTING || | 149 if (state_ == State::STARTING || |
| 147 state_ == State::WAITING_FOR_ON_READ_COMPLETED) { | 150 state_ == State::WAITING_FOR_ON_READ_COMPLETED) { |
| 148 // Uninteresting Resume: just delegate to the original resource controller. | 151 // Uninteresting Resume: just delegate to the original resource controller. |
| 149 controller()->Resume(); | 152 controller()->Resume(); |
| 150 return; | 153 return; |
| 151 } | 154 } |
| 152 bool defer = false; | |
| 153 if (!DoLoop(&defer)) { | |
| 154 controller()->Cancel(); | |
| 155 return; | |
| 156 } | |
| 157 | 155 |
| 158 if (!defer) | 156 // Can't call DoLoop synchronously, as it may call into |next_handler_| |
| 159 controller()->Resume(); | 157 // synchronously, which is what called Resume(). |
| 158 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 159 FROM_HERE, base::Bind(&InterceptingResourceHandler::AdvanceState, |
| 160 weak_ptr_factory_.GetWeakPtr())); |
| 160 } | 161 } |
| 161 | 162 |
| 162 void InterceptingResourceHandler::UseNewHandler( | 163 void InterceptingResourceHandler::UseNewHandler( |
| 163 std::unique_ptr<ResourceHandler> new_handler, | 164 std::unique_ptr<ResourceHandler> new_handler, |
| 164 const std::string& payload_for_old_handler) { | 165 const std::string& payload_for_old_handler) { |
| 165 new_handler_ = std::move(new_handler); | 166 new_handler_ = std::move(new_handler); |
| 166 new_handler_->SetController(this); | 167 new_handler_->SetController(this); |
| 167 payload_for_old_handler_ = payload_for_old_handler; | 168 payload_for_old_handler_ = payload_for_old_handler; |
| 168 } | 169 } |
| 169 | 170 |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 272 if (*defer) | 273 if (*defer) |
| 273 return true; | 274 return true; |
| 274 } | 275 } |
| 275 | 276 |
| 276 state_ = State::PASS_THROUGH; | 277 state_ = State::PASS_THROUGH; |
| 277 first_read_buffer_double_ = nullptr; | 278 first_read_buffer_double_ = nullptr; |
| 278 next_handler_->SetController(controller()); | 279 next_handler_->SetController(controller()); |
| 279 return true; | 280 return true; |
| 280 } | 281 } |
| 281 | 282 |
| 283 void InterceptingResourceHandler::AdvanceState() { |
| 284 bool defer = false; |
| 285 if (!DoLoop(&defer)) { |
| 286 controller()->Cancel(); |
| 287 return; |
| 288 } |
| 289 |
| 290 if (!defer) |
| 291 controller()->Resume(); |
| 292 } |
| 293 |
| 282 } // namespace content | 294 } // namespace content |
| OLD | NEW |