blob: 47f5ea16921bdade9bf0d2f6e6d36e3d3829c9e6 [file] [log] [blame]
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001// 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.
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 <string>
9
10#include "base/basictypes.h"
11#include "base/memory/ref_counted.h"
Ben Murdocheb525c52013-07-10 11:40:50 +010012#include "base/time/time.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000013#include "net/base/net_export.h"
14
15namespace net {
16
Torne (Richard Coles)03b57e02014-08-28 12:05:23 +010017class NetworkDelegate;
Torne (Richard Coles)58218062012-11-14 11:43:16 +000018class URLRequest;
19class URLRequestThrottlerHeaderInterface;
20
21// Interface provided on entries of the URL request throttler manager.
22class NET_EXPORT URLRequestThrottlerEntryInterface
23 : public base::RefCountedThreadSafe<URLRequestThrottlerEntryInterface> {
24 public:
25 URLRequestThrottlerEntryInterface() {}
26
27 // Returns true when we have encountered server errors and are doing
28 // exponential back-off, unless the request has load flags that mean
29 // it is likely to be user-initiated, or the NetworkDelegate returns
30 // false for |CanThrottleRequest(request)|.
31 //
32 // URLRequestHttpJob checks this method prior to every request; it
33 // cancels requests if this method returns true.
Torne (Richard Coles)03b57e02014-08-28 12:05:23 +010034 virtual bool ShouldRejectRequest(
35 const URLRequest& request,
36 NetworkDelegate* network_delegate) const = 0;
Torne (Richard Coles)58218062012-11-14 11:43:16 +000037
38 // Calculates a recommended sending time for the next request and reserves it.
39 // The sending time is not earlier than the current exponential back-off
40 // release time or |earliest_time|. Moreover, the previous results of
41 // the method are taken into account, in order to make sure they are spread
42 // properly over time.
43 // Returns the recommended delay before sending the next request, in
44 // milliseconds. The return value is always positive or 0.
45 // Although it is not mandatory, respecting the value returned by this method
46 // is helpful to avoid traffic overload.
47 virtual int64 ReserveSendingTimeForNextRequest(
48 const base::TimeTicks& earliest_time) = 0;
49
50 // Returns the time after which requests are allowed.
51 virtual base::TimeTicks GetExponentialBackoffReleaseTime() const = 0;
52
53 // This method needs to be called each time a response is received.
54 virtual void UpdateWithResponse(
55 const std::string& host,
56 const URLRequestThrottlerHeaderInterface* response) = 0;
57
58 // Lets higher-level modules, that know how to parse particular response
59 // bodies, notify of receiving malformed content for the given URL. This will
60 // be handled by the throttler as if an HTTP 503 response had been received to
61 // the request, i.e. it will count as a failure, unless the HTTP response code
62 // indicated is already one of those that will be counted as an error.
63 virtual void ReceivedContentWasMalformed(int response_code) = 0;
64
65 protected:
66 friend class base::RefCountedThreadSafe<URLRequestThrottlerEntryInterface>;
67 virtual ~URLRequestThrottlerEntryInterface() {}
68
69 private:
70 friend class base::RefCounted<URLRequestThrottlerEntryInterface>;
71 DISALLOW_COPY_AND_ASSIGN(URLRequestThrottlerEntryInterface);
72};
73
74} // namespace net
75
76#endif // NET_URL_REQUEST_URL_REQUEST_THROTTLER_ENTRY_INTERFACE_H_