Bug 1064231 - unify instantiation process for RIL services. r=smaug
authorVicamo Yang <vyang@mozilla.com>
Sun, 21 Sep 2014 15:24:44 +0800
changeset 206417 a9a2db3660cbcb852cbfc556dcb0dd4c36ef81e3
parent 206416 381d918e37028e42031b69dec42e0c789da098b4
child 206418 455058215590b5d1d1e9de22b466cf1b8328862d
push id8888
push usercbook@mozilla.com
push dateMon, 22 Sep 2014 11:11:33 +0000
treeherderfx-team@c8d6b64c45e8 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssmaug
bugs1064231
milestone35.0a1
Bug 1064231 - unify instantiation process for RIL services. r=smaug
dom/mobileconnection/MobileConnectionArray.cpp
dom/mobileconnection/interfaces/nsIMobileConnectionService.idl
dom/mobileconnection/ipc/MobileConnectionIPCService.cpp
dom/mobileconnection/ipc/MobileConnectionIPCService.h
dom/mobileconnection/moz.build
dom/mobilemessage/MobileMessageManager.cpp
dom/mobilemessage/MobileMessageService.cpp
dom/mobilemessage/MobileMessageService.h
dom/mobilemessage/SmsServicesFactory.cpp
dom/mobilemessage/SmsServicesFactory.h
dom/mobilemessage/interfaces/nsIMmsService.idl
dom/mobilemessage/interfaces/nsIMobileMessageDatabaseService.idl
dom/mobilemessage/interfaces/nsIMobileMessageService.idl
dom/mobilemessage/interfaces/nsIRilMobileMessageDatabaseService.idl
dom/mobilemessage/interfaces/nsISmsService.idl
dom/mobilemessage/ipc/SmsIPCService.cpp
dom/mobilemessage/ipc/SmsIPCService.h
dom/mobilemessage/moz.build
dom/telephony/Telephony.cpp
dom/telephony/TelephonyFactory.cpp
dom/telephony/TelephonyFactory.h
dom/telephony/moz.build
dom/telephony/nsITelephonyService.idl
layout/build/nsLayoutModule.cpp
--- a/dom/mobileconnection/MobileConnectionArray.cpp
+++ b/dom/mobileconnection/MobileConnectionArray.cpp
@@ -4,16 +4,23 @@
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #include "mozilla/dom/MobileConnectionArray.h"
 #include "mozilla/dom/MozMobileConnectionArrayBinding.h"
 #include "mozilla/Preferences.h"
 #include "nsServiceManagerUtils.h"
 
+// Service instantiation
+#include "ipc/MobileConnectionIPCService.h"
+#if defined(MOZ_WIDGET_GONK) && defined(MOZ_B2G_RIL)
+#include "nsIGonkMobileConnectionService.h"
+#endif
+#include "nsXULAppAPI.h" // For XRE_GetProcessType()
+
 using namespace mozilla::dom;
 
 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(MobileConnectionArray,
                                       mWindow,
                                       mMobileConnections)
 
 NS_IMPL_CYCLE_COLLECTING_ADDREF(MobileConnectionArray)
 NS_IMPL_CYCLE_COLLECTING_RELEASE(MobileConnectionArray)
@@ -84,8 +91,24 @@ MobileConnectionArray::IndexedGetter(uin
   }
 
   if (!mMobileConnections[aIndex]) {
     mMobileConnections[aIndex] = new MobileConnection(mWindow, aIndex);
   }
 
   return mMobileConnections[aIndex];
 }
+
+already_AddRefed<nsIMobileConnectionService>
+NS_CreateMobileConnectionService()
+{
+  nsCOMPtr<nsIMobileConnectionService> service;
+
+  if (XRE_GetProcessType() == GeckoProcessType_Content) {
+    service = new mozilla::dom::mobileconnection::MobileConnectionIPCService();
+  } else {
+#if defined(MOZ_WIDGET_GONK) && defined(MOZ_B2G_RIL)
+    service = do_CreateInstance(GONK_MOBILECONNECTION_SERVICE_CONTRACTID);
+#endif
+  }
+
+  return service.forget();
+}
--- a/dom/mobileconnection/interfaces/nsIMobileConnectionService.idl
+++ b/dom/mobileconnection/interfaces/nsIMobileConnectionService.idl
@@ -204,16 +204,23 @@ interface nsIMobileConnectionCallback : 
 [scriptable, uuid(eaba3c4a-0dd5-4919-b1a2-7812e49dbbcb)]
 interface nsIMobileConnectionService : nsISupports
 {
   readonly attribute unsigned long numItems;
 
   nsIMobileConnection getItemByServiceId(in unsigned long serviceId);
 };
 
