Bug 775588 - Expose tmpdir and profiledir. r=khuey
☠☠ backed out by 154d7c224af2 ☠ ☠
authorDavid Rajchenbach-Teller <dteller@mozilla.com>
Thu, 26 Jul 2012 21:17:23 -0400
changeset 100676 75e0a5a581bb2ea726ef11604916201bfca119e6
parent 100675 1fd32775559f7e6a20c5a5354d441457af0c9f2c
child 100677 b154dfeb69052c2c9046cf3c72236f8ab174f813
push id12600
push userryanvm@gmail.com
push dateFri, 27 Jul 2012 01:17:14 +0000
treeherdermozilla-inbound@a7e20d9dc2f7 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerskhuey
bugs775588
milestone17.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 775588 - Expose tmpdir and profiledir. r=khuey
dom/system/OSFileConstants.cpp
--- a/dom/system/OSFileConstants.cpp
+++ b/dom/system/OSFileConstants.cpp
@@ -48,61 +48,107 @@ namespace mozilla {
 // Use an anonymous namespace to hide the symbols and avoid any collision
 // with, for instance, |extern bool gInitialized;|
 namespace {
 /**
  * |true| if this module has been initialized, |false| otherwise
  */
 bool gInitialized = false;
 
+typedef struct {
+  /**
+   * The name of the directory holding all the libraries (libxpcom, libnss, etc.)
+   */
+  nsString libdir;
+  nsString tmpdir;
+  nsString profiledir;
+} Paths;
+
 /**
- * The name of the directory holding all the libraries (libxpcom, libnss, etc.)
+ * System directories.
+ */
+Paths* gPaths;
+
+}
+
+/**
+ * Return the path to one of the special directories.
  */
-nsString* gLibDirectory;
+nsresult GetPathToSpecialDir(const char *aKey, nsString aOutPath)
+{
+  nsCOMPtr<nsIFile> file;
+  nsresult rv = NS_GetSpecialDirectory(aKey, getter_AddRefs(file));
+  if (NS_FAILED(rv) || !file) {
+    return rv;
+  }
+
+  return file->GetPath(aOutPath);
 }
 
 /**
  * Perform the part of initialization that can only be
  * executed on the main thread.
  */
 nsresult InitOSFileConstants()
 {
   MOZ_ASSERT(NS_IsMainThread());
   if (gInitialized) {
     return NS_OK;
   }
 
   gInitialized = true;
 
-  // Initialize gLibDirectory
-  nsCOMPtr<nsIFile> xpcomLib;
-  nsresult rv = NS_GetSpecialDirectory("XpcomLib", getter_AddRefs(xpcomLib));
-  if (NS_FAILED(rv) || !xpcomLib) {
+  ScopedDeletePtr<Paths> paths(new Paths);
+
+  // Initialize paths->libdir
+  nsresult rv = NS_GetSpecialDirectory(aKey, getter_AddRefs(file));
+
+  nsCOMPtr<nsIFile> file;
+  nsresult rv = NS_GetSpecialDirectory("XpcomLib", getter_AddRefs(file));
+  if (NS_FAILED(rv) || !file) {
     return rv;
   }
 
   nsCOMPtr<nsIFile> libDir;
-  rv = xpcomLib->GetParent(getter_AddRefs(libDir));
+  rv = file->GetParent(getter_AddRefs(libDir));
+  if (NS_FAILED(rv)) {
+    return rv;
+  }
+
+  rv = libDir->GetPath(paths.rwget()->libdir);
   if (NS_FAILED(rv)) {
     return rv;
   }
 
-  gLibDirectory = new nsString();
-  return libDir->GetPath(*gLibDirectory);
+  rv = GetPathToSpecialDir("TmpD", paths.rwget()->tmpdir);
+  if (NS_FAILED(rv)) {
+    return rv;
+  }
+
+  rv = GetPathToSpecialDir("ProfD", paths.rwget()->profiledir);
+  if (NS_FAILED(rv)) {
+    return rv;
+  }
+
+  gPaths = paths.forget();
+  return NS_OK;
 }
 
+/**
+ * Perform the cleaning up that can only be executed on the main thread.
+ */
 void CleanupOSFileConstants()
 {
   MOZ_ASSERT(NS_IsMainThread());
   if (!gInitialized) {
     return;
   }
 
   gInitialized = false;
-  delete gLibDirectory;
+  delete gPaths;
 }
 
 
 /**
  * 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.
@@ -473,16 +519,27 @@ JSObject *GetOrCreateObjectProperty(JSCo
     JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL,
       JSMSG_UNEXPECTED_TYPE, aProperty, "not an object");
     return NULL;
   }
   return JS_DefineObject(cx, aObject, aProperty, NULL, NULL, JSPROP_ENUMERATE);
 }
 
 /**
+ * Set a property of an object from a nsString.
+ */
+bool SetStringProperty(JSContext *cx, JSObject *aObject, const char *aProperty,
+                       const nsString aValue)
+{
+    JSString* strValue = JS_NewUCStringCopyZ(cx, aValue.get());
+    jsval valValue = STRING_TO_JSVAL(strValue);
+    return JS_SetProperty(cx, aObject, aProperty, &valValue);
+}
+
+/**
  * Define OS-specific constants.
  *
  * This function creates or uses JS object |OS.Constants| to store
  * all its constants.
  */
 bool DefineOSFileConstants(JSContext *cx, JSObject *global)
 {
   MOZ_ASSERT(gInitialized);
@@ -547,38 +604,44 @@ bool DefineOSFileConstants(JSContext *cx
   JSObject *objPath;
   if (!(objPath = GetOrCreateObjectProperty(cx, objConstants, "Path"))) {
     return false;
   }
 
 
   // Locate libxul
   {
-    nsAutoString xulPath(*gLibDirectory);
+    nsAutoString xulPath(gPaths->libdir);
 
     xulPath.Append(PR_GetDirectorySeparator());
 
 #if defined(XP_MACOSX)
     // Under MacOS X, for some reason, libxul is called simply "XUL"
     xulPath.Append(NS_LITERAL_STRING("XUL"));
 #else
     // On other platforms, libxul is a library "xul" with regular
     // library prefix/suffix
     xulPath.Append(NS_LITERAL_STRING(DLL_PREFIX));
     xulPath.Append(NS_LITERAL_STRING("xul"));
     xulPath.Append(NS_LITERAL_STRING(DLL_SUFFIX));
 #endif // defined(XP_MACOSX)
 
-    JSString* strPathToLibXUL = JS_NewUCStringCopyZ(cx, xulPath.get());
-    jsval valXul = STRING_TO_JSVAL(strPathToLibXUL);
-    if (!JS_SetProperty(cx, objPath, "libxul", &valXul)) {
+    if (!SetStringProperty(cx, objPath, "libxul", xulPath)) {
       return false;
     }
   }
 
+  if (!SetStringProperty(cx, objPath, "tmpdir", gPaths->tmpdir)) {
+    return false;
+  }
+
+  if (!SetStringProperty(cx, objPath, "profiledir", gPaths->libdir)) {
+    return false;
+  }
+
   return true;
 }
 
 NS_IMPL_ISUPPORTS1(OSFileConstantsService, nsIOSFileConstantsService)
 
 OSFileConstantsService::OSFileConstantsService()
 {
   MOZ_ASSERT(NS_IsMainThread());