Make USB permissions work in the new permission message system
[chromium-blink-merge.git] / content / renderer / scheduler / resource_dispatch_throttler.h
blob66fc78660d99f20ee1183cd840bfad0f2bf34059
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
3 // found in the LICENSE file.
5 #ifndef CONTENT_RENDERER_SCHEDULER_RESOURCE_DISPATCH_THROTTLER_H_
6 #define CONTENT_RENDERER_SCHEDULER_RESOURCE_DISPATCH_THROTTLER_H_
8 #include <deque>
10 #include "base/threading/thread_checker.h"
11 #include "base/time/time.h"
12 #include "base/timer/timer.h"
13 #include "content/common/content_export.h"
14 #include "ipc/ipc_sender.h"
16 namespace scheduler {
17 class RendererScheduler;
20 namespace content {
22 // Utility class for throttling a stream of resource requests targetted to a
23 // specific IPC sender. The throttling itself is very basic:
24 // * When there is no high-priority work imminent to the main thread, as
25 // indicated by the RendererScheduler, throttling is disabled.
26 // * When >= N requests have been sent in a given time window, requests are
27 // throttled. A timer periodically flushes a portion of the queued requests
28 // until all such requests have been flushed.
29 // TODO(jdduke): Remove this class after resource requests become sufficiently
30 // cheap on the IO thread, crbug.com/440037.
31 class CONTENT_EXPORT ResourceDispatchThrottler : public IPC::Sender {
32 public:
33 // |flush_period| and |max_requests_per_flush| must be strictly positive
34 // in duration/value.
35 ResourceDispatchThrottler(IPC::Sender* proxied_sender,
36 scheduler::RendererScheduler* scheduler,
37 base::TimeDelta flush_period,
38 uint32 max_requests_per_flush);
39 ~ResourceDispatchThrottler() override;
41 // IPC::Sender implementation:
42 bool Send(IPC::Message* msg) override;
44 private:
45 friend class ResourceDispatchThrottlerForTest;
47 // Virtual for testing.
48 virtual base::TimeTicks Now() const;
49 virtual void ScheduleFlush();
51 void Flush();
52 void FlushAll();
53 bool ForwardMessage(IPC::Message* msg);
55 base::ThreadChecker thread_checker_;
57 IPC::Sender* const proxied_sender_;
58 scheduler::RendererScheduler* const scheduler_;
59 const base::TimeDelta flush_period_;
60 const uint32 max_requests_per_flush_;
62 base::Timer flush_timer_;
63 base::TimeTicks last_sent_request_time_;
64 uint32 sent_requests_since_last_flush_;
65 std::deque<IPC::Message*> throttled_messages_;
67 DISALLOW_COPY_AND_ASSIGN(ResourceDispatchThrottler);
70 } // namespace content
72 #endif // CONTENT_RENDERER_SCHEDULER_RESOURCE_DISPATCH_THROTTLER_H_