Index: content/renderer/resource_dispatch_throttler.h |
diff --git a/content/renderer/resource_dispatch_throttler.h b/content/renderer/resource_dispatch_throttler.h |
new file mode 100644 |
index 0000000000000000000000000000000000000000..44defbcc1e8dff0624003ad8caf69e543a271a20 |
--- /dev/null |
+++ b/content/renderer/resource_dispatch_throttler.h |
@@ -0,0 +1,74 @@ |
+// Copyright 2015 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#ifndef CONTENT_RENDERER_RESOURCE_DISPATCH_THROTTLER_H_ |
+#define CONTENT_RENDERER_RESOURCE_DISPATCH_THROTTLER_H_ |
+ |
+#include <map> |
+ |
+#include "base/threading/thread_checker.h" |
+#include "base/time/time.h" |
+#include "base/timer/timer.h" |
+#include "content/common/content_export.h" |
+#include "ipc/ipc_sender.h" |
+ |
+namespace content { |
+ |
+class RendererScheduler; |
+ |
+// Utility class for throttling a stream of resource requests targetted to a |
+// specific IPC sender. The throttling itself is very basic: |
+// * When there is no high-priority work imminent to the main thread, as |
+// indicated by the RendererScheduler, throttling is disabled. |
+// * When >= N requests have been sent in a given time window, requests are |
+// throttled. A timer periodically flushes a portion of the queued requests |
+// until all such requests have been flushed. |
+// TODO(jdduke): Remove this class after resource requests become sufficiently |
+// cheap on the IO thread, crbug.com/440037. |
+class CONTENT_EXPORT ResourceDispatchThrottler : public IPC::Sender { |
Sami
2015/01/27 14:06:06
Should this live in content/renderer/scheduler? It
jdduke (slow)
2015/01/27 16:59:09
Yeah, I had it there originally, and I'd be happy
|
+ public: |
+ // |flush_period| and |max_requests_per_flush| must be strictly positive |
+ // in duration/value. |
+ ResourceDispatchThrottler(IPC::Sender* proxied_sender, |
+ RendererScheduler* scheduler, |
+ base::TimeDelta flush_period, |
+ uint32 max_requests_per_flush); |
+ ~ResourceDispatchThrottler() override; |
+ |
+ // IPC::Sender implementation: |
+ bool Send(IPC::Message* msg) override; |
+ |
+ private: |
+ friend class ResourceDispatchThrottlerTest; |
+ |
+ // Virtual for testing. |
+ virtual base::TimeTicks Now() const; |
+ virtual void ScheduleFlush(); |
+ |
+ void Flush(); |
+ bool ForwardRequest(IPC::Message* msg); |
+ bool OnRequestResource(IPC::Message* msg); |
+ bool OnDidChangePriority(IPC::Message* msg); |
+ bool OnReleaseDownloadedFile(IPC::Message* msg); |
+ bool OnCancelRequest(IPC::Message* msg); |
+ |
+ base::ThreadChecker thread_checker_; |
+ |
+ IPC::Sender* const proxied_sender_; |
+ RendererScheduler* const scheduler_; |
+ const base::TimeDelta flush_period_; |
+ const uint32 max_requests_per_flush_; |
+ |
+ base::Timer flush_timer_; |
+ base::TimeTicks last_sent_request_time_; |
+ uint32 sent_requests_since_last_flush_; |
+ std::map<int, IPC::Message*> throttled_requests_; |
+ bool is_forwarding_request_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(ResourceDispatchThrottler); |
+}; |
+ |
+} // namespace content |
+ |
+#endif // CONTENT_RENDERER_RESOURCE_DISPATCH_THROTTLER_H_ |