OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CONTENT_RENDERER_SCHEDULER_RESOURCE_DISPATCH_THROTTLER_H_ | 5 #ifndef CONTENT_RENDERER_SCHEDULER_RESOURCE_DISPATCH_THROTTLER_H_ |
6 #define CONTENT_RENDERER_SCHEDULER_RESOURCE_DISPATCH_THROTTLER_H_ | 6 #define CONTENT_RENDERER_SCHEDULER_RESOURCE_DISPATCH_THROTTLER_H_ |
7 | 7 |
8 #include <deque> | 8 #include <deque> |
9 | 9 |
10 #include "base/threading/thread_checker.h" | 10 #include "base/threading/thread_checker.h" |
11 #include "base/time/time.h" | 11 #include "base/time/time.h" |
12 #include "base/timer/timer.h" | 12 #include "base/timer/timer.h" |
13 #include "content/common/content_export.h" | 13 #include "content/common/content_export.h" |
14 #include "ipc/ipc_sender.h" | 14 #include "ipc/ipc_sender.h" |
15 | 15 |
| 16 namespace scheduler { |
| 17 class RendererScheduler; |
| 18 } |
| 19 |
16 namespace content { | 20 namespace content { |
17 | 21 |
18 class RendererScheduler; | |
19 | |
20 // Utility class for throttling a stream of resource requests targetted to a | 22 // Utility class for throttling a stream of resource requests targetted to a |
21 // specific IPC sender. The throttling itself is very basic: | 23 // specific IPC sender. The throttling itself is very basic: |
22 // * When there is no high-priority work imminent to the main thread, as | 24 // * When there is no high-priority work imminent to the main thread, as |
23 // indicated by the RendererScheduler, throttling is disabled. | 25 // indicated by the RendererScheduler, throttling is disabled. |
24 // * When >= N requests have been sent in a given time window, requests are | 26 // * 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 | 27 // throttled. A timer periodically flushes a portion of the queued requests |
26 // until all such requests have been flushed. | 28 // until all such requests have been flushed. |
27 // TODO(jdduke): Remove this class after resource requests become sufficiently | 29 // TODO(jdduke): Remove this class after resource requests become sufficiently |
28 // cheap on the IO thread, crbug.com/440037. | 30 // cheap on the IO thread, crbug.com/440037. |
29 class CONTENT_EXPORT ResourceDispatchThrottler : public IPC::Sender { | 31 class CONTENT_EXPORT ResourceDispatchThrottler : public IPC::Sender { |
30 public: | 32 public: |
31 // |flush_period| and |max_requests_per_flush| must be strictly positive | 33 // |flush_period| and |max_requests_per_flush| must be strictly positive |
32 // in duration/value. | 34 // in duration/value. |
33 ResourceDispatchThrottler(IPC::Sender* proxied_sender, | 35 ResourceDispatchThrottler(IPC::Sender* proxied_sender, |
34 RendererScheduler* scheduler, | 36 scheduler::RendererScheduler* scheduler, |
35 base::TimeDelta flush_period, | 37 base::TimeDelta flush_period, |
36 uint32 max_requests_per_flush); | 38 uint32 max_requests_per_flush); |
37 ~ResourceDispatchThrottler() override; | 39 ~ResourceDispatchThrottler() override; |
38 | 40 |
39 // IPC::Sender implementation: | 41 // IPC::Sender implementation: |
40 bool Send(IPC::Message* msg) override; | 42 bool Send(IPC::Message* msg) override; |
41 | 43 |
42 private: | 44 private: |
43 friend class ResourceDispatchThrottlerForTest; | 45 friend class ResourceDispatchThrottlerForTest; |
44 | 46 |
45 // Virtual for testing. | 47 // Virtual for testing. |
46 virtual base::TimeTicks Now() const; | 48 virtual base::TimeTicks Now() const; |
47 virtual void ScheduleFlush(); | 49 virtual void ScheduleFlush(); |
48 | 50 |
49 void Flush(); | 51 void Flush(); |
50 void FlushAll(); | 52 void FlushAll(); |
51 bool ForwardMessage(IPC::Message* msg); | 53 bool ForwardMessage(IPC::Message* msg); |
52 | 54 |
53 base::ThreadChecker thread_checker_; | 55 base::ThreadChecker thread_checker_; |
54 | 56 |
55 IPC::Sender* const proxied_sender_; | 57 IPC::Sender* const proxied_sender_; |
56 RendererScheduler* const scheduler_; | 58 scheduler::RendererScheduler* const scheduler_; |
57 const base::TimeDelta flush_period_; | 59 const base::TimeDelta flush_period_; |
58 const uint32 max_requests_per_flush_; | 60 const uint32 max_requests_per_flush_; |
59 | 61 |
60 base::Timer flush_timer_; | 62 base::Timer flush_timer_; |
61 base::TimeTicks last_sent_request_time_; | 63 base::TimeTicks last_sent_request_time_; |
62 uint32 sent_requests_since_last_flush_; | 64 uint32 sent_requests_since_last_flush_; |
63 std::deque<IPC::Message*> throttled_messages_; | 65 std::deque<IPC::Message*> throttled_messages_; |
64 | 66 |
65 DISALLOW_COPY_AND_ASSIGN(ResourceDispatchThrottler); | 67 DISALLOW_COPY_AND_ASSIGN(ResourceDispatchThrottler); |
66 }; | 68 }; |
67 | 69 |
68 } // namespace content | 70 } // namespace content |
69 | 71 |
70 #endif // CONTENT_RENDERER_SCHEDULER_RESOURCE_DISPATCH_THROTTLER_H_ | 72 #endif // CONTENT_RENDERER_SCHEDULER_RESOURCE_DISPATCH_THROTTLER_H_ |
OLD | NEW |