1/* 2 * Copyright (C) 2008, 2014 Apple Inc. All Rights Reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY 14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR 17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 */ 25 26#ifndef NetworkStateNotifier_h 27#define NetworkStateNotifier_h 28 29#include <functional> 30#include <wtf/FastMalloc.h> 31#include <wtf/Noncopyable.h> 32#include <wtf/Vector.h> 33 34#if PLATFORM(MAC) 35 36#include <wtf/RetainPtr.h> 37#include "Timer.h" 38 39typedef const struct __CFArray * CFArrayRef; 40typedef const struct __SCDynamicStore * SCDynamicStoreRef; 41 42#elif PLATFORM(WIN) 43 44#include <windows.h> 45 46#elif PLATFORM(IOS) 47 48#include <wtf/RetainPtr.h> 49OBJC_CLASS WebNetworkStateObserver; 50 51#endif 52 53namespace WebCore { 54 55class NetworkStateNotifier { 56 WTF_MAKE_NONCOPYABLE(NetworkStateNotifier); WTF_MAKE_FAST_ALLOCATED; 57public: 58 NetworkStateNotifier(); 59#if PLATFORM(EFL) || PLATFORM(IOS) 60 ~NetworkStateNotifier(); 61#endif 62 void addNetworkStateChangeListener(std::function<void (bool isOnLine)>); 63 64 bool onLine() const; 65 66private: 67#if !PLATFORM(IOS) 68 bool m_isOnLine; 69#endif 70 Vector<std::function<void (bool)>> m_listeners; 71 72 void notifyNetworkStateChange() const; 73 void updateState(); 74 75#if PLATFORM(MAC) 76 void networkStateChangeTimerFired(Timer<NetworkStateNotifier>&); 77 78 static void dynamicStoreCallback(SCDynamicStoreRef, CFArrayRef changedKeys, void *info); 79 80 RetainPtr<SCDynamicStoreRef> m_store; 81 Timer<NetworkStateNotifier> m_networkStateChangeTimer; 82 83#elif PLATFORM(WIN) 84 static void CALLBACK addrChangeCallback(void*, BOOLEAN timedOut); 85 static void callAddressChanged(void*); 86 void addressChanged(); 87 88 void registerForAddressChange(); 89 HANDLE m_waitHandle; 90 OVERLAPPED m_overlapped; 91 92#elif PLATFORM(EFL) 93 void networkInterfaceChanged(); 94 static Eina_Bool readSocketCallback(void* userData, Ecore_Fd_Handler*); 95 96 int m_netlinkSocket; 97 Ecore_Fd_Handler* m_fdHandler; 98 99#elif PLATFORM(IOS) 100 void registerObserverIfNecessary() const; 101 friend void setOnLine(const NetworkStateNotifier*, bool); 102 103 mutable bool m_isOnLine; 104 mutable bool m_isOnLineInitialized; 105 mutable RetainPtr<WebNetworkStateObserver> m_observer; 106#endif 107}; 108 109#if !PLATFORM(COCOA) && !PLATFORM(WIN) && !PLATFORM(EFL) 110 111inline NetworkStateNotifier::NetworkStateNotifier() 112 : m_isOnLine(true) 113{ 114} 115 116inline void NetworkStateNotifier::updateState() { } 117 118#endif 119 120#if !PLATFORM(IOS) 121inline bool NetworkStateNotifier::onLine() const 122{ 123 return m_isOnLine; 124} 125#endif 126 127NetworkStateNotifier& networkStateNotifier(); 128 129} // namespace WebCore 130 131#endif // NetworkStateNotifier_h 132