Bug 495674: Internet connection should be initiated if needed [r=biesi]
authorMark Finkle <mfinkle@mozilla.com>
Wed, 18 Nov 2009 10:20:45 -0500
changeset 35003 784cd37c4665339158201245c2374aa0584aee3a
parent 35002 d0badc9bc4965fb73b50a68263faec293ee084dc
child 35004 df8f6d952af0a7dc50d204c6014711e77a077ec6
push idunknown
push userunknown
push dateunknown
reviewersbiesi
bugs495674
milestone1.9.3a1pre
Bug 495674: Internet connection should be initiated if needed [r=biesi]
config/autoconf.mk.in
configure.in
netwerk/base/src/Makefile.in
netwerk/base/src/nsAutodialMaemo.cpp
netwerk/base/src/nsAutodialMaemo.h
netwerk/base/src/nsIOService.cpp
netwerk/base/src/nsNativeConnectionHelper.cpp
netwerk/base/src/nsSocketTransport2.cpp
netwerk/build/Makefile.in
netwerk/build/nsNetModule.cpp
netwerk/system/Makefile.in
netwerk/system/maemo/Makefile.in
netwerk/system/maemo/nsMaemoNetworkLinkService.cpp
netwerk/system/maemo/nsMaemoNetworkLinkService.h
netwerk/system/maemo/nsMaemoNetworkManager.cpp
netwerk/system/maemo/nsMaemoNetworkManager.h
toolkit/library/Makefile.in
toolkit/toolkit-tiers.mk
--- a/config/autoconf.mk.in
+++ b/config/autoconf.mk.in
@@ -625,16 +625,20 @@ MOZ_PLATFORM_HILDON = @MOZ_PLATFORM_HILD
 
 LIBHILDONMIME_CFLAGS	= @LIBHILDONMIME_CFLAGS@
 LIBHILDONMIME_LIBS	= @LIBHILDONMIME_LIBS@
 LIBOSSO_CFLAGS 	= @LIBOSSO_CFLAGS@
 LIBOSSO_LIBS 	= @LIBOSSO_LIBS@
 LIBHILDONFM_CFLAGS	= @LIBHILDONFM_CFLAGS@
 LIBHILDONFM_LIBS	= @LIBHILDONFM_LIBS@
 
+MOZ_ENABLE_LIBCONIC = @MOZ_ENABLE_LIBCONIC@
+LIBCONIC_CFLAGS     = @LIBCONIC_CFLAGS@
+LIBCONIC_LIBS       = @LIBCONIC_LIBS@
+
 MACOS_SDK_DIR	= @MACOS_SDK_DIR@
 NEXT_ROOT	= @NEXT_ROOT@
 GCC_VERSION	= @GCC_VERSION@
 XCODEBUILD_VERSION= @XCODEBUILD_VERSION@
 HAS_XCODE_2_1	= @HAS_XCODE_2_1@
 UNIVERSAL_BINARY= @UNIVERSAL_BINARY@
 HAVE_DTRACE= @HAVE_DTRACE@
 
--- a/configure.in
+++ b/configure.in
@@ -6198,16 +6198,38 @@ dnl = Disable zipwriter
 dnl ========================================================
 MOZ_ARG_DISABLE_BOOL(zipwriter,
 [  --disable-zipwriter             Disable zipwriter component],
     MOZ_ZIPWRITER=,
     MOZ_ZIPWRITER=1 )
 AC_SUBST(MOZ_ZIPWRITER)
 
 dnl ========================================================
+dnl = Disable libconic
+dnl ========================================================
+MOZ_ENABLE_LIBCONIC=1
+MOZ_ARG_DISABLE_BOOL(libconic,
+[  --disable-libconic              Disable libconic],
+    MOZ_ENABLE_LIBCONIC=,
+    MOZ_ENABLE_LIBCONIC=1 )
+
+if test -n "$MOZ_ENABLE_LIBCONIC"; then
+    PKG_CHECK_MODULES(LIBCONIC, conic,
+                      MOZ_ENABLE_LIBCONIC=1,
+                      MOZ_ENABLE_LIBCONIC=)
+fi
+if test "$MOZ_ENABLE_LIBCONIC"; then
+    AC_DEFINE(MOZ_ENABLE_LIBCONIC)
+fi
+
+AC_SUBST(MOZ_ENABLE_LIBCONIC)
+AC_SUBST(LIBCONIC_CFLAGS)
+AC_SUBST(LIBCONIC_LIBS)
+ 
+dnl ========================================================
 dnl = Hildon and OSSO checks
 dnl ========================================================
 PKG_CHECK_MODULES(LIBHILDONMIME,libhildonmime,
                   MOZ_PLATFORM_HILDON=1,
                   MOZ_PLATFORM_HILDON=)
 if test $MOZ_PLATFORM_HILDON; then
    AC_DEFINE(MOZ_PLATFORM_HILDON)
    X11_COMPOSITED_PLUGINS="yes"
