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_
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"
18 class RendererScheduler
;
20 // Utility class for throttling a stream of resource requests targetted to a
21 // specific IPC sender. The throttling itself is very basic:
22 // * When there is no high-priority work imminent to the main thread, as
23 // indicated by the RendererScheduler, throttling is disabled.
24 // * When >= N requests have been sent in a given time window, requests are
25 // throttled. A timer periodically flushes a portion of the queued requests
26 // until all such requests have been flushed.
27 // TODO(jdduke): Remove this class after resource requests become sufficiently
28 // cheap on the IO thread, crbug.com/440037.
29 class CONTENT_EXPORT ResourceDispatchThrottler
: public IPC::Sender
{
31 // |flush_period| and |max_requests_per_flush| must be strictly positive
33 ResourceDispatchThrottler(IPC::Sender
* proxied_sender
,
34 RendererScheduler
* scheduler
,
35 base::TimeDelta flush_period
,
36 uint32 max_requests_per_flush
);
37 ~ResourceDispatchThrottler() override
;
39 // IPC::Sender implementation:
40 bool Send(IPC::Message
* msg
) override
;
43 friend class ResourceDispatchThrottlerForTest
;
45 // Virtual for testing.
46 virtual base::TimeTicks
Now() const;
47 virtual void ScheduleFlush();
51 bool ForwardMessage(IPC::Message
* msg
);
53 base::ThreadChecker thread_checker_
;
55 IPC::Sender
* const proxied_sender_
;
56 RendererScheduler
* const scheduler_
;
57 const base::TimeDelta flush_period_
;
58 const uint32 max_requests_per_flush_
;
60 base::Timer flush_timer_
;
61 base::TimeTicks last_sent_request_time_
;
62 uint32 sent_requests_since_last_flush_
;
63 std::deque
<IPC::Message
*> throttled_messages_
;
65 DISALLOW_COPY_AND_ASSIGN(ResourceDispatchThrottler
);
68 } // namespace content
70 #endif // CONTENT_RENDERER_SCHEDULER_RESOURCE_DISPATCH_THROTTLER_H_