+%{C++
+template<typename T> struct already_AddRefed;
+
+already_AddRefed<nsIMobileConnectionService>
+NS_CreateMobileConnectionService();
+%}
+
 [scriptable, uuid(04db7331-54fe-4cf7-9347-b9481350f4c2)]
 interface nsIMobileConnection : nsISupports
 {
   const long ICC_SERVICE_CLASS_VOICE      = (1 << 0);
   const long ICC_SERVICE_CLASS_DATA       = (1 << 1);
   const long ICC_SERVICE_CLASS_FAX        = (1 << 2);
   const long ICC_SERVICE_CLASS_SMS        = (1 << 3);
   const long ICC_SERVICE_CLASS_DATA_SYNC  = (1 << 4);
--- a/dom/mobileconnection/ipc/MobileConnectionIPCService.cpp
+++ b/dom/mobileconnection/ipc/MobileConnectionIPCService.cpp
@@ -9,31 +9,16 @@
 #include "mozilla/StaticPtr.h"
 
 using namespace mozilla;
 using namespace mozilla::dom;
 using namespace mozilla::dom::mobileconnection;
 
 NS_IMPL_ISUPPORTS(MobileConnectionIPCService, nsIMobileConnectionService)
 
-StaticRefPtr<MobileConnectionIPCService> sService;
-
-/* static */MobileConnectionIPCService*
-MobileConnectionIPCService::GetSingleton()
-{
-  MOZ_ASSERT(NS_IsMainThread());
-
-  if (sService) {
-    return sService;
-  }
-
-  sService = new MobileConnectionIPCService();
-  return sService;
-}
-
 MobileConnectionIPCService::MobileConnectionIPCService()
 {
   int32_t numRil = Preferences::GetInt("ril.numRadioInterfaces", 1);
   mItems.SetLength(numRil);
 }
 
 MobileConnectionIPCService::~MobileConnectionIPCService()
 {
--- a/dom/mobileconnection/ipc/MobileConnectionIPCService.h
+++ b/dom/mobileconnection/ipc/MobileConnectionIPCService.h
@@ -14,22 +14,19 @@ namespace dom {
 namespace mobileconnection {
 
 class MobileConnectionIPCService MOZ_FINAL : public nsIMobileConnectionService
 {
 public:
   NS_DECL_ISUPPORTS
   NS_DECL_NSIMOBILECONNECTIONSERVICE
 
-  static MobileConnectionIPCService*
-  GetSingleton();
+  MobileConnectionIPCService();
 
 private:
-  MobileConnectionIPCService();
-
   // MOZ_FINAL suppresses -Werror,-Wdelete-non-virtual-dtor
   ~MobileConnectionIPCService();
 
   nsTArray<nsRefPtr<MobileConnectionChild>> mItems;
 };
 
 } // namespace mobileconnection
 } // namespace dom
--- a/dom/mobileconnection/moz.build
+++ b/dom/mobileconnection/moz.build
@@ -13,17 +13,16 @@ EXPORTS.mozilla.dom += [
     'MobileConnectionArray.h',
     'MobileConnectionInfo.h',
     'MobileNetworkInfo.h',
 ]
 
 EXPORTS.mozilla.dom.mobileconnection += [
     'ipc/MobileConnectionChild.h',
     'ipc/MobileConnectionIPCSerializer.h',
-    'ipc/MobileConnectionIPCService.h',
     'ipc/MobileConnectionParent.h',
 ]
 
 XPIDL_SOURCES += [
     'interfaces/nsICellInfo.idl',
     'interfaces/nsIMobileCellInfo.idl',
     'interfaces/nsIMobileConnectionInfo.idl',
     'interfaces/nsIMobileConnectionService.idl',
--- a/dom/mobilemessage/MobileMessageManager.cpp
+++ b/dom/mobilemessage/MobileMessageManager.cpp
@@ -18,20 +18,33 @@
 #include "mozilla/dom/ToJSValue.h"
 #include "mozilla/Preferences.h"
 #include "mozilla/Services.h"
 #include "nsIDOMMozMmsMessage.h"
 #include "nsIDOMMozSmsMessage.h"
 #include "nsIMmsService.h"
 #include "nsIMobileMessageCallback.h"
 #include "nsIMobileMessageDatabaseService.h"
+#include "nsIMobileMessageService.h"
 #include "nsIObserverService.h"
 #include "nsISmsService.h"
 #include "nsServiceManagerUtils.h" // For do_GetService()
 
+// Service instantiation
+#include "ipc/SmsIPCService.h"
+#include "MobileMessageService.h"
+#ifdef MOZ_WIDGET_ANDROID
+#include "android/MobileMessageDatabaseService.h"
+#include "android/SmsService.h"
+#elif defined(MOZ_WIDGET_GONK) && defined(MOZ_B2G_RIL)
+#include "nsIRilMobileMessageDatabaseService.h"
+#include "gonk/SmsService.h"
+#endif
+#include "nsXULAppAPI.h" // For XRE_GetProcessType()
+
 #define RECEIVED_EVENT_NAME         NS_LITERAL_STRING("received")
 #define RETRIEVING_EVENT_NAME       NS_LITERAL_STRING("retrieving")
 #define SENDING_EVENT_NAME          NS_LITERAL_STRING("sending")
 #define SENT_EVENT_NAME             NS_LITERAL_STRING("sent")
 #define FAILED_EVENT_NAME           NS_LITERAL_STRING("failed")
 #define DELIVERY_SUCCESS_EVENT_NAME NS_LITERAL_STRING("deliverysuccess")
 #define DELIVERY_ERROR_EVENT_NAME   NS_LITERAL_STRING("deliveryerror")
 #define READ_SUCCESS_EVENT_NAME     NS_LITERAL_STRING("readsuccess")
@@ -681,8 +694,67 @@ MobileMessageManager::GetSmscAddress(con
     return nullptr;
   }
 
   return request.forget();
 }
 
 } // namespace dom
 } // namespace mozilla
