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 a value of true. |
| 60 NavigationHandleImpl* FindNavigationHandle( |
| 61 int render_process_id, |
| 62 int render_frame_host_id, |
| 63 const UIChecksPerformedCallback& callback) { |
| 64 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 65 |
| 66 if (g_ui_checks_always_succeed) { |
| 67 SendCheckResultToIOThread(callback, NavigationThrottle::PROCEED); |
| 68 return nullptr; |
| 69 } |
| 70 |
| 71 RenderFrameHostImpl* render_frame_host = |
| 72 RenderFrameHostImpl::FromID(render_process_id, render_frame_host_id); |
| 73 if (!render_frame_host) { |
| 74 SendCheckResultToIOThread(callback, NavigationThrottle::CANCEL); |
| 75 return nullptr; |
| 76 } |
| 77 |
| 78 NavigationHandleImpl* navigation_handle = |
| 79 render_frame_host->frame_tree_node() |
| 80 ->navigator() |
| 81 ->GetNavigationHandleForFrameHost(render_frame_host); |
| 82 if (!navigation_handle) { |
| 83 SendCheckResultToIOThread(callback, NavigationThrottle::CANCEL); |
| 84 return nullptr; |
| 85 } |
| 86 return navigation_handle; |
| 87 } |
| 88 |
46 void CheckWillStartRequestOnUIThread( | 89 void CheckWillStartRequestOnUIThread( |
47 UIChecksPerformedCallback callback, | 90 UIChecksPerformedCallback callback, |
48 int render_process_id, | 91 int render_process_id, |
49 int render_frame_host_id, | 92 int render_frame_host_id, |
50 const std::string& method, | 93 const std::string& method, |
51 const scoped_refptr<content::ResourceRequestBodyImpl>& | 94 const scoped_refptr<content::ResourceRequestBodyImpl>& |
52 resource_request_body, | 95 resource_request_body, |
53 const Referrer& sanitized_referrer, | 96 const Referrer& sanitized_referrer, |
54 bool has_user_gesture, | 97 bool has_user_gesture, |
55 ui::PageTransition transition, | 98 ui::PageTransition transition, |
56 bool is_external_protocol, | 99 bool is_external_protocol, |
57 RequestContextType request_context_type) { | 100 RequestContextType request_context_type) { |
58 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 101 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
59 RenderFrameHostImpl* render_frame_host = | 102 NavigationHandleImpl* navigation_handle = |
60 RenderFrameHostImpl::FromID(render_process_id, render_frame_host_id); | 103 FindNavigationHandle(render_process_id, render_frame_host_id, callback); |
61 if (!render_frame_host) { | 104 if (!navigation_handle) |
62 SendCheckResultToIOThread(callback, NavigationThrottle::PROCEED); | |
63 return; | 105 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 | 106 |
73 navigation_handle->WillStartRequest( | 107 navigation_handle->WillStartRequest( |
74 method, resource_request_body, sanitized_referrer, has_user_gesture, | 108 method, resource_request_body, sanitized_referrer, has_user_gesture, |
75 transition, is_external_protocol, request_context_type, | 109 transition, is_external_protocol, request_context_type, |
76 base::Bind(&SendCheckResultToIOThread, callback)); | 110 base::Bind(&SendCheckResultToIOThread, callback)); |
77 } | 111 } |
78 | 112 |
79 void CheckWillRedirectRequestOnUIThread( | 113 void CheckWillRedirectRequestOnUIThread( |
80 UIChecksPerformedCallback callback, | 114 UIChecksPerformedCallback callback, |
81 int render_process_id, | 115 int render_process_id, |
82 int render_frame_host_id, | 116 int render_frame_host_id, |
83 const GURL& new_url, | 117 const GURL& new_url, |
84 const std::string& new_method, | 118 const std::string& new_method, |
85 const GURL& new_referrer_url, | 119 const GURL& new_referrer_url, |
86 bool new_is_external_protocol, | 120 bool new_is_external_protocol, |
87 scoped_refptr<net::HttpResponseHeaders> headers) { | 121 scoped_refptr<net::HttpResponseHeaders> headers) { |
88 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 122 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
89 RenderFrameHostImpl* render_frame_host = | 123 NavigationHandleImpl* navigation_handle = |
90 RenderFrameHostImpl::FromID(render_process_id, render_frame_host_id); | 124 FindNavigationHandle(render_process_id, render_frame_host_id, callback); |
91 if (!render_frame_host) { | 125 if (!navigation_handle) |
92 SendCheckResultToIOThread(callback, NavigationThrottle::PROCEED); | |
93 return; | 126 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 | 127 |
103 GURL new_validated_url(new_url); | 128 GURL new_validated_url(new_url); |
104 RenderProcessHost::FromID(render_process_id) | 129 RenderProcessHost::FromID(render_process_id) |
105 ->FilterURL(false, &new_validated_url); | 130 ->FilterURL(false, &new_validated_url); |
106 navigation_handle->WillRedirectRequest( | 131 navigation_handle->WillRedirectRequest( |
107 new_validated_url, new_method, new_referrer_url, new_is_external_protocol, | 132 new_validated_url, new_method, new_referrer_url, new_is_external_protocol, |
108 headers, base::Bind(&SendCheckResultToIOThread, callback)); | 133 headers, base::Bind(&SendCheckResultToIOThread, callback)); |
109 } | 134 } |
110 | 135 |
111 void WillProcessResponseOnUIThread( | 136 void WillProcessResponseOnUIThread( |
112 UIChecksPerformedCallback callback, | 137 UIChecksPerformedCallback callback, |
113 int render_process_id, | 138 int render_process_id, |
114 int render_frame_host_id, | 139 int render_frame_host_id, |
115 scoped_refptr<net::HttpResponseHeaders> headers, | 140 scoped_refptr<net::HttpResponseHeaders> headers, |
116 const SSLStatus& ssl_status, | 141 const SSLStatus& ssl_status, |
117 std::unique_ptr<NavigationData> navigation_data) { | 142 std::unique_ptr<NavigationData> navigation_data) { |
118 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 143 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
119 RenderFrameHostImpl* render_frame_host = | 144 NavigationHandleImpl* navigation_handle = |
120 RenderFrameHostImpl::FromID(render_process_id, render_frame_host_id); | 145 FindNavigationHandle(render_process_id, render_frame_host_id, callback); |
121 if (!render_frame_host) { | 146 if (!navigation_handle) |
122 SendCheckResultToIOThread(callback, NavigationThrottle::PROCEED); | |
123 return; | 147 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 | 148 |
133 if (navigation_data) | 149 if (navigation_data) |
134 navigation_handle->set_navigation_data(std::move(navigation_data)); | 150 navigation_handle->set_navigation_data(std::move(navigation_data)); |
135 | 151 |
| 152 RenderFrameHostImpl* render_frame_host = |
| 153 RenderFrameHostImpl::FromID(render_process_id, render_frame_host_id); |
| 154 DCHECK(render_frame_host); |
136 navigation_handle->WillProcessResponse( | 155 navigation_handle->WillProcessResponse( |
137 render_frame_host, headers, ssl_status, | 156 render_frame_host, headers, ssl_status, |
138 base::Bind(&SendCheckResultToIOThread, callback)); | 157 base::Bind(&SendCheckResultToIOThread, callback)); |
139 } | 158 } |
140 | 159 |
141 } // namespace | 160 } // namespace |
142 | 161 |
143 NavigationResourceThrottle::NavigationResourceThrottle( | 162 NavigationResourceThrottle::NavigationResourceThrottle( |
144 net::URLRequest* request, | 163 net::URLRequest* request, |
145 ResourceDispatcherHostDelegate* resource_dispatcher_host_delegate, | 164 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, | 286 base::Bind(&WillProcessResponseOnUIThread, callback, render_process_id, |
268 render_frame_id, response_headers, ssl_status, | 287 render_frame_id, response_headers, ssl_status, |
269 base::Passed(&cloned_data))); | 288 base::Passed(&cloned_data))); |
270 *defer = true; | 289 *defer = true; |
271 } | 290 } |
272 | 291 |
273 const char* NavigationResourceThrottle::GetNameForLogging() const { | 292 const char* NavigationResourceThrottle::GetNameForLogging() const { |
274 return "NavigationResourceThrottle"; | 293 return "NavigationResourceThrottle"; |
275 } | 294 } |
276 | 295 |
| 296 void NavigationResourceThrottle::set_ui_checks_always_succeed_for_testing( |
| 297 bool ui_checks_always_succeed) { |
| 298 g_ui_checks_always_succeed = ui_checks_always_succeed; |
| 299 } |
| 300 |
277 void NavigationResourceThrottle::OnUIChecksPerformed( | 301 void NavigationResourceThrottle::OnUIChecksPerformed( |
278 NavigationThrottle::ThrottleCheckResult result) { | 302 NavigationThrottle::ThrottleCheckResult result) { |
279 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 303 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
280 if (result == NavigationThrottle::CANCEL_AND_IGNORE) { | 304 if (result == NavigationThrottle::CANCEL_AND_IGNORE) { |
281 controller()->CancelAndIgnore(); | 305 controller()->CancelAndIgnore(); |
282 } else if (result == NavigationThrottle::CANCEL) { | 306 } else if (result == NavigationThrottle::CANCEL) { |
283 controller()->Cancel(); | 307 controller()->Cancel(); |
284 } else if (result == NavigationThrottle::BLOCK_REQUEST) { | 308 } else if (result == NavigationThrottle::BLOCK_REQUEST) { |
285 controller()->CancelWithError(net::ERR_BLOCKED_BY_CLIENT); | 309 controller()->CancelWithError(net::ERR_BLOCKED_BY_CLIENT); |
286 } else { | 310 } else { |
287 controller()->Resume(); | 311 controller()->Resume(); |
288 } | 312 } |
289 } | 313 } |
290 | 314 |
291 } // namespace content | 315 } // namespace content |
OLD | NEW |