1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=2 et sw=2 tw=80: */
3 /* ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
16 * The Original Code is Nokia.
18 * The Initial Developer of the Original Code is Nokia Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 2009
20 * the Initial Developer. All Rights Reserved.
23 * Jeremias Bosch <jeremias.bosch@gmail.com>
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
39 #include "nsMaemoNetworkManager.h"
40 #include "mozilla/Monitor.h"
42 #include <dbus/dbus.h>
43 #include <dbus/dbus-glib-lowlevel.h>
45 #include <conic/conicconnection.h>
46 #include <conic/conicconnectionevent.h>
47 #include <conicstatisticsevent.h>
52 #include "nsIOService.h"
53 #include "nsIObserverService.h"
54 #include "nsIOService.h"
56 #include "nsThreadUtils.h"
58 #include "nsINetworkLinkService.h"
62 InternalState_Invalid
= -1,
63 InternalState_Disconnected
,
64 InternalState_Connected
67 static InternalState gInternalState
= InternalState_Invalid
;
68 static ConIcConnection
* gConnection
= nsnull
;
69 static PRBool gConnectionCallbackInvoked
= PR_FALSE
;
71 using namespace mozilla
;
73 static Monitor
* gMonitor
= nsnull
;
75 static void NotifyNetworkLinkObservers()
77 nsCOMPtr
<nsIIOService
> ioService
= do_GetService("@mozilla.org/network/io-service;1");
81 ioService
->SetOffline(gInternalState
!= InternalState_Connected
);
85 connection_event_callback(ConIcConnection
*aConnection
,
86 ConIcConnectionEvent
*aEvent
,
89 ConIcConnectionStatus status
= con_ic_connection_event_get_status(aEvent
);
91 MonitorAutoEnter
mon(*gMonitor
);
93 // When we are not connected, we are always disconnected.
94 gInternalState
= (CON_IC_STATUS_CONNECTED
== status
?
95 InternalState_Connected
: InternalState_Disconnected
);
97 gConnectionCallbackInvoked
= PR_TRUE
;
101 NotifyNetworkLinkObservers();
105 nsMaemoNetworkManager::OpenConnectionSync()
107 if (NS_IsMainThread() || !gConnection
)
110 // protect gInternalState. This also allows us
111 // to block and wait in this method on this thread
112 // until our callback on the main thread.
113 MonitorAutoEnter
mon(*gMonitor
);
115 gConnectionCallbackInvoked
= PR_FALSE
;
117 if (!con_ic_connection_connect(gConnection
,
118 CON_IC_CONNECT_FLAG_NONE
))
119 g_error("openConnectionSync: Error while connecting. %p \n",
120 (void*) PR_GetCurrentThread());
122 while (!gConnectionCallbackInvoked
)
125 if (gInternalState
== InternalState_Connected
)
132 nsMaemoNetworkManager::CloseConnection()
135 con_ic_connection_disconnect(gConnection
);
139 nsMaemoNetworkManager::IsConnected()
141 return gInternalState
== InternalState_Connected
;
145 nsMaemoNetworkManager::GetLinkStatusKnown()
147 return gInternalState
!= InternalState_Invalid
;
151 nsMaemoNetworkManager::Startup()
156 gMonitor
= new Monitor("MaemoAutodialer");
161 dbus_error_init(&error
);
163 DBusConnection
* dbusConnection
= dbus_bus_get(DBUS_BUS_SYSTEM
, &error
);
164 NS_ASSERTION(dbusConnection
, "Error when connecting to the session bus");
166 dbus_connection_setup_with_g_main(dbusConnection
, nsnull
);
168 // grab a connection:
169 gConnection
= con_ic_connection_new();
170 NS_ASSERTION(gConnection
, "Error when creating connection");
177 g_signal_connect(G_OBJECT(gConnection
),
179 G_CALLBACK(connection_event_callback
),
182 g_object_set(G_OBJECT(gConnection
),
183 "automatic-connection-events",
190 nsMaemoNetworkManager::Shutdown()
192 gConnection
= nsnull
;
195 // notify anyone waiting
196 MonitorAutoEnter
mon(*gMonitor
);
197 gInternalState
= InternalState_Invalid
;
201 // We are leaking the gMonitor because a race condition could occur. We need
202 // a notification after xpcom-shutdown-threads so we can safely delete the monitor