+
+already_AddRefed<nsISmsService>
+NS_CreateSmsService()
+{
+  nsCOMPtr<nsISmsService> smsService;
+
+  if (XRE_GetProcessType() == GeckoProcessType_Content) {
+    smsService = SmsIPCService::GetSingleton();
+  } else {
+#ifdef MOZ_WIDGET_ANDROID
+    smsService = new SmsService();
+#elif defined(MOZ_WIDGET_GONK) && defined(MOZ_B2G_RIL)
+    smsService = new SmsService();
+#endif
+  }
+
+  return smsService.forget();
+}
+
+already_AddRefed<nsIMobileMessageDatabaseService>
+NS_CreateMobileMessageDatabaseService()
+{
+  nsCOMPtr<nsIMobileMessageDatabaseService> mobileMessageDBService;
+  if (XRE_GetProcessType() == GeckoProcessType_Content) {
+    mobileMessageDBService = SmsIPCService::GetSingleton();
+  } else {
+#ifdef MOZ_WIDGET_ANDROID
+    mobileMessageDBService = new MobileMessageDatabaseService();
+#elif defined(MOZ_WIDGET_GONK) && defined(MOZ_B2G_RIL)
+    mobileMessageDBService =
+      do_CreateInstance(RIL_MOBILE_MESSAGE_DATABASE_SERVICE_CONTRACTID);
+#endif
+  }
+
+  return mobileMessageDBService.forget();
+}
+
+already_AddRefed<nsIMmsService>
+NS_CreateMmsService()
+{
+  nsCOMPtr<nsIMmsService> mmsService;
+
+  if (XRE_GetProcessType() == GeckoProcessType_Content) {
+    mmsService = SmsIPCService::GetSingleton();
+  } else {
+#if defined(MOZ_WIDGET_GONK) && defined(MOZ_B2G_RIL)
+    mmsService = do_CreateInstance("@mozilla.org/mms/rilmmsservice;1");
+#endif
+  }
+
+  return mmsService.forget();
+}
+
+already_AddRefed<nsIMobileMessageService>
+NS_CreateMobileMessageService()
+{
+  nsCOMPtr<nsIMobileMessageService> service = new MobileMessageService();
+  return service.forget();
+}
--- a/dom/mobilemessage/MobileMessageService.cpp
+++ b/dom/mobilemessage/MobileMessageService.cpp
@@ -9,30 +9,16 @@
 #include "DeletedMessageInfo.h"
 
 namespace mozilla {
 namespace dom {
 namespace mobilemessage {
 
 NS_IMPL_ISUPPORTS(MobileMessageService, nsIMobileMessageService)
 
-/* static */ StaticRefPtr<MobileMessageService> MobileMessageService::sSingleton;
-
-/* static */ already_AddRefed<MobileMessageService>
-MobileMessageService::GetInstance()
-{
-  if (!sSingleton) {
-    sSingleton = new MobileMessageService();
-    ClearOnShutdown(&sSingleton);
-  }
-
-  nsRefPtr<MobileMessageService> service = sSingleton.get();
-  return service.forget();
-}
-
 NS_IMETHODIMP
 MobileMessageService::CreateSmsMessage(int32_t aId,
                                        uint64_t aThreadId,
                                        const nsAString& aIccId,
                                        const nsAString& aDelivery,
                                        const nsAString& aDeliveryStatus,
                                        const nsAString& aSender,
                                        const nsAString& aReceiver,
--- a/dom/mobilemessage/MobileMessageService.h
+++ b/dom/mobilemessage/MobileMessageService.h
@@ -1,35 +1,32 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #ifndef mozilla_dom_mobilemessage_MobileMessageService_h
 #define mozilla_dom_mobilemessage_MobileMessageService_h
 
+#include "mozilla/Attributes.h" // For MOZ_FINAL
 #include "nsIMobileMessageService.h"
-#include "mozilla/ClearOnShutdown.h"
-#include "mozilla/StaticPtr.h"
 
 namespace mozilla {
 namespace dom {
 namespace mobilemessage {
 
 class MobileMessageService MOZ_FINAL : public nsIMobileMessageService
 {
 public:
   NS_DECL_ISUPPORTS
   NS_DECL_NSIMOBILEMESSAGESERVICE
 
-  static already_AddRefed<MobileMessageService> GetInstance();
+  MobileMessageService() { MOZ_COUNT_CTOR(MobileMessageService); }
 
 private:
-  ~MobileMessageService() {}
-
-  static StaticRefPtr<MobileMessageService> sSingleton;
-
+  // MOZ_FINAL suppresses -Werror,-Wdelete-non-virtual-dtor
+  ~MobileMessageService() { MOZ_COUNT_DTOR(MobileMessageService); }
 };
 
 } // namespace mobilemessage
 } // namespace dom
 } // namespace mozilla
 
 #endif // mozilla_dom_mobilemessage_MobileMessageService_h
deleted file mode 100644
--- a/dom/mobilemessage/SmsServicesFactory.cpp
+++ /dev/null
@@ -1,77 +0,0 @@
-/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
-/* This Source Code Form is subject to the terms of the Mozilla Public
- * License, v. 2.0. If a copy of the MPL was not distributed with this
- * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
-
-#include "SmsServicesFactory.h"
-#include "nsXULAppAPI.h"
-#include "ipc/SmsIPCService.h"
-#ifdef MOZ_WIDGET_ANDROID
-#include "android/MobileMessageDatabaseService.h"
-#include "android/SmsService.h"
-#elif defined(MOZ_WIDGET_GONK) && defined(MOZ_B2G_RIL)
-#include "gonk/SmsService.h"
-#endif
-#include "nsServiceManagerUtils.h"
-
-#define RIL_MMSSERVICE_CONTRACTID "@mozilla.org/mms/rilmmsservice;1"
-#define RIL_MOBILE_MESSAGE_DATABASE_SERVICE_CONTRACTID "@mozilla.org/mobilemessage/rilmobilemessagedatabaseservice;1"
-
-namespace mozilla {
-namespace dom {
-namespace mobilemessage {
-
-/* static */ already_AddRefed<nsISmsService>
-SmsServicesFactory::CreateSmsService()
-{
-  nsCOMPtr<nsISmsService> smsService;
-
-  if (XRE_GetProcessType() == GeckoProcessType_Content) {
-    smsService = new SmsIPCService();
-  } else {
-#ifdef MOZ_WIDGET_ANDROID
-    smsService = new SmsService();
-#elif defined(MOZ_WIDGET_GONK) && defined(MOZ_B2G_RIL)
-    smsService = new SmsService();
-#endif
-  }
-
-  return smsService.forget();
-}
-
-/* static */ already_AddRefed<nsIMobileMessageDatabaseService>
-SmsServicesFactory::CreateMobileMessageDatabaseService()
-{
-  nsCOMPtr<nsIMobileMessageDatabaseService> mobileMessageDBService;
-  if (XRE_GetProcessType() == GeckoProcessType_Content) {
-    mobileMessageDBService = new SmsIPCService();
-  } else {
-#ifdef MOZ_WIDGET_ANDROID
-    mobileMessageDBService = new MobileMessageDatabaseService();
-#elif defined(MOZ_WIDGET_GONK) && defined(MOZ_B2G_RIL)
-    mobileMessageDBService = do_GetService(RIL_MOBILE_MESSAGE_DATABASE_SERVICE_CONTRACTID);
-#endif
-  }
-
-  return mobileMessageDBService.forget();
-}
-
-/* static */ already_AddRefed<nsIMmsService>
-SmsServicesFactory::CreateMmsService()
-{
-  nsCOMPtr<nsIMmsService> mmsService;
-
-  if (XRE_GetProcessType() == GeckoProcessType_Content) {
-    mmsService = new SmsIPCService();
-  } else {
-#if defined(MOZ_WIDGET_GONK) && defined(MOZ_B2G_RIL)
-    mmsService = do_CreateInstance(RIL_MMSSERVICE_CONTRACTID);
-#endif
-  }
-
-  return mmsService.forget();
-}
-
-} // namespace mobilemessage
-} // namespace dom
-} // namespace mozilla
deleted file mode 100644
--- a/dom/mobilemessage/SmsServicesFactory.h
+++ /dev/null
@@ -1,31 +0,0 @@
-/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
-/* This Source Code Form is subject to the terms of the Mozilla Public
- * License, v. 2.0. If a copy of the MPL was not distributed with this
- * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
-
-#ifndef mozilla_dom_mobilemessage_SmsServicesFactory_h
-#define mozilla_dom_mobilemessage_SmsServicesFactory_h
-
-#include "nsCOMPtr.h"
-
-class nsISmsService;
-class nsIMmsService;
-class nsIMobileMessageDatabaseService;
-
-namespace mozilla {
-namespace dom {
-namespace mobilemessage {
-
-class SmsServicesFactory
-{
-public:
-  static already_AddRefed<nsISmsService> CreateSmsService();
-  static already_AddRefed<nsIMobileMessageDatabaseService> CreateMobileMessageDatabaseService();
-  static already_AddRefed<nsIMmsService> CreateMmsService();
-};
-
-} // namespace mobilemessage
-} // namespace dom
-} // namespace mozilla
-
-#endif // mozilla_dom_mobilemessage_SmsServicesFactory_h
--- a/dom/mobilemessage/interfaces/nsIMmsService.idl
+++ b/dom/mobilemessage/interfaces/nsIMmsService.idl
@@ -1,13 +1,14 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
  * You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #include "nsISupports.idl"
+
 interface nsIMobileMessageCallback;
 interface nsIDOMBlob;
 
 %{C++
 #define MMS_SERVICE_CID { 0x06d9124b, 0x80e0, 0x40ed, \
   { 0x98, 0x71, 0x4d, 0x23, 0x4a, 0x0f, 0xd4, 0x31 } }
 #define MMS_SERVICE_CONTRACTID "@mozilla.org/mms/mmsservice;1"
 %}
@@ -23,8 +24,15 @@ interface nsIMmsService : nsISupports
 
   void retrieve(in long id,
                 in nsIMobileMessageCallback request);
 
   void sendReadReport(in DOMString messageID,
                       in DOMString toAddress,
                       in DOMString iccId);
 };
+
+%{C++
+template<typename T> struct already_AddRefed;
+
+already_AddRefed<nsIMmsService>
+NS_CreateMmsService();
+%}
--- a/dom/mobilemessage/interfaces/nsIMobileMessageDatabaseService.idl
+++ b/dom/mobilemessage/interfaces/nsIMobileMessageDatabaseService.idl
@@ -41,8 +41,15 @@ interface nsIMobileMessageDatabaseServic
 
   void markMessageRead(in long messageId,
                        in boolean value,
                        in boolean sendReadReport,
                        in nsIMobileMessageCallback request);
 
   nsICursorContinueCallback createThreadCursor(in nsIMobileMessageCursorCallback callback);
 };
+
+%{C++
+template<typename T> struct already_AddRefed;
+
+already_AddRefed<nsIMobileMessageDatabaseService>
+NS_CreateMobileMessageDatabaseService();
+%}
--- a/dom/mobilemessage/interfaces/nsIMobileMessageService.idl
+++ b/dom/mobilemessage/interfaces/nsIMobileMessageService.idl
@@ -59,8 +59,15 @@ interface nsIMobileMessageService : nsIS
                                             in DOMString          aLastMessageType);
 
   nsIDeletedMessageInfo createDeletedMessageInfo(
     [array, size_is(msgCount)] in long messageIds,
     in uint32_t msgCount,
     [array, size_is(threadCount)] in unsigned long long threadIds,
     in uint32_t threadCount);
 };
+
+%{C++
+template<typename T> struct already_AddRefed;
+
+already_AddRefed<nsIMobileMessageService>
+NS_CreateMobileMessageService();
+%}
--- a/dom/mobilemessage/interfaces/nsIRilMobileMessageDatabaseService.idl
+++ b/dom/mobilemessage/interfaces/nsIRilMobileMessageDatabaseService.idl
@@ -29,16 +29,23 @@ interface nsIRilMobileMessageDatabaseRec
 interface nsIRilMobileMessageDatabaseConcatenationCallback : nsISupports
 {
   /**
    * |aCompleteMessage|: jsval: the completely concatenated message. Noted, this value might be null.
    */
   void notify(in nsresult aRv, in jsval aCompleteMessage);
 };
 
