2 * Copyright (C) 2008 Apple Inc. All Rights Reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
27 #include "NetworkStateNotifier.h"
29 #include <wtf/MainThread.h>
30 #include <wtf/Vector.h>
37 void NetworkStateNotifier::updateState()
39 // Assume that we're online until proven otherwise.
45 if (::GetAdaptersAddresses(AF_UNSPEC, 0, 0, 0, &size) != ERROR_BUFFER_OVERFLOW)
\r
48 buffer.resize(size);
\r
49 PIP_ADAPTER_ADDRESSES addresses = reinterpret_cast<PIP_ADAPTER_ADDRESSES>(buffer.data());
\r
51 if (::GetAdaptersAddresses(AF_UNSPEC, 0, 0, addresses, &size) != ERROR_SUCCESS) {
52 // We couldn't determine whether we're online or not, so assume that we are.
56 for (; addresses; addresses = addresses->Next) {
\r
57 if (addresses->IfType == MIB_IF_TYPE_LOOPBACK)
\r
60 if (addresses->OperStatus != IfOperStatusUp)
\r
63 // We found an interface that was up.
\r
67 // We didn't find any valid interfaces, so we must be offline.
71 void NetworkStateNotifier::addressChanged()
73 bool oldOnLine = m_isOnLine;
77 if (m_isOnLine == oldOnLine)
80 if (m_networkStateChangedFunction)
81 m_networkStateChangedFunction();
84 void NetworkStateNotifier::callAddressChanged(void* context)
86 static_cast<NetworkStateNotifier*>(context)->addressChanged();
89 void CALLBACK NetworkStateNotifier::addrChangeCallback(void* context, BOOLEAN timedOut)
91 // NotifyAddrChange only notifies us of a single address change. Now that we've been notified,
92 // we need to call it again so we'll get notified the *next* time.
93 static_cast<NetworkStateNotifier*>(context)->registerForAddressChange();
95 callOnMainThread(callAddressChanged, context);
98 void NetworkStateNotifier::registerForAddressChange()
101 ::NotifyAddrChange(&handle, &m_overlapped);
104 NetworkStateNotifier::NetworkStateNotifier()
106 , m_networkStateChangedFunction(0)
110 memset(&m_overlapped, 0, sizeof(m_overlapped));
112 // FIXME: Check m_overlapped on WinCE.
114 m_overlapped.hEvent = ::CreateEvent(0, false, false, 0);
116 ::RegisterWaitForSingleObject(&m_waitHandle, m_overlapped.hEvent, addrChangeCallback, this, INFINITE, 0);
118 registerForAddressChange();
122 } // namespace WebCore