Ignore title parameter for navigator.registerProtocolHandler
[chromium-blink-merge.git] / components / policy / core / common / cloud / rate_limiter.h
blob59492572ef0b5fc29beac77d55829c48559822e8
1 // Copyright (c) 2013 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 COMPONENTS_POLICY_CORE_COMMON_CLOUD_RATE_LIMITER_H_
6 #define COMPONENTS_POLICY_CORE_COMMON_CLOUD_RATE_LIMITER_H_
8 #include <queue>
10 #include "base/basictypes.h"
11 #include "base/callback.h"
12 #include "base/cancelable_callback.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/threading/non_thread_safe.h"
16 #include "base/time/time.h"
17 #include "components/policy/policy_export.h"
19 namespace base {
20 class SequencedTaskRunner;
21 class TickClock;
24 namespace policy {
26 // A simple class to limit the rate at which a callback is invoked.
27 class POLICY_EXPORT RateLimiter : public base::NonThreadSafe {
28 public:
29 // Will limit invocations of |callback| to |max_requests| per |duration|.
30 // |task_runner| is used to post delayed tasks, and |clock| is used to
31 // measure elapsed time.
32 RateLimiter(size_t max_requests,
33 const base::TimeDelta& duration,
34 const base::Closure& callback,
35 scoped_refptr<base::SequencedTaskRunner> task_runner,
36 scoped_ptr<base::TickClock> clock);
37 ~RateLimiter();
39 // Posts a request to invoke |callback_|. It is invoked immediately if the
40 // rate in the preceding |duration_| period is within the limit, otherwise
41 // the callback will be invoked later, ensuring the allowed rate is not
42 // exceeded.
43 void PostRequest();
45 private:
46 const size_t max_requests_;
47 const base::TimeDelta duration_;
48 base::Closure callback_;
49 scoped_refptr<base::SequencedTaskRunner> task_runner_;
50 scoped_ptr<base::TickClock> clock_;
52 std::queue<base::TimeTicks> invocation_times_;
53 base::CancelableClosure delayed_callback_;
55 DISALLOW_COPY_AND_ASSIGN(RateLimiter);
58 } // namespace policy
60 #endif // COMPONENTS_POLICY_CORE_COMMON_CLOUD_RATE_LIMITER_H_