+%{C++
+#define RIL_MOBILE_MESSAGE_DATABASE_SERVICE_CID \
+  { 0x29785f90, 0x6b5b, 0x11e2, { 0x92, 0x01, 0x3b, 0x28, 0x01, 0x70, 0xb2, 0xec } }
+#define RIL_MOBILE_MESSAGE_DATABASE_SERVICE_CONTRACTID \
+  "@mozilla.org/mobilemessage/rilmobilemessagedatabaseservice;1"
+%}
+
 [scriptable, uuid(0b437a5c-a2bc-11e3-bd1b-dbb173eb35f8)]
 interface nsIRilMobileMessageDatabaseService : nsIMobileMessageDatabaseService
 {
   /**
    * |aMessage| Object: should contain the following properties for internal use:
    *   - |type| DOMString: "sms" or "mms"
    *   - |timestamp| Number: the timestamp of received message
    *   - |iccId| DOMString: [optional] the ICC ID of the SIM for receiving
--- a/dom/mobilemessage/interfaces/nsISmsService.idl
+++ b/dom/mobilemessage/interfaces/nsISmsService.idl
@@ -28,8 +28,15 @@ interface nsISmsService : nsISupports
 
   boolean isSilentNumber(in DOMString number);
   void addSilentNumber(in DOMString number);
   void removeSilentNumber(in DOMString number);
 
   void getSmscAddress(in unsigned long serviceId,
                       in nsIMobileMessageCallback request);
 };
+
+%{C++
+template<typename T> struct already_AddRefed;
+
+already_AddRefed<nsISmsService>
+NS_CreateSmsService();
+%}
--- a/dom/mobilemessage/ipc/SmsIPCService.cpp
+++ b/dom/mobilemessage/ipc/SmsIPCService.cpp
@@ -26,16 +26,19 @@ const char* kObservedPrefs[] = {
   kPrefMmsDefaultServiceId,
   kPrefSmsDefaultServiceId,
   nullptr
 };
 
 // TODO: Bug 767082 - WebSMS: sSmsChild leaks at shutdown
 PSmsChild* gSmsChild;
 
+// SmsIPCService is owned by nsLayoutModule.
+SmsIPCService* sSingleton = nullptr;
+
 PSmsChild*
 GetSmsChild()
 {
   MOZ_ASSERT(NS_IsMainThread());
 
   if (!gSmsChild) {
     gSmsChild = ContentChild::GetSingleton()->SendPSmsConstructor();
 
@@ -96,23 +99,41 @@ getDefaultServiceId(const char* aPrefKey
 } // anonymous namespace
 
 NS_IMPL_ISUPPORTS(SmsIPCService,
                   nsISmsService,
                   nsIMmsService,
                   nsIMobileMessageDatabaseService,
                   nsIObserver)
 
+/* static */ already_AddRefed<SmsIPCService>
+SmsIPCService::GetSingleton()
+{
+  MOZ_ASSERT(NS_IsMainThread());
+
+  if (!sSingleton) {
+    sSingleton = new SmsIPCService();
+  }
+
+  nsRefPtr<SmsIPCService> service = sSingleton;
+  return service.forget();
+}
+
 SmsIPCService::SmsIPCService()
 {
   Preferences::AddStrongObservers(this, kObservedPrefs);
   mMmsDefaultServiceId = getDefaultServiceId(kPrefMmsDefaultServiceId);
   mSmsDefaultServiceId = getDefaultServiceId(kPrefSmsDefaultServiceId);
 }
 
+SmsIPCService::~SmsIPCService()
+{
+  sSingleton = nullptr;
+}
+
 /*
  * Implementation of nsIObserver.
  */
 
 NS_IMETHODIMP
 SmsIPCService::Observe(nsISupports* aSubject,
                        const char* aTopic,
                        const char16_t* aData)
--- a/dom/mobilemessage/ipc/SmsIPCService.h
+++ b/dom/mobilemessage/ipc/SmsIPCService.h
@@ -25,20 +25,24 @@ class SmsIPCService MOZ_FINAL : public n
 {
 public:
   NS_DECL_ISUPPORTS
   NS_DECL_NSISMSSERVICE
   NS_DECL_NSIMMSSERVICE
   NS_DECL_NSIMOBILEMESSAGEDATABASESERVICE
   NS_DECL_NSIOBSERVER
 
+  static already_AddRefed<SmsIPCService>
+  GetSingleton();
+
+private:
   SmsIPCService();
 
-private:
-  ~SmsIPCService() {}
+  // MOZ_FINAL suppresses -Werror,-Wdelete-non-virtual-dtor
+  ~SmsIPCService();
 
   uint32_t mMmsDefaultServiceId;
   uint32_t mSmsDefaultServiceId;
 };
 
 } // namespace mobilemessage
 } // namespace dom
 } // namespace mozilla
