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