| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef NET_BASE_NETWORK_THROTTLE_MANAGER_H_ | 5 #ifndef NET_BASE_NETWORK_THROTTLE_MANAGER_H_ |
| 6 #define NET_BASE_NETWORK_THROTTLE_MANAGER_H_ | 6 #define NET_BASE_NETWORK_THROTTLE_MANAGER_H_ |
| 7 | 7 |
| 8 #include <memory> | 8 #include <memory> |
| 9 | 9 |
| 10 #include "base/memory/weak_ptr.h" | 10 #include "base/memory/weak_ptr.h" |
| 11 #include "net/base/net_export.h" | 11 #include "net/base/net_export.h" |
| 12 #include "net/base/request_priority.h" | 12 #include "net/base/request_priority.h" |
| 13 | 13 |
| 14 namespace net { | 14 namespace net { |
| 15 | 15 |
| 16 // This class controls throttling based on priority level and number of | 16 // This class controls throttling based on priority level and number of |
| 17 // outstanding requests. It vends Throttle objects, and tracks | 17 // outstanding requests. It vends Throttle objects, and tracks |
| 18 // outstanding requests by the lifetime of those objects. Consumers | 18 // outstanding requests by the lifetime of those objects. Consumers |
| 19 // determine whether or not they are throttled by consulting those | 19 // determine whether or not they are throttled by consulting those |
| 20 // Throttle objects. | 20 // Throttle objects. |
| 21 // | 21 // |
| 22 // This class must outlive all Throttles created from it via CreateThrottle(). | 22 // This class must outlive all Throttles created from it via CreateThrottle(). |
| 23 // | 23 // |
| 24 // Methods are virtual to allow for test mocks. | 24 // Methods are virtual to allow for test mocks. |
| 25 class NET_EXPORT_PRIVATE NetworkThrottleManager { | 25 class NET_EXPORT_PRIVATE NetworkThrottleManager { |
| 26 public: | 26 public: |
| 27 class Throttle; | |
| 28 | |
| 29 // Abstract base class other classes can inherit from to get | 27 // Abstract base class other classes can inherit from to get |
| 30 // notifications from throttle state changes. | 28 // notifications from throttle state changes. |
| 31 class NET_EXPORT_PRIVATE ThrottleDelegate { | 29 class NET_EXPORT_PRIVATE ThrottleDelegate { |
| 32 public: | 30 public: |
| 33 // Called when a throttle is unblocked. | 31 // Called whenever the throttle state of this stream has changed. |
| 32 // The new state can be determined through Throttle::IsThrottled(). |
| 34 // | 33 // |
| 35 // Note that this call may occur as the result of either a call to | 34 // Note that this call may occur as the result of either a call to |
| 36 // Throttle::SetPriority (on the throttle related to this delegate | 35 // Throttle::SetPriority (on the throttle related to this delegate |
| 37 // or another throttle) or the destruction of a Throttle, and if | 36 // or another throttle) or the destruction of a Throttle, and if |
| 38 // so will occur synchronously during those events. It will not | 37 // so will occur synchronously during those events. It will not |
| 39 // be called from the destructor of the Throttle associated with | 38 // be called from the destructor of the Throttle associated with |
| 40 // the ThrottleDelegate. | 39 // the ThrottleDelegate. |
| 41 virtual void OnThrottleUnblocked(Throttle* throttle) = 0; | 40 virtual void OnThrottleStateChanged() = 0; |
| 42 | 41 |
| 43 protected: | 42 protected: |
| 44 virtual ~ThrottleDelegate() {} | 43 virtual ~ThrottleDelegate() {} |
| 45 }; | 44 }; |
| 46 | 45 |
| 47 // Class owned by external stream representations that | 46 // Class owned by external stream representations that |
| 48 // routes notifications. It may be constructed in either the | 47 // routes notifications. It may be constructed in either the |
| 49 // blocked or unblocked state according to the state of the | 48 // throttled or unthrottled state according to the state of the |
| 50 // NetworkThrottleManager; if it's constructed in the unblocked | 49 // NetworkThrottleManager; if it's constructed in the throttled |
| 51 // state, it will only make a single transition to unblocked, | 50 // state, it will only make a single transition to unthrottled, |
| 52 // which will be signaled by delegate->OnThrottleUnblocked(this). | 51 // which will be signaled by delegate->OnThrottleStateChanged(). |
| 53 // If it's constructed in the unblocked state, it will remain | 52 // If it's constructed in the unthrottled state, it will remain |
| 54 // there. | 53 // there. |
| 55 class NET_EXPORT_PRIVATE Throttle { | 54 class NET_EXPORT_PRIVATE Throttle { |
| 56 public: | 55 public: |
| 57 virtual ~Throttle() {} | 56 virtual ~Throttle() {} |
| 58 | 57 |
| 59 virtual bool IsBlocked() const = 0; | 58 virtual bool IsThrottled() const = 0; |
| 60 | |
| 61 virtual RequestPriority Priority() const = 0; | |
| 62 | 59 |
| 63 // Note that this may result in a possibly reentrant call to | 60 // Note that this may result in a possibly reentrant call to |
| 64 // |ThrottleDelegate::OnThrottleUnblocked|, as well as the resumption | 61 // |ThrottleDelegate::OnThrottleStateChanged|, as well as the resumption |
| 65 // of this or other requests, which may result in request completion | 62 // of this or other requests, which may result in request completion |
| 66 // and destruction before return. Any caller of this function | 63 // and destruction before return. Any caller of this function |
| 67 // should not rely on this object or containing objects surviving | 64 // should not rely on this object or containing objects surviving |
| 68 // this call. | 65 // this call. |
| 69 // | |
| 70 // This call is a no-op if the priority is set to its current value. | |
| 71 virtual void SetPriority(RequestPriority priority) = 0; | 66 virtual void SetPriority(RequestPriority priority) = 0; |
| 72 | 67 |
| 73 protected: | 68 protected: |
| 74 Throttle() {} | 69 Throttle() {} |
| 75 | 70 |
| 76 private: | 71 private: |
| 77 DISALLOW_COPY_AND_ASSIGN(Throttle); | 72 DISALLOW_COPY_AND_ASSIGN(Throttle); |
| 78 }; | 73 }; |
| 79 | 74 |
| 80 virtual ~NetworkThrottleManager() {} | 75 virtual ~NetworkThrottleManager() {} |
| 81 | 76 |
| 82 // Caller must ensure that |*delegate| outlives the returned | 77 // Caller must ensure that |*delegate| outlives the returned |
| 83 // Throttle. | 78 // Throttle. |
| 84 virtual std::unique_ptr<Throttle> CreateThrottle(ThrottleDelegate* delegate, | 79 virtual std::unique_ptr<Throttle> CreateThrottle(ThrottleDelegate* delegate, |
| 85 RequestPriority priority, | 80 RequestPriority priority, |
| 86 bool ignore_limits) = 0; | 81 bool ignore_limits) = 0; |
| 87 | 82 |
| 83 static std::unique_ptr<NetworkThrottleManager> CreateThrottler(); |
| 84 |
| 88 protected: | 85 protected: |
| 89 NetworkThrottleManager() {} | 86 NetworkThrottleManager() {} |
| 90 | 87 |
| 91 private: | 88 private: |
| 92 DISALLOW_COPY_AND_ASSIGN(NetworkThrottleManager); | 89 DISALLOW_COPY_AND_ASSIGN(NetworkThrottleManager); |
| 93 }; | 90 }; |
| 94 | 91 |
| 95 } // namespace net | 92 } // namespace net |
| 96 | 93 |
| 97 #endif // NET_BASE_NETWORK_THROTTLE_MANAGER_H_ | 94 #endif // NET_BASE_NETWORK_THROTTLE_MANAGER_H_ |
| OLD | NEW |