1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /* vim:expandtab:shiftwidth=4:tabstop=4:
3 : */
4 : /* ***** BEGIN LICENSE BLOCK *****
5 : * Version: NPL 1.1/GPL 2.0/LGPL 2.1
6 : *
7 : *
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/
12 : *
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.
17 : *
18 : * The Original Code is Novell code.
19 : *
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.
23 : *
24 : * Original Author: Robert O'Callahan (rocallahan@novell.com)
25 : *
26 : * Contributor(s):
27 : *
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.
39 : *
40 : * ***** END LICENSE BLOCK ***** */
41 :
42 : #include "nsNetworkManagerListener.h"
43 :
44 : #include "nsNetCID.h"
45 : #include "nsServiceManagerUtils.h"
46 : #include "nsIObserverService.h"
47 : #include "nsStringAPI.h"
48 : #include "nsCRT.h"
49 :
50 : // Define NetworkManager API constants. This avoids a dependency on
51 : // NetworkManager-devel.
52 : #define NM_DBUS_SERVICE "org.freedesktop.NetworkManager"
53 : #define NM_DBUS_PATH "/org/freedesktop/NetworkManager"
54 : #define NM_DBUS_INTERFACE "org.freedesktop.NetworkManager"
55 : #define NM_DBUS_SIGNAL_STATE_CHANGE "StateChange" /* Deprecated in 0.7.x */
56 : #define NM_DBUS_SIGNAL_STATE_CHANGED "StateChanged"
57 :
58 : #define NM_STATE_CONNECTED_OLD 3 /* Before NM 0.9.0 */
59 : #define NM_STATE_CONNECTED_LOCAL 50
60 : #define NM_STATE_CONNECTED_SITE 60
61 : #define NM_STATE_CONNECTED_GLOBAL 70
62 :
63 1386 : nsNetworkManagerListener::nsNetworkManagerListener() :
64 : mLinkUp(true), mNetworkManagerActive(false),
65 1386 : mOK(true), mManageIOService(true)
66 : {
67 1386 : }
68 :
69 4149 : nsNetworkManagerListener::~nsNetworkManagerListener() {
70 1383 : if (mDBUS) {
71 1383 : mDBUS->RemoveClient(this);
72 : }
73 5532 : }
74 :
75 12488 : NS_IMPL_ISUPPORTS1(nsNetworkManagerListener, nsINetworkLinkService)
76 :
77 : nsresult
78 1 : nsNetworkManagerListener::GetIsLinkUp(bool* aIsUp) {
79 1 : *aIsUp = mLinkUp;
80 1 : return NS_OK;
81 : }
82 :
83 : nsresult
84 0 : nsNetworkManagerListener::GetLinkStatusKnown(bool* aKnown) {
85 0 : *aKnown = mNetworkManagerActive;
86 0 : return NS_OK;
87 : }
88 :
89 : nsresult
90 0 : nsNetworkManagerListener::GetLinkType(PRUint32 *aLinkType)
91 : {
92 0 : NS_ENSURE_ARG_POINTER(aLinkType);
93 :
94 : // XXX This function has not yet been implemented for this platform
95 0 : *aLinkType = nsINetworkLinkService::LINK_TYPE_UNKNOWN;
96 0 : return NS_OK;
97 : }
98 :
99 : nsresult
100 1386 : nsNetworkManagerListener::Init() {
101 1386 : mDBUS = nsDBusService::Get();
102 1386 : if (!mDBUS)
103 0 : return NS_ERROR_OUT_OF_MEMORY;
104 1386 : nsresult rv = mDBUS->AddClient(this);
105 1386 : if (NS_FAILED(rv)) {
106 0 : mDBUS = nsnull;
107 0 : return rv;
108 : }
109 1386 : if (!mOK)
110 0 : return NS_ERROR_FAILURE;
111 1386 : return NS_OK;
112 : }
113 :
114 : static void
115 1386 : NetworkStatusNotify(DBusPendingCall *pending, void* user_data) {
116 1386 : DBusMessage* msg = dbus_pending_call_steal_reply(pending);
117 1386 : if (!msg)
118 0 : return;
119 1386 : if (dbus_message_get_type(msg) == DBUS_MESSAGE_TYPE_METHOD_RETURN) {
120 0 : static_cast<nsNetworkManagerListener*>(user_data)->UpdateNetworkStatus(msg);
121 : }
122 1386 : dbus_message_unref(msg);
123 : }
124 :
125 : void
126 1386 : nsNetworkManagerListener::RegisterWithConnection(DBusConnection* connection) {
127 : DBusError error;
128 1386 : dbus_error_init(&error);
129 :
130 : dbus_bus_add_match(connection,
131 : "type='signal',"
132 : "interface='" NM_DBUS_INTERFACE "',"
133 : "sender='" NM_DBUS_SERVICE "',"
134 1386 : "path='" NM_DBUS_PATH "'", &error);
135 1386 : mOK = !dbus_error_is_set(&error);
136 1386 : dbus_error_free(&error);
137 1386 : if (!mOK)
138 0 : return;
139 :
140 : DBusMessage* msg =
141 : dbus_message_new_method_call(NM_DBUS_SERVICE, NM_DBUS_PATH,
142 1386 : NM_DBUS_INTERFACE, "state");
143 1386 : if (!msg) {
144 0 : mOK = false;
145 0 : return;
146 : }
147 :
148 1386 : DBusPendingCall* reply = mDBUS->SendWithReply(this, msg);
149 1386 : if (!reply) {
150 0 : mOK = false;
151 0 : return;
152 : }
153 :
154 1386 : dbus_pending_call_set_notify(reply, NetworkStatusNotify, this, NULL);
155 1386 : dbus_pending_call_unref(reply);
156 : }
157 :
158 : void
159 0 : nsNetworkManagerListener::NotifyNetworkStatusObservers() {
160 : nsCOMPtr<nsIObserverService> observerService =
161 0 : do_GetService("@mozilla.org/observer-service;1");
162 0 : if (!observerService)
163 : return;
164 :
165 : const PRUnichar* status;
166 0 : if (mNetworkManagerActive) {
167 0 : status = mLinkUp ? NS_LITERAL_STRING(NS_NETWORK_LINK_DATA_UP).get()
168 0 : : NS_LITERAL_STRING(NS_NETWORK_LINK_DATA_DOWN).get();
169 : } else {
170 0 : status = NS_LITERAL_STRING(NS_NETWORK_LINK_DATA_UNKNOWN).get();
171 : }
172 :
173 0 : observerService->NotifyObservers(static_cast<nsISupports*>(this),
174 0 : NS_NETWORK_LINK_TOPIC, status);
175 : }
176 :
177 : void
178 0 : nsNetworkManagerListener::UnregisterWithConnection(DBusConnection* connection) {
179 0 : mNetworkManagerActive = false;
180 0 : NotifyNetworkStatusObservers();
181 0 : }
182 :
183 : bool
184 1386 : nsNetworkManagerListener::HandleMessage(DBusMessage* message) {
185 2772 : if (dbus_message_is_signal(message, NM_DBUS_INTERFACE,
186 1386 : NM_DBUS_SIGNAL_STATE_CHANGE) ||
187 : dbus_message_is_signal(message, NM_DBUS_INTERFACE,
188 1386 : NM_DBUS_SIGNAL_STATE_CHANGED)) {
189 0 : UpdateNetworkStatus(message);
190 0 : return true;
191 : }
192 1386 : return false;
193 : }
194 :
195 : void
196 0 : nsNetworkManagerListener::UpdateNetworkStatus(DBusMessage* msg) {
197 : PRInt32 result;
198 0 : if (!dbus_message_get_args(msg, NULL, DBUS_TYPE_UINT32, &result,
199 0 : DBUS_TYPE_INVALID))
200 0 : return;
201 :
202 0 : mNetworkManagerActive = true;
203 :
204 0 : bool wasUp = mLinkUp;
205 : mLinkUp = result == NM_STATE_CONNECTED_OLD ||
206 : result == NM_STATE_CONNECTED_LOCAL ||
207 : result == NM_STATE_CONNECTED_SITE ||
208 0 : result == NM_STATE_CONNECTED_GLOBAL;
209 0 : if (wasUp == mLinkUp)
210 0 : return;
211 :
212 0 : NotifyNetworkStatusObservers();
213 : }
214 :
|