1 // Copyright 2012 The Chromium Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef NET_URL_REQUEST_URL_REQUEST_THROTTLER_ENTRY_INTERFACE_H_ 6 #define NET_URL_REQUEST_URL_REQUEST_THROTTLER_ENTRY_INTERFACE_H_ 7 8 #include <stdint.h> 9 10 #include "base/memory/ref_counted.h" 11 #include "base/time/time.h" 12 #include "net/base/net_export.h" 13 14 namespace net { 15 16 class URLRequest; 17 18 // Interface provided on entries of the URL request throttler manager. 19 class NET_EXPORT URLRequestThrottlerEntryInterface 20 : public base::RefCountedThreadSafe<URLRequestThrottlerEntryInterface> { 21 public: 22 URLRequestThrottlerEntryInterface() = default; 23 24 URLRequestThrottlerEntryInterface(const URLRequestThrottlerEntryInterface&) = 25 delete; 26 URLRequestThrottlerEntryInterface& operator=( 27 const URLRequestThrottlerEntryInterface&) = delete; 28 29 // Returns true when we have encountered server errors and are doing 30 // exponential back-off, unless the request has load flags that mean 31 // it is likely to be user-initiated. 32 // 33 // URLRequestHttpJob checks this method prior to every request; it 34 // cancels requests if this method returns true. 35 virtual bool ShouldRejectRequest(const URLRequest& request) const = 0; 36 37 // Calculates a recommended sending time for the next request and reserves it. 38 // The sending time is not earlier than the current exponential back-off 39 // release time or |earliest_time|. Moreover, the previous results of 40 // the method are taken into account, in order to make sure they are spread 41 // properly over time. 42 // Returns the recommended delay before sending the next request, in 43 // milliseconds. The return value is always positive or 0. 44 // Although it is not mandatory, respecting the value returned by this method 45 // is helpful to avoid traffic overload. 46 virtual int64_t ReserveSendingTimeForNextRequest( 47 const base::TimeTicks& earliest_time) = 0; 48 49 // Returns the time after which requests are allowed. 50 virtual base::TimeTicks GetExponentialBackoffReleaseTime() const = 0; 51 52 // This method needs to be called each time a response is received. 53 virtual void UpdateWithResponse(int status_code) = 0; 54 55 // Lets higher-level modules, that know how to parse particular response 56 // bodies, notify of receiving malformed content for the given URL. This will 57 // be handled by the throttler as if an HTTP 503 response had been received to 58 // the request, i.e. it will count as a failure, unless the HTTP response code 59 // indicated is already one of those that will be counted as an error. 60 virtual void ReceivedContentWasMalformed(int response_code) = 0; 61 62 protected: 63 friend class base::RefCountedThreadSafe<URLRequestThrottlerEntryInterface>; 64 virtual ~URLRequestThrottlerEntryInterface() = default; 65 66 private: 67 friend class base::RefCounted<URLRequestThrottlerEntryInterface>; 68 }; 69 70 } // namespace net 71 72 #endif // NET_URL_REQUEST_URL_REQUEST_THROTTLER_ENTRY_INTERFACE_H_ 73