Bug 755724 Part C - Move ScopedAppData into the XPCOM glue, r=glandium
authorBenjamin Smedberg <benjamin@smedbergs.us>
Fri, 08 Jun 2012 08:41:30 -0400
changeset 97049 efe2ccacf0409e957db86a00064f343959a908f4
parent 97048 d3c41f0a8d7c674f84b2456ed9110201b9734834
child 97050 d977dae53ff9353e80e34ad286cf02571f305459
push id22957
push usermh@glandium.org
push dateWed, 20 Jun 2012 08:54:19 +0000
treeherdermozilla-central@c3190d715044 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersglandium
bugs755724
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 755724 Part C - Move ScopedAppData into the XPCOM glue, r=glandium
toolkit/xre/CreateAppData.cpp
toolkit/xre/Makefile.in
toolkit/xre/nsAppData.cpp
toolkit/xre/nsAppRunner.cpp
toolkit/xre/nsAppRunner.h
webapprt/mac/webapprt.mm
webapprt/win/webapprt.cpp
xpcom/glue/AppData.cpp
xpcom/glue/AppData.h
xpcom/glue/Makefile.in
xpcom/glue/objs.mk
rename from toolkit/xre/nsAppData.cpp
rename to toolkit/xre/CreateAppData.cpp
--- a/toolkit/xre/nsAppData.cpp
+++ b/toolkit/xre/CreateAppData.cpp
@@ -1,93 +1,20 @@
+/* -*- Mode: C++; tab-width: 8; 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 "nsXULAppAPI.h"
 #include "nsINIParser.h"
 #include "nsIFile.h"
-#include "nsAppRunner.h"
-#include "nsCRTGlue.h"
 #include "nsAutoPtr.h"
-
-void
-SetAllocatedString(const char *&str, const char *newvalue)
-{
-  NS_Free(const_cast<char*>(str));
-  if (newvalue) {
-    str = NS_strdup(newvalue);
-  }
-  else {
-    str = nsnull;
-  }
-}
-
-void
-SetAllocatedString(const char *&str, const nsACString &newvalue)
-{
-  NS_Free(const_cast<char*>(str));
-  if (newvalue.IsEmpty()) {
-    str = nsnull;
-  }
-  else {
-    str = ToNewCString(newvalue);
-  }
-}
-
-ScopedAppData::ScopedAppData(const nsXREAppData* aAppData)
-{
-  Zero();
-
-  this->size = aAppData->size;
+#include "mozilla/AppData.h"
 
-  SetAllocatedString(this->vendor, aAppData->vendor);
-  SetAllocatedString(this->name, aAppData->name);
-  SetAllocatedString(this->version, aAppData->version);
-  SetAllocatedString(this->buildID, aAppData->buildID);
-  SetAllocatedString(this->ID, aAppData->ID);
-  SetAllocatedString(this->copyright, aAppData->copyright);
-  SetAllocatedString(this->profile, aAppData->profile);
-  SetStrongPtr(this->directory, aAppData->directory);
-  this->flags = aAppData->flags;
-
-  if (aAppData->size > offsetof(nsXREAppData, xreDirectory)) {
-    SetStrongPtr(this->xreDirectory, aAppData->xreDirectory);
-    SetAllocatedString(this->minVersion, aAppData->minVersion);
-    SetAllocatedString(this->maxVersion, aAppData->maxVersion);
-  }
-
-  if (aAppData->size > offsetof(nsXREAppData, crashReporterURL)) {
-    SetAllocatedString(this->crashReporterURL, aAppData->crashReporterURL);
-  }
-
-  if (aAppData->size > offsetof(nsXREAppData, UAName)) {
-    SetAllocatedString(this->UAName, aAppData->UAName);
-  }
-}
-
-ScopedAppData::~ScopedAppData()
-{
-  SetAllocatedString(this->vendor, nsnull);
-  SetAllocatedString(this->name, nsnull);
-  SetAllocatedString(this->version, nsnull);
-  SetAllocatedString(this->buildID, nsnull);
-  SetAllocatedString(this->ID, nsnull);
-  SetAllocatedString(this->copyright, nsnull);
-  SetAllocatedString(this->profile, nsnull);
-
-  NS_IF_RELEASE(this->directory);
-
-  SetStrongPtr(this->xreDirectory, (nsIFile*) nsnull);
-  SetAllocatedString(this->minVersion, nsnull);
-  SetAllocatedString(this->maxVersion, nsnull);
-
-  SetAllocatedString(this->crashReporterURL, nsnull);
-  SetAllocatedString(this->UAName, nsnull);
-}
+using namespace mozilla;
 
 nsresult
 XRE_CreateAppData(nsIFile* aINIFile, nsXREAppData **aAppData)
 {
   NS_ENSURE_ARG(aINIFile && aAppData);
 
   nsAutoPtr<ScopedAppData> data(new ScopedAppData());
   if (!data)
@@ -98,19 +25,17 @@ XRE_CreateAppData(nsIFile* aINIFile, nsX
     return rv;
 
   if (!data->directory) {
     nsCOMPtr<nsIFile> appDir;
     rv = aINIFile->GetParent(getter_AddRefs(appDir));
     if (NS_FAILED(rv))
       return rv;
 
-    rv = CallQueryInterface(appDir, &data->directory);
-    if (NS_FAILED(rv))
-      return rv;
+    appDir.forget(&data->directory);
   }
 
   *aAppData = data.forget();
   return NS_OK;
 }
 
 struct ReadString {
   const char *section;
--- a/toolkit/xre/Makefile.in
+++ b/toolkit/xre/Makefile.in
@@ -28,17 +28,17 @@ ifeq ($(OS_ARCH),WINNT)
 XPIDLSRCS += nsIWinAppHelper.idl
 endif
 
 CPPSRCS = \
   nsAppRunner.cpp \
   nsConsoleWriter.cpp \
   nsXREDirProvider.cpp \
   nsNativeAppSupportBase.cpp \
-  nsAppData.cpp \
+  CreateAppData.cpp \
   nsSigHandlers.cpp \
   nsEmbedFunctions.cpp \
   ProfileReset.cpp \
   $(NULL)
 
 ifeq ($(MOZ_GL_DEFAULT_PROVIDER),GLX)
 DEFINES += -DUSE_GLX_TEST
 CPPSRCS += glxtest.cpp
--- a/toolkit/xre/nsAppRunner.cpp
+++ b/toolkit/xre/nsAppRunner.cpp
@@ -21,16 +21,17 @@
 #endif // MOZ_WIDGET_QT
 
 #include "mozilla/dom/ContentParent.h"
 #include "mozilla/dom/ContentChild.h"
 
 #include "mozilla/Util.h"
 
 #include "nsAppRunner.h"
+#include "mozilla/AppData.h"
 #include "nsUpdateDriver.h"
 #include "ProfileReset.h"
 
 #ifdef MOZ_INSTRUMENT_EVENT_LOOP
 #include "EventTracer.h"
 #endif
 
 #ifdef XP_MACOSX
--- a/toolkit/xre/nsAppRunner.h
+++ b/toolkit/xre/nsAppRunner.h
@@ -103,56 +103,16 @@ WinLaunchChild(const PRUnichar *exePath,
 BOOL
 WriteStatusPending(LPCWSTR updateDirPath);
 BOOL
 WriteStatusApplied(LPCWSTR updateDirPath);
 #endif
 
 #define NS_NATIVEAPPSUPPORT_CONTRACTID "@mozilla.org/toolkit/native-app-support;1"
 
-// Like nsXREAppData, but releases all strong refs/allocated memory
-// in the destructor.
-class ScopedAppData : public nsXREAppData
-{
-public:
-  ScopedAppData() { Zero(); this->size = sizeof(*this); }
-
-  ScopedAppData(const nsXREAppData* aAppData);
-
-  void Zero() { memset(this, 0, sizeof(*this)); }
-
-  ~ScopedAppData();
-};
-
-/**
- * Given "str" is holding a string allocated with NS_Alloc, or null:
- * replace the value in "str" with a new value.
- *
- * @param newvalue Null is permitted. The string is cloned with
- *                 NS_strdup
- */
-void SetAllocatedString(const char *&str, const char *newvalue);
-
-/**
- * Given "str" is holding a string allocated with NS_Alloc, or null:
- * replace the value in "str" with a new value.
- *
- * @param newvalue If "newvalue" is the empty string, "str" will be set
- *                 to null.
- */
-void SetAllocatedString(const char *&str, const nsACString &newvalue);
-
-template<class T>
-void SetStrongPtr(T *&ptr, T* newvalue)
-{
-  NS_IF_RELEASE(ptr);
-  ptr = newvalue;
-  NS_IF_ADDREF(ptr);
-}
-
 namespace mozilla {
 namespace startup {
 extern GeckoProcessType sChildProcessType;
 }
 }
 
 /**
  * Set up platform specific error handling such as suppressing DLL load dialog
--- a/webapprt/mac/webapprt.mm
+++ b/webapprt/mac/webapprt.mm
@@ -23,16 +23,19 @@
 #include "nsXPCOMGlue.h"
 #include "nsINIParser.h"
 #include "nsXPCOMPrivate.h"              // for MAXPATHLEN and XPCOM_DLL
 #include "nsXULAppAPI.h"
 #include "nsComponentManagerUtils.h"
 #include "nsCOMPtr.h"
 #include "nsIFile.h"
 #include "nsStringGlue.h"
+#include "mozilla/AppData.h"
+
+using namespace mozilla;
 
 const char WEBAPPRT_EXECUTABLE[] = "webapprt-stub";
 const char FXAPPINI_NAME[] = "application.ini";
 const char WEBAPPINI_NAME[] = "webapp.ini";
 const char WEBRTINI_NAME[] = "webapprt.ini";
 
 //need the correct relative path here
 const char APP_CONTENTS_PATH[] = "/Contents/MacOS/";
@@ -77,29 +80,16 @@ AttemptGRELoad(char *greDir)
   rv = XPCOMGlueLoadXULFunctions(kXULFuncs);
   if (NS_FAILED(rv)) {
     return rv;
   }
 
   return rv;
 }
 
-// Copied from toolkit/xre/nsAppData.cpp.
-void
-SetAllocatedString(const char *&str, const char *newvalue)
-{
-  NS_Free(const_cast<char*>(str));
-  if (newvalue) {
-    str = NS_strdup(newvalue);
-  } else {
-    str = nsnull;
-  }
-}
-
-
 int
 main(int argc, char **argv)
 {
   NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
 
   NSString *firefoxPath = nil;
   NSString *alternateBinaryID = nil;
 
--- a/webapprt/win/webapprt.cpp
+++ b/webapprt/win/webapprt.cpp
@@ -13,16 +13,19 @@
 
 // Mozilla headers (alphabetical)
 #include "nsIFile.h"
 #include "nsINIParser.h"
 #include "nsWindowsWMain.cpp"   // we want a wmain entry point
 #include "nsXPCOMGlue.h"
 #include "nsXPCOMPrivate.h"     // for MAXPATHLEN and XPCOM_DLL
 #include "nsXULAppAPI.h"
+#include "mozilla/AppData.h"
+
+using namespace mozilla;
 
 XRE_GetFileFromPathType XRE_GetFileFromPath;
 XRE_CreateAppDataType XRE_CreateAppData;
 XRE_FreeAppDataType XRE_FreeAppData;
 XRE_mainType XRE_main;
 
 namespace {
   const char kAPP_INI[] = "application.ini";
@@ -36,29 +39,16 @@ namespace {
 
   wchar_t curExePath[MAXPATHLEN];
   wchar_t backupFilePath[MAXPATHLEN];
   wchar_t iconPath[MAXPATHLEN];
   char profile[MAXPATHLEN];
   int* pargc;
   char*** pargv;
 
-  // Copied from toolkit/xre/nsAppData.cpp.
-  void
-  SetAllocatedString(const char *&str, const char *newvalue)
-  {
-    NS_Free(const_cast<char*>(str));
-    if (newvalue) {
-      str = NS_strdup(newvalue);
-    }
-    else {
-      str = nsnull;
-    }
-  }
-
   nsresult
   joinPath(char* const dest,
            char const* const dir,
            char const* const leaf,
            size_t bufferSize)
   {
     size_t dirLen = strlen(dir);
     size_t leafLen = strlen(leaf);
copy from toolkit/xre/nsAppData.cpp
copy to xpcom/glue/AppData.cpp
--- a/toolkit/xre/nsAppData.cpp
+++ b/xpcom/glue/AppData.cpp
@@ -1,19 +1,22 @@
+/* -*- Mode: C++; tab-width: 8; 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/AppData.h"
 #include "nsXULAppAPI.h"
 #include "nsINIParser.h"
 #include "nsIFile.h"
-#include "nsAppRunner.h"
 #include "nsCRTGlue.h"
 #include "nsAutoPtr.h"
 
+namespace mozilla {
+
 void
 SetAllocatedString(const char *&str, const char *newvalue)
 {
   NS_Free(const_cast<char*>(str));
   if (newvalue) {
     str = NS_strdup(newvalue);
   }
   else {
@@ -79,162 +82,9 @@ ScopedAppData::~ScopedAppData()
   SetStrongPtr(this->xreDirectory, (nsIFile*) nsnull);
   SetAllocatedString(this->minVersion, nsnull);
   SetAllocatedString(this->maxVersion, nsnull);
 
   SetAllocatedString(this->crashReporterURL, nsnull);
   SetAllocatedString(this->UAName, nsnull);
 }
 
-nsresult
-XRE_CreateAppData(nsIFile* aINIFile, nsXREAppData **aAppData)
-{
-  NS_ENSURE_ARG(aINIFile && aAppData);
-
-  nsAutoPtr<ScopedAppData> data(new ScopedAppData());
-  if (!data)
-    return NS_ERROR_OUT_OF_MEMORY;
-
-  nsresult rv = XRE_ParseAppData(aINIFile, data);
-  if (NS_FAILED(rv))
-    return rv;
-
-  if (!data->directory) {
-    nsCOMPtr<nsIFile> appDir;
-    rv = aINIFile->GetParent(getter_AddRefs(appDir));
-    if (NS_FAILED(rv))
-      return rv;
-
-    rv = CallQueryInterface(appDir, &data->directory);
-    if (NS_FAILED(rv))
-      return rv;
-  }
-
-  *aAppData = data.forget();
-  return NS_OK;
-}
-
-struct ReadString {
-  const char *section;
-  const char *key;
-  const char **buffer;
-};
-
-static void
-ReadStrings(nsINIParser &parser, const ReadString *reads)
-{
-  nsresult rv;
-  nsCString str;
-
-  while (reads->section) {
-    rv = parser.GetString(reads->section, reads->key, str);
-    if (NS_SUCCEEDED(rv)) {
-      SetAllocatedString(*reads->buffer, str);
-    }
-
-    ++reads;
-  }
-}
-
-struct ReadFlag {
-  const char *section;
-  const char *key;
-  PRUint32 flag;
-};
-
-static void
-ReadFlags(nsINIParser &parser, const ReadFlag *reads, PRUint32 *buffer)
-{
-  nsresult rv;
-  char buf[6]; // large enough to hold "false"
-
-  while (reads->section) {
-    rv = parser.GetString(reads->section, reads->key, buf, sizeof(buf));
-    if (NS_SUCCEEDED(rv) || rv == NS_ERROR_LOSS_OF_SIGNIFICANT_DATA) {
-      if (buf[0] == '1' || buf[0] == 't' || buf[0] == 'T') {
-        *buffer |= reads->flag;
-      }
-      if (buf[0] == '0' || buf[0] == 'f' || buf[0] == 'F') {
-        *buffer &= ~reads->flag;
-      }
-    }
-
-    ++reads;
-  }
-}
-
-nsresult
-XRE_ParseAppData(nsIFile* aINIFile, nsXREAppData *aAppData)
-{
-  NS_ENSURE_ARG(aINIFile && aAppData);
-
-  nsresult rv;
-
-  nsINIParser parser;
-  rv = parser.Init(aINIFile);
-  if (NS_FAILED(rv))
-    return rv;
-
-  nsCString str;
-
-  ReadString strings[] = {
-    { "App", "Vendor",    &aAppData->vendor },
-    { "App", "Name",      &aAppData->name },
-    { "App", "Version",   &aAppData->version },
-    { "App", "BuildID",   &aAppData->buildID },
-    { "App", "ID",        &aAppData->ID },
-    { "App", "Copyright", &aAppData->copyright },
-    { "App", "Profile",   &aAppData->profile },
-    { nsnull }
-  };
-  ReadStrings(parser, strings);
-
-  ReadFlag flags[] = {
-    { "XRE", "EnableProfileMigrator", NS_XRE_ENABLE_PROFILE_MIGRATOR },
-    { "XRE", "EnableExtensionManager", NS_XRE_ENABLE_EXTENSION_MANAGER },
-    { nsnull }
-  };
-  ReadFlags(parser, flags, &aAppData->flags);
-
-  if (aAppData->size > offsetof(nsXREAppData, xreDirectory)) {
-    ReadString strings2[] = {
-      { "Gecko", "MinVersion", &aAppData->minVersion },
-      { "Gecko", "MaxVersion", &aAppData->maxVersion },
-      { nsnull }
-    };
-    ReadStrings(parser, strings2);
-  }
-
-  if (aAppData->size > offsetof(nsXREAppData, crashReporterURL)) {
-    ReadString strings3[] = {
-      { "Crash Reporter", "ServerURL", &aAppData->crashReporterURL },
-      { nsnull }
-    };
-    ReadStrings(parser, strings3);
-    ReadFlag flags2[] = {
-      { "Crash Reporter", "Enabled", NS_XRE_ENABLE_CRASH_REPORTER },
-      { nsnull }
-    };
-    ReadFlags(parser, flags2, &aAppData->flags);
-  }
-
-  if (aAppData->size > offsetof(nsXREAppData, UAName)) {
-    ReadString strings4[] = {
-      { "App", "UAName",    &aAppData->UAName },
-      { nsnull }
-    };
-    ReadStrings(parser, strings4);
-  }
-
-  return NS_OK;
-}
-
-void
-XRE_FreeAppData(nsXREAppData *aAppData)
-{
-  if (!aAppData) {
-    NS_ERROR("Invalid arg");
-    return;
-  }
-
-  ScopedAppData* sad = static_cast<ScopedAppData*>(aAppData);
-  delete sad;
-}
+} // namespace mozilla
new file mode 100644
--- /dev/null
+++ b/xpcom/glue/AppData.h
@@ -0,0 +1,57 @@
+/* -*- Mode: C++; tab-width: 8; 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_AppData_h
+#define mozilla_AppData_h
+
+#include "nsXREAppData.h"
+#include "nscore.h"
+#include "nsStringGlue.h"
+
+namespace mozilla {
+
+// Like nsXREAppData, but releases all strong refs/allocated memory
+// in the destructor.
+class NS_COM_GLUE ScopedAppData : public nsXREAppData
+{
+public:
+  ScopedAppData() { Zero(); this->size = sizeof(*this); }
+
+  ScopedAppData(const nsXREAppData* aAppData);
+
+  void Zero() { memset(this, 0, sizeof(*this)); }
+
+  ~ScopedAppData();
+};
+
+/**
+ * Given "str" is holding a string allocated with NS_Alloc, or null:
+ * replace the value in "str" with a new value.
+ *
+ * @param newvalue Null is permitted. The string is cloned with
+ *                 NS_strdup
+ */
+void SetAllocatedString(const char *&str, const char *newvalue);
+
+/**
+ * Given "str" is holding a string allocated with NS_Alloc, or null:
+ * replace the value in "str" with a new value.
+ *
+ * @param newvalue If "newvalue" is the empty string, "str" will be set
+ *                 to null.
+ */
+void SetAllocatedString(const char *&str, const nsACString &newvalue);
+
+template<class T>
+void SetStrongPtr(T *&ptr, T* newvalue)
+{
+  NS_IF_RELEASE(ptr);
+  ptr = newvalue;
+  NS_IF_ADDREF(ptr);
+}
+
+} // namespace mozilla
+
+#endif
--- a/xpcom/glue/Makefile.in
+++ b/xpcom/glue/Makefile.in
@@ -86,16 +86,17 @@ EXPORTS = \
 		nsThreadUtils.h \
 		nsTPriorityQueue.h \
 		nsProxyRelease.h \
 		nsXPTCUtils.h \
 		nsCycleCollectorUtils.h \
 		$(NULL)
 
 EXPORTS_mozilla = \
+  AppData.h \
   AutoRestore.h \
   BlockingResourceBase.h \
   CondVar.h \
   DeadlockDetector.h \
   FileUtils.h \
   GenericFactory.h \
   IntentionalCrash.h \
   Monitor.h \
--- a/xpcom/glue/objs.mk
+++ b/xpcom/glue/objs.mk
@@ -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/.
 
 XPCOM_GLUE_SRC_LCPPSRCS =        \
+  AppData.cpp                    \
   nsArrayEnumerator.cpp          \
   nsArrayUtils.cpp               \
   nsCategoryCache.cpp            \
   nsCOMPtr.cpp                   \
   nsCOMArray.cpp                 \
   nsCRTGlue.cpp                  \
   nsClassInfoImpl.cpp            \
   nsComponentManagerUtils.cpp    \