diff options
Diffstat (limited to 'chromium/third_party/WebKit/Source/core/page/NetworkStateNotifier.h')
-rw-r--r-- | chromium/third_party/WebKit/Source/core/page/NetworkStateNotifier.h | 68 |
1 files changed, 66 insertions, 2 deletions
diff --git a/chromium/third_party/WebKit/Source/core/page/NetworkStateNotifier.h b/chromium/third_party/WebKit/Source/core/page/NetworkStateNotifier.h index 7e5fb725d6e..138aaf52dd2 100644 --- a/chromium/third_party/WebKit/Source/core/page/NetworkStateNotifier.h +++ b/chromium/third_party/WebKit/Source/core/page/NetworkStateNotifier.h @@ -26,17 +26,32 @@ #ifndef NetworkStateNotifier_h #define NetworkStateNotifier_h +#include "public/platform/WebConnectionType.h" #include "wtf/FastAllocBase.h" +#include "wtf/HashMap.h" #include "wtf/Noncopyable.h" #include "wtf/ThreadingPrimitives.h" +#include "wtf/Vector.h" namespace WebCore { +class ExecutionContext; + class NetworkStateNotifier { WTF_MAKE_NONCOPYABLE(NetworkStateNotifier); WTF_MAKE_FAST_ALLOCATED; public: + class NetworkStateObserver { + public: + // Will be called on the thread of the context passed in addObserver. + virtual void connectionTypeChange(blink::WebConnectionType) = 0; + }; + NetworkStateNotifier() - : m_isOnLine(true) { } + : m_isOnLine(true) + , m_type(blink::ConnectionTypeOther) + , m_testUpdatesOnly(false) + { + } bool onLine() const { @@ -46,13 +61,62 @@ public: void setOnLine(bool); + blink::WebConnectionType connectionType() const + { + MutexLocker locker(m_mutex); + return m_type; + } + + void setWebConnectionType(blink::WebConnectionType); + + // 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 + // during notification. + 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). + void setTestUpdatesOnly(bool); + // Tests should call this as it will change the type regardless of the value of m_testUpdatesOnly. + void setWebConnectionTypeForTest(blink::WebConnectionType); + private: + struct ObserverList { + ObserverList() + : iterating(false) + { + } + bool iterating; + Vector<NetworkStateObserver*> observers; + Vector<size_t> zeroedObservers; // Indices in observers that are 0. + }; + + void setWebConnectionTypeImpl(blink::WebConnectionType); + + typedef HashMap<ExecutionContext*, OwnPtr<ObserverList> > ObserverListMap; + + void notifyObserversOnContext(ExecutionContext*, blink::WebConnectionType); + + ObserverList* lockAndFindObserverList(ExecutionContext*); + + // Removed observers are nulled out in the list in case the list is being + // iterated over. Once done iterating, call this to clean up nulled + // observers. + void collectZeroedObservers(ObserverList*, ExecutionContext*); + mutable Mutex m_mutex; bool m_isOnLine; + blink::WebConnectionType m_type; + ObserverListMap m_observers; + bool m_testUpdatesOnly; }; NetworkStateNotifier& networkStateNotifier(); -}; +} // namespace WebCore #endif // NetworkStateNotifier_h |