OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright (C) 2008 Apple Inc. All Rights Reserved. | 2 * Copyright (C) 2008 Apple Inc. All Rights Reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
6 * are met: | 6 * are met: |
7 * 1. Redistributions of source code must retain the above copyright | 7 * 1. Redistributions of source code must retain the above copyright |
8 * notice, this list of conditions and the following disclaimer. | 8 * notice, this list of conditions and the following disclaimer. |
9 * 2. Redistributions in binary form must reproduce the above copyright | 9 * 2. Redistributions in binary form must reproduce the above copyright |
10 * notice, this list of conditions and the following disclaimer in the | 10 * notice, this list of conditions and the following disclaimer in the |
11 * documentation and/or other materials provided with the distribution. | 11 * documentation and/or other materials provided with the distribution. |
12 * | 12 * |
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY | 13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY |
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | 14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR | 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR |
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | 17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | 18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | 19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY | 20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
24 */ | 24 */ |
25 | 25 |
26 #include "config.h" | 26 #include "config.h" |
27 #include "NetworkStateNotifier.h" | 27 #include "NetworkStateNotifier.h" |
28 | 28 |
29 #include <wtf/MainThread.h> | 29 namespace WebCore { |
30 #include <wtf/Vector.h> | |
31 | 30 |
32 #include <winsock2.h> | 31 // Chromium doesn't currently support network state notifications. This cuases |
M-A Ruel
2008/10/02 20:35:09
causes
| |
33 #include <iphlpapi.h> | 32 // an extra DLL to get loaded into the renderer which can slow things down a |
34 | 33 // bit. We may want an alternate design. |
35 namespace WebCore { | |
36 | 34 |
37 void NetworkStateNotifier::updateState() | 35 void NetworkStateNotifier::updateState() |
38 { | 36 { |
39 // Assume that we're online until proven otherwise. | |
40 m_isOnLine = true; | |
41 return; | |
42 | |
43 Vector<char> buffer; | |
44 DWORD size = 0; | |
45 | |
46 if (::GetAdaptersAddresses(AF_UNSPEC, 0, 0, 0, &size) != ERROR_BUFFER_OVERFL OW) | |
47 return; | |
48 | |
49 buffer.resize(size); | |
50 PIP_ADAPTER_ADDRESSES addresses = reinterpret_cast<PIP_ADAPTER_ADDRESSES>(bu ffer.data()); | |
51 | |
52 if (::GetAdaptersAddresses(AF_UNSPEC, 0, 0, addresses, &size) != ERROR_SUCCE SS) { | |
53 // We couldn't determine whether we're online or not, so assume that we are. | |
54 return; | |
55 } | |
56 | |
57 for (; addresses; addresses = addresses->Next) { | |
58 if (addresses->IfType == MIB_IF_TYPE_LOOPBACK) | |
59 continue; | |
60 | |
61 if (addresses->OperStatus != IfOperStatusUp) | |
62 continue; | |
63 | |
64 // We found an interface that was up. | |
65 return; | |
66 } | |
67 | |
68 // We didn't find any valid interfaces, so we must be offline. | |
69 m_isOnLine = false; | |
70 } | 37 } |
71 | 38 |
72 void NetworkStateNotifier::addressChanged() | 39 void NetworkStateNotifier::addressChanged() |
73 { | 40 { |
74 bool oldOnLine = m_isOnLine; | |
75 | |
76 updateState(); | |
77 | |
78 if (m_isOnLine == oldOnLine) | |
79 return; | |
80 | |
81 if (m_networkStateChangedFunction) | |
82 m_networkStateChangedFunction(); | |
83 } | 41 } |
84 | 42 |
85 void NetworkStateNotifier::callAddressChanged(void* context) | 43 void NetworkStateNotifier::callAddressChanged(void* context) |
86 { | 44 { |
87 static_cast<NetworkStateNotifier*>(context)->addressChanged(); | |
88 } | 45 } |
89 | 46 |
90 void CALLBACK NetworkStateNotifier::addrChangeCallback(void* context, BOOLEAN ti medOut) | 47 void CALLBACK NetworkStateNotifier::addrChangeCallback(void* context, BOOLEAN ti medOut) |
91 { | 48 { |
92 callOnMainThread(callAddressChanged, context); | |
93 } | 49 } |
94 | 50 |
95 void NetworkStateNotifier::registerForAddressChange() | 51 void NetworkStateNotifier::registerForAddressChange() |
96 { | 52 { |
97 HANDLE handle; | |
98 ::NotifyAddrChange(&handle, &m_overlapped); | |
99 } | 53 } |
100 | 54 |
101 NetworkStateNotifier::NetworkStateNotifier() | 55 NetworkStateNotifier::NetworkStateNotifier() |
102 : m_isOnLine(false) | 56 : m_isOnLine(true) |
103 { | 57 { |
104 updateState(); | |
105 | |
106 memset(&m_overlapped, 0, sizeof(m_overlapped)); | |
107 | |
108 m_overlapped.hEvent = ::CreateEvent(0, false, false, 0); | |
109 | |
110 ::RegisterWaitForSingleObject(&m_waitHandle, m_overlapped.hEvent, addrChange Callback, this, INFINITE, 0); | |
111 | |
112 registerForAddressChange(); | |
113 } | 58 } |
114 | 59 |
115 } | 60 } |
OLD | NEW |