| 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 20 matching lines...) Expand all Loading... |
| 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/Optional.h" |
| 37 #include "platform/wtf/ThreadingPrimitives.h" | 37 #include "platform/wtf/ThreadingPrimitives.h" |
| 38 #include "platform/wtf/Time.h" | 38 #include "platform/wtf/Time.h" |
| 39 #include "platform/wtf/Vector.h" | 39 #include "platform/wtf/Vector.h" |
| 40 #include "public/platform/WebConnectionType.h" | 40 #include "public/platform/WebConnectionType.h" |
| 41 #include "public/platform/WebEffectiveConnectionType.h" |
| 41 | 42 |
| 42 namespace blink { | 43 namespace blink { |
| 43 | 44 |
| 44 class PLATFORM_EXPORT NetworkStateNotifier { | 45 class PLATFORM_EXPORT NetworkStateNotifier { |
| 45 WTF_MAKE_NONCOPYABLE(NetworkStateNotifier); | 46 WTF_MAKE_NONCOPYABLE(NetworkStateNotifier); |
| 46 USING_FAST_MALLOC(NetworkStateNotifier); | 47 USING_FAST_MALLOC(NetworkStateNotifier); |
| 47 | 48 |
| 48 public: | 49 public: |
| 49 struct NetworkState { | 50 struct NetworkState { |
| 50 static const int kInvalidMaxBandwidth = -1; | 51 static const int kInvalidMaxBandwidth = -1; |
| 51 bool on_line_initialized = false; | 52 bool on_line_initialized = false; |
| 52 bool on_line = true; | 53 bool on_line = true; |
| 53 bool connection_initialized = false; | 54 bool connection_initialized = false; |
| 54 WebConnectionType type = kWebConnectionTypeOther; | 55 WebConnectionType type = kWebConnectionTypeOther; |
| 55 double max_bandwidth_mbps = kInvalidMaxBandwidth; | 56 double max_bandwidth_mbps = kInvalidMaxBandwidth; |
| 57 WebEffectiveConnectionType effective_type = |
| 58 WebEffectiveConnectionType::kTypeUnknown; |
| 56 Optional<TimeDelta> http_rtt; | 59 Optional<TimeDelta> http_rtt; |
| 57 Optional<TimeDelta> transport_rtt; | 60 Optional<TimeDelta> transport_rtt; |
| 58 Optional<double> downlink_throughput_mbps; | 61 Optional<double> downlink_throughput_mbps; |
| 59 }; | 62 }; |
| 60 | 63 |
| 61 class NetworkStateObserver { | 64 class NetworkStateObserver { |
| 62 public: | 65 public: |
| 63 // Will be called on the task runner that is passed in add*Observer. | 66 // Will be called on the task runner that is passed in add*Observer. |
| 64 virtual void ConnectionChange( | 67 virtual void ConnectionChange( |
| 65 WebConnectionType, | 68 WebConnectionType, |
| 66 double max_bandwidth_mbps, | 69 double max_bandwidth_mbps, |
| 70 WebEffectiveConnectionType, |
| 67 const Optional<TimeDelta>& http_rtt, | 71 const Optional<TimeDelta>& http_rtt, |
| 68 const Optional<TimeDelta>& transport_rtt, | 72 const Optional<TimeDelta>& transport_rtt, |
| 69 const Optional<double>& downlink_throughput_mbps) {} | 73 const Optional<double>& downlink_throughput_mbps) {} |
| 70 virtual void OnLineStateChange(bool on_line) {} | 74 virtual void OnLineStateChange(bool on_line) {} |
| 71 }; | 75 }; |
| 72 | 76 |
| 73 NetworkStateNotifier() : has_override_(false) {} | 77 NetworkStateNotifier() : has_override_(false) {} |
| 74 | 78 |
| 75 // Can be called on any thread. | 79 // Can be called on any thread. |
| 76 bool OnLine() const { | 80 bool OnLine() const { |
| 77 MutexLocker locker(mutex_); | 81 MutexLocker locker(mutex_); |
| 78 const NetworkState& state = has_override_ ? override_ : state_; | 82 const NetworkState& state = has_override_ ? override_ : state_; |
| 79 DCHECK(state.on_line_initialized); | 83 DCHECK(state.on_line_initialized); |
| 80 return state.on_line; | 84 return state.on_line; |
| 81 } | 85 } |
| 82 | 86 |
| 87 // Returns the current effective connection type, which is the connection type |
| 88 // whose typical performance is most similar to the measured performance of |
| 89 // the network in use. |
| 90 WebEffectiveConnectionType EffectiveType() const { |
| 91 MutexLocker locker(mutex_); |
| 92 const NetworkState& state = has_override_ ? override_ : state_; |
| 93 DCHECK(state.on_line_initialized); |
| 94 return state.effective_type; |
| 95 } |
| 96 |
| 83 // Returns the current HTTP RTT estimate. If the estimate is unavailable, the | 97 // Returns the current HTTP RTT estimate. If the estimate is unavailable, the |
| 84 // returned optional value is null. | 98 // returned optional value is null. |
| 85 Optional<TimeDelta> HttpRtt() const { | 99 Optional<TimeDelta> HttpRtt() const { |
| 86 MutexLocker locker(mutex_); | 100 MutexLocker locker(mutex_); |
| 87 const NetworkState& state = has_override_ ? override_ : state_; | 101 const NetworkState& state = has_override_ ? override_ : state_; |
| 88 DCHECK(state.on_line_initialized); | 102 DCHECK(state.on_line_initialized); |
| 89 return state.http_rtt; | 103 return state.http_rtt; |
| 90 } | 104 } |
| 91 | 105 |
| 92 // Returns the current transport RTT estimate. If the estimate is unavailable, | 106 // Returns the current transport RTT estimate. If the estimate is unavailable, |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 139 | 153 |
| 140 // Can be called on any thread. | 154 // Can be called on any thread. |
| 141 double MaxBandwidth() const { | 155 double MaxBandwidth() const { |
| 142 MutexLocker locker(mutex_); | 156 MutexLocker locker(mutex_); |
| 143 const NetworkState& state = has_override_ ? override_ : state_; | 157 const NetworkState& state = has_override_ ? override_ : state_; |
| 144 DCHECK(state.connection_initialized); | 158 DCHECK(state.connection_initialized); |
| 145 return state.max_bandwidth_mbps; | 159 return state.max_bandwidth_mbps; |
| 146 } | 160 } |
| 147 | 161 |
| 148 void SetWebConnection(WebConnectionType, double max_bandwidth_mbps); | 162 void SetWebConnection(WebConnectionType, double max_bandwidth_mbps); |
| 149 void SetNetworkQuality(TimeDelta http_rtt, | 163 void SetNetworkQuality(WebEffectiveConnectionType, |
| 164 TimeDelta http_rtt, |
| 150 TimeDelta transport_rtt, | 165 TimeDelta transport_rtt, |
| 151 int downlink_throughput_kbps); | 166 int downlink_throughput_kbps); |
| 152 | 167 |
| 153 // When called, successive setWebConnectionType/setOnLine calls are stored, | 168 // When called, successive setWebConnectionType/setOnLine calls are stored, |
| 154 // and supplied overridden values are used instead until clearOverride() is | 169 // and supplied overridden values are used instead until clearOverride() is |
| 155 // called. This is used for layout tests (see crbug.com/377736) and inspector | 170 // called. This is used for layout tests (see crbug.com/377736) and inspector |
| 156 // emulation. | 171 // emulation. |
| 157 // | 172 // |
| 158 // Since this class is a singleton, tests must clear override when completed | 173 // Since this class is a singleton, tests must clear override when completed |
| 159 // to avoid indeterminate state across the test harness. | 174 // to avoid indeterminate state across the test harness. |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 232 | 247 |
| 233 ObserverListMap connection_observers_; | 248 ObserverListMap connection_observers_; |
| 234 ObserverListMap on_line_state_observers_; | 249 ObserverListMap on_line_state_observers_; |
| 235 }; | 250 }; |
| 236 | 251 |
| 237 PLATFORM_EXPORT NetworkStateNotifier& GetNetworkStateNotifier(); | 252 PLATFORM_EXPORT NetworkStateNotifier& GetNetworkStateNotifier(); |
| 238 | 253 |
| 239 } // namespace blink | 254 } // namespace blink |
| 240 | 255 |
| 241 #endif // NetworkStateNotifier_h | 256 #endif // NetworkStateNotifier_h |
| OLD | NEW |