OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #ifndef CONTENT_RENDERER_SCHEDULER_THROTTLED_MESSAGE_SENDER_H_ |
| 6 #define CONTENT_RENDERER_SCHEDULER_THROTTLED_MESSAGE_SENDER_H_ |
| 7 |
| 8 #include <deque> |
| 9 |
| 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" |
| 15 |
| 16 namespace content { |
| 17 |
| 18 class RendererScheduler; |
| 19 |
| 20 // Utility class for throttling a stream of IPC messages targetted to a specific |
| 21 // IPC sender. The throttling itself is very basic: |
| 22 // * When there is no high-priority work on the main thread, as indicated by |
| 23 // the RendererScheduler, throttling is disabled. |
| 24 // * When >= N messages have been sent in a given time window, messages are |
| 25 // queued. A timer periodically flushes a portion of the queued messages |
| 26 // until all messages have been flushed. |
| 27 // * If a sync IPC is received, all queued messages are flushed immediately. |
| 28 // |
| 29 // This simplistic approach is particularly useful for IPC types that can |
| 30 // 1) be sent at a very high rate (multiple messages per frame) and |
| 31 // 2) interrupt page responsiveness due to the induced work, particularly on |
| 32 // the receiver's IO thread. |
| 33 // Such is the case, for example, for ResourceHostMsg_RequestResource messages. |
| 34 class CONTENT_EXPORT ThrottledMessageSender : public IPC::Sender { |
| 35 public: |
| 36 // |max_sent_messages_per_second| must be strictly positive. |
| 37 ThrottledMessageSender(IPC::Sender* proxied_sender, |
| 38 RendererScheduler* scheduler, |
| 39 int max_sent_messages_per_second); |
| 40 ~ThrottledMessageSender() override; |
| 41 |
| 42 // IPC::Sender implementation: |
| 43 bool Send(IPC::Message* msg) override; |
| 44 |
| 45 protected: |
| 46 // Virtual for testing. |
| 47 virtual base::TimeTicks Now() const; |
| 48 |
| 49 private: |
| 50 friend class ThrottledMessageSenderTest; |
| 51 |
| 52 void FlushAll(); |
| 53 void FlushThrottled(); |
| 54 bool SendViaProxiedSender(IPC::Message* msg); |
| 55 |
| 56 base::ThreadChecker thread_checker_; |
| 57 IPC::Sender* const proxied_sender_; |
| 58 RendererScheduler* const scheduler_; |
| 59 |
| 60 const int max_sent_messages_per_flush_; |
| 61 const base::TimeDelta flush_period_; |
| 62 base::TimeTicks last_sent_message_time_; |
| 63 int sent_messages_since_last_flush_; |
| 64 base::Timer flush_timer_; |
| 65 |
| 66 std::deque<IPC::Message*> throttled_messages_; |
| 67 bool is_sending_via_proxied_; |
| 68 |
| 69 DISALLOW_COPY_AND_ASSIGN(ThrottledMessageSender); |
| 70 }; |
| 71 |
| 72 } // namespace content |
| 73 |
| 74 #endif // CONTENT_RENDERER_SCHEDULER_THROTTLED_MESSAGE_SENDER_H_ |
OLD | NEW |