--- a/netwerk/base/src/Makefile.in
+++ b/netwerk/base/src/Makefile.in
@@ -87,16 +87,21 @@ CPPSRCS		= \
 		nsBase64Encoder.cpp \
 		nsSerializationHelper.cpp \
 		nsDNSPrefetch.cpp \
 		$(NULL)
 
 ifeq ($(MOZ_WIDGET_TOOLKIT),os2)
 	CPPSRCS += nsURLHelperOS2.cpp
 else
+ifdef MOZ_ENABLE_LIBCONIC
+	CPPSRCS += nsAutodialMaemo.cpp
+	CPPSRCS += nsNativeConnectionHelper.cpp
+	LOCAL_INCLUDES += -I$(srcdir)/../../system/maemo
+endif
 ifeq ($(MOZ_WIDGET_TOOLKIT),windows)
 	CPPSRCS += nsURLHelperWin.cpp
 	CPPSRCS += nsNativeConnectionHelper.cpp
 ifneq ($(OS_ARCH), WINCE)
 	CPPSRCS += nsAutodialWin.cpp
 else
 	CPPSRCS += nsAutodialWinCE.cpp
 endif
@@ -119,8 +124,12 @@ EXTRA_JS_MODULES = \
 
 # we don't want the shared lib, but we want to force the creation of a
 # static lib.
 FORCE_STATIC_LIB = 1
 
 include $(topsrcdir)/config/rules.mk
 
 DEFINES += -DIMPL_NS_NET
+
+ifdef MOZ_ENABLE_LIBCONIC
+	OS_INCLUDES += $(GLIB_CFLAGS) $(LIBCONIC_CFLAGS)
+endif
new file mode 100644
--- /dev/null
+++ b/netwerk/base/src/nsAutodialMaemo.cpp
@@ -0,0 +1,77 @@
+/* ***** BEGIN LICENSE BLOCK *****
+ * Version: MPL 1.1/GPL 2.0/LGPL 2.1
+ *
+ * The contents of this file are subject to the Mozilla Public License Version
+ * 1.1 (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ * http://www.mozilla.org/MPL/
+ *
+ * Software distributed under the License is distributed on an "AS IS" basis,
+ * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
+ * for the specific language governing rights and limitations under the
+ * License.
+ *
+ * The Original Code is MaemoAutodial.
+ *
+ * The Initial Developer of the Original Code is
+ * Nokia Corporation.
+ * Portions created by the Initial Developer are Copyright (C) 2009
+ * the Initial Developer. All Rights Reserved.
+ *
+ * Contributor(s):
+ * Jeremias Bosch <jeremias.bosch@gmail.com>
+ *
+ * Alternatively, the contents of this file may be used under the terms of
+ * either the GNU General Public License Version 2 or later (the "GPL"), or
+ * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
+ * in which case the provisions of the GPL or the LGPL are applicable instead
+ * of those above. If you wish to allow use of your version of this file only
+ * under the terms of either the GPL or the LGPL, and not to allow others to
+ * use your version of this file under the terms of the MPL, indicate your
+ * decision by deleting the provisions above and replace them with the notice
+ * and other provisions required by the GPL or the LGPL. If you do not delete
+ * the provisions above, a recipient may use your version of this file under
+ * the terms of any one of the MPL, the GPL or the LGPL.
+ *
+ * ***** END LICENSE BLOCK ***** */
+
+#include "nsAutodialMaemo.h"
+#include "nsNetCID.h"
+#include "nsCOMPtr.h"
+#include "nsIPrefBranch.h"
+#include "nsIPrefService.h"
+#include "nsIServiceManager.h"
+#include "nsMaemoNetworkManager.h"
+
+
+nsAutodial::nsAutodial()
+{
+}
+
+nsAutodial::~nsAutodial()
+{
+}
+
+nsresult
+nsAutodial::Init()
+{
+  return NS_OK;
+}
+
+nsresult
+nsAutodial::DialDefault(const PRUnichar* hostName)
+{
+  if (nsMaemoNetworkManager::OpenConnectionSync())
+    return NS_OK;
+
+  return NS_ERROR_FAILURE;
+}
+
+PRBool
+nsAutodial::ShouldDialOnNetworkError()
+{
+  if (nsMaemoNetworkManager::IsConnected())
+    return PR_FALSE;
+
+  return PR_TRUE;
+}
new file mode 100644
--- /dev/null
+++ b/netwerk/base/src/nsAutodialMaemo.h
@@ -0,0 +1,59 @@
+/* ***** BEGIN LICENSE BLOCK *****
+ * Version: MPL 1.1/GPL 2.0/LGPL 2.1
+ *
+ * The contents of this file are subject to the Mozilla Public License Version
+ * 1.1 (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ * http://www.mozilla.org/MPL/
+ *
+ * Software distributed under the License is distributed on an "AS IS" basis,
+ * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
+ * for the specific language governing rights and limitations under the
+ * License.
+ *
+ * The Original Code is MaemoAutodial.
+ *
+ * The Initial Developer of the Original Code is
+ * Nokia Corporation.
+ * Portions created by the Initial Developer are Copyright (C) 2009
+ * the Initial Developer. All Rights Reserved.
+ *
+ * Contributor(s):
+ * Jeremias Bosch <jeremias.bosch@gmail.com>
+ *
+ * Alternatively, the contents of this file may be used under the terms of
+ * either the GNU General Public License Version 2 or later (the "GPL"), or
+ * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
+ * in which case the provisions of the GPL or the LGPL are applicable instead
+ * of those above. If you wish to allow use of your version of this file only
+ * under the terms of either the GPL or the LGPL, and not to allow others to
+ * use your version of this file under the terms of the MPL, indicate your
+ * decision by deleting the provisions above and replace them with the notice
+ * and other provisions required by the GPL or the LGPL. If you do not delete
+ * the provisions above, a recipient may use your version of this file under
+ * the terms of any one of the MPL, the GPL or the LGPL.
+ *
+ * ***** END LICENSE BLOCK ***** */
+
+#ifndef nsAutodialMaemo_h__
+#define nsAutodialMaemo_h__
+
+#include "nspr.h"
+#include "nscore.h"
+
+class nsAutodial
+{
+public:
+    nsAutodial();
+    ~nsAutodial();
+
+    nsresult Init();
+
+    // Dial the default RAS dialup connection.
+    nsresult DialDefault(const PRUnichar* hostName);
+
+    // Should we try to dial on network error?
+    PRBool ShouldDialOnNetworkError();
+};
+
+#endif /* nsAutodialMaemo_h__ */
--- a/netwerk/base/src/nsIOService.cpp
+++ b/netwerk/base/src/nsIOService.cpp
@@ -68,17 +68,17 @@
 #include "nsISocketTransport.h"
 #include "nsCRT.h"
 #include "nsSimpleNestedURI.h"
 #include "nsNetUtil.h"
 #include "nsThreadUtils.h"
 #include "nsIPermissionManager.h"
 #include "nsTArray.h"
 
