OLD | NEW |
---|---|
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 <memory> | 7 #include <memory> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/callback.h" | 10 #include "base/callback.h" |
11 #include "base/location.h" | 11 #include "base/location.h" |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
14 #include "content/browser/frame_host/navigation_handle_impl.h" | 14 #include "content/browser/frame_host/navigation_handle_impl.h" |
15 #include "content/browser/frame_host/navigator.h" | |
15 #include "content/browser/frame_host/render_frame_host_impl.h" | 16 #include "content/browser/frame_host/render_frame_host_impl.h" |
16 #include "content/browser/loader/navigation_resource_handler.h" | 17 #include "content/browser/loader/navigation_resource_handler.h" |
17 #include "content/browser/loader/resource_request_info_impl.h" | 18 #include "content/browser/loader/resource_request_info_impl.h" |
18 #include "content/public/browser/browser_thread.h" | 19 #include "content/public/browser/browser_thread.h" |
19 #include "content/public/browser/navigation_data.h" | 20 #include "content/public/browser/navigation_data.h" |
20 #include "content/public/browser/resource_context.h" | 21 #include "content/public/browser/resource_context.h" |
21 #include "content/public/browser/resource_controller.h" | 22 #include "content/public/browser/resource_controller.h" |
22 #include "content/public/browser/resource_dispatcher_host_delegate.h" | 23 #include "content/public/browser/resource_dispatcher_host_delegate.h" |
23 #include "content/public/browser/resource_request_info.h" | 24 #include "content/public/browser/resource_request_info.h" |
24 #include "content/public/browser/ssl_status.h" | 25 #include "content/public/browser/ssl_status.h" |
25 #include "content/public/common/referrer.h" | 26 #include "content/public/common/referrer.h" |
26 #include "net/url_request/redirect_info.h" | 27 #include "net/url_request/redirect_info.h" |
27 #include "net/url_request/url_request.h" | 28 #include "net/url_request/url_request.h" |
28 #include "net/url_request/url_request_context.h" | 29 #include "net/url_request/url_request_context.h" |
29 #include "net/url_request/url_request_job_factory.h" | 30 #include "net/url_request/url_request_job_factory.h" |
30 #include "ui/base/page_transition_types.h" | 31 #include "ui/base/page_transition_types.h" |
31 | 32 |
32 namespace content { | 33 namespace content { |
33 | 34 |
34 namespace { | 35 namespace { |
36 | |
37 // Used in unit tests to make UI thread checks succeed even if there is no | |
38 // NavigationHandle. | |
39 bool g_ui_checks_always_succeed = false; | |
40 | |
35 typedef base::Callback<void(NavigationThrottle::ThrottleCheckResult)> | 41 typedef base::Callback<void(NavigationThrottle::ThrottleCheckResult)> |
36 UIChecksPerformedCallback; | 42 UIChecksPerformedCallback; |
37 | 43 |
38 void SendCheckResultToIOThread(UIChecksPerformedCallback callback, | 44 void SendCheckResultToIOThread(UIChecksPerformedCallback callback, |
39 NavigationThrottle::ThrottleCheckResult result) { | 45 NavigationThrottle::ThrottleCheckResult result) { |
40 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 46 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
41 DCHECK_NE(result, NavigationThrottle::DEFER); | 47 DCHECK_NE(result, NavigationThrottle::DEFER); |
42 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | 48 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
43 base::Bind(callback, result)); | 49 base::Bind(callback, result)); |
44 } | 50 } |
45 | 51 |
52 // Returns the NavigationHandle to use for a navigation in the frame specified | |
53 // by |render_process_id| and |render_frame_host_id|. If not found, |callback| | |
54 // will be invoked to cancel the request. | |
55 // | |
56 // Note: in unit test |callback| may be invoked with a value of proceed if no | |
57 // handle is found. This happens when | |
58 // NavigationResourceThrottle::set_ui_checks_always_succeed_for_testing is | |
59 // called with | |
nasko
2016/09/26 15:51:50
nit: combine lines together.
clamy
2016/09/26 16:27:29
Done.
| |
60 // a value of true. | |
61 NavigationHandleImpl* FindNavigationHandle( | |
62 int render_process_id, | |
63 int render_frame_host_id, | |
64 const UIChecksPerformedCallback& callback) { | |
65 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
66 | |
67 if (g_ui_checks_always_succeed) { | |
68 SendCheckResultToIOThread(callback, NavigationThrottle::PROCEED); | |
69 return nullptr; | |
70 } | |
71 | |
72 RenderFrameHostImpl* render_frame_host = | |
73 RenderFrameHostImpl::FromID(render_process_id, render_frame_host_id); | |
74 if (!render_frame_host) { | |
75 SendCheckResultToIOThread(callback, NavigationThrottle::CANCEL); | |
76 return nullptr; | |
77 } | |
78 | |
79 NavigationHandleImpl* navigation_handle = | |
80 render_frame_host->frame_tree_node() | |
81 ->navigator() | |
82 ->GetNavigationHandleForFrameHost(render_frame_host); | |
83 if (!navigation_handle) { | |
84 SendCheckResultToIOThread(callback, NavigationThrottle::CANCEL); | |
85 return nullptr; | |
86 } | |
87 return navigation_handle; | |
88 } | |
89 | |
46 void CheckWillStartRequestOnUIThread( | 90 void CheckWillStartRequestOnUIThread( |
47 UIChecksPerformedCallback callback, | 91 UIChecksPerformedCallback callback, |
48 int render_process_id, | 92 int render_process_id, |
49 int render_frame_host_id, | 93 int render_frame_host_id, |
50 const std::string& method, | 94 const std::string& method, |
51 const scoped_refptr<content::ResourceRequestBodyImpl>& | 95 const scoped_refptr<content::ResourceRequestBodyImpl>& |
52 resource_request_body, | 96 resource_request_body, |
53 const Referrer& sanitized_referrer, | 97 const Referrer& sanitized_referrer, |
54 bool has_user_gesture, | 98 bool has_user_gesture, |
55 ui::PageTransition transition, | 99 ui::PageTransition transition, |
56 bool is_external_protocol, | 100 bool is_external_protocol, |
57 RequestContextType request_context_type) { | 101 RequestContextType request_context_type) { |
58 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 102 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
59 RenderFrameHostImpl* render_frame_host = | 103 NavigationHandleImpl* navigation_handle = |
60 RenderFrameHostImpl::FromID(render_process_id, render_frame_host_id); | 104 FindNavigationHandle(render_process_id, render_frame_host_id, callback); |
61 if (!render_frame_host) { | 105 if (!navigation_handle) |
62 SendCheckResultToIOThread(callback, NavigationThrottle::PROCEED); | |
63 return; | 106 return; |
64 } | |
65 | |
66 NavigationHandleImpl* navigation_handle = | |
67 render_frame_host->navigation_handle(); | |
68 if (!navigation_handle) { | |
69 SendCheckResultToIOThread(callback, NavigationThrottle::PROCEED); | |
70 return; | |
71 } | |
72 | 107 |
73 navigation_handle->WillStartRequest( | 108 navigation_handle->WillStartRequest( |
74 method, resource_request_body, sanitized_referrer, has_user_gesture, | 109 method, resource_request_body, sanitized_referrer, has_user_gesture, |
75 transition, is_external_protocol, request_context_type, | 110 transition, is_external_protocol, request_context_type, |
76 base::Bind(&SendCheckResultToIOThread, callback)); | 111 base::Bind(&SendCheckResultToIOThread, callback)); |
77 } | 112 } |
78 | 113 |
79 void CheckWillRedirectRequestOnUIThread( | 114 void CheckWillRedirectRequestOnUIThread( |
80 UIChecksPerformedCallback callback, | 115 UIChecksPerformedCallback callback, |
81 int render_process_id, | 116 int render_process_id, |
82 int render_frame_host_id, | 117 int render_frame_host_id, |
83 const GURL& new_url, | 118 const GURL& new_url, |
84 const std::string& new_method, | 119 const std::string& new_method, |
85 const GURL& new_referrer_url, | 120 const GURL& new_referrer_url, |
86 bool new_is_external_protocol, | 121 bool new_is_external_protocol, |
87 scoped_refptr<net::HttpResponseHeaders> headers) { | 122 scoped_refptr<net::HttpResponseHeaders> headers) { |
88 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 123 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
89 RenderFrameHostImpl* render_frame_host = | 124 NavigationHandleImpl* navigation_handle = |
90 RenderFrameHostImpl::FromID(render_process_id, render_frame_host_id); | 125 FindNavigationHandle(render_process_id, render_frame_host_id, callback); |
91 if (!render_frame_host) { | 126 if (!navigation_handle) |
92 SendCheckResultToIOThread(callback, NavigationThrottle::PROCEED); | |
93 return; | 127 return; |
94 } | |
95 | |
96 NavigationHandleImpl* navigation_handle = | |
97 render_frame_host->navigation_handle(); | |
98 if (!navigation_handle) { | |
99 SendCheckResultToIOThread(callback, NavigationThrottle::PROCEED); | |
100 return; | |
101 } | |
102 | 128 |
103 GURL new_validated_url(new_url); | 129 GURL new_validated_url(new_url); |
104 RenderProcessHost::FromID(render_process_id) | 130 RenderProcessHost::FromID(render_process_id) |
105 ->FilterURL(false, &new_validated_url); | 131 ->FilterURL(false, &new_validated_url); |
106 navigation_handle->WillRedirectRequest( | 132 navigation_handle->WillRedirectRequest( |
107 new_validated_url, new_method, new_referrer_url, new_is_external_protocol, | 133 new_validated_url, new_method, new_referrer_url, new_is_external_protocol, |
108 headers, base::Bind(&SendCheckResultToIOThread, callback)); | 134 headers, base::Bind(&SendCheckResultToIOThread, callback)); |
109 } | 135 } |
110 | 136 |
111 void WillProcessResponseOnUIThread( | 137 void WillProcessResponseOnUIThread( |
112 UIChecksPerformedCallback callback, | 138 UIChecksPerformedCallback callback, |
113 int render_process_id, | 139 int render_process_id, |
114 int render_frame_host_id, | 140 int render_frame_host_id, |
115 scoped_refptr<net::HttpResponseHeaders> headers, | 141 scoped_refptr<net::HttpResponseHeaders> headers, |
116 const SSLStatus& ssl_status, | 142 const SSLStatus& ssl_status, |
117 std::unique_ptr<NavigationData> navigation_data) { | 143 std::unique_ptr<NavigationData> navigation_data) { |
118 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 144 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
119 RenderFrameHostImpl* render_frame_host = | 145 NavigationHandleImpl* navigation_handle = |
120 RenderFrameHostImpl::FromID(render_process_id, render_frame_host_id); | 146 FindNavigationHandle(render_process_id, render_frame_host_id, callback); |
121 if (!render_frame_host) { | 147 if (!navigation_handle) |
122 SendCheckResultToIOThread(callback, NavigationThrottle::PROCEED); | |
123 return; | 148 return; |
124 } | |
125 | |
126 NavigationHandleImpl* navigation_handle = | |
127 render_frame_host->navigation_handle(); | |
128 if (!navigation_handle) { | |
129 SendCheckResultToIOThread(callback, NavigationThrottle::PROCEED); | |
130 return; | |
131 } | |
132 | 149 |
133 if (navigation_data) | 150 if (navigation_data) |
134 navigation_handle->set_navigation_data(std::move(navigation_data)); | 151 navigation_handle->set_navigation_data(std::move(navigation_data)); |
135 | 152 |
153 RenderFrameHostImpl* render_frame_host = | |
154 RenderFrameHostImpl::FromID(render_process_id, render_frame_host_id); | |
155 DCHECK(render_frame_host); | |
136 navigation_handle->WillProcessResponse( | 156 navigation_handle->WillProcessResponse( |
137 render_frame_host, headers, ssl_status, | 157 render_frame_host, headers, ssl_status, |
138 base::Bind(&SendCheckResultToIOThread, callback)); | 158 base::Bind(&SendCheckResultToIOThread, callback)); |
139 } | 159 } |
140 | 160 |
141 } // namespace | 161 } // namespace |
142 | 162 |
143 NavigationResourceThrottle::NavigationResourceThrottle( | 163 NavigationResourceThrottle::NavigationResourceThrottle( |
144 net::URLRequest* request, | 164 net::URLRequest* request, |
145 ResourceDispatcherHostDelegate* resource_dispatcher_host_delegate, | 165 ResourceDispatcherHostDelegate* resource_dispatcher_host_delegate, |
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
267 base::Bind(&WillProcessResponseOnUIThread, callback, render_process_id, | 287 base::Bind(&WillProcessResponseOnUIThread, callback, render_process_id, |
268 render_frame_id, response_headers, ssl_status, | 288 render_frame_id, response_headers, ssl_status, |
269 base::Passed(&cloned_data))); | 289 base::Passed(&cloned_data))); |
270 *defer = true; | 290 *defer = true; |
271 } | 291 } |
272 | 292 |
273 const char* NavigationResourceThrottle::GetNameForLogging() const { | 293 const char* NavigationResourceThrottle::GetNameForLogging() const { |
274 return "NavigationResourceThrottle"; | 294 return "NavigationResourceThrottle"; |
275 } | 295 } |
276 | 296 |
297 void NavigationResourceThrottle::set_ui_checks_always_succeed_for_testing( | |
298 bool ui_checks_always_succeed) { | |
299 g_ui_checks_always_succeed = ui_checks_always_succeed; | |
300 } | |
301 | |
277 void NavigationResourceThrottle::OnUIChecksPerformed( | 302 void NavigationResourceThrottle::OnUIChecksPerformed( |
278 NavigationThrottle::ThrottleCheckResult result) { | 303 NavigationThrottle::ThrottleCheckResult result) { |
279 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 304 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
280 if (result == NavigationThrottle::CANCEL_AND_IGNORE) { | 305 if (result == NavigationThrottle::CANCEL_AND_IGNORE) { |
281 controller()->CancelAndIgnore(); | 306 controller()->CancelAndIgnore(); |
282 } else if (result == NavigationThrottle::CANCEL) { | 307 } else if (result == NavigationThrottle::CANCEL) { |
283 controller()->Cancel(); | 308 controller()->Cancel(); |
284 } else if (result == NavigationThrottle::BLOCK_REQUEST) { | 309 } else if (result == NavigationThrottle::BLOCK_REQUEST) { |
285 controller()->CancelWithError(net::ERR_BLOCKED_BY_CLIENT); | 310 controller()->CancelWithError(net::ERR_BLOCKED_BY_CLIENT); |
286 } else { | 311 } else { |
287 controller()->Resume(); | 312 controller()->Resume(); |
288 } | 313 } |
289 } | 314 } |
290 | 315 |
291 } // namespace content | 316 } // namespace content |
OLD | NEW |