| 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 26 matching lines...) Expand all Loading... |
| 37 #include "wtf/ThreadingPrimitives.h" | 37 #include "wtf/ThreadingPrimitives.h" |
| 38 #include "wtf/Vector.h" | 38 #include "wtf/Vector.h" |
| 39 | 39 |
| 40 namespace blink { | 40 namespace blink { |
| 41 | 41 |
| 42 class CORE_EXPORT NetworkStateNotifier { | 42 class CORE_EXPORT NetworkStateNotifier { |
| 43 WTF_MAKE_NONCOPYABLE(NetworkStateNotifier); | 43 WTF_MAKE_NONCOPYABLE(NetworkStateNotifier); |
| 44 USING_FAST_MALLOC(NetworkStateNotifier); | 44 USING_FAST_MALLOC(NetworkStateNotifier); |
| 45 | 45 |
| 46 public: | 46 public: |
| 47 struct NetworkState { |
| 48 static const int kInvalidMaxBandwidth = -1; |
| 49 bool onLineInitialized = false; |
| 50 bool onLine = true; |
| 51 bool connectionInitialized = false; |
| 52 WebConnectionType type = WebConnectionTypeOther; |
| 53 double maxBandwidthMbps = kInvalidMaxBandwidth; |
| 54 }; |
| 55 |
| 47 class NetworkStateObserver { | 56 class NetworkStateObserver { |
| 48 public: | 57 public: |
| 49 // Will be called on the task runner that is passed in add*Observer. | 58 // Will be called on the task runner that is passed in add*Observer. |
| 50 virtual void connectionChange(WebConnectionType, double maxBandwidthMbps) {} | 59 virtual void connectionChange(WebConnectionType, double maxBandwidthMbps) {} |
| 51 virtual void onLineStateChange(bool onLine) {} | 60 virtual void onLineStateChange(bool onLine) {} |
| 52 }; | 61 }; |
| 53 | 62 |
| 54 NetworkStateNotifier() : m_hasOverride(false) {} | 63 NetworkStateNotifier() : m_hasOverride(false) {} |
| 55 | 64 |
| 56 // Can be called on any thread. | 65 // Can be called on any thread. |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 122 void removeOnLineObserver(NetworkStateObserver*, PassRefPtr<WebTaskRunner>); | 131 void removeOnLineObserver(NetworkStateObserver*, PassRefPtr<WebTaskRunner>); |
| 123 | 132 |
| 124 private: | 133 private: |
| 125 struct ObserverList { | 134 struct ObserverList { |
| 126 ObserverList() : iterating(false) {} | 135 ObserverList() : iterating(false) {} |
| 127 bool iterating; | 136 bool iterating; |
| 128 Vector<NetworkStateObserver*> observers; | 137 Vector<NetworkStateObserver*> observers; |
| 129 Vector<size_t> zeroedObservers; // Indices in observers that are 0. | 138 Vector<size_t> zeroedObservers; // Indices in observers that are 0. |
| 130 }; | 139 }; |
| 131 | 140 |
| 132 struct NetworkState { | |
| 133 static const int kInvalidMaxBandwidth = -1; | |
| 134 bool onLineInitialized = false; | |
| 135 bool onLine = true; | |
| 136 bool connectionInitialized = false; | |
| 137 WebConnectionType type = WebConnectionTypeOther; | |
| 138 double maxBandwidthMbps = kInvalidMaxBandwidth; | |
| 139 }; | |
| 140 friend CrossThreadCopier<NetworkStateNotifier::NetworkState>; | |
| 141 | |
| 142 // This helper scope issues required notifications when mutating the state if | 141 // This helper scope issues required notifications when mutating the state if |
| 143 // something has changed. It's only possible to mutate the state on the main | 142 // something has changed. It's only possible to mutate the state on the main |
| 144 // thread. Note that ScopedNotifier must be destroyed when not holding a lock | 143 // thread. Note that ScopedNotifier must be destroyed when not holding a lock |
| 145 // so that onLine notifications can be dispatched without a deadlock. | 144 // so that onLine notifications can be dispatched without a deadlock. |
| 146 class ScopedNotifier { | 145 class ScopedNotifier { |
| 147 public: | 146 public: |
| 148 explicit ScopedNotifier(NetworkStateNotifier&); | 147 explicit ScopedNotifier(NetworkStateNotifier&); |
| 149 ~ScopedNotifier(); | 148 ~ScopedNotifier(); |
| 150 | 149 |
| 151 private: | 150 private: |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 193 | 192 |
| 194 ObserverListMap m_connectionObservers; | 193 ObserverListMap m_connectionObservers; |
| 195 ObserverListMap m_onLineStateObservers; | 194 ObserverListMap m_onLineStateObservers; |
| 196 }; | 195 }; |
| 197 | 196 |
| 198 CORE_EXPORT NetworkStateNotifier& networkStateNotifier(); | 197 CORE_EXPORT NetworkStateNotifier& networkStateNotifier(); |
| 199 | 198 |
| 200 } // namespace blink | 199 } // namespace blink |
| 201 | 200 |
| 202 #endif // NetworkStateNotifier_h | 201 #endif // NetworkStateNotifier_h |
| OLD | NEW |