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