NetworkStateNotifier.h [plain text]
#ifndef NetworkStateNotifier_h
#define NetworkStateNotifier_h
#include <wtf/FastMalloc.h>
#include <wtf/Function.h>
#include <wtf/Noncopyable.h>
#include <wtf/Vector.h>
#if PLATFORM(MAC)
#include <wtf/RetainPtr.h>
#include "Timer.h"
typedef const struct __CFArray * CFArrayRef;
typedef const struct __SCDynamicStore * SCDynamicStoreRef;
#elif PLATFORM(WIN)
#include <windows.h>
#elif PLATFORM(IOS)
#include <wtf/RetainPtr.h>
OBJC_CLASS WebNetworkStateObserver;
#endif
namespace WebCore {
class NetworkStateNotifier {
WTF_MAKE_NONCOPYABLE(NetworkStateNotifier); WTF_MAKE_FAST_ALLOCATED;
public:
NetworkStateNotifier();
#if PLATFORM(IOS)
~NetworkStateNotifier();
#endif
void addNetworkStateChangeListener(WTF::Function<void (bool isOnLine)>&&);
bool onLine() const;
private:
#if !PLATFORM(IOS)
bool m_isOnLine;
#endif
Vector<WTF::Function<void (bool)>> m_listeners;
void notifyNetworkStateChange() const;
void updateState();
#if PLATFORM(MAC)
void networkStateChangeTimerFired();
static void dynamicStoreCallback(SCDynamicStoreRef, CFArrayRef changedKeys, void *info);
RetainPtr<SCDynamicStoreRef> m_store;
Timer m_networkStateChangeTimer;
#elif PLATFORM(WIN)
static void CALLBACK addrChangeCallback(void*, BOOLEAN timedOut);
void addressChanged();
void registerForAddressChange();
HANDLE m_waitHandle;
OVERLAPPED m_overlapped;
#elif PLATFORM(IOS)
void registerObserverIfNecessary() const;
friend void setOnLine(const NetworkStateNotifier*, bool);
mutable bool m_isOnLine;
mutable bool m_isOnLineInitialized;
mutable RetainPtr<WebNetworkStateObserver> m_observer;
#endif
};
#if !PLATFORM(COCOA) && !PLATFORM(WIN)
inline NetworkStateNotifier::NetworkStateNotifier()
: m_isOnLine(true)
{
}
inline void NetworkStateNotifier::updateState() { }
#endif
#if !PLATFORM(IOS)
inline bool NetworkStateNotifier::onLine() const
{
return m_isOnLine;
}
#endif
WEBCORE_EXPORT NetworkStateNotifier& networkStateNotifier();
}
#endif // NetworkStateNotifier_h