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..adb2cdeeef228d420d3c7c25a53e1126ad470cff 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 Data& data = m_hasOverride ? m_overrideData : m_data; |
| + 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 Data& data = m_hasOverride ? m_overrideData : m_data; |
| + 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 Data& data = m_hasOverride ? m_overrideData : m_data; |
| + DCHECK(data.connectionInitialized); |
| + return data.maxBandwidthMbps; |
| } |
| void setWebConnection(WebConnectionType, double maxBandwidthMbps); |
| + // When called, successive setWebConnectionType/setOnLine calls are ignored, |
| + // 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. |
| + 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,38 @@ private: |
| Vector<size_t> zeroedObservers; // Indices in observers that are 0. |
| }; |
| - const int kInvalidMaxBandwidth = -1; |
| + struct Data { |
|
jkarlin
2016/06/28 14:23:41
How about a slightly more descriptive name such as
dgozman
2016/06/28 17:14:23
Done.
|
| + static const int kInvalidMaxBandwidth = -1; |
| + Data() |
| + : onLineInitialized(false) |
| + , onLine(true) |
| + , connectionInitialized(false) |
| + , type(WebConnectionTypeOther) |
| + , maxBandwidthMbps(kInvalidMaxBandwidth) |
| + { |
| + } |
| + bool onLineInitialized; |
|
jkarlin
2016/06/28 14:23:41
Please initialize the members here instead of in t
dgozman
2016/06/28 17:14:23
Done.
|
| + bool onLine; |
| + bool connectionInitialized; |
| + WebConnectionType type; |
| + double maxBandwidthMbps; |
| + }; |
| - void setWebConnectionImpl(WebConnectionType, double maxBandwidthMbps); |
| - void setMaxBandwidthImpl(double maxBandwidthMbps); |
| + class ScopedNotifier { |
|
jkarlin
2016/06/28 14:23:41
This class needs documentation
dgozman
2016/06/28 17:14:23
Done.
|
| + public: |
| + explicit ScopedNotifier(NetworkStateNotifier&); |
|
jkarlin
2016/06/28 14:23:41
Why not use a raw pointer?
dgozman
2016/06/28 17:14:23
Because it's never null.
jkarlin
2016/06/28 17:57:11
Ah, sorry, Chrome style hiccup.
|
| + ~ScopedNotifier(); |
| + private: |
| + NetworkStateNotifier& m_notifier; |
| + Data 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 +174,10 @@ private: |
| void collectZeroedObservers(ObserverList*, ExecutionContext*); |
| mutable Mutex m_mutex; |
| - bool m_initialized; |
| - bool m_isOnLine; |
| - WebConnectionType m_type; |
| - double m_maxBandwidthMbps; |
| + Data m_data; |
| + bool m_hasOverride; |
| + Data m_overrideData; |
| ObserverListMap m_observers; |
| - bool m_testUpdatesOnly; |
| }; |
| CORE_EXPORT NetworkStateNotifier& networkStateNotifier(); |