-#if defined(XP_WIN)
+#if defined(XP_WIN) || defined(MOZ_ENABLE_LIBCONIC)
 #include "nsNativeConnectionHelper.h"
 #endif
 
 #define PORT_PREF_PREFIX           "network.security.ports."
 #define PORT_PREF(x)               PORT_PREF_PREFIX x
 #define AUTODIAL_PREF              "network.autodial-helper.enabled"
 #define MANAGE_OFFLINE_STATUS_PREF "network.manage-offline-status"
 
@@ -981,29 +981,28 @@ nsIOService::TrackNetworkLinkStatusForOf
     // check to make sure this won't collide with Autodial
     if (mSocketTransportService) {
         PRBool autodialEnabled = PR_FALSE;
         mSocketTransportService->GetAutodialEnabled(&autodialEnabled);
         // If autodialing-on-link-down is enabled, check if the OS auto dial 
         // option is set to always autodial. If so, then we are 
         // always up for the purposes of offline management.
         if (autodialEnabled) {
-#if defined(XP_WIN)
-            // On Windows, need to do some registry checking to see if
-            // autodial is enabled at the OS level. Only if that is
-            // enabled are we always up for the purposes of offline
-            // management.
+#if defined(XP_WIN) || defined(MOZ_ENABLE_LIBCONIC)
+            // On Windows and Maemo (libconic) we should first check with the OS
+            // to see if autodial is enabled.  If it is enabled then we are
+            // allowed to manage the offline state.
             if(nsNativeConnectionHelper::IsAutodialEnabled()) 
                 return SetOffline(PR_FALSE);
 #else
             return SetOffline(PR_FALSE);
 #endif
         }
     }
