Index: net/base/network_stream_throttler.h |
diff --git a/net/base/network_stream_throttler.h b/net/base/network_stream_throttler.h |
new file mode 100644 |
index 0000000000000000000000000000000000000000..75ee556088cfd76668d7fa8f0863ec48f76cd565 |
--- /dev/null |
+++ b/net/base/network_stream_throttler.h |
@@ -0,0 +1,87 @@ |
+// Copyright 2016 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 NET_BASE_NETWORK_STREAM_THROTTLER_H_ |
+#define NET_BASE_NETWORK_STREAM_THROTTLER_H_ |
+ |
+#include "base/memory/weak_ptr.h" |
+#include "net/base/priority_queue.h" |
+#include "net/base/request_priority.h" |
+ |
+namespace net { |
+ |
+class NetworkStreamThrottler { |
+ public: |
+ // Abstract base class other classes can inherit from to get |
+ // notifications from throttle state changes. |
+ class Delegate { |
+ public: |
+ virtual ~Delegate() {} |
+ |
+ // Called whenever the throttle state of this stream has changed. |
+ // The new state can be determined through Throttle::throttled(). |
+ virtual void OnThrottleStateChanged() = 0; |
+ }; |
+ |
+ // Concrete class owned by external stream representations that |
+ // routes notifications. |
+ class Throttle { |
+ public: |
+ ~Throttle(); |
+ |
+ bool throttled() { return throttled_; } |
+ |
+ // The caller must make sure that |*delegate| outlives this |
+ // object. |
+ void set_delegate(Delegate* delegate) { delegate_ = delegate; } |
+ |
+ // Note that this may result in a possibly reentrant call to |
+ // |Delegate::OnThrottleStateChanged|. |
+ void SetPriority(RequestPriority priority); |
+ |
+ private: |
+ friend NetworkStreamThrottler; |
+ |
+ using QueuePointer = PriorityQueue<Throttle*>::Pointer; |
+ |
+ Throttle(bool throttled, |
+ RequestPriority priority, |
+ base::WeakPtr<NetworkStreamThrottler> throttler); |
+ |
+ void NotifyThrottleStateChanged(bool new_throttle_state); |
+ QueuePointer queue_pointer() { return queue_pointer_; } |
+ void set_queue_pointer(const QueuePointer& pointer) { |
+ queue_pointer_ = pointer; |
+ } |
+ |
+ bool throttled_; |
+ RequestPriority priority_; |
+ Delegate* delegate_; |
+ PriorityQueue<Throttle*>::Pointer queue_pointer_; |
+ |
+ base::WeakPtr<NetworkStreamThrottler> throttler_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(Throttle); |
+ }; |
+ |
+ NetworkStreamThrottler(); |
+ ~NetworkStreamThrottler(); |
+ |
+ scoped_ptr<Throttle> CreateThrottle(RequestPriority priority); |
+ |
+ private: |
+ void OnStreamPriorityChanged(Throttle* throttle, |
+ RequestPriority old_priority, |
+ RequestPriority new_priority); |
+ void OnStreamDestroyed(Throttle* throttle); |
+ |
+ PriorityQueue<Throttle*> priority_queue_; |
+ base::WeakPtrFactory<NetworkStreamThrottler> factory_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(NetworkStreamThrottler); |
+}; |
+ |
+} // namespace net |
+ |
+#endif // NET_BASE_NETWORK_STREAM_THROTTLER_H_ |