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

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

Issue 1616943003: Teach navigation throttles how to cancel requests in WillProcessResponse. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase. Created 4 years, 10 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 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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/navigation_resource_throttle.h" 5 #include "content/browser/loader/navigation_resource_throttle.h"
6 6
7 #include "base/bind.h"
7 #include "base/callback.h" 8 #include "base/callback.h"
9 #include "base/location.h"
10 #include "base/logging.h"
8 #include "content/browser/frame_host/navigation_handle_impl.h" 11 #include "content/browser/frame_host/navigation_handle_impl.h"
9 #include "content/browser/frame_host/render_frame_host_impl.h" 12 #include "content/browser/frame_host/render_frame_host_impl.h"
13 #include "content/public/browser/browser_thread.h"
10 #include "content/public/browser/resource_context.h" 14 #include "content/public/browser/resource_context.h"
11 #include "content/public/browser/resource_controller.h" 15 #include "content/public/browser/resource_controller.h"
12 #include "content/public/browser/resource_request_info.h" 16 #include "content/public/browser/resource_request_info.h"
13 #include "content/public/common/referrer.h" 17 #include "content/public/common/referrer.h"
14 #include "net/url_request/redirect_info.h" 18 #include "net/url_request/redirect_info.h"
15 #include "net/url_request/url_request.h" 19 #include "net/url_request/url_request.h"
16 #include "net/url_request/url_request_context.h" 20 #include "net/url_request/url_request_context.h"
17 #include "net/url_request/url_request_job_factory.h" 21 #include "net/url_request/url_request_job_factory.h"
18 #include "ui/base/page_transition_types.h" 22 #include "ui/base/page_transition_types.h"
19 23
20 namespace content { 24 namespace content {
21 25
22 namespace { 26 namespace {
23 typedef base::Callback<void(NavigationThrottle::ThrottleCheckResult)> 27 typedef base::Callback<void(NavigationThrottle::ThrottleCheckResult)>
24 UIChecksPerformedCallback; 28 UIChecksPerformedCallback;
25 29
26 void SendCheckResultToIOThread(UIChecksPerformedCallback callback, 30 void SendCheckResultToIOThread(UIChecksPerformedCallback callback,
27 NavigationThrottle::ThrottleCheckResult result) { 31 NavigationThrottle::ThrottleCheckResult result) {
28 DCHECK_CURRENTLY_ON(BrowserThread::UI); 32 DCHECK_CURRENTLY_ON(BrowserThread::UI);
29 CHECK(result != NavigationThrottle::DEFER); 33 DCHECK_NE(result, NavigationThrottle::DEFER);
30 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, 34 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
31 base::Bind(callback, result)); 35 base::Bind(callback, result));
32 } 36 }
33 37
34 void CheckWillStartRequestOnUIThread(UIChecksPerformedCallback callback, 38 void CheckWillStartRequestOnUIThread(UIChecksPerformedCallback callback,
35 int render_process_id, 39 int render_process_id,
36 int render_frame_host_id, 40 int render_frame_host_id,
37 bool is_post, 41 bool is_post,
38 const Referrer& sanitized_referrer, 42 const Referrer& sanitized_referrer,
39 bool has_user_gesture, 43 bool has_user_gesture,
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
86 GURL new_validated_url(new_url); 90 GURL new_validated_url(new_url);
87 RenderProcessHost::FromID(render_process_id) 91 RenderProcessHost::FromID(render_process_id)
88 ->FilterURL(false, &new_validated_url); 92 ->FilterURL(false, &new_validated_url);
89 navigation_handle->WillRedirectRequest( 93 navigation_handle->WillRedirectRequest(
90 new_validated_url, new_method_is_post, new_referrer_url, 94 new_validated_url, new_method_is_post, new_referrer_url,
91 new_is_external_protocol, headers, 95 new_is_external_protocol, headers,
92 base::Bind(&SendCheckResultToIOThread, callback)); 96 base::Bind(&SendCheckResultToIOThread, callback));
93 } 97 }
94 98
95 void WillProcessResponseOnUIThread( 99 void WillProcessResponseOnUIThread(
100 UIChecksPerformedCallback callback,
96 int render_process_id, 101 int render_process_id,
97 int render_frame_host_id, 102 int render_frame_host_id,
98 scoped_refptr<net::HttpResponseHeaders> headers) { 103 scoped_refptr<net::HttpResponseHeaders> headers) {
99 DCHECK_CURRENTLY_ON(BrowserThread::UI); 104 DCHECK_CURRENTLY_ON(BrowserThread::UI);
100 RenderFrameHostImpl* render_frame_host = 105 RenderFrameHostImpl* render_frame_host =
101 RenderFrameHostImpl::FromID(render_process_id, render_frame_host_id); 106 RenderFrameHostImpl::FromID(render_process_id, render_frame_host_id);
102 if (!render_frame_host) 107 if (!render_frame_host) {
108 SendCheckResultToIOThread(callback, NavigationThrottle::PROCEED);
103 return; 109 return;
110 }
104 111
105 NavigationHandleImpl* navigation_handle = 112 NavigationHandleImpl* navigation_handle =
106 render_frame_host->navigation_handle(); 113 render_frame_host->navigation_handle();
107 if (!navigation_handle) 114 if (!navigation_handle) {
115 SendCheckResultToIOThread(callback, NavigationThrottle::PROCEED);
108 return; 116 return;
117 }
109 118
110 navigation_handle->ReadyToCommitNavigation(render_frame_host, headers); 119 navigation_handle->WillProcessResponse(
120 render_frame_host, headers,
121 base::Bind(&SendCheckResultToIOThread, callback));
111 } 122 }
112 123
113 } // namespace 124 } // namespace
114 125
115 NavigationResourceThrottle::NavigationResourceThrottle(net::URLRequest* request) 126 NavigationResourceThrottle::NavigationResourceThrottle(net::URLRequest* request)
116 : request_(request), weak_ptr_factory_(this) {} 127 : request_(request), weak_ptr_factory_(this) {}
117 128
118 NavigationResourceThrottle::~NavigationResourceThrottle() {} 129 NavigationResourceThrottle::~NavigationResourceThrottle() {}
119 130
120 void NavigationResourceThrottle::WillStartRequest(bool* defer) { 131 void NavigationResourceThrottle::WillStartRequest(bool* defer) {
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
197 return; 208 return;
198 209
199 // Send a copy of the response headers to the NavigationHandle on the UI 210 // Send a copy of the response headers to the NavigationHandle on the UI
200 // thread. 211 // thread.
201 scoped_refptr<net::HttpResponseHeaders> response_headers; 212 scoped_refptr<net::HttpResponseHeaders> response_headers;
202 if (request_->response_headers()) { 213 if (request_->response_headers()) {
203 response_headers = new net::HttpResponseHeaders( 214 response_headers = new net::HttpResponseHeaders(
204 request_->response_headers()->raw_headers()); 215 request_->response_headers()->raw_headers());
205 } 216 }
206 217
218 UIChecksPerformedCallback callback =
219 base::Bind(&NavigationResourceThrottle::OnUIChecksPerformed,
220 weak_ptr_factory_.GetWeakPtr());
221
207 BrowserThread::PostTask( 222 BrowserThread::PostTask(
208 BrowserThread::UI, FROM_HERE, 223 BrowserThread::UI, FROM_HERE,
209 base::Bind(&WillProcessResponseOnUIThread, render_process_id, 224 base::Bind(&WillProcessResponseOnUIThread, callback, render_process_id,
210 render_frame_id, response_headers)); 225 render_frame_id, response_headers));
226 *defer = true;
211 } 227 }
212 228
213 const char* NavigationResourceThrottle::GetNameForLogging() const { 229 const char* NavigationResourceThrottle::GetNameForLogging() const {
214 return "NavigationResourceThrottle"; 230 return "NavigationResourceThrottle";
215 } 231 }
216 232
217 void NavigationResourceThrottle::OnUIChecksPerformed( 233 void NavigationResourceThrottle::OnUIChecksPerformed(
218 NavigationThrottle::ThrottleCheckResult result) { 234 NavigationThrottle::ThrottleCheckResult result) {
219 DCHECK_CURRENTLY_ON(BrowserThread::IO); 235 DCHECK_CURRENTLY_ON(BrowserThread::IO);
220 if (result == NavigationThrottle::CANCEL_AND_IGNORE) { 236 if (result == NavigationThrottle::CANCEL_AND_IGNORE) {
221 controller()->CancelAndIgnore(); 237 controller()->CancelAndIgnore();
222 } else if (result == NavigationThrottle::CANCEL) { 238 } else if (result == NavigationThrottle::CANCEL) {
223 controller()->Cancel(); 239 controller()->Cancel();
224 } else { 240 } else {
225 controller()->Resume(); 241 controller()->Resume();
226 } 242 }
227 } 243 }
228 244
229 } // namespace content 245 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/frame_host/navigation_handle_impl_unittest.cc ('k') | content/public/browser/navigation_throttle.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698