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 "components/navigation_interception/intercept_navigation_throttle.h" | 5 #include "components/navigation_interception/intercept_navigation_throttle.h" |
6 | 6 |
7 #include "components/navigation_interception/navigation_params.h" | 7 #include "components/navigation_interception/navigation_params.h" |
8 #include "content/public/browser/browser_thread.h" | 8 #include "content/public/browser/browser_thread.h" |
9 #include "content/public/browser/navigation_handle.h" | 9 #include "content/public/browser/navigation_handle.h" |
10 | 10 |
11 using content::BrowserThread; | |
12 | |
11 namespace navigation_interception { | 13 namespace navigation_interception { |
12 | 14 |
15 namespace { | |
16 | |
17 using ChecksPerformedCallback = base::Callback<void(bool)>; | |
18 | |
19 // This is used to run |should_ignore_callback| if it can destroy the | |
20 // WebContents (and the InterceptNavigationThrottle along). In that case, | |
21 // |on_checks_performed_callback| will be a no-op. | |
22 void RunCallback( | |
23 content::WebContents* web_contents, | |
24 const NavigationParams& navigation_params, | |
25 InterceptNavigationThrottle::CheckCallback should_ignore_callback, | |
26 ChecksPerformedCallback on_checks_performed_callback) { | |
27 bool should_ignore_navigation = | |
28 should_ignore_callback.Run(web_contents, navigation_params); | |
29 on_checks_performed_callback.Run(should_ignore_navigation); | |
30 } | |
31 | |
32 } // namespace | |
33 | |
13 InterceptNavigationThrottle::InterceptNavigationThrottle( | 34 InterceptNavigationThrottle::InterceptNavigationThrottle( |
14 content::NavigationHandle* navigation_handle, | 35 content::NavigationHandle* navigation_handle, |
15 CheckCallback should_ignore_callback) | 36 CheckCallback should_ignore_callback, |
37 bool run_callback_synchronously) | |
16 : content::NavigationThrottle(navigation_handle), | 38 : content::NavigationThrottle(navigation_handle), |
17 should_ignore_callback_(should_ignore_callback) {} | 39 should_ignore_callback_(should_ignore_callback), |
40 run_callback_synchronously_(run_callback_synchronously), | |
41 weak_factory_(this) {} | |
18 | 42 |
19 InterceptNavigationThrottle::~InterceptNavigationThrottle() {} | 43 InterceptNavigationThrottle::~InterceptNavigationThrottle() {} |
20 | 44 |
21 content::NavigationThrottle::ThrottleCheckResult | 45 content::NavigationThrottle::ThrottleCheckResult |
22 InterceptNavigationThrottle::WillStartRequest() { | 46 InterceptNavigationThrottle::WillStartRequest() { |
23 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 47 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
24 return CheckIfShouldIgnoreNavigation(false); | 48 return CheckIfShouldIgnoreNavigation(false); |
25 } | 49 } |
26 | 50 |
27 content::NavigationThrottle::ThrottleCheckResult | 51 content::NavigationThrottle::ThrottleCheckResult |
28 InterceptNavigationThrottle::WillRedirectRequest() { | 52 InterceptNavigationThrottle::WillRedirectRequest() { |
29 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 53 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
30 return CheckIfShouldIgnoreNavigation(true); | 54 return CheckIfShouldIgnoreNavigation(true); |
31 } | 55 } |
32 | 56 |
33 content::NavigationThrottle::ThrottleCheckResult | 57 content::NavigationThrottle::ThrottleCheckResult |
34 InterceptNavigationThrottle::CheckIfShouldIgnoreNavigation(bool is_redirect) { | 58 InterceptNavigationThrottle::CheckIfShouldIgnoreNavigation(bool is_redirect) { |
35 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 59 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
36 NavigationParams navigation_params( | 60 NavigationParams navigation_params( |
37 navigation_handle()->GetURL(), navigation_handle()->GetReferrer(), | 61 navigation_handle()->GetURL(), navigation_handle()->GetReferrer(), |
38 navigation_handle()->HasUserGesture(), navigation_handle()->IsPost(), | 62 navigation_handle()->HasUserGesture(), navigation_handle()->IsPost(), |
39 navigation_handle()->GetPageTransition(), is_redirect, | 63 navigation_handle()->GetPageTransition(), is_redirect, |
40 navigation_handle()->IsExternalProtocol(), true); | 64 navigation_handle()->IsExternalProtocol(), true); |
41 | 65 |
42 bool should_ignore_navigation = should_ignore_callback_.Run( | 66 if (run_callback_synchronously_) { |
43 navigation_handle()->GetWebContents(), navigation_params); | 67 bool should_ignore_navigation = should_ignore_callback_.Run( |
44 return should_ignore_navigation | 68 navigation_handle()->GetWebContents(), navigation_params); |
45 ? content::NavigationThrottle::CANCEL_AND_IGNORE | 69 return should_ignore_navigation |
46 : content::NavigationThrottle::PROCEED; | 70 ? content::NavigationThrottle::CANCEL_AND_IGNORE |
71 : content::NavigationThrottle::PROCEED; | |
72 } | |
73 | |
74 BrowserThread::PostTask( | |
75 BrowserThread::UI, FROM_HERE, | |
76 base::Bind(&InterceptNavigationThrottle::RunCallbackAsynchronously, | |
77 weak_factory_.GetWeakPtr(), navigation_params)); | |
78 return DEFER; | |
79 } | |
80 | |
81 void InterceptNavigationThrottle::RunCallbackAsynchronously( | |
82 const NavigationParams& navigation_params) { | |
83 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
84 | |
85 // Run the callback in a helper function as it may lead ot the destruction of | |
86 // this InterceptNavigationThrottle. | |
87 RunCallback( | |
nasko
2015/11/04 22:44:26
If the callback can destruct this object, then it
clamy
2015/11/05 15:52:23
The callback needs the WebContents to run, which m
| |
88 navigation_handle()->GetWebContents(), navigation_params, | |
89 should_ignore_callback_, | |
90 base::Bind(&InterceptNavigationThrottle::OnAsynchronousChecksPerformed, | |
91 weak_factory_.GetWeakPtr())); | |
92 } | |
93 | |
94 void InterceptNavigationThrottle::OnAsynchronousChecksPerformed( | |
95 bool should_ignore_navigation) { | |
96 if (should_ignore_navigation) | |
97 navigation_handle()->CancelDeferredNavigation(true); | |
98 else | |
99 navigation_handle()->Resume(); | |
47 } | 100 } |
48 | 101 |
49 } // namespace navigation_interception | 102 } // namespace navigation_interception |
OLD | NEW |