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 #include "net/base/network_stream_throttler.h" |
| 6 |
| 7 namespace net { |
| 8 |
| 9 namespace { |
| 10 |
| 11 class NetworkStreamThrottlerImpl : public NetworkStreamThrottler { |
| 12 public: |
| 13 class ThrottleImpl : public NetworkStreamThrottler::Throttle { |
| 14 public: |
| 15 ~ThrottleImpl() override; |
| 16 |
| 17 // Throttle |
| 18 bool IsThrottled() const override { return throttled_; } |
| 19 void SetPriority(RequestPriority priority) override; |
| 20 |
| 21 private: |
| 22 friend NetworkStreamThrottlerImpl; |
| 23 |
| 24 using QueuePointer = PriorityQueue<ThrottleImpl*>::Pointer; |
| 25 |
| 26 // Caller must arrange that |*delegate| and |*throttler| outlives |
| 27 // the ThrottleImpl class. |
| 28 ThrottleImpl(bool throttled, |
| 29 RequestPriority priority, |
| 30 Delegate* delegate, |
| 31 NetworkStreamThrottlerImpl* throttler); |
| 32 |
| 33 QueuePointer queue_pointer() const { return queue_pointer_; } |
| 34 void set_queue_pointer(const QueuePointer& pointer) { |
| 35 queue_pointer_ = pointer; |
| 36 } |
| 37 |
| 38 // Note that this call calls the delegate, and hence |
| 39 // may result in re-entrant calls into the throtter or |
| 40 // ThrottleImpl. The throttler should not rely on |
| 41 // any state other than its own existence being persistent |
| 42 // across this call. |
| 43 void NotifyUnthrottled(); |
| 44 |
| 45 bool throttled_; |
| 46 Delegate* const delegate_; |
| 47 PriorityQueue<ThrottleImpl*>::Pointer queue_pointer_; |
| 48 |
| 49 NetworkStreamThrottlerImpl* const throttler_; |
| 50 |
| 51 DISALLOW_COPY_AND_ASSIGN(ThrottleImpl); |
| 52 }; |
| 53 |
| 54 NetworkStreamThrottlerImpl(); |
| 55 ~NetworkStreamThrottlerImpl() override; |
| 56 |
| 57 std::unique_ptr<Throttle> CreateThrottle(Delegate* delegate, |
| 58 RequestPriority priority, |
| 59 bool ignore_limits) override; |
| 60 |
| 61 private: |
| 62 void OnStreamPriorityChanged(ThrottleImpl* throttle, |
| 63 RequestPriority new_priority); |
| 64 void OnStreamDestroyed(ThrottleImpl* throttle); |
| 65 |
| 66 PriorityQueue<ThrottleImpl*> priority_queue_; |
| 67 |
| 68 DISALLOW_COPY_AND_ASSIGN(NetworkStreamThrottlerImpl); |
| 69 }; |
| 70 |
| 71 // Currently this is a null implementation that does no throttling; |
| 72 // all entries are created in the unthrottled state, and no throttle state |
| 73 // change notifications are transmitted. |
| 74 |
| 75 NetworkStreamThrottlerImpl::ThrottleImpl::~ThrottleImpl() { |
| 76 if (throttler_) |
| 77 throttler_->OnStreamDestroyed(this); |
| 78 } |
| 79 |
| 80 void NetworkStreamThrottlerImpl::ThrottleImpl::SetPriority( |
| 81 RequestPriority priority) { |
| 82 if (throttler_) |
| 83 throttler_->OnStreamPriorityChanged(this, priority); |
| 84 } |
| 85 |
| 86 NetworkStreamThrottlerImpl::ThrottleImpl::ThrottleImpl( |
| 87 bool throttled, |
| 88 RequestPriority priority, |
| 89 NetworkStreamThrottler::Delegate* delegate, |
| 90 NetworkStreamThrottlerImpl* throttler) |
| 91 : throttled_(throttled), delegate_(delegate), throttler_(throttler) {} |
| 92 |
| 93 void NetworkStreamThrottlerImpl::ThrottleImpl::NotifyUnthrottled() { |
| 94 throttled_ = false; |
| 95 // TODO(rdsmith): Figure out if/how this needs to be made asynchronous. |
| 96 if (delegate_) |
| 97 delegate_->OnThrottleStateChanged(); |
| 98 } |
| 99 |
| 100 NetworkStreamThrottlerImpl::NetworkStreamThrottlerImpl() |
| 101 : priority_queue_(MAXIMUM_PRIORITY + 1) {} |
| 102 |
| 103 NetworkStreamThrottlerImpl::~NetworkStreamThrottlerImpl() {} |
| 104 |
| 105 std::unique_ptr<NetworkStreamThrottler::Throttle> |
| 106 NetworkStreamThrottlerImpl::CreateThrottle( |
| 107 NetworkStreamThrottler::Delegate* delegate, |
| 108 RequestPriority priority, |
| 109 bool ignore_limits) { |
| 110 std::unique_ptr<NetworkStreamThrottlerImpl::ThrottleImpl> stream( |
| 111 new ThrottleImpl(false, priority, delegate, this)); |
| 112 |
| 113 stream->set_queue_pointer(priority_queue_.Insert(stream.get(), priority)); |
| 114 |
| 115 return std::move(stream); |
| 116 } |
| 117 |
| 118 void NetworkStreamThrottlerImpl::OnStreamPriorityChanged( |
| 119 NetworkStreamThrottlerImpl::ThrottleImpl* stream, |
| 120 RequestPriority new_priority) { |
| 121 priority_queue_.Erase(stream->queue_pointer()); |
| 122 stream->set_queue_pointer(priority_queue_.Insert(stream, new_priority)); |
| 123 } |
| 124 |
| 125 void NetworkStreamThrottlerImpl::OnStreamDestroyed(ThrottleImpl* stream) { |
| 126 priority_queue_.Erase(stream->queue_pointer()); |
| 127 } |
| 128 |
| 129 } // namespace |
| 130 |
| 131 // static |
| 132 std::unique_ptr<NetworkStreamThrottler> |
| 133 NetworkStreamThrottler::CreateThrottler() { |
| 134 return std::unique_ptr<NetworkStreamThrottler>( |
| 135 new NetworkStreamThrottlerImpl); |
| 136 } |
| 137 |
| 138 } // namespace net |
OLD | NEW |