1/*
2    Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies)
3
4    This library is free software; you can redistribute it and/or
5    modify it under the terms of the GNU Library General Public
6    License as published by the Free Software Foundation; either
7    version 2 of the License, or (at your option) any later version.
8
9    This library is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    Library General Public License for more details.
13
14    You should have received a copy of the GNU Library General Public License
15    along with this library; see the file COPYING.LIB.  If not, write to
16    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17    Boston, MA 02110-1301, USA.
18*/
19
20#include "config.h"
21#include "NetworkStateNotifier.h"
22
23#if (PLATFORM(QT) && !defined(QT_NO_BEARERMANAGEMENT))
24
25#include "NetworkStateNotifierPrivate.h"
26#include <QNetworkConfigurationManager>
27#include <QTimer>
28#include <wtf/PassOwnPtr.h>
29
30namespace WebCore {
31
32NetworkStateNotifierPrivate::NetworkStateNotifierPrivate(NetworkStateNotifier* notifier)
33    : m_online(false)
34    , m_networkAccessAllowed(true)
35    , m_notifier(notifier)
36{
37    ASSERT(notifier);
38
39    // Initialization is delayed because QNetworkConfigurationManager starts a new thread that causes
40    // deadlock on Mac because all the static initializers share the same lock. Both NetworkStateNotifier and Qt internals
41    // triggered in new thread use static initializer. See also: http://openradar.appspot.com/11217150.
42    QTimer::singleShot(0, this, SLOT(initialize()));
43}
44
45void NetworkStateNotifierPrivate::setNetworkAccessAllowed(bool isAllowed)
46{
47    if (isAllowed == m_networkAccessAllowed)
48        return;
49
50    m_networkAccessAllowed = isAllowed;
51    if (m_online)
52        m_notifier->updateState();
53}
54
55void NetworkStateNotifierPrivate::setOnlineState(bool isOnline)
56{
57    if (m_online == isOnline)
58        return;
59
60    m_online = isOnline;
61    if (m_networkAccessAllowed)
62        m_notifier->updateState();
63}
64
65void NetworkStateNotifierPrivate::initialize()
66{
67    m_configurationManager = adoptPtr(new QNetworkConfigurationManager());
68    setOnlineState(m_configurationManager->isOnline());
69    connect(m_configurationManager.get(), SIGNAL(onlineStateChanged(bool)), this, SLOT(setOnlineState(bool)));
70}
71
72NetworkStateNotifierPrivate::~NetworkStateNotifierPrivate()
73{
74}
75
76void NetworkStateNotifier::updateState()
77{
78    if (m_isOnLine == p->effectivelyOnline())
79        return;
80
81    m_isOnLine = p->effectivelyOnline();
82    if (m_networkStateChangedFunction)
83        m_networkStateChangedFunction();
84}
85
86NetworkStateNotifier::NetworkStateNotifier()
87    : m_isOnLine(true)
88    , m_networkStateChangedFunction(0)
89{
90    p = new NetworkStateNotifierPrivate(this);
91    m_isOnLine = p->effectivelyOnline();
92}
93
94void NetworkStateNotifier::setNetworkAccessAllowed(bool isAllowed)
95{
96    p->setNetworkAccessAllowed(isAllowed);
97}
98
99} // namespace WebCore
100
101#include "moc_NetworkStateNotifierPrivate.cpp"
102
103#endif
104