Chromium Code Reviews| Index: content/public/browser/navigation_throttle.h |
| diff --git a/content/public/browser/navigation_throttle.h b/content/public/browser/navigation_throttle.h |
| index eb079fa40f64618dea62f18e6cb89bbab9573f7a..10ccf856457d0a6a2c8b12d2a87adb06b133b3f0 100644 |
| --- a/content/public/browser/navigation_throttle.h |
| +++ b/content/public/browser/navigation_throttle.h |
| @@ -6,6 +6,7 @@ |
| #define CONTENT_PUBLIC_BROWSER_NAVIGATION_THROTTLE_H_ |
| #include "content/common/content_export.h" |
| +#include "net/base/net_errors.h" |
| namespace content { |
| class NavigationHandle; |
| @@ -14,30 +15,30 @@ class NavigationHandle; |
| // UI thread. |
| class CONTENT_EXPORT NavigationThrottle { |
| public: |
| - // This is returned to the NavigationHandle to allow the navigation to |
| - // proceed, or to cancel it. |
| - enum ThrottleCheckResult { |
| + // Represents what a NavigationThrottle can decide to do to a navigation. Note |
| + // that this enum is implicitly convertable to ThrottleCheckResult. |
| + enum ThrottleAction { |
| // The navigation proceeds uninterrupted. |
| PROCEED, |
| // Defers the navigation until the NavigationThrottle calls |
| - // NavigationHandle::Resume or NavigationHandle::CancelDeferredRequest. |
| - // If the NavigationHandle is destroyed while the navigation is deferred, |
| - // the navigation will be canceled in the network stack. |
| + // NavigationHandle::Resume or NavigationHandle::CancelDeferredRequest. If |
| + // the NavigationHandle is destroyed while the navigation is deferred, the |
| + // navigation will be canceled in the network stack. |
| DEFER, |
| // Cancels the navigation. |
| CANCEL, |
| - // Cancels the navigation and makes the requester of the navigation acts |
| + // Cancels the navigation and makes the requester of the navigation act |
| // like the request was never made. |
| CANCEL_AND_IGNORE, |
| // Blocks a navigation due to rules asserted before the request is made. |
| // This can only be returned from WillStartRequest and also from |
| // WillRedirectRequest when PlzNavigate is enabled. This will result in an |
| - // error page for net::ERR_BLOCKED_BY_CLIENT being loaded in the frame that |
| - // is navigated. |
| + // default net_error code of net::ERR_BLOCKED_BY_CLIENT being loaded in |
| + // the frame that is navigated. |
| BLOCK_REQUEST, |
| // Blocks a navigation due to rules asserted by a response (for instance, |
| @@ -46,6 +47,65 @@ class CONTENT_EXPORT NavigationThrottle { |
| BLOCK_RESPONSE, |
| }; |
| + // ThrottleCheckResult, the return value for NavigationThrottle decision |
| + // methods, is a ThrottleAction value with an attached net::Error. |
| + // |
| + // ThrottleCheckResult is implicitly convertible from ThrottleAction, allowing |
| + // all of the following examples to work: |
| + // |
| + // ThrottleCheckResult WillStartRequest() override { |
| + // // Uses default error for PROCEED (net::OK). |
| + // return PROCEED; |
| + // } |
| + // |
| + // ThrottleCheckResult WillStartRequest() override { |
| + // // Uses default error for BLOCK_REQUEST (net::ERR_BLOCKED_BY_CLIENT). |
| + // return BLOCK_REQUEST; |
| + // } |
| + // |
| + // ThrottleCheckResult WillStartRequest() override { |
| + // // Identical to previous example (net::ERR_BLOCKED_BY_CLIENT) |
| + // return {BLOCK_REQUEST}; |
| + // } |
| + // |
| + // ThrottleCheckResult WillStartRequest() override { |
| + // // Uses a custom error code of ERR_FILE_NOT_FOUND. |
| + // return {BLOCK_REQUEST, net::ERR_FILE_NOT_FOUND}; |
| + // } |
| + class CONTENT_EXPORT ThrottleCheckResult { |
| + public: |
| + // Construct with just a ThrottleAction, using the default net::Error for |
| + // that action. |
| + constexpr ThrottleCheckResult(ThrottleAction action) |
| + : ThrottleCheckResult(action, GetDefaultNetErrorCode(action)) {} |
| + |
| + // Construct with an action and an error. |
| + constexpr ThrottleCheckResult(ThrottleAction action, |
| + net::Error net_error_code) |
| + : action_(action), net_error_code_(net_error_code) {} |
| + |
| + ThrottleAction action() { return action_; } |
| + net::Error net_error_code() { return net_error_code_; } |
| + |
| + bool operator==(const NavigationThrottle::ThrottleCheckResult& rhs) const; |
| + bool operator!=(const NavigationThrottle::ThrottleCheckResult& rhs) const; |
| + |
| + private: |
| + static constexpr net::Error GetDefaultNetErrorCode(ThrottleAction action) { |
| + return (action == PROCEED || action == DEFER) |
| + ? net::OK |
| + : ((action == CANCEL || action == CANCEL_AND_IGNORE) |
| + ? net::ERR_ABORTED |
| + : (action == BLOCK_REQUEST |
| + ? net::ERR_BLOCKED_BY_CLIENT |
| + : (action == BLOCK_RESPONSE |
| + ? net::ERR_BLOCKED_BY_RESPONSE |
| + : net::ERR_FAILED))); |
| + } |
| + ThrottleAction action_ : 16; |
| + net::Error net_error_code_ : 16; |
|
nasko
2017/05/11 18:05:44
Chatting with dcheng@ over why we want to pack the
|
| + }; |
| + |
| NavigationThrottle(NavigationHandle* navigation_handle); |
| virtual ~NavigationThrottle(); |
| @@ -77,8 +137,8 @@ class CONTENT_EXPORT NavigationThrottle { |
| // asynchronously. |
| virtual ThrottleCheckResult WillProcessResponse(); |
| - // Returns the name of the throttle for logging purposes. It must not return |
| - // nullptr. |
| + // Returns the name of the throttle for logging purposes. It must not |
| + // return nullptr. |
| virtual const char* GetNameForLogging() = 0; |
| // The NavigationHandle that is tracking the information related to this |
| @@ -89,6 +149,15 @@ class CONTENT_EXPORT NavigationThrottle { |
| NavigationHandle* navigation_handle_; |
| }; |
| +#if defined(UNIT_TEST) |
| +// Test-only operator== to enable assertions like: |
| +// EXPECT_EQ(NavigationThrottle::PROCEED, throttle->WillProcessResponse()) |
| +inline bool operator==(NavigationThrottle::ThrottleAction lhs, |
| + const NavigationThrottle::ThrottleCheckResult& rhs) { |
| + return NavigationThrottle::ThrottleCheckResult(lhs) == rhs; |
| +} |
| +#endif |
| + |
| } // namespace content |
| #endif // CONTENT_PUBLIC_BROWSER_NAVIGATION_THROTTLE_H_ |