From ea4315c1ff09ec851393e7ea0d53057b048dc863 Mon Sep 17 00:00:00 2001 From: Benjamin Smedberg Date: Fri, 8 Jun 2012 08:41:30 -0400 Subject: [PATCH] Bug 755724 Part C - Move ScopedAppData into the XPCOM glue, r=glandium --HG-- rename : toolkit/xre/nsAppData.cpp => toolkit/xre/CreateAppData.cpp rename : toolkit/xre/nsAppData.cpp => xpcom/glue/AppData.cpp extra : rebase_source : 6223397345e77b00f14d93a1f188c042fc5f89ea --- .../xre/{nsAppData.cpp => CreateAppData.cpp} | 83 +---------------- toolkit/xre/Makefile.in | 2 +- toolkit/xre/nsAppRunner.cpp | 1 + toolkit/xre/nsAppRunner.h | 40 --------- webapprt/mac/webapprt.mm | 16 +--- webapprt/win/webapprt.cpp | 16 +--- xpcom/glue/AppData.cpp | 90 +++++++++++++++++++ xpcom/glue/AppData.h | 57 ++++++++++++ xpcom/glue/Makefile.in | 1 + xpcom/glue/objs.mk | 1 + 10 files changed, 161 insertions(+), 146 deletions(-) rename toolkit/xre/{nsAppData.cpp => CreateAppData.cpp} (62%) create mode 100644 xpcom/glue/AppData.cpp create mode 100644 xpcom/glue/AppData.h diff --git a/toolkit/xre/nsAppData.cpp b/toolkit/xre/CreateAppData.cpp similarity index 62% rename from toolkit/xre/nsAppData.cpp rename to toolkit/xre/CreateAppData.cpp index 437079c8da8..93708c5e420 100644 --- a/toolkit/xre/nsAppData.cpp +++ b/toolkit/xre/CreateAppData.cpp @@ -1,3 +1,4 @@ +/* -*- 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/. */ @@ -5,84 +6,10 @@ #include "nsXULAppAPI.h" #include "nsINIParser.h" #include "nsIFile.h" -#include "nsAppRunner.h" -#include "nsCRTGlue.h" #include "nsAutoPtr.h" +#include "mozilla/AppData.h" -void -SetAllocatedString(const char *&str, const char *newvalue) -{ - NS_Free(const_cast(str)); - if (newvalue) { - str = NS_strdup(newvalue); - } - else { - str = nsnull; - } -} - -void -SetAllocatedString(const char *&str, const nsACString &newvalue) -{ - NS_Free(const_cast(str)); - if (newvalue.IsEmpty()) { - str = nsnull; - } - else { - str = ToNewCString(newvalue); - } -} - -ScopedAppData::ScopedAppData(const nsXREAppData* aAppData) -{ - Zero(); - - this->size = aAppData->size; - - 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) @@ -103,9 +30,7 @@ XRE_CreateAppData(nsIFile* aINIFile, nsXREAppData **aAppData) if (NS_FAILED(rv)) return rv; - rv = CallQueryInterface(appDir, &data->directory); - if (NS_FAILED(rv)) - return rv; + appDir.forget(&data->directory); } *aAppData = data.forget(); diff --git a/toolkit/xre/Makefile.in b/toolkit/xre/Makefile.in index f5b0c2a9714..da950662b75 100644 --- a/toolkit/xre/Makefile.in +++ b/toolkit/xre/Makefile.in @@ -33,7 +33,7 @@ CPPSRCS = \ nsConsoleWriter.cpp \ nsXREDirProvider.cpp \ nsNativeAppSupportBase.cpp \ - nsAppData.cpp \ + CreateAppData.cpp \ nsSigHandlers.cpp \ nsEmbedFunctions.cpp \ ProfileReset.cpp \ diff --git a/toolkit/xre/nsAppRunner.cpp b/toolkit/xre/nsAppRunner.cpp index 48b3823f94c..a2863a55810 100644 --- a/toolkit/xre/nsAppRunner.cpp +++ b/toolkit/xre/nsAppRunner.cpp @@ -26,6 +26,7 @@ #include "mozilla/Util.h" #include "nsAppRunner.h" +#include "mozilla/AppData.h" #include "nsUpdateDriver.h" #include "ProfileReset.h" diff --git a/toolkit/xre/nsAppRunner.h b/toolkit/xre/nsAppRunner.h index a969da251a5..f7a94220ade 100644 --- a/toolkit/xre/nsAppRunner.h +++ b/toolkit/xre/nsAppRunner.h @@ -108,46 +108,6 @@ WriteStatusApplied(LPCWSTR updateDirPath); #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 -void SetStrongPtr(T *&ptr, T* newvalue) -{ - NS_IF_RELEASE(ptr); - ptr = newvalue; - NS_IF_ADDREF(ptr); -} - namespace mozilla { namespace startup { extern GeckoProcessType sChildProcessType; diff --git a/webapprt/mac/webapprt.mm b/webapprt/mac/webapprt.mm index b0387d4a75a..174fe8f4da8 100644 --- a/webapprt/mac/webapprt.mm +++ b/webapprt/mac/webapprt.mm @@ -28,6 +28,9 @@ #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"; @@ -82,19 +85,6 @@ AttemptGRELoad(char *greDir) return rv; } -// Copied from toolkit/xre/nsAppData.cpp. -void -SetAllocatedString(const char *&str, const char *newvalue) -{ - NS_Free(const_cast(str)); - if (newvalue) { - str = NS_strdup(newvalue); - } else { - str = nsnull; - } -} - - int main(int argc, char **argv) { diff --git a/webapprt/win/webapprt.cpp b/webapprt/win/webapprt.cpp index e62b76f58b6..17537bef3f5 100644 --- a/webapprt/win/webapprt.cpp +++ b/webapprt/win/webapprt.cpp @@ -18,6 +18,9 @@ #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; @@ -41,19 +44,6 @@ namespace { int* pargc; char*** pargv; - // Copied from toolkit/xre/nsAppData.cpp. - void - SetAllocatedString(const char *&str, const char *newvalue) - { - NS_Free(const_cast(str)); - if (newvalue) { - str = NS_strdup(newvalue); - } - else { - str = nsnull; - } - } - nsresult joinPath(char* const dest, char const* const dir, diff --git a/xpcom/glue/AppData.cpp b/xpcom/glue/AppData.cpp new file mode 100644 index 00000000000..8caa314fc1b --- /dev/null +++ b/xpcom/glue/AppData.cpp @@ -0,0 +1,90 @@ +/* -*- 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 "nsCRTGlue.h" +#include "nsAutoPtr.h" + +namespace mozilla { + +void +SetAllocatedString(const char *&str, const char *newvalue) +{ + NS_Free(const_cast(str)); + if (newvalue) { + str = NS_strdup(newvalue); + } + else { + str = nsnull; + } +} + +void +SetAllocatedString(const char *&str, const nsACString &newvalue) +{ + NS_Free(const_cast(str)); + if (newvalue.IsEmpty()) { + str = nsnull; + } + else { + str = ToNewCString(newvalue); + } +} + +ScopedAppData::ScopedAppData(const nsXREAppData* aAppData) +{ + Zero(); + + this->size = aAppData->size; + + 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); +} + +} // namespace mozilla diff --git a/xpcom/glue/AppData.h b/xpcom/glue/AppData.h new file mode 100644 index 00000000000..bb771815686 --- /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 +void SetStrongPtr(T *&ptr, T* newvalue) +{ + NS_IF_RELEASE(ptr); + ptr = newvalue; + NS_IF_ADDREF(ptr); +} + +} // namespace mozilla + +#endif diff --git a/xpcom/glue/Makefile.in b/xpcom/glue/Makefile.in index 19644b22a0b..cf0a23aeff8 100644 --- a/xpcom/glue/Makefile.in +++ b/xpcom/glue/Makefile.in @@ -91,6 +91,7 @@ EXPORTS = \ $(NULL) EXPORTS_mozilla = \ + AppData.h \ AutoRestore.h \ BlockingResourceBase.h \ CondVar.h \ diff --git a/xpcom/glue/objs.mk b/xpcom/glue/objs.mk index aa20ad87524..1cc93fe74d9 100644 --- a/xpcom/glue/objs.mk +++ b/xpcom/glue/objs.mk @@ -3,6 +3,7 @@ # file, You can obtain one at http://mozilla.org/MPL/2.0/. XPCOM_GLUE_SRC_LCPPSRCS = \ + AppData.cpp \ nsArrayEnumerator.cpp \ nsArrayUtils.cpp \ nsCategoryCache.cpp \