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
Родитель 5afb9ba642
Коммит 7b269a47ba
10 изменённых файлов: 161 добавлений и 146 удалений

Просмотреть файл

@ -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<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);
}
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();

Просмотреть файл

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

Просмотреть файл

@ -26,6 +26,7 @@
#include "mozilla/Util.h"
#include "nsAppRunner.h"
#include "mozilla/AppData.h"
#include "nsUpdateDriver.h"
#include "ProfileReset.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<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;

Просмотреть файл

@ -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<char*>(str));
if (newvalue) {
str = NS_strdup(newvalue);
} else {
str = nsnull;
}
}
int
main(int argc, char **argv)
{

Просмотреть файл

@ -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<char*>(str));
if (newvalue) {
str = NS_strdup(newvalue);
}
else {
str = nsnull;
}
}
nsresult
joinPath(char* const dest,
char const* const dir,

90
xpcom/glue/AppData.cpp Normal 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
Просмотреть файл

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

Просмотреть файл

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

Просмотреть файл

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