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

Side by Side Diff: content/public/browser/navigation_throttle.h

Issue 2867833002: NavigationThrottle: allow customization of net::Error when blocking
Patch Set: Add unit tests. Created 3 years, 7 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 unified diff | Download patch
OLDNEW
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 #ifndef CONTENT_PUBLIC_BROWSER_NAVIGATION_THROTTLE_H_ 5 #ifndef CONTENT_PUBLIC_BROWSER_NAVIGATION_THROTTLE_H_
6 #define CONTENT_PUBLIC_BROWSER_NAVIGATION_THROTTLE_H_ 6 #define CONTENT_PUBLIC_BROWSER_NAVIGATION_THROTTLE_H_
7 7
8 #include "content/common/content_export.h" 8 #include "content/common/content_export.h"
9 #include "net/base/net_errors.h"
9 10
10 namespace content { 11 namespace content {
11 class NavigationHandle; 12 class NavigationHandle;
12 13
13 // A NavigationThrottle tracks and allows interaction with a navigation on the 14 // A NavigationThrottle tracks and allows interaction with a navigation on the
14 // UI thread. 15 // UI thread.
15 class CONTENT_EXPORT NavigationThrottle { 16 class CONTENT_EXPORT NavigationThrottle {
16 public: 17 public:
17 // This is returned to the NavigationHandle to allow the navigation to 18 // Represents what a NavigationThrottle can decide to do to a navigation. Note
18 // proceed, or to cancel it. 19 // that this enum is implicitly convertable to ThrottleCheckResult.
19 enum ThrottleCheckResult { 20 enum ThrottleAction {
20 // The navigation proceeds uninterrupted. 21 // The navigation proceeds uninterrupted.
21 PROCEED, 22 PROCEED,
22 23
23 // Defers the navigation until the NavigationThrottle calls 24 // Defers the navigation until the NavigationThrottle calls
24 // NavigationHandle::Resume or NavigationHandle::CancelDeferredRequest. 25 // NavigationHandle::Resume or NavigationHandle::CancelDeferredRequest. If
25 // If the NavigationHandle is destroyed while the navigation is deferred, 26 // the NavigationHandle is destroyed while the navigation is deferred, the
26 // the navigation will be canceled in the network stack. 27 // navigation will be canceled in the network stack.
27 DEFER, 28 DEFER,
28 29
29 // Cancels the navigation. 30 // Cancels the navigation.
30 CANCEL, 31 CANCEL,
31 32
32 // Cancels the navigation and makes the requester of the navigation acts 33 // Cancels the navigation and makes the requester of the navigation act
33 // like the request was never made. 34 // like the request was never made.
34 CANCEL_AND_IGNORE, 35 CANCEL_AND_IGNORE,
35 36
36 // Blocks a navigation due to rules asserted before the request is made. 37 // Blocks a navigation due to rules asserted before the request is made.
37 // This can only be returned from WillStartRequest and also from 38 // This can only be returned from WillStartRequest and also from
38 // WillRedirectRequest when PlzNavigate is enabled. This will result in an 39 // WillRedirectRequest when PlzNavigate is enabled. This will result in an
39 // error page for net::ERR_BLOCKED_BY_CLIENT being loaded in the frame that 40 // default net_error code of net::ERR_BLOCKED_BY_CLIENT being loaded in
40 // is navigated. 41 // the frame that is navigated.
41 BLOCK_REQUEST, 42 BLOCK_REQUEST,
42 43
43 // Blocks a navigation due to rules asserted by a response (for instance, 44 // Blocks a navigation due to rules asserted by a response (for instance,
44 // embedding restrictions like 'X-Frame-Options'). This result will only 45 // embedding restrictions like 'X-Frame-Options'). This result will only
45 // be returned from WillProcessResponse. 46 // be returned from WillProcessResponse.
46 BLOCK_RESPONSE, 47 BLOCK_RESPONSE,
47 }; 48 };
48 49
50 // ThrottleCheckResult, the return value for NavigationThrottle decision
51 // methods, is a ThrottleAction value with an attached net::Error.
52 //
53 // ThrottleCheckResult is implicitly convertible from ThrottleAction, allowing
54 // all of the following examples to work:
55 //
56 // ThrottleCheckResult WillStartRequest() override {
57 // // Uses default error for PROCEED (net::OK).
58 // return PROCEED;
59 // }
60 //
61 // ThrottleCheckResult WillStartRequest() override {
62 // // Uses default error for BLOCK_REQUEST (net::ERR_BLOCKED_BY_CLIENT).
63 // return BLOCK_REQUEST;
64 // }
65 //
66 // ThrottleCheckResult WillStartRequest() override {
67 // // Identical to previous example (net::ERR_BLOCKED_BY_CLIENT)
68 // return {BLOCK_REQUEST};
69 // }
70 //
71 // ThrottleCheckResult WillStartRequest() override {
72 // // Uses a custom error code of ERR_FILE_NOT_FOUND.
73 // return {BLOCK_REQUEST, net::ERR_FILE_NOT_FOUND};
74 // }
75 class CONTENT_EXPORT ThrottleCheckResult {
76 public:
77 // Construct with just a ThrottleAction, using the default net::Error for
78 // that action.
79 constexpr ThrottleCheckResult(ThrottleAction action)
80 : ThrottleCheckResult(action, GetDefaultNetErrorCode(action)) {}
81
82 // Construct with an action and an error.
83 constexpr ThrottleCheckResult(ThrottleAction action,
84 net::Error net_error_code)
85 : action_(action), net_error_code_(net_error_code) {}
86
87 ThrottleAction action() { return action_; }
88 net::Error net_error_code() { return net_error_code_; }
89
90 bool operator==(const NavigationThrottle::ThrottleCheckResult& rhs) const;
91 bool operator!=(const NavigationThrottle::ThrottleCheckResult& rhs) const;
92
93 private:
94 static constexpr net::Error GetDefaultNetErrorCode(ThrottleAction action) {
95 return (action == PROCEED || action == DEFER)
96 ? net::OK
97 : ((action == CANCEL || action == CANCEL_AND_IGNORE)
98 ? net::ERR_ABORTED
99 : (action == BLOCK_REQUEST
100 ? net::ERR_BLOCKED_BY_CLIENT
101 : (action == BLOCK_RESPONSE
102 ? net::ERR_BLOCKED_BY_RESPONSE
103 : net::ERR_FAILED)));
104 }
105 ThrottleAction action_ : 16;
106 net::Error net_error_code_ : 16;
nasko 2017/05/11 18:05:44 Chatting with dcheng@ over why we want to pack the
107 };
108
49 NavigationThrottle(NavigationHandle* navigation_handle); 109 NavigationThrottle(NavigationHandle* navigation_handle);
50 virtual ~NavigationThrottle(); 110 virtual ~NavigationThrottle();
51 111
52 // Called when a network request is about to be made for this navigation. 112 // Called when a network request is about to be made for this navigation.
53 // 113 //
54 // The implementer is responsible for ensuring that the WebContents this 114 // The implementer is responsible for ensuring that the WebContents this
55 // throttle is associated with remain alive during the duration of this 115 // throttle is associated with remain alive during the duration of this
56 // method. Failing to do so will result in use-after-free bugs. Should the 116 // method. Failing to do so will result in use-after-free bugs. Should the
57 // implementer need to destroy the WebContents, it should return CANCEL, 117 // implementer need to destroy the WebContents, it should return CANCEL,
58 // CANCEL_AND_IGNORE or DEFER and perform the destruction asynchronously. 118 // CANCEL_AND_IGNORE or DEFER and perform the destruction asynchronously.
(...skipping 11 matching lines...) Expand all
70 // Called when a response's headers and metadata are available. 130 // Called when a response's headers and metadata are available.
71 // 131 //
72 // The implementer is responsible for ensuring that the WebContents this 132 // The implementer is responsible for ensuring that the WebContents this
73 // throttle is associated with remain alive during the duration of this 133 // throttle is associated with remain alive during the duration of this
74 // method. Failing to do so will result in use-after-free bugs. Should the 134 // method. Failing to do so will result in use-after-free bugs. Should the
75 // implementer need to destroy the WebContents, it should return CANCEL, 135 // implementer need to destroy the WebContents, it should return CANCEL,
76 // CANCEL_AND_IGNORE, or BLOCK_RESPONSE and perform the destruction 136 // CANCEL_AND_IGNORE, or BLOCK_RESPONSE and perform the destruction
77 // asynchronously. 137 // asynchronously.
78 virtual ThrottleCheckResult WillProcessResponse(); 138 virtual ThrottleCheckResult WillProcessResponse();
79 139
80 // Returns the name of the throttle for logging purposes. It must not return 140 // Returns the name of the throttle for logging purposes. It must not
81 // nullptr. 141 // return nullptr.
82 virtual const char* GetNameForLogging() = 0; 142 virtual const char* GetNameForLogging() = 0;
83 143
84 // The NavigationHandle that is tracking the information related to this 144 // The NavigationHandle that is tracking the information related to this
85 // navigation. 145 // navigation.
86 NavigationHandle* navigation_handle() const { return navigation_handle_; } 146 NavigationHandle* navigation_handle() const { return navigation_handle_; }
87 147
88 private: 148 private:
89 NavigationHandle* navigation_handle_; 149 NavigationHandle* navigation_handle_;
90 }; 150 };
91 151
152 #if defined(UNIT_TEST)
153 // Test-only operator== to enable assertions like:
154 // EXPECT_EQ(NavigationThrottle::PROCEED, throttle->WillProcessResponse())
155 inline bool operator==(NavigationThrottle::ThrottleAction lhs,
156 const NavigationThrottle::ThrottleCheckResult& rhs) {
157 return NavigationThrottle::ThrottleCheckResult(lhs) == rhs;
158 }
159 #endif
160
92 } // namespace content 161 } // namespace content
93 162
94 #endif // CONTENT_PUBLIC_BROWSER_NAVIGATION_THROTTLE_H_ 163 #endif // CONTENT_PUBLIC_BROWSER_NAVIGATION_THROTTLE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698