OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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 NET_BASE_NETWORK_STREAM_THROTTLER_H_ |
| 6 #define NET_BASE_NETWORK_STREAM_THROTTLER_H_ |
| 7 |
| 8 #include "base/memory/weak_ptr.h" |
| 9 #include "net/base/priority_queue.h" |
| 10 #include "net/base/request_priority.h" |
| 11 |
| 12 namespace net { |
| 13 |
| 14 class NetworkStreamThrottler { |
| 15 public: |
| 16 // Abstract base class other classes can inherit from to get |
| 17 // notifications from throttle state changes. |
| 18 class Delegate { |
| 19 public: |
| 20 virtual ~Delegate() {} |
| 21 |
| 22 // Called whenever the throttle state of this stream has changed. |
| 23 // The new state can be determined through Throttle::throttled(). |
| 24 virtual void OnThrottleStateChanged() = 0; |
| 25 }; |
| 26 |
| 27 // Concrete class owned by external stream representations that |
| 28 // routes notifications. |
| 29 class Throttle { |
| 30 public: |
| 31 ~Throttle(); |
| 32 |
| 33 bool throttled() { return throttled_; } |
| 34 |
| 35 // The caller must make sure that |*delegate| outlives this |
| 36 // object. |
| 37 void set_delegate(Delegate* delegate) { delegate_ = delegate; } |
| 38 |
| 39 // Note that this may result in a possibly reentrant call to |
| 40 // |Delegate::OnThrottleStateChanged|. |
| 41 void SetPriority(RequestPriority priority); |
| 42 |
| 43 private: |
| 44 friend NetworkStreamThrottler; |
| 45 |
| 46 using QueuePointer = PriorityQueue<Throttle*>::Pointer; |
| 47 |
| 48 Throttle(bool throttled, |
| 49 RequestPriority priority, |
| 50 base::WeakPtr<NetworkStreamThrottler> throttler); |
| 51 |
| 52 void NotifyThrottleStateChanged(bool new_throttle_state); |
| 53 QueuePointer queue_pointer() { return queue_pointer_; } |
| 54 void set_queue_pointer(const QueuePointer& pointer) { |
| 55 queue_pointer_ = pointer; |
| 56 } |
| 57 |
| 58 bool throttled_; |
| 59 RequestPriority priority_; |
| 60 Delegate* delegate_; |
| 61 PriorityQueue<Throttle*>::Pointer queue_pointer_; |
| 62 |
| 63 base::WeakPtr<NetworkStreamThrottler> throttler_; |
| 64 |
| 65 DISALLOW_COPY_AND_ASSIGN(Throttle); |
| 66 }; |
| 67 |
| 68 NetworkStreamThrottler(); |
| 69 ~NetworkStreamThrottler(); |
| 70 |
| 71 scoped_ptr<Throttle> CreateThrottle(RequestPriority priority); |
| 72 |
| 73 private: |
| 74 void OnStreamPriorityChanged(Throttle* throttle, |
| 75 RequestPriority old_priority, |
| 76 RequestPriority new_priority); |
| 77 void OnStreamDestroyed(Throttle* throttle); |
| 78 |
| 79 PriorityQueue<Throttle*> priority_queue_; |
| 80 base::WeakPtrFactory<NetworkStreamThrottler> factory_; |
| 81 |
| 82 DISALLOW_COPY_AND_ASSIGN(NetworkStreamThrottler); |
| 83 }; |
| 84 |
| 85 } // namespace net |
| 86 |
| 87 #endif // NET_BASE_NETWORK_STREAM_THROTTLER_H_ |
OLD | NEW |