| 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_throttle_manager.h" | |
| 6 | |
| 7 #include "base/logging.h" | |
| 8 #include "net/base/priority_queue.h" | |
| 9 | |
| 10 namespace net { | |
| 11 | |
| 12 namespace { | |
| 13 | |
| 14 class NetworkThrottleManagerImpl : public NetworkThrottleManager { | |
| 15 public: | |
| 16 class ThrottleImpl : public NetworkThrottleManager::Throttle { | |
| 17 public: | |
| 18 using QueuePointer = PriorityQueue<ThrottleImpl*>::Pointer; | |
| 19 | |
| 20 // Caller must arrange that |*delegate| and |*throttler| outlive | |
| 21 // the ThrottleImpl class. | |
| 22 ThrottleImpl(bool throttled, | |
| 23 RequestPriority priority, | |
| 24 ThrottleDelegate* delegate, | |
| 25 NetworkThrottleManagerImpl* throttler); | |
| 26 | |
| 27 ~ThrottleImpl() override; | |
| 28 | |
| 29 // Throttle | |
| 30 bool IsThrottled() const override; | |
| 31 void SetPriority(RequestPriority priority) override; | |
| 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 throttler 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 private: | |
| 46 bool throttled_; | |
| 47 ThrottleDelegate* const delegate_; | |
| 48 PriorityQueue<ThrottleImpl*>::Pointer queue_pointer_; | |
| 49 | |
| 50 NetworkThrottleManagerImpl* const throttler_; | |
| 51 | |
| 52 DISALLOW_COPY_AND_ASSIGN(ThrottleImpl); | |
| 53 }; | |
| 54 | |
| 55 NetworkThrottleManagerImpl(); | |
| 56 ~NetworkThrottleManagerImpl() override; | |
| 57 | |
| 58 std::unique_ptr<Throttle> CreateThrottle(ThrottleDelegate* delegate, | |
| 59 RequestPriority priority, | |
| 60 bool ignore_limits) override; | |
| 61 | |
| 62 private: | |
| 63 void OnStreamPriorityChanged(ThrottleImpl* throttle, | |
| 64 RequestPriority new_priority); | |
| 65 void OnStreamDestroyed(ThrottleImpl* throttle); | |
| 66 | |
| 67 PriorityQueue<ThrottleImpl*> priority_queue_; | |
| 68 | |
| 69 DISALLOW_COPY_AND_ASSIGN(NetworkThrottleManagerImpl); | |
| 70 }; | |
| 71 | |
| 72 // Currently this is a null implementation that does no throttling; | |
| 73 // all entries are created in the unthrottled state, and no throttle state | |
| 74 // change notifications are transmitted. | |
| 75 | |
| 76 NetworkThrottleManagerImpl::ThrottleImpl::ThrottleImpl( | |
| 77 bool throttled, | |
| 78 RequestPriority priority, | |
| 79 NetworkThrottleManager::ThrottleDelegate* delegate, | |
| 80 NetworkThrottleManagerImpl* throttler) | |
| 81 : throttled_(throttled), delegate_(delegate), throttler_(throttler) { | |
| 82 DCHECK(delegate); | |
| 83 } | |
| 84 | |
| 85 NetworkThrottleManagerImpl::ThrottleImpl::~ThrottleImpl() { | |
| 86 throttler_->OnStreamDestroyed(this); | |
| 87 } | |
| 88 | |
| 89 void NetworkThrottleManagerImpl::ThrottleImpl::SetPriority( | |
| 90 RequestPriority priority) { | |
| 91 throttler_->OnStreamPriorityChanged(this, priority); | |
| 92 } | |
| 93 | |
| 94 bool NetworkThrottleManagerImpl::ThrottleImpl::IsThrottled() const { | |
| 95 return throttled_; | |
| 96 } | |
| 97 | |
| 98 void NetworkThrottleManagerImpl::ThrottleImpl::NotifyUnthrottled() { | |
| 99 // This methods should only be called once, and only if the | |
| 100 // current state is throttled. | |
| 101 DCHECK(throttled_); | |
| 102 throttled_ = false; | |
| 103 delegate_->OnThrottleStateChanged(); | |
| 104 } | |
| 105 | |
| 106 NetworkThrottleManagerImpl::NetworkThrottleManagerImpl() | |
| 107 : priority_queue_(MAXIMUM_PRIORITY + 1) {} | |
| 108 | |
| 109 NetworkThrottleManagerImpl::~NetworkThrottleManagerImpl() {} | |
| 110 | |
| 111 std::unique_ptr<NetworkThrottleManager::Throttle> | |
| 112 NetworkThrottleManagerImpl::CreateThrottle( | |
| 113 NetworkThrottleManager::ThrottleDelegate* delegate, | |
| 114 RequestPriority priority, | |
| 115 bool ignore_limits) { | |
| 116 std::unique_ptr<NetworkThrottleManagerImpl::ThrottleImpl> stream( | |
| 117 new ThrottleImpl(false, priority, delegate, this)); | |
| 118 | |
| 119 stream->set_queue_pointer(priority_queue_.Insert(stream.get(), priority)); | |
| 120 | |
| 121 return std::move(stream); | |
| 122 } | |
| 123 | |
| 124 void NetworkThrottleManagerImpl::OnStreamPriorityChanged( | |
| 125 NetworkThrottleManagerImpl::ThrottleImpl* stream, | |
| 126 RequestPriority new_priority) { | |
| 127 priority_queue_.Erase(stream->queue_pointer()); | |
| 128 stream->set_queue_pointer(priority_queue_.Insert(stream, new_priority)); | |
| 129 } | |
| 130 | |
| 131 void NetworkThrottleManagerImpl::OnStreamDestroyed(ThrottleImpl* stream) { | |
| 132 priority_queue_.Erase(stream->queue_pointer()); | |
| 133 } | |
| 134 | |
| 135 } // namespace | |
| 136 | |
| 137 // static | |
| 138 std::unique_ptr<NetworkThrottleManager> | |
| 139 NetworkThrottleManager::CreateThrottler() { | |
| 140 return std::unique_ptr<NetworkThrottleManager>( | |
| 141 new NetworkThrottleManagerImpl); | |
| 142 } | |
| 143 | |
| 144 } // namespace net | |
| OLD | NEW |