Bug 750178 - Expose OS.Constants as an xpcom component. r=khuey
authorDavid Rajchenbach-Teller <dteller@mozilla.com>
Wed, 04 Jul 2012 08:30:58 -0400
changeset 103595 f9fb64b005637f891f156b8b4a0db6cd60da1c28
parent 103594 1911af67056984976a521d269f5c6047f4037254
child 103596 460c8abe0a91480df915bea63094e55e5415e112
push id191
push userlsblakk@mozilla.com
push dateFri, 05 Oct 2012 17:12:53 +0000
treeherdermozilla-release@ddb22ac6c03b [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerskhuey
bugs750178
milestone16.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 750178 - Expose OS.Constants as an xpcom component. r=khuey
browser/installer/package-manifest.in
dom/system/Makefile.in
dom/system/OSFileConstants.cpp
dom/system/OSFileConstants.h
dom/system/nsIOSFileConstantsService.idl
dom/workers/RuntimeService.cpp
--- a/browser/installer/package-manifest.in
+++ b/browser/installer/package-manifest.in
@@ -156,16 +156,17 @@
 @BINPATH@/components/content_xslt.xpt
 @BINPATH@/components/content_xtf.xpt
 @BINPATH@/components/cookie.xpt
 @BINPATH@/components/directory.xpt
 @BINPATH@/components/docshell.xpt
 @BINPATH@/components/dom.xpt
 @BINPATH@/components/dom_apps.xpt
 @BINPATH@/components/dom_base.xpt
+@BINPATH@/components/dom_system.xpt
 #ifdef MOZ_B2G_RIL
 @BINPATH@/components/dom_telephony.xpt
 @BINPATH@/components/dom_wifi.xpt
 @BINPATH@/components/dom_system_gonk.xpt
 #endif
 @BINPATH@/components/dom_battery.xpt
 #ifdef MOZ_B2G_BT
 @BINPATH@/components/dom_bluetooth.xpt
--- a/dom/system/Makefile.in
+++ b/dom/system/Makefile.in
@@ -5,16 +5,17 @@
 DEPTH       = ../..
 topsrcdir   = @top_srcdir@
 srcdir      = @srcdir@
 VPATH       = @srcdir@
 
 include $(DEPTH)/config/autoconf.mk
 
 MODULE          = dom
+XPIDL_MODULE    = dom_system
 LIBRARY_NAME    = domsystem_s
 LIBXUL_LIBRARY  = 1
 
 ifneq (,$(filter qt gtk2,$(MOZ_WIDGET_TOOLKIT)))
 DIRS = unix
 endif
 
 ifneq (,$(filter windows,$(MOZ_WIDGET_TOOLKIT)))
@@ -26,16 +27,19 @@ DIRS = android
 endif
 
 ifdef MOZ_B2G_RIL
 DIRS += gonk
 endif
 
 DEFINES += -DDLL_PREFIX=\"$(DLL_PREFIX)\" -DDLL_SUFFIX=\"$(DLL_SUFFIX)\"
 
+XPIDLSRCS = \
+  nsIOSFileConstantsService.idl \
+  $(NULL)
 
 CPPSRCS     = \
   nsDeviceSensors.cpp \
   OSFileConstants.cpp \
   $(NULL)
 
 # We fire the nsDOMDeviceAcceleration
 LOCAL_INCLUDES += \
--- a/dom/system/OSFileConstants.cpp
+++ b/dom/system/OSFileConstants.cpp
@@ -28,16 +28,17 @@
 #include "nsThreadUtils.h"
 #include "nsDirectoryServiceUtils.h"
 #include "nsIXULRuntime.h"
 #include "nsXPCOMCIDInternal.h"
 #include "nsServiceManagerUtils.h"
 #include "nsString.h"
 
 #include "OSFileConstants.h"
+#include "nsIOSFileConstantsService.h"
 
 /**
  * This module defines the basic libc constants (error numbers, open modes,
  * etc.) used by OS.File and possibly other OS-bound JavaScript libraries.
  */
 
 
 namespace mozilla {
@@ -59,17 +60,17 @@ nsString* gLibDirectory;
 /**
  * Perform the part of initialization that can only be
  * executed on the main thread.
  */
 nsresult InitOSFileConstants()
 {
   MOZ_ASSERT(NS_IsMainThread());
   if (gInitialized) {
-    return NS_ERROR_ALREADY_INITIALIZED;
+    return NS_OK;
   }
 
   gInitialized = true;
 
   // Initialize gLibDirectory
   nsCOMPtr<nsIFile> xpcomLib;
   nsresult rv = NS_GetSpecialDirectory("XpcomLib", getter_AddRefs(xpcomLib));
   if (NS_FAILED(rv) || !xpcomLib) {
@@ -81,25 +82,25 @@ nsresult InitOSFileConstants()
   if (NS_FAILED(rv)) {
     return rv;
   }
 
   gLibDirectory = new nsString();
   return libDir->GetPath(*gLibDirectory);
 }
 
-nsresult CleanupOSFileConstants()
+void CleanupOSFileConstants()
 {
   MOZ_ASSERT(NS_IsMainThread());
   if (!gInitialized) {
-    return NS_ERROR_NOT_INITIALIZED;
+    return;
   }
 
+  gInitialized = false;
   delete gLibDirectory;
-  return NS_OK;
 }
 
 
 /**
  * Define a simple read-only property holding an integer.
  *
  * @param name The name of the constant. Used both as the JS name for the
  * constant and to access its value. Must be defined.
@@ -469,10 +470,41 @@ bool DefineOSFileConstants(JSContext *cx
     if (!JS_SetProperty(cx, objPath, "libxul", &valXul)) {
       return false;
     }
   }
 
   return true;
 }
 
+NS_IMPL_ISUPPORTS1(OSFileConstantsService, nsIOSFileConstantsService)
+
+OSFileConstantsService::OSFileConstantsService()
+{
+  MOZ_ASSERT(NS_IsMainThread());
+}
+
+OSFileConstantsService::~OSFileConstantsService()
+{
+  mozilla::CleanupOSFileConstants();
+}
+
+
+NS_IMETHODIMP
+OSFileConstantsService::Init(JSContext *aCx)
+{
+  nsresult rv = mozilla::InitOSFileConstants();
+  if (NS_FAILED(rv)) {
+    return rv;
+  }
+
+  JSObject *global = JS_GetGlobalForScopeChain(aCx);
+  if (!global) {
+    return NS_ERROR_NOT_AVAILABLE;
+  }
+  if (!mozilla::DefineOSFileConstants(aCx, global)) {
+    return NS_ERROR_FAILURE;
+  }
+
+  return NS_OK;
+}
+
 } // namespace mozilla
-
--- a/dom/system/OSFileConstants.h
+++ b/dom/system/OSFileConstants.h
@@ -1,42 +1,58 @@
 /* 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_osfileconstants_h__
 #define mozilla_osfileconstants_h__
 
 #include "jspubtd.h"
+#include "nsIOSFileConstantsService.h"
 
 namespace mozilla {
 
 /**
  * Perform initialization of this module.
  *
  * This function _must_ be called:
  * - from the main thread;
- * - only once;
  * - before any Chrome Worker is created.
+ *
+ * The function is idempotent.
  */
 nsresult InitOSFileConstants();
 
 /**
  * Perform cleanup of this module.
  *
  * This function _must_ be called:
  * - from the main thread;
- * - only once;
  * - after all Chrome Workers are dead.
+ *
+ * The function is idempotent.
  */
-nsresult CleanupOSFileConstants();
+void CleanupOSFileConstants();
 
 /**
  * Define OS-specific constants.
  *
  * This function creates or uses JS object |OS.Constants| to store
  * all its constants.
  */
 bool DefineOSFileConstants(JSContext *cx, JSObject *global);
 
+/**
+ * XPConnect initializer, for use in the main thread.
+ */
+class OSFileConstantsService: public nsIOSFileConstantsService
+{
+ public:
+  NS_DECL_ISUPPORTS
+  NS_DECL_NSIOSFILECONSTANTSSERVICE
+  OSFileConstantsService();
+private:
+  ~OSFileConstantsService();
+};
+
 }
 
 #endif // mozilla_osfileconstants_h__
new file mode 100644
--- /dev/null
+++ b/dom/system/nsIOSFileConstantsService.idl
@@ -0,0 +1,28 @@
+/* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */
+/* vim: set ts=2 et sw=2 tw=40: */
+/* 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"
+
+[scriptable, uuid(d6dd239f-34d6-4b34-baa1-f69ab4a20bc4)]
+interface nsIOSFileConstantsService: nsISupports
+{
+  /**
+   * Inject module OS.Constants in the environment.
+   *
+   * This method must be called only from the main thread.
+   * Method is idempotent.
+   */
+  [implicit_jscontext]
+  void init();
+};
+
+%{ C++
+
+// {4BBE1B96-8956-457F-A03F-9C27435F2AFA}
+#define OSFILECONSTANTSSERVICE_CID {0x4BBE1B96,0x8956,0x457F,{0xA0,0x3F,0x9C,0x27,0x43,0x5F,0x2A,0xFA}}
+#define OSFILECONSTANTSSERVICE_CONTRACTID "@mozilla.org/net/osfileconstantsservice;1"
+
+%}
--- a/dom/workers/RuntimeService.cpp
+++ b/dom/workers/RuntimeService.cpp
@@ -1045,20 +1045,17 @@ RuntimeService::Cleanup()
       }
 
       nsresult rv =
         obs->RemoveObserver(this, NS_XPCOM_SHUTDOWN_THREADS_OBSERVER_ID);
       mObserved = NS_FAILED(rv);
     }
   }
 
-  nsresult rv = CleanupOSFileConstants();
-  if (NS_FAILED(rv)) {
-    MOZ_NOT_REACHED("Could not cleanup OSFileConstants");
-  }
+  CleanupOSFileConstants();
 }
 
 // static
 PLDHashOperator
 RuntimeService::AddAllTopLevelWorkersToArray(const nsACString& aKey,
                                              WorkerDomainInfo* aData,
                                              void* aUserArg)
 {