| 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 |
| 27 // Abstract base class other classes can inherit from to get | 29 // Abstract base class other classes can inherit from to get |
| 28 // notifications from throttle state changes. | 30 // notifications from throttle state changes. |
| 29 class NET_EXPORT_PRIVATE ThrottleDelegate { | 31 class NET_EXPORT_PRIVATE ThrottleDelegate { |
| 30 public: | 32 public: |
| 31 // Called whenever the throttle state of this stream has changed. | 33 // Called when a throttle is unblocked. |
| 32 // The new state can be determined through Throttle::IsThrottled(). | |
| 33 // | 34 // |
| 34 // Note that this call may occur as the result of either a call to | 35 // Note that this call may occur as the result of either a call to |
| 35 // Throttle::SetPriority (on the throttle related to this delegate | 36 // Throttle::SetPriority (on the throttle related to this delegate |
| 36 // or another throttle) or the destruction of a Throttle, and if | 37 // or another throttle) or the destruction of a Throttle, and if |
| 37 // so will occur synchronously during those events. It will not | 38 // so will occur synchronously during those events. It will not |
| 38 // be called from the destructor of the Throttle associated with | 39 // be called from the destructor of the Throttle associated with |
| 39 // the ThrottleDelegate. | 40 // the ThrottleDelegate. |
| 40 virtual void OnThrottleStateChanged() = 0; | 41 virtual void OnThrottleUnblocked(Throttle* throttle) = 0; |
| 41 | 42 |
| 42 protected: | 43 protected: |
| 43 virtual ~ThrottleDelegate() {} | 44 virtual ~ThrottleDelegate() {} |
| 44 }; | 45 }; |
| 45 | 46 |
| 46 // Class owned by external stream representations that | 47 // Class owned by external stream representations that |
| 47 // routes notifications. It may be constructed in either the | 48 // routes notifications. It may be constructed in either the |
| 48 // throttled or unthrottled state according to the state of the | 49 // blocked or unblocked state according to the state of the |
| 49 // NetworkThrottleManager; if it's constructed in the throttled | 50 // NetworkThrottleManager; if it's constructed in the unblocked |
| 50 // state, it will only make a single transition to unthrottled, | 51 // state, it will only make a single transition to unblocked, |
| 51 // which will be signaled by delegate->OnThrottleStateChanged(). | 52 // which will be signaled by delegate->OnThrottleUnblocked(this). |
| 52 // If it's constructed in the unthrottled state, it will remain | 53 // If it's constructed in the unblocked state, it will remain |
| 53 // there. | 54 // there. |
| 54 class NET_EXPORT_PRIVATE Throttle { | 55 class NET_EXPORT_PRIVATE Throttle { |
| 55 public: | 56 public: |
| 56 virtual ~Throttle() {} | 57 virtual ~Throttle() {} |
| 57 | 58 |
| 58 virtual bool IsThrottled() const = 0; | 59 virtual bool IsBlocked() const = 0; |
| 60 |
| 61 virtual RequestPriority Priority() const = 0; |
| 59 | 62 |
| 60 // Note that this may result in a possibly reentrant call to | 63 // Note that this may result in a possibly reentrant call to |
| 61 // |ThrottleDelegate::OnThrottleStateChanged|, as well as the resumption | 64 // |ThrottleDelegate::OnThrottleUnblocked|, as well as the resumption |
| 62 // of this or other requests, which may result in request completion | 65 // of this or other requests, which may result in request completion |
| 63 // and destruction before return. Any caller of this function | 66 // and destruction before return. Any caller of this function |
| 64 // should not rely on this object or containing objects surviving | 67 // should not rely on this object or containing objects surviving |
| 65 // this call. | 68 // this call. |
| 69 // |
| 70 // This call is a no-op if the priority is set to its current value. |
| 66 virtual void SetPriority(RequestPriority priority) = 0; | 71 virtual void SetPriority(RequestPriority priority) = 0; |
| 67 | 72 |
| 68 protected: | 73 protected: |
| 69 Throttle() {} | 74 Throttle() {} |
| 70 | 75 |
| 71 private: | 76 private: |
| 72 DISALLOW_COPY_AND_ASSIGN(Throttle); | 77 DISALLOW_COPY_AND_ASSIGN(Throttle); |
| 73 }; | 78 }; |
| 74 | 79 |
| 75 virtual ~NetworkThrottleManager() {} | 80 virtual ~NetworkThrottleManager() {} |
| 76 | 81 |
| 77 // Caller must ensure that |*delegate| outlives the returned | 82 // Caller must ensure that |*delegate| outlives the returned |
| 78 // Throttle. | 83 // Throttle. |
| 79 virtual std::unique_ptr<Throttle> CreateThrottle(ThrottleDelegate* delegate, | 84 virtual std::unique_ptr<Throttle> CreateThrottle(ThrottleDelegate* delegate, |
| 80 RequestPriority priority, | 85 RequestPriority priority, |
| 81 bool ignore_limits) = 0; | 86 bool ignore_limits) = 0; |
| 82 | 87 |
| 83 static std::unique_ptr<NetworkThrottleManager> CreateThrottler(); | |
| 84 | |
| 85 protected: | 88 protected: |
| 86 NetworkThrottleManager() {} | 89 NetworkThrottleManager() {} |
| 87 | 90 |
| 88 private: | 91 private: |
| 89 DISALLOW_COPY_AND_ASSIGN(NetworkThrottleManager); | 92 DISALLOW_COPY_AND_ASSIGN(NetworkThrottleManager); |
| 90 }; | 93 }; |
| 91 | 94 |
| 92 } // namespace net | 95 } // namespace net |
| 93 | 96 |
| 94 #endif // NET_BASE_NETWORK_THROTTLE_MANAGER_H_ | 97 #endif // NET_BASE_NETWORK_THROTTLE_MANAGER_H_ |
| OLD | NEW |