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
This commit is contained in:
Benjamin Smedberg 2012-06-08 08:41:30 -04:00
parent b8289dc31e
commit ea4315c1ff
10 changed files with 161 additions and 146 deletions

View File

@ -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 /* 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 * 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/. */ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
@ -5,84 +6,10 @@
#include "nsXULAppAPI.h" #include "nsXULAppAPI.h"
#include "nsINIParser.h" #include "nsINIParser.h"
#include "nsIFile.h" #include "nsIFile.h"
#include "nsAppRunner.h"
#include "nsCRTGlue.h"
#include "nsAutoPtr.h" #include "nsAutoPtr.h"
#include "mozilla/AppData.h"
void using namespace mozilla;
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;
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);
}
nsresult nsresult
XRE_CreateAppData(nsIFile* aINIFile, nsXREAppData **aAppData) XRE_CreateAppData(nsIFile* aINIFile, nsXREAppData **aAppData)
@ -103,9 +30,7 @@ XRE_CreateAppData(nsIFile* aINIFile, nsXREAppData **aAppData)
if (NS_FAILED(rv)) if (NS_FAILED(rv))
return rv; return rv;
rv = CallQueryInterface(appDir, &data->directory); appDir.forget(&data->directory);
if (NS_FAILED(rv))
return rv;
} }
*aAppData = data.forget(); *aAppData = data.forget();

View File

@ -33,7 +33,7 @@ CPPSRCS = \
nsConsoleWriter.cpp \ nsConsoleWriter.cpp \
nsXREDirProvider.cpp \ nsXREDirProvider.cpp \
nsNativeAppSupportBase.cpp \ nsNativeAppSupportBase.cpp \
nsAppData.cpp \ CreateAppData.cpp \
nsSigHandlers.cpp \ nsSigHandlers.cpp \
nsEmbedFunctions.cpp \ nsEmbedFunctions.cpp \
ProfileReset.cpp \ ProfileReset.cpp \

View File

@ -26,6 +26,7 @@
#include "mozilla/Util.h" #include "mozilla/Util.h"
#include "nsAppRunner.h" #include "nsAppRunner.h"
#include "mozilla/AppData.h"
#include "nsUpdateDriver.h" #include "nsUpdateDriver.h"
#include "ProfileReset.h" #include "ProfileReset.h"

View File

@ -108,46 +108,6 @@ WriteStatusApplied(LPCWSTR updateDirPath);
#define NS_NATIVEAPPSUPPORT_CONTRACTID "@mozilla.org/toolkit/native-app-support;1" #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 mozilla {
namespace startup { namespace startup {
extern GeckoProcessType sChildProcessType; extern GeckoProcessType sChildProcessType;

View File

@ -28,6 +28,9 @@
#include "nsCOMPtr.h" #include "nsCOMPtr.h"
#include "nsIFile.h" #include "nsIFile.h"
#include "nsStringGlue.h" #include "nsStringGlue.h"
#include "mozilla/AppData.h"
using namespace mozilla;
const char WEBAPPRT_EXECUTABLE[] = "webapprt-stub"; const char WEBAPPRT_EXECUTABLE[] = "webapprt-stub";
const char FXAPPINI_NAME[] = "application.ini"; const char FXAPPINI_NAME[] = "application.ini";
@ -82,19 +85,6 @@ AttemptGRELoad(char *greDir)
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 int
main(int argc, char **argv) main(int argc, char **argv)
{ {

View File

@ -18,6 +18,9 @@
#include "nsXPCOMGlue.h" #include "nsXPCOMGlue.h"
#include "nsXPCOMPrivate.h" // for MAXPATHLEN and XPCOM_DLL #include "nsXPCOMPrivate.h" // for MAXPATHLEN and XPCOM_DLL
#include "nsXULAppAPI.h" #include "nsXULAppAPI.h"
#include "mozilla/AppData.h"
using namespace mozilla;
XRE_GetFileFromPathType XRE_GetFileFromPath; XRE_GetFileFromPathType XRE_GetFileFromPath;
XRE_CreateAppDataType XRE_CreateAppData; XRE_CreateAppDataType XRE_CreateAppData;
@ -41,19 +44,6 @@ namespace {
int* pargc; int* pargc;
char*** pargv; 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 nsresult
joinPath(char* const dest, joinPath(char* const dest,
char const* const dir, char const* const dir,

90
xpcom/glue/AppData.cpp Normal file
View File

@ -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<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;
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

57
xpcom/glue/AppData.h Normal file
View File

@ -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

View File

@ -91,6 +91,7 @@ EXPORTS = \
$(NULL) $(NULL)
EXPORTS_mozilla = \ EXPORTS_mozilla = \
AppData.h \
AutoRestore.h \ AutoRestore.h \
BlockingResourceBase.h \ BlockingResourceBase.h \
CondVar.h \ CondVar.h \

View File

@ -3,6 +3,7 @@
# file, You can obtain one at http://mozilla.org/MPL/2.0/. # file, You can obtain one at http://mozilla.org/MPL/2.0/.
XPCOM_GLUE_SRC_LCPPSRCS = \ XPCOM_GLUE_SRC_LCPPSRCS = \
AppData.cpp \
nsArrayEnumerator.cpp \ nsArrayEnumerator.cpp \
nsArrayUtils.cpp \ nsArrayUtils.cpp \
nsCategoryCache.cpp \ nsCategoryCache.cpp \