1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* vim:expandtab:shiftwidth=4:tabstop=4:
4 /* ***** BEGIN LICENSE BLOCK *****
5 * Version: NPL 1.1/GPL 2.0/LGPL 2.1
8 * The contents of this file are subject to the Mozilla Public
9 * License Version 1.1 (the "License"); you may not use this file
10 * except in compliance with the License. You may obtain a copy of
11 * the License at http://www.mozilla.org/MPL/
13 * Software distributed under the License is distributed on an "AS
14 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
15 * implied. See the License for the specific language governing
16 * rights and limitations under the License.
18 * The Original Code is Novell code.
20 * The Initial Developer of the Original Code is Novell, Inc.
21 * Portions created by the Initial Developer are Copyright (C) 2006
22 * the Initial Developer. All Rights Reserved.
24 * Original Author: Robert O'Callahan (rocallahan@novell.com)
28 * Alternatively, the contents of this file may be used under the terms of
29 * either the GNU General Public License Version 2 or later (the "GPL"), or
30 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
31 * in which case the provisions of the GPL or the LGPL are applicable instead
32 * of those above. If you wish to allow use of your version of this file only
33 * under the terms of either the GPL or the LGPL, and not to allow others to
34 * use your version of this file under the terms of the NPL, indicate your
35 * decision by deleting the provisions above and replace them with the notice
36 * and other provisions required by the GPL or the LGPL. If you do not delete
37 * the provisions above, a recipient may use your version of this file under
38 * the terms of any one of the NPL, the GPL or the LGPL.
40 * ***** END LICENSE BLOCK ***** */
42 #include "nsNetworkManagerListener.h"
45 #include "nsServiceManagerUtils.h"
46 #include "nsIObserverService.h"
47 #include "nsStringAPI.h"
48 #include "nsIPrefBranch2.h"
49 #include "nsIPrefService.h"
51 // Define NetworkManager API constants. This avoids a dependency on
52 // NetworkManager-devel.
53 #define NM_DBUS_SERVICE "org.freedesktop.NetworkManager"
54 #define NM_DBUS_PATH "/org/freedesktop/NetworkManager"
55 #define NM_DBUS_INTERFACE "org.freedesktop.NetworkManager"
56 #define NM_DBUS_SIGNAL_STATE_CHANGE "StateChange"
66 nsNetworkManagerListener::nsNetworkManagerListener() :
67 mLinkUp(PR_TRUE
), mNetworkManagerActive(PR_FALSE
),
68 mOK(PR_TRUE
), mManageIOService(PR_TRUE
)
72 nsNetworkManagerListener::~nsNetworkManagerListener() {
74 mDBUS
->RemoveClient(this);
78 NS_IMPL_ISUPPORTS1(nsNetworkManagerListener
, nsINetworkLinkService
)
81 nsNetworkManagerListener::GetIsLinkUp(PRBool
* aIsUp
) {
87 nsNetworkManagerListener::GetLinkStatusKnown(PRBool
* aKnown
) {
88 *aKnown
= mNetworkManagerActive
;
93 nsNetworkManagerListener::Init() {
94 mDBUS
= nsDBusService::Get();
96 return NS_ERROR_OUT_OF_MEMORY
;
97 nsresult rv
= mDBUS
->AddClient(this);
103 return NS_ERROR_FAILURE
;
108 NetworkStatusNotify(DBusPendingCall
*pending
, void* user_data
) {
109 DBusMessage
* msg
= dbus_pending_call_steal_reply(pending
);
112 if (dbus_message_get_type(msg
) == DBUS_MESSAGE_TYPE_METHOD_RETURN
) {
113 static_cast<nsNetworkManagerListener
*>(user_data
)->UpdateNetworkStatus(msg
);
115 dbus_message_unref(msg
);
119 nsNetworkManagerListener::RegisterWithConnection(DBusConnection
* connection
) {
121 dbus_error_init(&error
);
123 dbus_bus_add_match(connection
,
125 "interface='" NM_DBUS_INTERFACE
"',"
126 "sender='" NM_DBUS_SERVICE
"',"
127 "path='" NM_DBUS_PATH
"'", &error
);
128 mOK
= !dbus_error_is_set(&error
);
129 dbus_error_free(&error
);
134 dbus_message_new_method_call(NM_DBUS_SERVICE
, NM_DBUS_PATH
,
135 NM_DBUS_INTERFACE
, "state");
141 DBusPendingCall
* reply
= mDBUS
->SendWithReply(this, msg
);
147 dbus_pending_call_set_notify(reply
, NetworkStatusNotify
, this, NULL
);
148 dbus_pending_call_unref(reply
);
152 nsNetworkManagerListener::NotifyNetworkStatusObservers() {
153 nsCOMPtr
<nsIObserverService
> observerService
=
154 do_GetService("@mozilla.org/observer-service;1");
155 if (!observerService
)
158 const PRUnichar
* status
;
159 if (mNetworkManagerActive
) {
160 status
= mLinkUp
? NS_LITERAL_STRING(NS_NETWORK_LINK_DATA_UP
).get()
161 : NS_LITERAL_STRING(NS_NETWORK_LINK_DATA_DOWN
).get();
163 status
= NS_LITERAL_STRING(NS_NETWORK_LINK_DATA_UNKNOWN
).get();
166 observerService
->NotifyObservers(static_cast<nsISupports
*>(this),
167 NS_NETWORK_LINK_TOPIC
, status
);
171 nsNetworkManagerListener::UnregisterWithConnection(DBusConnection
* connection
) {
172 mNetworkManagerActive
= PR_FALSE
;
173 NotifyNetworkStatusObservers();
177 nsNetworkManagerListener::HandleMessage(DBusMessage
* message
) {
178 if (dbus_message_is_signal(message
, NM_DBUS_INTERFACE
,
179 NM_DBUS_SIGNAL_STATE_CHANGE
)) {
180 UpdateNetworkStatus(message
);
187 nsNetworkManagerListener::UpdateNetworkStatus(DBusMessage
* msg
) {
189 if (!dbus_message_get_args(msg
, NULL
, DBUS_TYPE_UINT32
, &result
,
193 // Don't update status if disabled by pref
194 nsCOMPtr
<nsIPrefBranch2
> prefs
=
195 do_GetService(NS_PREFSERVICE_CONTRACTID
);
197 PRBool ignore
= PR_FALSE
;
198 prefs
->GetBoolPref("toolkit.networkmanager.disable", &ignore
);
203 mNetworkManagerActive
= PR_TRUE
;
205 PRBool wasUp
= mLinkUp
;
206 mLinkUp
= result
== NM_STATE_CONNECTED
;
207 if (wasUp
== mLinkUp
)
210 NotifyNetworkStatusObservers();