-  
+
     PRBool isUp;
     nsresult rv = mNetworkLinkService->GetIsLinkUp(&isUp);
     NS_ENSURE_SUCCESS(rv, rv);
     return SetOffline(!isUp);
 }
 
 NS_IMETHODIMP
 nsIOService::EscapeString(const nsACString& aString,
--- a/netwerk/base/src/nsNativeConnectionHelper.cpp
+++ b/netwerk/base/src/nsNativeConnectionHelper.cpp
@@ -32,39 +32,48 @@
  * decision by deleting the provisions above and replace them with the notice
  * and other provisions required by the GPL or the LGPL. If you do not delete
  * the provisions above, a recipient may use your version of this file under
  * the terms of any one of the MPL, the GPL or the LGPL.
  *
  * ***** END LICENSE BLOCK ***** */
 
 #include "nsNativeConnectionHelper.h"
-#ifdef WINCE
+
+#if defined(MOZ_ENABLE_LIBCONIC)
+#include "nsAutodialMaemo.h"
+#elif defined(WINCE)
 #include "nsAutodialWinCE.h"
 #else
 #include "nsAutodialWin.h"
 #endif
+
 #include "nsIOService.h"
 
 //-----------------------------------------------------------------------------
 // API typically invoked on the socket transport thread
 //-----------------------------------------------------------------------------
 
 PRBool
 nsNativeConnectionHelper::OnConnectionFailed(const PRUnichar* hostName)
 {
+  // On mobile platforms, instead of relying on the link service, we
+  // should ask the dialer directly.  This allows the dialer to update
+  // link status more forcefully rather than passively watching link
+  // status changes.
+#if !defined(MOZ_ENABLE_LIBCONIC) && !defined(WINCE_WINDOWS_MOBILE)
     if (gIOService->IsLinkUp())
         return PR_FALSE;
+#endif
 
     nsAutodial autodial;
-
-    if (autodial.ShouldDialOnNetworkError()) 
+    if (autodial.ShouldDialOnNetworkError())
         return NS_SUCCEEDED(autodial.DialDefault(hostName));
-    else
-        return PR_FALSE;
+
+    return PR_FALSE;
 }
 
 PRBool
 nsNativeConnectionHelper::IsAutodialEnabled()
 {
     nsAutodial autodial;
     return autodial.Init() == NS_OK && autodial.ShouldDialOnNetworkError();
 }
--- a/netwerk/base/src/nsSocketTransport2.cpp
+++ b/netwerk/base/src/nsSocketTransport2.cpp
@@ -66,17 +66,17 @@
 #include "nsISocketProviderService.h"
 #include "nsISocketProvider.h"
 #include "nsISSLSocketControl.h"
 #include "nsINSSErrorsService.h"
 #include "nsIPipe.h"
 #include "nsIProgrammingLanguage.h"
 #include "nsIClassInfoImpl.h"
 
-#if defined(XP_WIN)
+#if defined(XP_WIN) || defined(MOZ_ENABLE_LIBCONIC)
 #include "nsNativeConnectionHelper.h"
 #endif
 
 //-----------------------------------------------------------------------------
 
 static NS_DEFINE_CID(kSocketProviderServiceCID, NS_SOCKETPROVIDERSERVICE_CID);
 static NS_DEFINE_CID(kDNSServiceCID, NS_DNSSERVICE_CID);
 
@@ -1260,17 +1260,17 @@ nsSocketTransport::RecoverFromError()
     if (mState == STATE_CONNECTING && mDNSRecord) {
         nsresult rv = mDNSRecord->GetNextAddr(SocketPort(), &mNetAddr);
         if (NS_SUCCEEDED(rv)) {
             LOG(("  trying again with next ip address\n"));
             tryAgain = PR_TRUE;
         }
     }
 
-#if defined(XP_WIN)
+#if defined(XP_WIN) || defined(MOZ_ENABLE_LIBCONIC)
     // If not trying next address, try to make a connection using dialup. 
     // Retry if that connection is made.
     if (!tryAgain) {
         PRBool autodialEnabled;
         gSocketTransportService->GetAutodialEnabled(&autodialEnabled);
         if (autodialEnabled) {
           tryAgain = nsNativeConnectionHelper::OnConnectionFailed(
                        NS_ConvertUTF8toUTF16(SocketHost()).get());
--- a/netwerk/build/Makefile.in
+++ b/netwerk/build/Makefile.in
@@ -77,16 +77,21 @@ ifeq ($(OS_ARCH),WINCE)
         ../system/wince/$(LIB_PREFIX)neckosystem_s.$(LIB_SUFFIX)
 endif
 
 ifeq (cocoa,$(MOZ_WIDGET_TOOLKIT))
     SHARED_LIBRARY_LIBS += \
         ../system/mac/$(LIB_PREFIX)neckosystem_s.$(LIB_SUFFIX)
 endif
 
+ifdef MOZ_ENABLE_LIBCONIC
+    SHARED_LIBRARY_LIBS += \
+        ../system/maemo/$(LIB_PREFIX)neckosystem_s.$(LIB_SUFFIX)
+endif
+
 LOCAL_INCLUDES	= \
 		-I$(srcdir)/../base/src \
 		-I$(srcdir)/../dns/src \
 		-I$(srcdir)/../socket/base \
 		-I$(srcdir)/../streamconv/src \
 		-I$(srcdir)/../streamconv/converters \
 		-I$(srcdir)/../mime/src \
 		-I$(srcdir)/../cache/src \
@@ -102,16 +107,20 @@ endif
 ifeq ($(OS_ARCH),WINCE)
     LOCAL_INCLUDES += -I$(srcdir)/../system/wince
 endif
 
 ifeq (cocoa,$(MOZ_WIDGET_TOOLKIT))
     LOCAL_INCLUDES += -I$(srcdir)/../system/mac
 endif
 
+ifdef MOZ_ENABLE_LIBCONIC
+    LOCAL_INCLUDES += -I$(srcdir)/../system/maemo
+endif
+
 ifdef NECKO_COOKIES
 SHARED_LIBRARY_LIBS += \
 		../cookie/src/$(LIB_PREFIX)neckocookie_s.$(LIB_SUFFIX) \
                 $(NULL)
 LOCAL_INCLUDES	+= -I$(srcdir)/../cookie/src
 endif
 
 ifdef NECKO_WIFI
--- a/netwerk/build/nsNetModule.cpp
+++ b/netwerk/build/nsNetModule.cpp
@@ -290,16 +290,19 @@ NS_GENERIC_FACTORY_CONSTRUCTOR_INIT(nsID
 
 ///////////////////////////////////////////////////////////////////////////////
 #if defined(XP_WIN)
 #include "nsNotifyAddrListener.h"
 NS_GENERIC_FACTORY_CONSTRUCTOR_INIT(nsNotifyAddrListener, Init)
 #elif defined(MOZ_WIDGET_COCOA)
 #include "nsNetworkLinkService.h"
 NS_GENERIC_FACTORY_CONSTRUCTOR_INIT(nsNetworkLinkService, Init)
+#elif defined(MOZ_ENABLE_LIBCONIC)
+#include "nsMaemoNetworkLinkService.h"
+NS_GENERIC_FACTORY_CONSTRUCTOR_INIT(nsMaemoNetworkLinkService, Init)
 #endif
 
 ///////////////////////////////////////////////////////////////////////////////
 
 #ifdef NECKO_PROTOCOL_ftp
 #include "nsFTPDirListingConv.h"
 nsresult NS_NewFTPDirListingConv(nsFTPDirListingConv** result);
 #endif
@@ -1155,13 +1158,19 @@ static const nsModuleComponentInfo gNetM
       nsNotifyAddrListenerConstructor
     },
 #elif defined(MOZ_WIDGET_COCOA)
     { NS_NETWORK_LINK_SERVICE_CLASSNAME,
       NS_NETWORK_LINK_SERVICE_CID,
       NS_NETWORK_LINK_SERVICE_CONTRACTID,
       nsNetworkLinkServiceConstructor
     },
+#elif defined(MOZ_ENABLE_LIBCONIC)
+    { NS_NETWORK_LINK_SERVICE_CLASSNAME,
+      NS_NETWORK_LINK_SERVICE_CID,
+      NS_NETWORK_LINK_SERVICE_CONTRACTID,
+      nsMaemoNetworkLinkServiceConstructor
+    },
 #endif
 };
 
 NS_IMPL_NSGETMODULE_WITH_CTOR_DTOR(necko, gNetModuleInfo,
                                    nsNetStartup, nsNetShutdown)
--- a/netwerk/system/Makefile.in
+++ b/netwerk/system/Makefile.in
@@ -51,9 +51,13 @@ endif
 ifeq ($(OS_ARCH),WINCE)
     DIRS += wince
 endif
 
 ifeq (cocoa,$(MOZ_WIDGET_TOOLKIT))
     DIRS += mac
 endif
 
+ifdef MOZ_ENABLE_LIBCONIC
+    DIRS += maemo
+endif
+
 include $(topsrcdir)/config/rules.mk
new file mode 100644
--- /dev/null
+++ b/netwerk/system/maemo/Makefile.in
@@ -0,0 +1,51 @@
+#
+# * ***** BEGIN LICENSE BLOCK *****
+# Version: MPL 1.1
+#
+# The contents of this file are subject to the Mozilla Public License Version
+# 1.1 (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+# http://www.mozilla.org/MPL/
+#
+# Software distributed under the License is distributed on an "AS IS" basis,
+# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
+# for the specific language governing rights and limitations under the
+# License.
+#
+# The Original Code is MaemoAutodial.
+#
+# The Initial Developer of the Original Code is
+# Nokia Corporation.
+# Portions created by the Initial Developer are Copyright (C) 2009
+# the Initial Developer. All Rights Reserved.
+#
+# Contributor(s):
+# Jeremias Bosch <jeremias.bosch@gmail.com>
+#
+# ***** END LICENSE BLOCK *****
+
+DEPTH		= ../../..
+topsrcdir	= @top_srcdir@
+srcdir		= @srcdir@
+VPATH		= @srcdir@
+
+include $(DEPTH)/config/autoconf.mk
+
+MODULE	       = necko
+LIBRARY_NAME   = neckosystem_s
+LIBXUL_LIBRARY = 1
+
+FORCE_STATIC_LIB = 1
+
+CPPSRCS += \
+	nsMaemoNetworkLinkService.cpp \
+	nsMaemoNetworkManager.cpp \
+	$(NULL)
+
+include $(topsrcdir)/config/rules.mk
+
+DEFINES += -DIMPL_NS_NET
+
+OS_INCLUDES += $(GLIB_CFLAGS) $(LIBCONIC_CFLAGS)
+LOCAL_INCLUDES += -I$(srcdir)/../../base/src
+ 
new file mode 100644
--- /dev/null
+++ b/netwerk/system/maemo/nsMaemoNetworkLinkService.cpp
@@ -0,0 +1,93 @@
+/* ***** BEGIN LICENSE BLOCK *****
+ * Version: MPL 1.1
+ *
+ * The contents of this file are subject to the Mozilla Public License Version
+ * 1.1 (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ * http://www.mozilla.org/MPL/
+ *
+ * Software distributed under the License is distributed on an "AS IS" basis,
+ * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
+ * for the specific language governing rights and limitations under the
+ * License.
+ *
+ * The Original Code is MaemoAutodial.
+ *
+ * The Initial Developer of the Original Code is
+ * Nokia Corporation.
+ * Portions created by the Initial Developer are Copyright (C) 2009
+ * the Initial Developer. All Rights Reserved.
+ *
+ * Contributor(s):
+ * Jeremias Bosch <jeremias.bosch@gmail.com>
+ *
+ * ***** END LICENSE BLOCK ***** */
+
+#include "nsMaemoNetworkLinkService.h"
+#include "nsCOMPtr.h"
+#include "nsIObserverService.h"
+#include "nsServiceManagerUtils.h"
+#include "nsString.h"
+#include "nsMaemoNetworkManager.h"
+
+NS_IMPL_ISUPPORTS2(nsMaemoNetworkLinkService,
+                   nsINetworkLinkService,
+                   nsIObserver)
+
+nsMaemoNetworkLinkService::nsMaemoNetworkLinkService()
+{
+}
+
+nsMaemoNetworkLinkService::~nsMaemoNetworkLinkService()
+{
+}
+
+NS_IMETHODIMP
+nsMaemoNetworkLinkService::GetIsLinkUp(PRBool *aIsUp)
+{
+  *aIsUp = nsMaemoNetworkManager::IsConnected();
+  return NS_OK;
+}
+
+NS_IMETHODIMP
+nsMaemoNetworkLinkService::GetLinkStatusKnown(PRBool *aIsKnown)
+{
+  *aIsKnown = nsMaemoNetworkManager::GetLinkStatusKnown();
+  return NS_OK;
+}
+
+NS_IMETHODIMP
+nsMaemoNetworkLinkService::Observe(nsISupports *aSubject,
+                                   const char *aTopic,
+                                   const PRUnichar *aData)
+{
+  if (!strcmp(aTopic, "xpcom-shutdown"))
+    Shutdown();
+
+  return NS_OK;
+}
+
+nsresult
+nsMaemoNetworkLinkService::Init(void)
+{
+  nsresult rv;
+
+  nsCOMPtr<nsIObserverService> observerService =
+    do_GetService("@mozilla.org/observer-service;1", &rv);
+  NS_ENSURE_SUCCESS(rv, rv);
+
+  rv = observerService->AddObserver(this, "xpcom-shutdown", PR_FALSE);
+  NS_ENSURE_SUCCESS(rv, rv);
+
+  if (!nsMaemoNetworkManager::Startup())
+    return NS_ERROR_FAILURE;
+
+  return NS_OK;
+}
+
+nsresult
+nsMaemoNetworkLinkService::Shutdown()
+{
+  nsMaemoNetworkManager::Shutdown();
+  return NS_OK;
+}
new file mode 100644
--- /dev/null
+++ b/netwerk/system/maemo/nsMaemoNetworkLinkService.h
@@ -0,0 +1,47 @@
+/* ***** BEGIN LICENSE BLOCK *****
+ * Version: MPL 1.1
+ *
+ * The contents of this file are subject to the Mozilla Public License Version
+ * 1.1 (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ * http://www.mozilla.org/MPL/
+ *
+ * Software distributed under the License is distributed on an "AS IS" basis,
+ * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
+ * for the specific language governing rights and limitations under the
+ * License.
+ *
+ * The Original Code is MaemoAutodial.
+ *
+ * The Initial Developer of the Original Code is
+ * Nokia Corporation.
+ * Portions created by the Initial Developer are Copyright (C) 2009
+ * the Initial Developer. All Rights Reserved.
+ *
+ * Contributor(s):
+ * Jeremias Bosch <jeremias.bosch@gmail.com>
+ *
+ * ***** END LICENSE BLOCK ***** */
+
+#ifndef NSMAEMONETWORKLINKSERVICE_H_
+#define NSMAEMONETWORKLINKSERVICE_H_
+
+#include "nsINetworkLinkService.h"
+#include "nsIObserver.h"
+
+class nsMaemoNetworkLinkService: public nsINetworkLinkService,
+                                 public nsIObserver
+{
+public:
+  NS_DECL_ISUPPORTS
+  NS_DECL_NSINETWORKLINKSERVICE
+  NS_DECL_NSIOBSERVER
+
+  nsMaemoNetworkLinkService();
+  virtual ~nsMaemoNetworkLinkService();
+
+  nsresult Init();
+  nsresult Shutdown();
+};
+
+#endif /* NSMAEMONETWORKLINKSERVICE_H_ */
new file mode 100644
--- /dev/null
+++ b/netwerk/system/maemo/nsMaemoNetworkManager.cpp
@@ -0,0 +1,191 @@
+/* ***** BEGIN LICENSE BLOCK *****
+ * Version: MPL 1.1
+ *
+ * The contents of this file are subject to the Mozilla Public License Version
+ * 1.1 (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ * http://www.mozilla.org/MPL/
+ *
+ * Software distributed under the License is distributed on an "AS IS" basis,
+ * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
+ * for the specific language governing rights and limitations under the
+ * License.
+ *
+ * The Original Code is MaemoAutodial.
+ *
+ * The Initial Developer of the Original Code is
+ * Nokia Corporation.
+ * Portions created by the Initial Developer are Copyright (C) 2009
+ * the Initial Developer. All Rights Reserved.
+ *
+ * Contributor(s):
+ * Jeremias Bosch <jeremias.bosch@gmail.com>
+ *
+ * ***** END LICENSE BLOCK ***** */
+
+#include "nsMaemoNetworkManager.h"
+#include "mozilla/Monitor.h"
+
+#include <dbus/dbus.h>
+#include <dbus/dbus-glib-lowlevel.h>
+
+#include <conic/conicconnection.h>
+#include <conic/conicconnectionevent.h>
+#include <conicstatisticsevent.h>
+
+#include <stdio.h>
+#include <unistd.h>
+
+#include "nsIOService.h"
+#include "nsIObserverService.h"
+#include "nsIOService.h"
+#include "nsCOMPtr.h"
+#include "nsThreadUtils.h"
+
+#include "nsINetworkLinkService.h"
+
+enum InternalState
+{
+  InternalState_Invalid = -1,
+  InternalState_Disconnected,
+  InternalState_Connected
+};
+
+static InternalState gInternalState = InternalState_Invalid;
+static ConIcConnection* gConnection = nsnull;
+static PRBool gConnectionCallbackInvoked = PR_FALSE;
+
+using namespace mozilla;
+
+static Monitor* gMonitor = nsnull;
+
+static void NotifyNetworkLinkObservers()
+{
+  nsCOMPtr<nsIIOService> ioService = do_GetService("@mozilla.org/network/io-service;1");
+  if (!ioService)
+    return;
+
+  ioService->SetOffline(gInternalState != InternalState_Connected);
+}
+
+static void
+connection_event_callback(ConIcConnection *aConnection,
+                          ConIcConnectionEvent *aEvent,
+                          gpointer aUser_data)
+{
+  ConIcConnectionStatus status = con_ic_connection_event_get_status(aEvent);
+  {
+    MonitorAutoEnter mon(*gMonitor);
+
+    // When we are not connected, we are always disconnected.
+    gInternalState = (CON_IC_STATUS_CONNECTED == status ?
+                     InternalState_Connected : InternalState_Disconnected);
+
+    gConnectionCallbackInvoked = PR_TRUE;
+    mon.Notify();
+  }
+
+  NotifyNetworkLinkObservers();
+}
+
+PRBool
+nsMaemoNetworkManager::OpenConnectionSync()
+{
+  if (NS_IsMainThread() || !gConnection)
+    return PR_FALSE;
+
+  // protect gInternalState.  This also allows us
+  // to block and wait in this method on this thread
+  // until our callback on the main thread.
+  MonitorAutoEnter mon(*gMonitor);
+
+  gConnectionCallbackInvoked = PR_FALSE;
+
+  if (!con_ic_connection_connect(gConnection,
+                                 CON_IC_CONNECT_FLAG_NONE))
+    g_error("openConnectionSync: Error while connecting. %p \n",
+            (void*) PR_GetCurrentThread());
+
+  while (!gConnectionCallbackInvoked)
+    mon.Wait();
+
+  if (gInternalState == InternalState_Connected)
+    return PR_TRUE;
+
+  return PR_FALSE;
+}
+
+void
+nsMaemoNetworkManager::CloseConnection()
+{
+  if (gConnection)
+    con_ic_connection_disconnect(gConnection);
+}
+
+PRBool
+nsMaemoNetworkManager::IsConnected()
+{
+  return gInternalState == InternalState_Connected;
+}
+
+PRBool
+nsMaemoNetworkManager::GetLinkStatusKnown()
+{
+  return gInternalState != InternalState_Invalid;
+}
+
+PRBool
+nsMaemoNetworkManager::Startup()
+{
+  if (gConnection)
+    return PR_TRUE;
+
+  gMonitor = new Monitor("MaemoAutodialer");
+  if (!gMonitor)
+    return PR_FALSE;
+
+  DBusError error;
+  dbus_error_init(&error);
+
+  DBusConnection* dbusConnection = dbus_bus_get(DBUS_BUS_SYSTEM, &error);
+  NS_ASSERTION(dbusConnection, "Error when connecting to the session bus");
+
+  dbus_connection_setup_with_g_main(dbusConnection, nsnull);
+
+  // grab a connection:
+  gConnection = con_ic_connection_new();
+  NS_ASSERTION(gConnection, "Error when creating connection");
+  if (!gConnection) {
+    delete gMonitor;
+    gMonitor = nsnull;
+    return PR_FALSE;
+  }
+
+  g_signal_connect(G_OBJECT(gConnection),
+                   "connection-event",
+                   G_CALLBACK(connection_event_callback),
+                   nsnull);
+  
+  g_object_set(G_OBJECT(gConnection),
+               "automatic-connection-events",
+               PR_TRUE,
+               nsnull);
+  return PR_TRUE;
+}
+
+void
+nsMaemoNetworkManager::Shutdown()
+{
+  gConnection = nsnull;
+
+  if (gMonitor) {
+    // notify anyone waiting
+    MonitorAutoEnter mon(*gMonitor);
+    gInternalState = InternalState_Invalid;    
+    mon.Notify();
+  }
+  
+  // We are leaking the gMonitor because a race condition could occur. We need
+  // a notification after xpcom-shutdown-threads so we can safely delete the monitor
+}
+
new file mode 100644
--- /dev/null
+++ b/netwerk/system/maemo/nsMaemoNetworkManager.h
@@ -0,0 +1,46 @@
+/* ***** BEGIN LICENSE BLOCK *****
+ * Version: MPL 1.1
+ *
+ * The contents of this file are subject to the Mozilla Public License Version
+ * 1.1 (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ * http://www.mozilla.org/MPL/
+ *
+ * Software distributed under the License is distributed on an "AS IS" basis,
+ * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
+ * for the specific language governing rights and limitations under the
+ * License.
+ *
+ * The Original Code is MaemoAutodial.
+ *
+ * The Initial Developer of the Original Code is
+ * Nokia Corporation.
+ * Portions created by the Initial Developer are Copyright (C) 2009
+ * the Initial Developer. All Rights Reserved.
+ *
+ * Contributor(s):
+ * Jeremias Bosch <jeremias.bosch@gmail.com>
+ *
+ * ***** END LICENSE BLOCK ***** */
+
+#ifndef NSMAEMONETWORKMANAGER_H_
+#define NSMAEMONETWORKMANAGER_H_
+
+#include "nscore.h"
+
+class nsMaemoNetworkManager
+{
+public:
+  // Can be called from any thread, most likely the socket transport thread
+  static PRBool OpenConnectionSync();
+  static void CloseConnection();
+
+  static PRBool IsConnected();
+  static PRBool GetLinkStatusKnown();
+
+  // Called from the nsMaemoNetworkLinkService (main thread only)
+  static PRBool Startup();
+  static void Shutdown();
+};
+
+#endif /* NSMAEMONETWORKMANAGER_H_ */
--- a/toolkit/library/Makefile.in
+++ b/toolkit/library/Makefile.in
@@ -196,16 +196,20 @@ EXTRA_DSO_LDOPTS += \
 	$(NULL)
 endif
 endif
 
 ifdef MOZ_PLATFORM_HILDON
 EXTRA_DSO_LDOPTS += $(LIBHILDONMIME_LIBS) $(LIBHILDONFM_LIBS)
 endif 
 
+ifdef MOZ_ENABLE_LIBCONIC
+EXTRA_DSO_LDOPTS += $(LIBCONIC_LIBS)
+endif
+
 ifdef NS_OSSO
 EXTRA_DSO_LDOPTS += $(LIBOSSO_LIBS)
 endif
 
 ifdef MOZ_ENABLE_DBUS
 EXTRA_DSO_LDOPTS += $(MOZ_DBUS_GLIB_LIBS)
 endif
 
--- a/toolkit/toolkit-tiers.mk
+++ b/toolkit/toolkit-tiers.mk
@@ -235,19 +235,22 @@ endif
 ifdef NS_TRACE_MALLOC
 tier_toolkit_dirs += tools/trace-malloc
 endif
 
 ifdef MOZ_ENABLE_GNOME_COMPONENT
 tier_toolkit_dirs    += toolkit/system/gnome
 endif
 
+ifndef MOZ_ENABLE_LIBCONIC
+# if libconic is present, it will do its own network monitoring
 ifdef MOZ_ENABLE_DBUS
 tier_toolkit_dirs    += toolkit/system/dbus
 endif
+endif
 
 ifdef MOZ_LEAKY
 tier_toolkit_dirs        += tools/leaky
 endif
 
 ifdef MOZ_MAPINFO
 tier_toolkit_dirs	+= tools/codesighs
 endif