1 /*
2 * Copyright (C) 2008 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 <wtf/FastAllocBase.h>
30 #include <wtf/Noncopyable.h>
31 #if PLATFORM(ANDROID)
32 // TODO: Upstream to webkit.org
33 #include "Connection.h"
34 #endif
35
36 #if PLATFORM(MAC)
37
38 #include <wtf/RetainPtr.h>
39 #include "Timer.h"
40
41 typedef const struct __CFArray * CFArrayRef;
42 typedef const struct __SCDynamicStore * SCDynamicStoreRef;
43
44 #elif PLATFORM(WIN)
45
46 #include <windows.h>
47
48 #elif PLATFORM(QT)
49
50 #include <QtCore/qglobal.h>
51
52 #ifdef QT_NO_BEARERMANAGEMENT
53 #undef ENABLE_QT_BEARER
54 #define ENABLE_QT_BEARER 0
55 #endif
56
57 #endif
58
59 namespace WebCore {
60
61 #if (PLATFORM(QT) && ENABLE(QT_BEARER))
62 class NetworkStateNotifierPrivate;
63 #endif
64
65 class NetworkStateNotifier {
66 WTF_MAKE_NONCOPYABLE(NetworkStateNotifier); WTF_MAKE_FAST_ALLOCATED;
67 public:
68 NetworkStateNotifier();
69 void setNetworkStateChangedFunction(void (*)());
70
onLine()71 bool onLine() const { return m_isOnLine; }
72
73 #if (PLATFORM(QT) && ENABLE(QT_BEARER))
74 void setNetworkAccessAllowed(bool);
75 #elif PLATFORM(ANDROID) || PLATFORM(CHROMIUM)
76 void setOnLine(bool);
77 #endif
78
79 #if PLATFORM(ANDROID)
networkStateChange(bool online)80 void networkStateChange(bool online) { setOnLine(online); }
81 // TODO: Upstream to webkit.org
82 void networkTypeChange(Connection::ConnectionType type);
83 // TODO: Upstream to webkit.org
type()84 Connection::ConnectionType type() const { return m_type; }
85 #endif
86
87 private:
88 bool m_isOnLine;
89 #if PLATFORM(ANDROID)
90 // TODO: Upstream to webkit.org
91 Connection::ConnectionType m_type;
92 #endif
93 void (*m_networkStateChangedFunction)();
94
95 void updateState();
96
97 #if PLATFORM(MAC)
98 void networkStateChangeTimerFired(Timer<NetworkStateNotifier>*);
99
100 static void dynamicStoreCallback(SCDynamicStoreRef, CFArrayRef changedKeys, void *info);
101
102 RetainPtr<SCDynamicStoreRef> m_store;
103 Timer<NetworkStateNotifier> m_networkStateChangeTimer;
104
105 #elif PLATFORM(WIN)
106 static void CALLBACK addrChangeCallback(void*, BOOLEAN timedOut);
107 static void callAddressChanged(void*);
108 void addressChanged();
109
110 void registerForAddressChange();
111 HANDLE m_waitHandle;
112 OVERLAPPED m_overlapped;
113
114 #elif PLATFORM(QT) && ENABLE(QT_BEARER)
115 friend class NetworkStateNotifierPrivate;
116 NetworkStateNotifierPrivate* p;
117 #endif
118 };
119
120 #if !PLATFORM(MAC) && !PLATFORM(WIN) && !(PLATFORM(QT) && ENABLE(QT_BEARER))
121
NetworkStateNotifier()122 inline NetworkStateNotifier::NetworkStateNotifier()
123 : m_isOnLine(true)
124 #if PLATFORM(ANDROID)
125 // TODO: Upstream to webkit.org
126 , m_type(Connection::UNKNOWN)
127 #endif
128 , m_networkStateChangedFunction(0)
129 {
130 }
131
updateState()132 inline void NetworkStateNotifier::updateState() { }
133
134 #endif
135
136 NetworkStateNotifier& networkStateNotifier();
137
138 };
139
140 #endif // NetworkStateNotifier_h
141