Index: components/navigation_interception/intercept_navigation_throttle.cc |
diff --git a/components/navigation_interception/intercept_navigation_throttle.cc b/components/navigation_interception/intercept_navigation_throttle.cc |
index cc256a1273e0a0889e1df2cbabd3f999c3350bbb..26ba894f326fa96dcfde91dd7f1823d96b8d2a31 100644 |
--- a/components/navigation_interception/intercept_navigation_throttle.cc |
+++ b/components/navigation_interception/intercept_navigation_throttle.cc |
@@ -10,11 +10,32 @@ |
namespace navigation_interception { |
+namespace { |
+ |
+// Runs the |should_ignore_callback|. This may result in the destruction of |
+// |throttle|, hence why it is not done inside the NavigationThrottle itself. |
+content::NavigationThrottle::ThrottleCheckResult CheckCallbackAndMaybeDestroy( |
+ const InterceptNavigationThrottle::CheckCallback& should_ignore_callback, |
+ content::WebContents* web_contents, |
+ const NavigationParams& navigation_params, |
+ base::WeakPtr<InterceptNavigationThrottle> throttle) { |
+ bool should_ignore_navigation = |
+ should_ignore_callback.Run(web_contents, navigation_params); |
+ if (!throttle) |
+ return content::NavigationThrottle::DESTROYED; |
+ return should_ignore_navigation |
+ ? content::NavigationThrottle::CANCEL_AND_IGNORE |
+ : content::NavigationThrottle::PROCEED; |
+} |
+ |
+} // namespace |
+ |
InterceptNavigationThrottle::InterceptNavigationThrottle( |
content::NavigationHandle* navigation_handle, |
CheckCallback should_ignore_callback) |
: content::NavigationThrottle(navigation_handle), |
- should_ignore_callback_(should_ignore_callback) {} |
+ should_ignore_callback_(should_ignore_callback), |
+ weak_factory_(this) {} |
InterceptNavigationThrottle::~InterceptNavigationThrottle() {} |
@@ -38,12 +59,9 @@ InterceptNavigationThrottle::CheckIfShouldIgnoreNavigation(bool is_redirect) { |
navigation_handle()->HasUserGesture(), navigation_handle()->IsPost(), |
navigation_handle()->GetPageTransition(), is_redirect, |
navigation_handle()->IsExternalProtocol(), true); |
- |
- bool should_ignore_navigation = should_ignore_callback_.Run( |
- navigation_handle()->GetWebContents(), navigation_params); |
- return should_ignore_navigation |
- ? content::NavigationThrottle::CANCEL_AND_IGNORE |
- : content::NavigationThrottle::PROCEED; |
+ return CheckCallbackAndMaybeDestroy( |
+ should_ignore_callback_, navigation_handle()->GetWebContents(), |
+ navigation_params, weak_factory_.GetWeakPtr()); |
} |
} // namespace navigation_interception |