Chromium Code Reviews| Index: third_party/WebKit/Source/core/page/NetworkStateNotifier.h |
| diff --git a/third_party/WebKit/Source/core/page/NetworkStateNotifier.h b/third_party/WebKit/Source/core/page/NetworkStateNotifier.h |
| index f25348326696b0878417f9c196ede217120dfa30..14acb6b38d500954c836f5d061f08f0f9c561c65 100644 |
| --- a/third_party/WebKit/Source/core/page/NetworkStateNotifier.h |
| +++ b/third_party/WebKit/Source/core/page/NetworkStateNotifier.h |
| @@ -48,11 +48,7 @@ public: |
| }; |
| NetworkStateNotifier() |
| - : m_initialized(false) |
| - , m_isOnLine(true) |
| - , m_type(WebConnectionTypeOther) |
| - , m_maxBandwidthMbps(kInvalidMaxBandwidth) |
| - , m_testUpdatesOnly(false) |
| + : m_hasOverride(false) |
| { |
| } |
| @@ -60,8 +56,9 @@ public: |
| bool onLine() const |
| { |
| MutexLocker locker(m_mutex); |
| - ASSERT(m_initialized); |
| - return m_isOnLine; |
| + const NetworkState& data = m_hasOverride ? m_override : m_state; |
| + DCHECK(data.onLineInitialized); |
| + return data.onLine; |
| } |
| void setOnLine(bool); |
| @@ -70,8 +67,9 @@ public: |
| WebConnectionType connectionType() const |
| { |
| MutexLocker locker(m_mutex); |
| - ASSERT(m_initialized); |
| - return m_type; |
| + const NetworkState& data = m_hasOverride ? m_override : m_state; |
| + DCHECK(data.connectionInitialized); |
| + return data.type; |
| } |
| // Can be called on any thread. |
| @@ -99,12 +97,23 @@ public: |
| double maxBandwidth() const |
| { |
| MutexLocker locker(m_mutex); |
| - ASSERT(m_initialized); |
| - return m_maxBandwidthMbps; |
| + const NetworkState& data = m_hasOverride ? m_override : m_state; |
| + DCHECK(data.connectionInitialized); |
| + return data.maxBandwidthMbps; |
| } |
| void setWebConnection(WebConnectionType, double maxBandwidthMbps); |
| + // When called, successive setWebConnectionType/setOnLine calls are ignored, |
|
jkarlin
2016/06/28 17:57:12
Hmm, not quite ignored, since they're stored and w
dgozman
2016/06/29 01:17:33
Done.
|
| + // and supplied overridden values are used instead. |
| + // This is used for layout tests (see crbug.com/377736) and inspector emulation. |
| + // |
| + // Since this class is a singleton, tests must clear override when completed to |
| + // avoid indeterminate state across the test harness. When switching in or out of test |
| + // mode, all state will be reset to default values. |
|
jkarlin
2016/06/28 17:57:12
This last sentence is no longer true.
dgozman
2016/06/29 01:17:33
Good catch! Done.
|
| + void setOverride(bool onLine, WebConnectionType, double maxBandwidthMbps); |
| + void clearOverride(); |
| + |
| // Must be called on the context's thread. An added observer must be removed |
| // before its ExecutionContext is deleted. It's possible for an observer to |
| // be called twice for the same event if it is first removed and then added |
| @@ -112,18 +121,6 @@ public: |
| void addObserver(NetworkStateObserver*, ExecutionContext*); |
| void removeObserver(NetworkStateObserver*, ExecutionContext*); |
| - // The following functions are for testing purposes. |
| - |
| - // When true, setWebConnectionType calls are ignored and only setWebConnectionTypeForTest |
| - // can update the connection type. This is used for layout tests (see crbug.com/377736). |
| - // |
| - // Since this class is a singleton, tests must call this with false when completed to |
| - // avoid indeterminate state across the test harness. When switching in or out of test |
| - // mode, all state will be reset to default values. |
| - void setTestUpdatesOnly(bool); |
| - // Tests should call this as it will change the type regardless of the value of m_testUpdatesOnly. |
| - void setWebConnectionForTest(WebConnectionType, double maxBandwidthMbps); |
| - |
| private: |
| struct ObserverList { |
| ObserverList() |
| @@ -135,16 +132,33 @@ private: |
| Vector<size_t> zeroedObservers; // Indices in observers that are 0. |
| }; |
| - const int kInvalidMaxBandwidth = -1; |
| + struct NetworkState { |
| + static const int kInvalidMaxBandwidth = -1; |
| + bool onLineInitialized = false; |
| + bool onLine = true; |
| + bool connectionInitialized = false; |
| + WebConnectionType type = WebConnectionTypeOther; |
| + double maxBandwidthMbps = kInvalidMaxBandwidth; |
| + }; |
| - void setWebConnectionImpl(WebConnectionType, double maxBandwidthMbps); |
| - void setMaxBandwidthImpl(double maxBandwidthMbps); |
| + // This helper scope acquires the lock to access the members of NetworkStateNotifier |
| + // and issues required notifications when mutating the state, if something changed. |
| + // It's only possible to mutate the state on main thread. |
|
jkarlin
2016/06/28 17:57:12
s/on main thread/on the main thread/
dgozman
2016/06/29 01:17:33
Done.
|
| + class ScopedNotifier { |
| + public: |
| + explicit ScopedNotifier(NetworkStateNotifier&); |
| + ~ScopedNotifier(); |
| + private: |
| + NetworkStateNotifier& m_notifier; |
| + NetworkState m_before; |
| + }; |
| // The ObserverListMap is cross-thread accessed, adding/removing Observers running |
| // within an ExecutionContext. Kept off-heap to ease cross-thread allocation and use; |
| // the observers are (already) responsible for explicitly unregistering while finalizing. |
| using ObserverListMap = HashMap<UntracedMember<ExecutionContext>, std::unique_ptr<ObserverList>>; |
| + void notifyObservers(WebConnectionType, double maxBandwidthMbps); |
| void notifyObserversOfConnectionChangeOnContext(WebConnectionType, double maxBandwidthMbps, ExecutionContext*); |
| ObserverList* lockAndFindObserverList(ExecutionContext*); |
| @@ -155,12 +169,10 @@ private: |
| void collectZeroedObservers(ObserverList*, ExecutionContext*); |
| mutable Mutex m_mutex; |
| - bool m_initialized; |
| - bool m_isOnLine; |
| - WebConnectionType m_type; |
| - double m_maxBandwidthMbps; |
| + NetworkState m_state; |
| + bool m_hasOverride; |
| + NetworkState m_override; |
| ObserverListMap m_observers; |
| - bool m_testUpdatesOnly; |
| }; |
| CORE_EXPORT NetworkStateNotifier& networkStateNotifier(); |