Rename firefox-*.mac64.dmg to firefox-*.mac.dmg. r=ted, a=benjamin, CLOSED TREE
[gecko.git] / netwerk / system / maemo / nsMaemoNetworkManager.cpp
bloba3ed750fe7e2ea5e5318aac8117d891652040457
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
14 * License.
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.
22 * Contributor(s):
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>
49 #include <stdio.h>
50 #include <unistd.h>
52 #include "nsIOService.h"
53 #include "nsIObserverService.h"
54 #include "nsIOService.h"
55 #include "nsCOMPtr.h"
56 #include "nsThreadUtils.h"
58 #include "nsINetworkLinkService.h"
60 enum InternalState
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");
78 if (!ioService)
79 return;
81 ioService->SetOffline(gInternalState != InternalState_Connected);
84 static void
85 connection_event_callback(ConIcConnection *aConnection,
86 ConIcConnectionEvent *aEvent,
87 gpointer aUser_data)
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;
98 mon.Notify();
101 NotifyNetworkLinkObservers();
104 PRBool
105 nsMaemoNetworkManager::OpenConnectionSync()
107 if (NS_IsMainThread() || !gConnection)
108 return PR_FALSE;
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)
123 mon.Wait();
125 if (gInternalState == InternalState_Connected)
126 return PR_TRUE;
128 return PR_FALSE;
131 void
132 nsMaemoNetworkManager::CloseConnection()
134 if (gConnection)
135 con_ic_connection_disconnect(gConnection);
138 PRBool
139 nsMaemoNetworkManager::IsConnected()
141 return gInternalState == InternalState_Connected;
144 PRBool
145 nsMaemoNetworkManager::GetLinkStatusKnown()
147 return gInternalState != InternalState_Invalid;
150 PRBool
151 nsMaemoNetworkManager::Startup()
153 if (gConnection)
154 return PR_TRUE;
156 gMonitor = new Monitor("MaemoAutodialer");
157 if (!gMonitor)
158 return PR_FALSE;
160 DBusError error;
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");
171 if (!gConnection) {
172 delete gMonitor;
173 gMonitor = nsnull;
174 return PR_FALSE;
177 g_signal_connect(G_OBJECT(gConnection),
178 "connection-event",
179 G_CALLBACK(connection_event_callback),
180 nsnull);
182 g_object_set(G_OBJECT(gConnection),
183 "automatic-connection-events",
184 PR_TRUE,
185 nsnull);
186 return PR_TRUE;
189 void
190 nsMaemoNetworkManager::Shutdown()
192 gConnection = nsnull;
194 if (gMonitor) {
195 // notify anyone waiting
196 MonitorAutoEnter mon(*gMonitor);
197 gInternalState = InternalState_Invalid;
198 mon.Notify();
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