Index: Source/core/page/NetworkStateNotifier.h |
diff --git a/Source/core/page/NetworkStateNotifier.h b/Source/core/page/NetworkStateNotifier.h |
index 7e5fb725d6ed50145f3b1ecab2a47a7eff2bd009..5eb4bf03950c79129d473d77fe7e8de4c04181e7 100644 |
--- a/Source/core/page/NetworkStateNotifier.h |
+++ b/Source/core/page/NetworkStateNotifier.h |
@@ -26,17 +26,33 @@ |
#ifndef NetworkStateNotifier_h |
#define NetworkStateNotifier_h |
+#include "public/platform/WebNetworkConnection.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::WebNetworkConnection::ConnectionType) = 0; |
+ }; |
+ |
NetworkStateNotifier() |
- : m_isOnLine(true) { } |
+ : m_isOnLine(true) |
+ , m_type(blink::WebNetworkConnection::Other) |
+ { |
+ } |
+ |
+ virtual ~NetworkStateNotifier(); |
bool onLine() const |
{ |
@@ -46,13 +62,51 @@ public: |
void setOnLine(bool); |
+ blink::WebNetworkConnection::ConnectionType connectionType() const |
+ { |
+ MutexLocker locker(m_mutex); |
+ return m_type; |
+ } |
+ |
+ void setWebConnectionType(blink::WebNetworkConnection::ConnectionType); |
+ |
+ // Must be called on the context's thread. An added observer must be |
+ // removed before its ExecutionContext is deleted. |
+ void addObserver(NetworkStateObserver*, ExecutionContext*); |
+ void removeObserver(NetworkStateObserver*, ExecutionContext*); |
+ |
+protected: |
+ virtual void notifyObserversOnContext(ExecutionContext*, blink::WebNetworkConnection::ConnectionType); |
+ |
private: |
+ friend class TestNetworkStateNotifier; |
+ struct ObserverList { |
+ ObserverList() |
+ : iterating(false) |
+ { |
+ } |
+ bool iterating; |
+ Vector<NetworkStateObserver*> observers; |
+ Vector<size_t> zeroedObservers; // Indices in observers that are 0. |
+ }; |
+ |
+ typedef HashMap<ExecutionContext*, OwnPtr<ObserverList> > ObserverListMap; |
+ |
+ 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::WebNetworkConnection::ConnectionType m_type; |
+ ObserverListMap m_observers; |
}; |
NetworkStateNotifier& networkStateNotifier(); |
-}; |
+} // namespace WebCore |
#endif // NetworkStateNotifier_h |