--- a/dom/mobilemessage/moz.build
+++ b/dom/mobilemessage/moz.build
@@ -8,18 +8,16 @@ DIRS += ['interfaces']
 
 MOCHITEST_MANIFESTS += ['tests/mochitest/mochitest.ini']
 XPCSHELL_TESTS_MANIFESTS += ['tests/xpcshell/xpcshell.ini']
 
 EXPORTS.mozilla.dom.mobilemessage += [
     'Constants.h',            # Required by almost all cpp files
     'ipc/SmsChild.h',
     'ipc/SmsParent.h',
-    'MobileMessageService.h', # Required by nsLayoutModule.cpp
-    'SmsServicesFactory.h',   # Required by nsLayoutModule.cpp
     'Types.h',                # Required by IPDL SmsTypes.h
 ]
 
 if CONFIG['MOZ_WIDGET_TOOLKIT'] == 'android':
     SOURCES += [
         'android/MobileMessageDatabaseService.cpp',
         'android/SmsService.cpp',
     ]
@@ -57,17 +55,16 @@ UNIFIED_SOURCES += [
     'ipc/SmsParent.cpp',
     'MmsMessage.cpp',
     'MobileMessageCallback.cpp',
     'MobileMessageCursorCallback.cpp',
     'MobileMessageManager.cpp',
     'MobileMessageService.cpp',
     'MobileMessageThread.cpp',
     'SmsMessage.cpp',
-    'SmsServicesFactory.cpp',
 ]
 
 IPDL_SOURCES += [
     'ipc/PMobileMessageCursor.ipdl',
     'ipc/PSms.ipdl',
     'ipc/PSmsRequest.ipdl',
     'ipc/SmsTypes.ipdlh',
 ]
--- a/dom/telephony/Telephony.cpp
+++ b/dom/telephony/Telephony.cpp
@@ -21,16 +21,23 @@
 #include "nsServiceManagerUtils.h"
 #include "nsThreadUtils.h"
 
 #include "CallsList.h"
 #include "TelephonyCall.h"
 #include "TelephonyCallGroup.h"
 #include "TelephonyCallId.h"
 
