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

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

Issue 2476163003: Refactor ResourceHandler API. (Closed)
Patch Set: Minor cleanups, one real fix Created 4 years, 1 month 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
« no previous file with comments | « content/browser/loader/throttling_resource_handler.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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_throttle.h"
(...skipping 15 matching lines...) Expand all
26 throttles_[i]->set_controller(this); 26 throttles_[i]->set_controller(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 void 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_or_cancel) {
40 DCHECK(!cancelled_by_resource_throttle_); 40 DCHECK(!cancelled_by_resource_throttle_);
41 41
42 *defer = false; 42 *defer_or_cancel = 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_or_cancel);
46 next_index_++; 46 next_index_++;
47 if (cancelled_by_resource_throttle_) 47 if (cancelled_by_resource_throttle_) {
48 return false; 48 // Nothing else to do here, already told controller() to cancel the
49 if (*defer) { 49 // request.
50 *defer_or_cancel = true;
51 return;
52 }
53 if (*defer_or_cancel) {
50 OnRequestDefered(index); 54 OnRequestDefered(index);
51 deferred_stage_ = DEFERRED_REDIRECT; 55 deferred_stage_ = DEFERRED_REDIRECT;
52 deferred_redirect_ = redirect_info; 56 deferred_redirect_ = redirect_info;
53 deferred_response_ = response; 57 deferred_response_ = response;
54 return true; // Do not cancel. 58 return;
55 } 59 }
56 } 60 }
57 61
58 next_index_ = 0; // Reset for next time. 62 next_index_ = 0; // Reset for next time.
59 63
60 return next_handler_->OnRequestRedirected(redirect_info, response, defer); 64 return next_handler_->OnRequestRedirected(redirect_info, response,
65 defer_or_cancel);
61 } 66 }
62 67
63 bool ThrottlingResourceHandler::OnWillStart(const GURL& url, bool* defer) { 68 void ThrottlingResourceHandler::OnWillStart(const GURL& url,
69 bool* defer_or_cancel) {
64 DCHECK(!cancelled_by_resource_throttle_); 70 DCHECK(!cancelled_by_resource_throttle_);
65 71
66 *defer = false; 72 *defer_or_cancel = false;
67 while (next_index_ < throttles_.size()) { 73 while (next_index_ < throttles_.size()) {
68 int index = next_index_; 74 int index = next_index_;
69 throttles_[index]->WillStartRequest(defer); 75 throttles_[index]->WillStartRequest(defer_or_cancel);
70 next_index_++; 76 next_index_++;
71 if (cancelled_by_resource_throttle_) 77 if (cancelled_by_resource_throttle_) {
72 return false; 78 // Nothing else to do here, already told controller() to cancel the
73 if (*defer) { 79 // request.
80 *defer_or_cancel = true;
81 return;
82 }
83 if (*defer_or_cancel) {
74 OnRequestDefered(index); 84 OnRequestDefered(index);
75 deferred_stage_ = DEFERRED_START; 85 deferred_stage_ = DEFERRED_START;
76 deferred_url_ = url; 86 deferred_url_ = url;
77 return true; // Do not cancel. 87 return;
78 } 88 }
79 } 89 }
80 90
81 next_index_ = 0; // Reset for next time. 91 next_index_ = 0; // Reset for next time.
82 92
83 return next_handler_->OnWillStart(url, defer); 93 next_handler_->OnWillStart(url, defer_or_cancel);
84 } 94 }
85 95
86 bool ThrottlingResourceHandler::OnResponseStarted(ResourceResponse* response, 96 void ThrottlingResourceHandler::OnResponseStarted(ResourceResponse* response,
87 bool* defer) { 97 bool* defer_or_cancel) {
88 DCHECK(!cancelled_by_resource_throttle_); 98 DCHECK(!cancelled_by_resource_throttle_);
89 99
90 while (next_index_ < throttles_.size()) { 100 while (next_index_ < throttles_.size()) {
91 int index = next_index_; 101 int index = next_index_;
92 throttles_[index]->WillProcessResponse(defer); 102 throttles_[index]->WillProcessResponse(defer_or_cancel);
93 next_index_++; 103 next_index_++;
94 if (cancelled_by_resource_throttle_) 104 if (cancelled_by_resource_throttle_) {
95 return false; 105 // Nothing else to do here, already told controller() to cancel the
96 if (*defer) { 106 // request.
107 *defer_or_cancel = true;
108 return;
109 }
110 if (*defer_or_cancel) {
97 OnRequestDefered(index); 111 OnRequestDefered(index);
98 deferred_stage_ = DEFERRED_RESPONSE; 112 deferred_stage_ = DEFERRED_RESPONSE;
99 deferred_response_ = response; 113 deferred_response_ = response;
100 return true; // Do not cancel. 114 return;
101 } 115 }
102 } 116 }
103 117
104 next_index_ = 0; // Reset for next time. 118 next_index_ = 0; // Reset for next time.
105 119
106 return next_handler_->OnResponseStarted(response, defer); 120 return next_handler_->OnResponseStarted(response, defer_or_cancel);
107 } 121 }
108 122
109 void ThrottlingResourceHandler::Cancel() { 123 void ThrottlingResourceHandler::Cancel() {
110 cancelled_by_resource_throttle_ = true; 124 cancelled_by_resource_throttle_ = true;
111 controller()->Cancel(); 125 controller()->Cancel();
112 } 126 }
113 127
114 void ThrottlingResourceHandler::CancelAndIgnore() { 128 void ThrottlingResourceHandler::CancelAndIgnore() {
115 cancelled_by_resource_throttle_ = true; 129 cancelled_by_resource_throttle_ = true;
116 controller()->CancelAndIgnore(); 130 controller()->CancelAndIgnore();
(...skipping 26 matching lines...) Expand all
143 break; 157 break;
144 } 158 }
145 } 159 }
146 160
147 void ThrottlingResourceHandler::ResumeStart() { 161 void ThrottlingResourceHandler::ResumeStart() {
148 DCHECK(!cancelled_by_resource_throttle_); 162 DCHECK(!cancelled_by_resource_throttle_);
149 163
150 GURL url = deferred_url_; 164 GURL url = deferred_url_;
151 deferred_url_ = GURL(); 165 deferred_url_ = GURL();
152 166
153 bool defer = false; 167 bool defer_or_cancel = false;
154 if (!OnWillStart(url, &defer)) { 168 OnWillStart(url, &defer_or_cancel);
155 controller()->Cancel(); 169 if (!defer_or_cancel)
156 } else if (!defer) {
157 controller()->Resume(); 170 controller()->Resume();
158 }
159 } 171 }
160 172
161 void ThrottlingResourceHandler::ResumeRedirect() { 173 void ThrottlingResourceHandler::ResumeRedirect() {
162 DCHECK(!cancelled_by_resource_throttle_); 174 DCHECK(!cancelled_by_resource_throttle_);
163 175
164 net::RedirectInfo redirect_info = deferred_redirect_; 176 net::RedirectInfo redirect_info = deferred_redirect_;
165 deferred_redirect_ = net::RedirectInfo(); 177 deferred_redirect_ = net::RedirectInfo();
166 scoped_refptr<ResourceResponse> response; 178 scoped_refptr<ResourceResponse> response;
167 deferred_response_.swap(response); 179 deferred_response_.swap(response);
168 180
169 bool defer = false; 181 bool defer_or_cancel = false;
170 if (!OnRequestRedirected(redirect_info, response.get(), &defer)) { 182 OnRequestRedirected(redirect_info, response.get(), &defer_or_cancel);
171 controller()->Cancel(); 183 if (!defer_or_cancel)
172 } else if (!defer) {
173 controller()->Resume(); 184 controller()->Resume();
174 }
175 } 185 }
176 186
177 void ThrottlingResourceHandler::ResumeResponse() { 187 void ThrottlingResourceHandler::ResumeResponse() {
178 DCHECK(!cancelled_by_resource_throttle_); 188 DCHECK(!cancelled_by_resource_throttle_);
179 189
180 scoped_refptr<ResourceResponse> response; 190 scoped_refptr<ResourceResponse> response;
181 deferred_response_.swap(response); 191 deferred_response_.swap(response);
182 192
183 bool defer = false; 193 bool defer_or_cancel = false;
184 if (!OnResponseStarted(response.get(), &defer)) { 194 OnResponseStarted(response.get(), &defer_or_cancel);
185 controller()->Cancel(); 195 if (!defer_or_cancel)
186 } else if (!defer) {
187 controller()->Resume(); 196 controller()->Resume();
188 }
189 } 197 }
190 198
191 void ThrottlingResourceHandler::OnRequestDefered(int throttle_index) { 199 void ThrottlingResourceHandler::OnRequestDefered(int throttle_index) {
192 request()->LogBlockedBy(throttles_[throttle_index]->GetNameForLogging()); 200 request()->LogBlockedBy(throttles_[throttle_index]->GetNameForLogging());
193 } 201 }
194 202
195 } // namespace content 203 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/loader/throttling_resource_handler.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698