OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/throttling_resource_handler.h" | 5 #include "content/browser/loader/throttling_resource_handler.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "content/browser/loader/resource_request_info_impl.h" | 9 #include "content/browser/loader/resource_request_info_impl.h" |
10 #include "content/public/browser/resource_throttle.h" | 10 #include "content/public/browser/resource_controller.h" |
11 #include "content/public/common/resource_response.h" | 11 #include "content/public/common/resource_response.h" |
12 #include "net/url_request/url_request.h" | 12 #include "net/url_request/url_request.h" |
13 | 13 |
14 namespace content { | 14 namespace content { |
15 | 15 |
16 ThrottlingResourceHandler::ThrottlingResourceHandler( | 16 ThrottlingResourceHandler::ThrottlingResourceHandler( |
17 std::unique_ptr<ResourceHandler> next_handler, | 17 std::unique_ptr<ResourceHandler> next_handler, |
18 net::URLRequest* request, | 18 net::URLRequest* request, |
19 ScopedVector<ResourceThrottle> throttles) | 19 ScopedVector<ResourceThrottle> throttles) |
20 : LayeredResourceHandler(request, std::move(next_handler)), | 20 : LayeredResourceHandler(request, std::move(next_handler)), |
21 deferred_stage_(DEFERRED_NONE), | 21 deferred_stage_(DEFERRED_NONE), |
22 throttles_(std::move(throttles)), | 22 throttles_(std::move(throttles)), |
23 next_index_(0), | 23 next_index_(0), |
24 cancelled_by_resource_throttle_(false) { | 24 cancelled_by_resource_throttle_(false) { |
25 for (size_t i = 0; i < throttles_.size(); ++i) { | 25 for (size_t i = 0; i < throttles_.size(); ++i) { |
26 throttles_[i]->set_controller(this); | 26 throttles_[i]->set_delegate(this); |
27 // Throttles must have a name, as otherwise, bugs where a throttle fails | 27 // Throttles must have a name, as otherwise, bugs where a throttle fails |
28 // to resume a request can be very difficult to debug. | 28 // to resume a request can be very difficult to debug. |
29 DCHECK(throttles_[i]->GetNameForLogging()); | 29 DCHECK(throttles_[i]->GetNameForLogging()); |
30 } | 30 } |
31 } | 31 } |
32 | 32 |
33 ThrottlingResourceHandler::~ThrottlingResourceHandler() { | 33 ThrottlingResourceHandler::~ThrottlingResourceHandler() { |
34 } | 34 } |
35 | 35 |
36 bool ThrottlingResourceHandler::OnRequestRedirected( | 36 bool ThrottlingResourceHandler::OnRequestRedirected( |
37 const net::RedirectInfo& redirect_info, | 37 const net::RedirectInfo& redirect_info, |
38 ResourceResponse* response, | 38 ResourceResponse* response, |
39 bool* defer) { | 39 bool* defer) { |
40 DCHECK(!cancelled_by_resource_throttle_); | 40 DCHECK(!cancelled_by_resource_throttle_); |
| 41 DCHECK(!*defer); |
41 | 42 |
42 *defer = false; | |
43 while (next_index_ < throttles_.size()) { | 43 while (next_index_ < throttles_.size()) { |
44 int index = next_index_; | 44 int index = next_index_; |
45 throttles_[index]->WillRedirectRequest(redirect_info, defer); | 45 throttles_[index]->WillRedirectRequest(redirect_info, defer); |
46 next_index_++; | 46 next_index_++; |
47 if (cancelled_by_resource_throttle_) | 47 if (cancelled_by_resource_throttle_) |
48 return false; | 48 return false; |
49 if (*defer) { | 49 if (*defer) { |
50 OnRequestDefered(index); | 50 OnRequestDeferred(index); |
51 deferred_stage_ = DEFERRED_REDIRECT; | 51 deferred_stage_ = DEFERRED_REDIRECT; |
52 deferred_redirect_ = redirect_info; | 52 deferred_redirect_ = redirect_info; |
53 deferred_response_ = response; | 53 deferred_response_ = response; |
54 return true; // Do not cancel. | 54 return true; // Do not cancel. |
55 } | 55 } |
56 } | 56 } |
57 | 57 |
58 next_index_ = 0; // Reset for next time. | 58 next_index_ = 0; // Reset for next time. |
59 | 59 |
60 return next_handler_->OnRequestRedirected(redirect_info, response, defer); | 60 return next_handler_->OnRequestRedirected(redirect_info, response, defer); |
61 } | 61 } |
62 | 62 |
63 bool ThrottlingResourceHandler::OnWillStart(const GURL& url, bool* defer) { | 63 bool ThrottlingResourceHandler::OnWillStart(const GURL& url, bool* defer) { |
64 DCHECK(!cancelled_by_resource_throttle_); | 64 DCHECK(!cancelled_by_resource_throttle_); |
| 65 DCHECK(!*defer); |
65 | 66 |
66 *defer = false; | |
67 while (next_index_ < throttles_.size()) { | 67 while (next_index_ < throttles_.size()) { |
68 int index = next_index_; | 68 int index = next_index_; |
69 throttles_[index]->WillStartRequest(defer); | 69 throttles_[index]->WillStartRequest(defer); |
70 next_index_++; | 70 next_index_++; |
71 if (cancelled_by_resource_throttle_) | 71 if (cancelled_by_resource_throttle_) |
72 return false; | 72 return false; |
73 if (*defer) { | 73 if (*defer) { |
74 OnRequestDefered(index); | 74 OnRequestDeferred(index); |
75 deferred_stage_ = DEFERRED_START; | 75 deferred_stage_ = DEFERRED_START; |
76 deferred_url_ = url; | 76 deferred_url_ = url; |
77 return true; // Do not cancel. | 77 return true; // Do not cancel. |
78 } | 78 } |
79 } | 79 } |
80 | 80 |
81 next_index_ = 0; // Reset for next time. | 81 next_index_ = 0; // Reset for next time. |
82 | 82 |
83 return next_handler_->OnWillStart(url, defer); | 83 return next_handler_->OnWillStart(url, defer); |
84 } | 84 } |
85 | 85 |
86 bool ThrottlingResourceHandler::OnResponseStarted(ResourceResponse* response, | 86 bool ThrottlingResourceHandler::OnResponseStarted(ResourceResponse* response, |
87 bool* defer) { | 87 bool* defer) { |
88 DCHECK(!cancelled_by_resource_throttle_); | 88 DCHECK(!cancelled_by_resource_throttle_); |
| 89 DCHECK(!*defer); |
89 | 90 |
90 while (next_index_ < throttles_.size()) { | 91 while (next_index_ < throttles_.size()) { |
91 int index = next_index_; | 92 int index = next_index_; |
92 throttles_[index]->WillProcessResponse(defer); | 93 throttles_[index]->WillProcessResponse(defer); |
93 next_index_++; | 94 next_index_++; |
94 if (cancelled_by_resource_throttle_) | 95 if (cancelled_by_resource_throttle_) |
95 return false; | 96 return false; |
96 if (*defer) { | 97 if (*defer) { |
97 OnRequestDefered(index); | 98 OnRequestDeferred(index); |
98 deferred_stage_ = DEFERRED_RESPONSE; | 99 deferred_stage_ = DEFERRED_RESPONSE; |
99 deferred_response_ = response; | 100 deferred_response_ = response; |
100 return true; // Do not cancel. | 101 return true; // Do not cancel. |
101 } | 102 } |
102 } | 103 } |
103 | 104 |
104 next_index_ = 0; // Reset for next time. | 105 next_index_ = 0; // Reset for next time. |
105 | 106 |
106 return next_handler_->OnResponseStarted(response, defer); | 107 return next_handler_->OnResponseStarted(response, defer); |
107 } | 108 } |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
181 deferred_response_.swap(response); | 182 deferred_response_.swap(response); |
182 | 183 |
183 bool defer = false; | 184 bool defer = false; |
184 if (!OnResponseStarted(response.get(), &defer)) { | 185 if (!OnResponseStarted(response.get(), &defer)) { |
185 controller()->Cancel(); | 186 controller()->Cancel(); |
186 } else if (!defer) { | 187 } else if (!defer) { |
187 controller()->Resume(); | 188 controller()->Resume(); |
188 } | 189 } |
189 } | 190 } |
190 | 191 |
191 void ThrottlingResourceHandler::OnRequestDefered(int throttle_index) { | 192 void ThrottlingResourceHandler::OnRequestDeferred(int throttle_index) { |
192 request()->LogBlockedBy(throttles_[throttle_index]->GetNameForLogging()); | 193 request()->LogBlockedBy(throttles_[throttle_index]->GetNameForLogging()); |
193 } | 194 } |
194 | 195 |
195 } // namespace content | 196 } // namespace content |
OLD | NEW |