Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / extensions / browser / extension_throttle_entry.h
blob61a70b276e78664624b9044a0ea4a40e8248a1f6
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef EXTENSIONS_BROWSER_EXTENSION_THROTTLE_ENTRY_H_
6 #define EXTENSIONS_BROWSER_EXTENSION_THROTTLE_ENTRY_H_
8 #include <queue>
9 #include <string>
11 #include "base/basictypes.h"
12 #include "base/time/time.h"
13 #include "extensions/browser/extension_throttle_entry_interface.h"
14 #include "net/base/backoff_entry.h"
15 #include "net/log/net_log.h"
17 namespace extensions {
19 class ExtensionThrottleManager;
21 // ExtensionThrottleEntry represents an entry of ExtensionThrottleManager.
22 // It analyzes requests of a specific URL over some period of time, in order to
23 // deduce the back-off time for every request.
24 // The back-off algorithm consists of two parts. Firstly, exponential back-off
25 // is used when receiving 5XX server errors or malformed response bodies.
26 // The exponential back-off rule is enforced by URLRequestHttpJob. Any
27 // request sent during the back-off period will be cancelled.
28 // Secondly, a sliding window is used to count recent requests to a given
29 // destination and provide guidance (to the application level only) on whether
30 // too many requests have been sent and when a good time to send the next one
31 // would be. This is never used to deny requests at the network level.
32 class ExtensionThrottleEntry : public ExtensionThrottleEntryInterface {
33 public:
34 // Sliding window period.
35 static const int kDefaultSlidingWindowPeriodMs;
37 // Maximum number of requests allowed in sliding window period.
38 static const int kDefaultMaxSendThreshold;
40 // Number of initial errors to ignore before starting exponential back-off.
41 static const int kDefaultNumErrorsToIgnore;
43 // Initial delay for exponential back-off.
44 static const int kDefaultInitialDelayMs;
46 // Factor by which the waiting time will be multiplied.
47 static const double kDefaultMultiplyFactor;
49 // Fuzzing percentage. ex: 10% will spread requests randomly
50 // between 90%-100% of the calculated time.
51 static const double kDefaultJitterFactor;
53 // Maximum amount of time we are willing to delay our request.
54 static const int kDefaultMaximumBackoffMs;
56 // Time after which the entry is considered outdated.
57 static const int kDefaultEntryLifetimeMs;
59 // The manager object's lifetime must enclose the lifetime of this object.
60 ExtensionThrottleEntry(ExtensionThrottleManager* manager,
61 const std::string& url_id);
63 // Same as above, but exposes the option to ignore
64 // net::LOAD_MAYBE_USER_GESTURE flag of the request.
65 ExtensionThrottleEntry(ExtensionThrottleManager* manager,
66 const std::string& url_id,
67 bool ignore_user_gesture_load_flag_for_tests);
69 // The life span of instances created with this constructor is set to
70 // infinite, and the number of initial errors to ignore is set to 0.
71 // It is only used by unit tests.
72 ExtensionThrottleEntry(ExtensionThrottleManager* manager,
73 const std::string& url_id,
74 const net::BackoffEntry::Policy* backoff_policy,
75 bool ignore_user_gesture_load_flag_for_tests);
77 // Used by the manager, returns true if the entry needs to be garbage
78 // collected.
79 bool IsEntryOutdated() const;
81 // Causes this entry to never reject requests due to back-off.
82 void DisableBackoffThrottling();
84 // Causes this entry to NULL its manager pointer.
85 void DetachManager();
87 // Implementation of ExtensionThrottleEntryInterface.
88 bool ShouldRejectRequest(const net::URLRequest& request) const override;
89 int64 ReserveSendingTimeForNextRequest(
90 const base::TimeTicks& earliest_time) override;
91 base::TimeTicks GetExponentialBackoffReleaseTime() const override;
92 void UpdateWithResponse(int status_code) override;
93 void ReceivedContentWasMalformed(int response_code) override;
94 const std::string& GetURLIdForDebugging() const override;
96 protected:
97 ~ExtensionThrottleEntry() override;
99 void Initialize();
101 // Returns true if the given response code is considered a success for
102 // throttling purposes.
103 bool IsConsideredSuccess(int response_code);
105 // Equivalent to TimeTicks::Now(), virtual to be mockable for testing purpose.
106 virtual base::TimeTicks ImplGetTimeNow() const;
108 // Retrieves the back-off entry object we're using. Used to enable a
109 // unit testing seam for dependency injection in tests.
110 virtual const net::BackoffEntry* GetBackoffEntry() const;
111 virtual net::BackoffEntry* GetBackoffEntry();
113 // Returns true if |load_flags| contains a flag that indicates an
114 // explicit request by the user to load the resource. We never
115 // throttle requests with such load flags.
116 static bool ExplicitUserRequest(const int load_flags);
118 // Used by tests.
119 base::TimeTicks sliding_window_release_time() const {
120 return sliding_window_release_time_;
123 // Used by tests.
124 void set_sliding_window_release_time(const base::TimeTicks& release_time) {
125 sliding_window_release_time_ = release_time;
128 // Valid and immutable after construction time.
129 net::BackoffEntry::Policy backoff_policy_;
131 private:
132 // Timestamp calculated by the sliding window algorithm for when we advise
133 // clients the next request should be made, at the earliest. Advisory only,
134 // not used to deny requests.
135 base::TimeTicks sliding_window_release_time_;
137 // A list of the recent send events. We use them to decide whether there are
138 // too many requests sent in sliding window.
139 std::queue<base::TimeTicks> send_log_;
141 const base::TimeDelta sliding_window_period_;
142 const int max_send_threshold_;
144 // True if DisableBackoffThrottling() has been called on this object.
145 bool is_backoff_disabled_;
147 // Access it through GetBackoffEntry() to allow a unit test seam.
148 net::BackoffEntry backoff_entry_;
150 // Weak back-reference to the manager object managing us.
151 ExtensionThrottleManager* manager_;
153 // Canonicalized URL string that this entry is for; used for logging only.
154 std::string url_id_;
156 net::BoundNetLog net_log_;
157 bool ignore_user_gesture_load_flag_for_tests_;
159 DISALLOW_COPY_AND_ASSIGN(ExtensionThrottleEntry);
162 } // namespace extensions
164 #endif // EXTENSIONS_BROWSER_EXTENSION_THROTTLE_ENTRY_H_