OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2008 Apple Inc. All Rights Reserved. | 2 * Copyright (C) 2008 Apple Inc. All Rights Reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
6 * are met: | 6 * are met: |
7 * 1. Redistributions of source code must retain the above copyright | 7 * 1. Redistributions of source code must retain the above copyright |
8 * notice, this list of conditions and the following disclaimer. | 8 * notice, this list of conditions and the following disclaimer. |
9 * 2. Redistributions in binary form must reproduce the above copyright | 9 * 2. Redistributions in binary form must reproduce the above copyright |
10 * notice, this list of conditions and the following disclaimer in the | 10 * notice, this list of conditions and the following disclaimer in the |
(...skipping 15 matching lines...) Expand all Loading... |
26 #ifndef NetworkStateNotifier_h | 26 #ifndef NetworkStateNotifier_h |
27 #define NetworkStateNotifier_h | 27 #define NetworkStateNotifier_h |
28 | 28 |
29 #include <memory> | 29 #include <memory> |
30 #include "platform/CrossThreadCopier.h" | 30 #include "platform/CrossThreadCopier.h" |
31 #include "platform/PlatformExport.h" | 31 #include "platform/PlatformExport.h" |
32 #include "platform/WebTaskRunner.h" | 32 #include "platform/WebTaskRunner.h" |
33 #include "platform/wtf/Allocator.h" | 33 #include "platform/wtf/Allocator.h" |
34 #include "platform/wtf/HashMap.h" | 34 #include "platform/wtf/HashMap.h" |
35 #include "platform/wtf/Noncopyable.h" | 35 #include "platform/wtf/Noncopyable.h" |
| 36 #include "platform/wtf/Optional.h" |
36 #include "platform/wtf/ThreadingPrimitives.h" | 37 #include "platform/wtf/ThreadingPrimitives.h" |
| 38 #include "platform/wtf/Time.h" |
37 #include "platform/wtf/Vector.h" | 39 #include "platform/wtf/Vector.h" |
38 #include "public/platform/WebConnectionType.h" | 40 #include "public/platform/WebConnectionType.h" |
39 | 41 |
40 namespace blink { | 42 namespace blink { |
41 | 43 |
42 class PLATFORM_EXPORT NetworkStateNotifier { | 44 class PLATFORM_EXPORT NetworkStateNotifier { |
43 WTF_MAKE_NONCOPYABLE(NetworkStateNotifier); | 45 WTF_MAKE_NONCOPYABLE(NetworkStateNotifier); |
44 USING_FAST_MALLOC(NetworkStateNotifier); | 46 USING_FAST_MALLOC(NetworkStateNotifier); |
45 | 47 |
46 public: | 48 public: |
47 struct NetworkState { | 49 struct NetworkState { |
48 static const int kInvalidMaxBandwidth = -1; | 50 static const int kInvalidMaxBandwidth = -1; |
49 bool on_line_initialized = false; | 51 bool on_line_initialized = false; |
50 bool on_line = true; | 52 bool on_line = true; |
51 bool connection_initialized = false; | 53 bool connection_initialized = false; |
52 WebConnectionType type = kWebConnectionTypeOther; | 54 WebConnectionType type = kWebConnectionTypeOther; |
53 double max_bandwidth_mbps = kInvalidMaxBandwidth; | 55 double max_bandwidth_mbps = kInvalidMaxBandwidth; |
| 56 Optional<TimeDelta> http_rtt; |
| 57 Optional<TimeDelta> transport_rtt; |
| 58 Optional<double> downlink_throughput_mbps; |
54 }; | 59 }; |
55 | 60 |
56 class NetworkStateObserver { | 61 class NetworkStateObserver { |
57 public: | 62 public: |
58 // Will be called on the task runner that is passed in add*Observer. | 63 // Will be called on the task runner that is passed in add*Observer. |
59 virtual void ConnectionChange(WebConnectionType, | 64 virtual void ConnectionChange( |
60 double max_bandwidth_mbps) {} | 65 WebConnectionType, |
| 66 double max_bandwidth_mbps, |
| 67 const Optional<TimeDelta>& http_rtt, |
| 68 const Optional<TimeDelta>& transport_rtt, |
| 69 const Optional<double>& downlink_throughput_mbps) {} |
61 virtual void OnLineStateChange(bool on_line) {} | 70 virtual void OnLineStateChange(bool on_line) {} |
62 }; | 71 }; |
63 | 72 |
64 NetworkStateNotifier() : has_override_(false) {} | 73 NetworkStateNotifier() : has_override_(false) {} |
65 | 74 |
66 // Can be called on any thread. | 75 // Can be called on any thread. |
67 bool OnLine() const { | 76 bool OnLine() const { |
68 MutexLocker locker(mutex_); | 77 MutexLocker locker(mutex_); |
69 const NetworkState& state = has_override_ ? override_ : state_; | 78 const NetworkState& state = has_override_ ? override_ : state_; |
70 DCHECK(state.on_line_initialized); | 79 DCHECK(state.on_line_initialized); |
71 return state.on_line; | 80 return state.on_line; |
72 } | 81 } |
73 | 82 |
| 83 // Returns the current HTTP RTT estimate. If the estimate is unavailable, the |
| 84 // returned optional value is null. |
| 85 Optional<TimeDelta> HttpRtt() const { |
| 86 MutexLocker locker(mutex_); |
| 87 const NetworkState& state = has_override_ ? override_ : state_; |
| 88 DCHECK(state.on_line_initialized); |
| 89 return state.http_rtt; |
| 90 } |
| 91 |
| 92 // Returns the current transport RTT estimate. If the estimate is unavailable, |
| 93 // the returned optional value is null. |
| 94 Optional<TimeDelta> TransportRtt() const { |
| 95 MutexLocker locker(mutex_); |
| 96 const NetworkState& state = has_override_ ? override_ : state_; |
| 97 DCHECK(state.on_line_initialized); |
| 98 return state.transport_rtt; |
| 99 } |
| 100 |
| 101 // Returns the current throughput estimate (in megabits per second). If the |
| 102 // estimate is unavailable, the returned optional value is null. |
| 103 Optional<double> DownlinkThroughputMbps() const { |
| 104 MutexLocker locker(mutex_); |
| 105 const NetworkState& state = has_override_ ? override_ : state_; |
| 106 DCHECK(state.on_line_initialized); |
| 107 return state.downlink_throughput_mbps; |
| 108 } |
| 109 |
74 void SetOnLine(bool); | 110 void SetOnLine(bool); |
75 | 111 |
76 // Can be called on any thread. | 112 // Can be called on any thread. |
77 WebConnectionType ConnectionType() const { | 113 WebConnectionType ConnectionType() const { |
78 MutexLocker locker(mutex_); | 114 MutexLocker locker(mutex_); |
79 const NetworkState& state = has_override_ ? override_ : state_; | 115 const NetworkState& state = has_override_ ? override_ : state_; |
80 DCHECK(state.connection_initialized); | 116 DCHECK(state.connection_initialized); |
81 return state.type; | 117 return state.type; |
82 } | 118 } |
83 | 119 |
(...skipping 19 matching lines...) Expand all Loading... |
103 | 139 |
104 // Can be called on any thread. | 140 // Can be called on any thread. |
105 double MaxBandwidth() const { | 141 double MaxBandwidth() const { |
106 MutexLocker locker(mutex_); | 142 MutexLocker locker(mutex_); |
107 const NetworkState& state = has_override_ ? override_ : state_; | 143 const NetworkState& state = has_override_ ? override_ : state_; |
108 DCHECK(state.connection_initialized); | 144 DCHECK(state.connection_initialized); |
109 return state.max_bandwidth_mbps; | 145 return state.max_bandwidth_mbps; |
110 } | 146 } |
111 | 147 |
112 void SetWebConnection(WebConnectionType, double max_bandwidth_mbps); | 148 void SetWebConnection(WebConnectionType, double max_bandwidth_mbps); |
| 149 void SetNetworkQuality(TimeDelta http_rtt, |
| 150 TimeDelta transport_rtt, |
| 151 int downlink_throughput_kbps); |
113 | 152 |
114 // When called, successive setWebConnectionType/setOnLine calls are stored, | 153 // When called, successive setWebConnectionType/setOnLine calls are stored, |
115 // and supplied overridden values are used instead until clearOverride() is | 154 // and supplied overridden values are used instead until clearOverride() is |
116 // called. This is used for layout tests (see crbug.com/377736) and inspector | 155 // called. This is used for layout tests (see crbug.com/377736) and inspector |
117 // emulation. | 156 // emulation. |
118 // | 157 // |
119 // Since this class is a singleton, tests must clear override when completed | 158 // Since this class is a singleton, tests must clear override when completed |
120 // to avoid indeterminate state across the test harness. | 159 // to avoid indeterminate state across the test harness. |
121 void SetOverride(bool on_line, WebConnectionType, double max_bandwidth_mbps); | 160 void SetOverride(bool on_line, WebConnectionType, double max_bandwidth_mbps); |
122 void ClearOverride(); | 161 void ClearOverride(); |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
193 | 232 |
194 ObserverListMap connection_observers_; | 233 ObserverListMap connection_observers_; |
195 ObserverListMap on_line_state_observers_; | 234 ObserverListMap on_line_state_observers_; |
196 }; | 235 }; |
197 | 236 |
198 PLATFORM_EXPORT NetworkStateNotifier& GetNetworkStateNotifier(); | 237 PLATFORM_EXPORT NetworkStateNotifier& GetNetworkStateNotifier(); |
199 | 238 |
200 } // namespace blink | 239 } // namespace blink |
201 | 240 |
202 #endif // NetworkStateNotifier_h | 241 #endif // NetworkStateNotifier_h |
OLD | NEW |