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

Unified Diff: components/navigation_interception/intercept_navigation_throttle.cc

Issue 1412113006: Prevent the destruction of WebContents in NavigationThrottle methods (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 2 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 side-by-side diff with in-line comments
Download patch
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

Powered by Google App Engine
This is Rietveld 408576698