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