+// Service instantiation
+#include "ipc/TelephonyIPCService.h"
+#if defined(MOZ_WIDGET_GONK) && defined(MOZ_B2G_RIL)
+#include "nsIGonkTelephonyService.h"
+#endif
+#include "nsXULAppAPI.h" // For XRE_GetProcessType()
+
 using namespace mozilla::dom;
 using mozilla::ErrorResult;
 
 class Telephony::Listener : public nsITelephonyListener
 {
   Telephony* mTelephony;
 
   virtual ~Listener() {}
@@ -733,8 +740,24 @@ Telephony::EnqueueEnumerationAck(const n
     return;
   }
 
   nsCOMPtr<nsIRunnable> task = new EnumerationAck(this, aType);
   if (NS_FAILED(NS_DispatchToCurrentThread(task))) {
     NS_WARNING("Failed to dispatch to current thread!");
   }
 }
+
+already_AddRefed<nsITelephonyService>
+NS_CreateTelephonyService()
+{
+  nsCOMPtr<nsITelephonyService> service;
+
+  if (XRE_GetProcessType() == GeckoProcessType_Content) {
+    service = new mozilla::dom::telephony::TelephonyIPCService();
+  } else {
+#if defined(MOZ_WIDGET_GONK) && defined(MOZ_B2G_RIL)
+    service = do_CreateInstance(GONK_TELEPHONY_SERVICE_CONTRACTID);
+#endif
+  }
+
+  return service.forget();
+}
deleted file mode 100644
--- a/dom/telephony/TelephonyFactory.cpp
+++ /dev/null
@@ -1,30 +0,0 @@
-/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
-/* This Source Code Form is subject to the terms of the Mozilla Public
- * License, v. 2.0. If a copy of the MPL was not distributed with this
- * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
-
-#include "mozilla/dom/telephony/TelephonyFactory.h"
-#if defined(MOZ_WIDGET_GONK) && defined(MOZ_B2G_RIL)
-#include "nsIGonkTelephonyService.h"
-#endif
-#include "nsServiceManagerUtils.h"
-#include "nsXULAppAPI.h"
-#include "ipc/TelephonyIPCService.h"
-
-USING_TELEPHONY_NAMESPACE
-
-/* static */ already_AddRefed<nsITelephonyService>
-TelephonyFactory::CreateTelephonyService()
-{
-  nsCOMPtr<nsITelephonyService> service;
-
-  if (XRE_GetProcessType() == GeckoProcessType_Content) {
-    service = new TelephonyIPCService();
-#if defined(MOZ_WIDGET_GONK) && defined(MOZ_B2G_RIL)
-  } else {
-    service = do_CreateInstance(GONK_TELEPHONY_SERVICE_CONTRACTID);
-#endif
-  }
-
-  return service.forget();
-}
deleted file mode 100644
--- a/dom/telephony/TelephonyFactory.h
+++ /dev/null
@@ -1,24 +0,0 @@
-/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
-/* This Source Code Form is subject to the terms of the Mozilla Public
- * License, v. 2.0. If a copy of the MPL was not distributed with this
- * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
-
-#ifndef mozilla_dom_telephony_TelephonyFactory_h
-#define mozilla_dom_telephony_TelephonyFactory_h
-
-#include "nsCOMPtr.h"
-#include "mozilla/dom/telephony/TelephonyCommon.h"
-
-class nsITelephonyService;
-
-BEGIN_TELEPHONY_NAMESPACE
-
-class TelephonyFactory
-{
-public:
-  static already_AddRefed<nsITelephonyService> CreateTelephonyService();
-};
-
-END_TELEPHONY_NAMESPACE
-
-#endif // mozilla_dom_telephony_TelephonyFactory_h
--- a/dom/telephony/moz.build
+++ b/dom/telephony/moz.build
@@ -17,29 +17,27 @@ EXPORTS.mozilla.dom += [
     'TelephonyCallGroup.h',
     'TelephonyCallId.h',
 ]
 
 EXPORTS.mozilla.dom.telephony += [
     'ipc/TelephonyChild.h',
     'ipc/TelephonyParent.h',
     'TelephonyCommon.h',
-    'TelephonyFactory.h',
 ]
 
 UNIFIED_SOURCES += [
     'CallsList.cpp',
     'ipc/TelephonyChild.cpp',
     'ipc/TelephonyIPCService.cpp',
     'ipc/TelephonyParent.cpp',
     'Telephony.cpp',
     'TelephonyCall.cpp',
     'TelephonyCallGroup.cpp',
     'TelephonyCallId.cpp',
-    'TelephonyFactory.cpp',
 ]
 
 IPDL_SOURCES += [
     'ipc/PTelephony.ipdl',
     'ipc/PTelephonyRequest.ipdl',
     'ipc/TelephonyTypes.ipdlh'
 ]
 
--- a/dom/telephony/nsITelephonyService.idl
+++ b/dom/telephony/nsITelephonyService.idl
@@ -260,8 +260,15 @@ interface nsITelephonyService : nsISuppo
   void conferenceCall(in unsigned long clientId);
   void separateCall(in unsigned long clientId, in unsigned long callIndex);
   void holdConference(in unsigned long clientId);
   void resumeConference(in unsigned long clientId);
 
   attribute bool microphoneMuted;
   attribute bool speakerEnabled;
 };
+
+%{C++
+template<typename T> struct already_AddRefed;
+
+already_AddRefed<nsITelephonyService>
+NS_CreateTelephonyService();
+%}
--- a/layout/build/nsLayoutModule.cpp
+++ b/layout/build/nsLayoutModule.cpp
@@ -120,25 +120,16 @@ using mozilla::dom::bluetooth::Bluetooth
 
 #ifdef MOZ_WIDGET_GONK
 #include "AudioManager.h"
 using mozilla::dom::gonk::AudioManager;
 #include "nsVolumeService.h"
 using mozilla::system::nsVolumeService;
 #endif
 
-#ifdef MOZ_B2G_RIL
-#include "nsIMobileConnectionService.h"
-#include "mozilla/dom/mobileconnection/MobileConnectionIPCService.h"
-using mozilla::dom::mobileconnection::MobileConnectionIPCService;
-#ifdef MOZ_WIDGET_GONK
-#include "nsIGonkMobileConnectionService.h"
-#endif
-#endif
-
 #include "AudioChannelAgent.h"
 using mozilla::dom::AudioChannelAgent;
 
 // Editor stuff
 #include "nsEditorCID.h"
 #include "nsEditor.h"
 #include "nsPlaintextEditor.h"
 #include "nsEditorController.h" //CID
@@ -220,48 +211,44 @@ static void Shutdown();
 #include "nsDeviceSensors.h"
 #ifdef MOZ_GAMEPAD
 #include "mozilla/dom/GamepadService.h"
 #endif
 #include "nsCSPService.h"
 #include "nsCSPContext.h"
 #include "nsISmsService.h"
 #include "nsIMmsService.h"
+#include "nsIMobileConnectionService.h"
 #include "nsIMobileMessageService.h"
 #include "nsIMobileMessageDatabaseService.h"
-#include "mozilla/dom/mobilemessage/MobileMessageService.h"
-#include "mozilla/dom/mobilemessage/SmsServicesFactory.h"
 #include "nsIPowerManagerService.h"
 #include "nsIAlarmHalService.h"
 #include "nsIMediaManager.h"
 #include "nsMixedContentBlocker.h"
 
 #include "AudioChannelService.h"
 
 #include "mozilla/dom/DataStoreService.h"
 
 #include "mozilla/dom/power/PowerManagerService.h"
 #include "mozilla/dom/alarm/AlarmHalService.h"
 #include "mozilla/dom/time/TimeService.h"
 #include "StreamingProtocolService.h"
 
-#include "mozilla/dom/telephony/TelephonyFactory.h"
 #include "nsITelephonyService.h"
 
 #ifdef MOZ_WIDGET_GONK
 #include "GonkGPSGeolocationProvider.h"
 #endif
 #include "MediaManager.h"
 
 #include "GMPService.h"
 
 using namespace mozilla;
 using namespace mozilla::dom;
-using namespace mozilla::dom::mobilemessage;
-using namespace mozilla::dom::telephony;
 using mozilla::dom::alarm::AlarmHalService;
 using mozilla::dom::power::PowerManagerService;
 using mozilla::dom::quota::QuotaManager;
 using mozilla::dom::workers::ServiceWorkerManager;
 using mozilla::dom::TCPSocketChild;
 using mozilla::dom::TCPSocketParent;
 using mozilla::dom::TCPServerSocketChild;
 using mozilla::dom::UDPSocketChild;
@@ -325,24 +312,22 @@ NS_GENERIC_FACTORY_CONSTRUCTOR(AudioChan
 NS_GENERIC_FACTORY_CONSTRUCTOR(nsDeviceSensors)
 
 #ifndef MOZ_WIDGET_GONK
 #if defined(ANDROID)
 NS_GENERIC_FACTORY_CONSTRUCTOR(nsHapticFeedback)
 #endif
 #endif
 NS_GENERIC_FACTORY_CONSTRUCTOR_INIT(ThirdPartyUtil, Init)
-NS_GENERIC_FACTORY_SINGLETON_CONSTRUCTOR(nsISmsService,
-                                         SmsServicesFactory::CreateSmsService)
-NS_GENERIC_FACTORY_SINGLETON_CONSTRUCTOR(nsIMmsService,
-                                         SmsServicesFactory::CreateMmsService)
+NS_GENERIC_FACTORY_SINGLETON_CONSTRUCTOR(nsISmsService, NS_CreateSmsService)
+NS_GENERIC_FACTORY_SINGLETON_CONSTRUCTOR(nsIMmsService, NS_CreateMmsService)
 NS_GENERIC_FACTORY_SINGLETON_CONSTRUCTOR(nsIMobileMessageService,
-                                         MobileMessageService::GetInstance)
+                                         NS_CreateMobileMessageService)
 NS_GENERIC_FACTORY_SINGLETON_CONSTRUCTOR(nsIMobileMessageDatabaseService,
-                                         SmsServicesFactory::CreateMobileMessageDatabaseService)
+                                         NS_CreateMobileMessageDatabaseService)
 NS_GENERIC_FACTORY_SINGLETON_CONSTRUCTOR(nsIPowerManagerService,
                                          PowerManagerService::GetInstance)
 NS_GENERIC_FACTORY_SINGLETON_CONSTRUCTOR(nsIAlarmHalService,
                                          AlarmHalService::GetInstance)
 NS_GENERIC_FACTORY_SINGLETON_CONSTRUCTOR(nsITimeService,
                                          TimeService::GetInstance)
 NS_GENERIC_FACTORY_SINGLETON_CONSTRUCTOR(nsIStreamingProtocolControllerService,
                                          StreamingProtocolControllerService::GetInstance)
@@ -358,18 +343,20 @@ NS_GENERIC_FACTORY_SINGLETON_CONSTRUCTOR
                                          GonkGPSGeolocationProvider::GetSingleton)
 // Since the nsVolumeService constructor calls into nsIPowerManagerService,
 // we need it to be constructed sometime after nsIPowerManagerService.
 NS_GENERIC_FACTORY_SINGLETON_CONSTRUCTOR(nsVolumeService,
                                          nsVolumeService::GetSingleton)
 #endif
 NS_GENERIC_FACTORY_SINGLETON_CONSTRUCTOR(nsIMediaManagerService,
                                          MediaManager::GetInstance)
+NS_GENERIC_FACTORY_SINGLETON_CONSTRUCTOR(nsIMobileConnectionService,
+                                         NS_CreateMobileConnectionService)
 NS_GENERIC_FACTORY_SINGLETON_CONSTRUCTOR(nsITelephonyService,
-                                         TelephonyFactory::CreateTelephonyService)
+                                         NS_CreateTelephonyService)
 
 //-----------------------------------------------------------------------------
 
 static bool gInitialized = false;
 
 // Perform our one-time intialization for this module
 
 // static
@@ -801,19 +788,17 @@ NS_DEFINE_NAMED_CID(NS_SYNTHVOICEREGISTR
 
 #ifdef ACCESSIBILITY
 NS_DEFINE_NAMED_CID(NS_ACCESSIBILITY_SERVICE_CID);
 #endif
 NS_DEFINE_NAMED_CID(TELEPHONY_SERVICE_CID);
 
 NS_DEFINE_NAMED_CID(GECKO_MEDIA_PLUGIN_SERVICE_CID);
 
-#ifdef MOZ_B2G_RIL
 NS_DEFINE_NAMED_CID(NS_MOBILE_CONNECTION_SERVICE_CID);
-#endif
 
 static nsresult
 CreateWindowCommandTableConstructor(nsISupports *aOuter,
                                     REFNSIID aIID, void **aResult)
 {
   nsresult rv;
   nsCOMPtr<nsIControllerCommandTable> commandTable =
       do_CreateInstance(NS_CONTROLLERCOMMANDTABLE_CONTRACTID, &rv);
@@ -933,41 +918,16 @@ nsEditingCommandTableConstructor(nsISupp
   if (NS_FAILED(rv)) return rv;
 
   // we don't know here whether we're being created as an instance,
   // or a service, so we can't become immutable
 
   return commandTable->QueryInterface(aIID, aResult);
 }
 
-#ifdef MOZ_B2G_RIL
-
-static nsresult
-nsIMobileConnectionServiceConstructor(nsISupports *aOuter, REFNSIID aIID,
-                                      void **aResult)
-{
-  nsCOMPtr<nsIMobileConnectionService> service;
-
-  if (XRE_GetProcessType() == GeckoProcessType_Content) {
-    service = MobileConnectionIPCService::GetSingleton();
-  } else {
-#ifdef MOZ_WIDGET_GONK
-    service = do_CreateInstance(GONK_MOBILECONNECTION_SERVICE_CONTRACTID);
-#endif
-  }
-
-  if (!service) {
-    return NS_ERROR_NOT_AVAILABLE;
-  }
-
-  return service->QueryInterface(aIID, aResult);
-}
-
-#endif
-
 static const mozilla::Module::CIDEntry kLayoutCIDs[] = {
   XPCONNECT_CIDENTRIES
 #ifdef DEBUG
   { &kNS_FRAME_UTIL_CID, false, nullptr, CreateNewFrameUtil },
   { &kNS_LAYOUT_DEBUGGER_CID, false, nullptr, CreateNewLayoutDebugger },
 #endif
   { &kNS_FRAMETRAVERSAL_CID, false, nullptr, CreateNewFrameTraversal },
   { &kNS_BOXOBJECT_CID, false, nullptr, CreateNewBoxObject },
@@ -1113,19 +1073,17 @@ static const mozilla::Module::CIDEntry k
   { &kNS_MEDIAMANAGERSERVICE_CID, false, nullptr, nsIMediaManagerServiceConstructor },
 #ifdef MOZ_GAMEPAD
   { &kNS_GAMEPAD_TEST_CID, false, nullptr, GamepadServiceTestConstructor },
 #endif
 #ifdef ACCESSIBILITY
   { &kNS_ACCESSIBILITY_SERVICE_CID, false, nullptr, CreateA11yService },
 #endif
   { &kTELEPHONY_SERVICE_CID, false, nullptr, nsITelephonyServiceConstructor },
-#ifdef MOZ_B2G_RIL
-  { &kNS_MOBILE_CONNECTION_SERVICE_CID, true, NULL, nsIMobileConnectionServiceConstructor },
-#endif
+  { &kNS_MOBILE_CONNECTION_SERVICE_CID, false, NULL, nsIMobileConnectionServiceConstructor },
   { nullptr }
 };
 
 static const mozilla::Module::ContractIDEntry kLayoutContracts[] = {
   XPCONNECT_CONTRACTS
   { "@mozilla.org/layout/xul-boxobject;1", &kNS_BOXOBJECT_CID },
 #ifdef MOZ_XUL
   { "@mozilla.org/layout/xul-boxobject-listbox;1", &kNS_LISTBOXOBJECT_CID },
@@ -1273,19 +1231,17 @@ static const mozilla::Module::ContractID
 #endif
   { MEDIAMANAGERSERVICE_CONTRACTID, &kNS_MEDIAMANAGERSERVICE_CID },
 #ifdef ACCESSIBILITY
   { "@mozilla.org/accessibilityService;1", &kNS_ACCESSIBILITY_SERVICE_CID },
   { "@mozilla.org/accessibleRetrieval;1", &kNS_ACCESSIBILITY_SERVICE_CID },
 #endif
   { TELEPHONY_SERVICE_CONTRACTID, &kTELEPHONY_SERVICE_CID },
   { "@mozilla.org/gecko-media-plugin-service;1",  &kGECKO_MEDIA_PLUGIN_SERVICE_CID },
-#ifdef MOZ_B2G_RIL
   { NS_MOBILE_CONNECTION_SERVICE_CONTRACTID, &kNS_MOBILE_CONNECTION_SERVICE_CID },
-#endif
   { nullptr }
 };
 
 static const mozilla::Module::CategoryEntry kLayoutCategories[] = {
   XPCONNECT_CATEGORIES
   { "content-policy", NS_DATADOCUMENTCONTENTPOLICY_CONTRACTID, NS_DATADOCUMENTCONTENTPOLICY_CONTRACTID },
   { "content-policy", NS_NODATAPROTOCOLCONTENTPOLICY_CONTRACTID, NS_NODATAPROTOCOLCONTENTPOLICY_CONTRACTID },
   { "content-policy", "CSPService", CSPSERVICE_CONTRACTID },
@@ -1298,19 +1254,17 @@ static const mozilla::Module::CategoryEn
 #endif
   CONTENTDLF_CATEGORIES
 #ifdef MOZ_WIDGET_GONK
   { "profile-after-change", "Gonk System Worker Manager", SYSTEMWORKERMANAGER_CONTRACTID },
 #endif
 #ifdef MOZ_B2G_BT
   { "profile-after-change", "Bluetooth Service", BLUETOOTHSERVICE_CONTRACTID },
 #endif
-#ifdef MOZ_B2G_RIL
   { "profile-after-change", "MobileConnection Service", NS_MOBILE_CONNECTION_SERVICE_CONTRACTID },
-#endif
   { nullptr }
 };
 
 static void
 LayoutModuleDtor()
 {
   Shutdown();
   nsContentUtils::XPCOMShutdown();