From d37894f60d71a895d5d359f18a5c6a4cec4b4b6f Mon Sep 17 00:00:00 2001 From: Benjamin Smedberg Date: Fri, 2 Dec 2016 09:07:24 -0500 Subject: [PATCH] Bug 1321593 part A - Refactor nsXREAppData: 1) make nsXREAppData strongly own its members 2) rename it to mozilla::XREAppData 3) separate out the static compiled data into StaticXREAppData 4) Remove XRE_CreateAppData and XRE_FreeAppData 5) remove the struct size and related size-checking code which was only ever useful for cross-version compatibility, r=glandium MozReview-Commit-ID: CQv1UrSaw4D --HG-- rename : xpcom/build/nsXREAppData.h => xpcom/build/XREAppData.h rename : xpcom/glue/AppData.cpp => xpcom/glue/XREAppData.cpp extra : source : eae2252a519f3ac5850f5110a6a1be45891ea5e9 --- browser/app/nsBrowserApp.cpp | 28 ++-- build/appini_header.py | 9 +- mozglue/android/APKOpen.cpp | 4 +- toolkit/xre/CreateAppData.cpp | 154 ++++--------------- toolkit/xre/ProfileReset.cpp | 7 +- toolkit/xre/nsAndroidStartup.cpp | 7 +- toolkit/xre/nsAppRunner.cpp | 74 ++++----- toolkit/xre/nsAppRunner.h | 6 +- toolkit/xre/nsNativeAppSupportWin.cpp | 2 +- xpcom/build/{nsXREAppData.h => XREAppData.h} | 134 ++++++++++++---- xpcom/build/moz.build | 2 +- xpcom/build/nsXULAppAPI.h | 24 +-- xpcom/glue/AppData.cpp | 95 ------------ xpcom/glue/AppData.h | 63 -------- xpcom/glue/XREAppData.cpp | 56 +++++++ xpcom/glue/moz.build | 1 - xpcom/glue/objs.mozbuild | 2 +- xpcom/system/nsIXULAppInfo.idl | 18 +-- 18 files changed, 272 insertions(+), 414 deletions(-) rename xpcom/build/{nsXREAppData.h => XREAppData.h} (67%) delete mode 100644 xpcom/glue/AppData.cpp delete mode 100644 xpcom/glue/AppData.h create mode 100644 xpcom/glue/XREAppData.cpp diff --git a/browser/app/nsBrowserApp.cpp b/browser/app/nsBrowserApp.cpp index 024c01f8c560..95f7e1ac61f8 100644 --- a/browser/app/nsBrowserApp.cpp +++ b/browser/app/nsBrowserApp.cpp @@ -4,7 +4,7 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ #include "nsXULAppAPI.h" -#include "mozilla/AppData.h" +#include "mozilla/XREAppData.h" #include "application.ini.h" #include "nsXPCOMGlue.h" #if defined(XP_WIN) @@ -163,8 +163,7 @@ static bool IsArg(const char* arg, const char* s) } XRE_GetFileFromPathType XRE_GetFileFromPath; -XRE_CreateAppDataType XRE_CreateAppData; -XRE_FreeAppDataType XRE_FreeAppData; +XRE_ParseAppDataType XRE_ParseAppData; XRE_TelemetryAccumulateType XRE_TelemetryAccumulate; XRE_StartupTimelineRecordType XRE_StartupTimelineRecord; XRE_mainType XRE_main; @@ -181,8 +180,7 @@ XRE_LibFuzzerGetFuncsType XRE_LibFuzzerGetFuncs; static const nsDynamicFunctionLoad kXULFuncs[] = { { "XRE_GetFileFromPath", (NSFuncPtr*) &XRE_GetFileFromPath }, - { "XRE_CreateAppData", (NSFuncPtr*) &XRE_CreateAppData }, - { "XRE_FreeAppData", (NSFuncPtr*) &XRE_FreeAppData }, + { "XRE_ParseAppData", (NSFuncPtr*) &XRE_ParseAppData }, { "XRE_TelemetryAccumulate", (NSFuncPtr*) &XRE_TelemetryAccumulate }, { "XRE_StartupTimelineRecord", (NSFuncPtr*) &XRE_StartupTimelineRecord }, { "XRE_main", (NSFuncPtr*) &XRE_main }, @@ -262,8 +260,8 @@ static int do_main(int argc, char* argv[], char* envp[], nsIFile *xreDirectory) } if (appini) { - nsXREAppData *appData; - rv = XRE_CreateAppData(appini, &appData); + XREAppData appData; + rv = XRE_ParseAppData(appini, appData); if (NS_FAILED(rv)) { Output("Couldn't read application.ini"); return 255; @@ -275,14 +273,13 @@ static int do_main(int argc, char* argv[], char* envp[], nsIFile *xreDirectory) appData->flags |= DllBlocklist_CheckStatus() ? NS_XRE_DLL_BLOCKLIST_ENABLED : 0; #endif - // xreDirectory already has a refcount from NS_NewLocalFile - appData->xreDirectory = xreDirectory; - int result = XRE_main(argc, argv, appData, mainFlags); - XRE_FreeAppData(appData); - return result; + appData.xreDirectory = xreDirectory; + appini->GetParent(getter_AddRefs(appData.directory)); + return XRE_main(argc, argv, appData, mainFlags); } - ScopedAppData appData(&sAppData); + XREAppData appData; + appData = sAppData; nsCOMPtr exeFile; rv = mozilla::BinaryPath::GetFile(argv[0], getter_AddRefs(exeFile)); if (NS_FAILED(rv)) { @@ -299,8 +296,7 @@ static int do_main(int argc, char* argv[], char* envp[], nsIFile *xreDirectory) greDir->Clone(getter_AddRefs(appSubdir)); appSubdir->Append(NS_LITERAL_STRING(kDesktopFolder)); - SetStrongPtr(appData.directory, static_cast(appSubdir.get())); - // xreDirectory already has a refcount from NS_NewLocalFile + appData.directory = appSubdir; appData.xreDirectory = xreDirectory; #if defined(HAS_DLL_BLOCKLIST) @@ -325,7 +321,7 @@ static int do_main(int argc, char* argv[], char* envp[], nsIFile *xreDirectory) XRE_LibFuzzerSetMain(argc, argv, libfuzzer_main); #endif - return XRE_main(argc, argv, &appData, mainFlags); + return XRE_main(argc, argv, appData, mainFlags); } static bool diff --git a/build/appini_header.py b/build/appini_header.py index 356a0692e7b7..7f8e4923ddd2 100644 --- a/build/appini_header.py +++ b/build/appini_header.py @@ -3,7 +3,7 @@ # file, You can obtain one at http://mozilla.org/MPL/2.0/. '''Parses a given application.ini file and outputs the corresponding - XULAppData structure as a C++ header file''' + StaticXREAppData structure as a C++ header file''' import ConfigParser import sys @@ -34,10 +34,8 @@ def main(output, file): if not 'Crash Reporter:serverurl' in appdata: appdata['Crash Reporter:serverurl'] = '' - output.write('''#include "nsXREAppData.h" - static const nsXREAppData sAppData = { - sizeof(nsXREAppData), - NULL, // directory + output.write('''#include "mozilla/XREAppData.h" + static const mozilla::StaticXREAppData sAppData = { "%(App:vendor)s", "%(App:name)s", "%(App:remotingname)s", @@ -46,7 +44,6 @@ def main(output, file): "%(App:id)s", NULL, // copyright %(flags)s, - NULL, // xreDirectory "%(Gecko:minversion)s", "%(Gecko:maxversion)s", "%(Crash Reporter:serverurl)s", diff --git a/mozglue/android/APKOpen.cpp b/mozglue/android/APKOpen.cpp index 3022f616df02..c0b2b83c0e11 100644 --- a/mozglue/android/APKOpen.cpp +++ b/mozglue/android/APKOpen.cpp @@ -444,7 +444,7 @@ FreeArgv(char** argv, int argc) delete[](argv); } -typedef void (*GeckoStart_t)(JNIEnv*, char**, int, const nsXREAppData*); +typedef void (*GeckoStart_t)(JNIEnv*, char**, int, const StaticXREAppData&); typedef int GeckoProcessType; extern "C" APKOPEN_EXPORT void MOZ_JNICALL @@ -463,7 +463,7 @@ Java_org_mozilla_gecko_mozglue_GeckoLoader_nativeRun(JNIEnv *jenv, jclass jc, jo } ElfLoader::Singleton.ExpectShutdown(false); - GeckoStart(jenv, argv, argc, &sAppData); + GeckoStart(jenv, argv, argc, sAppData); ElfLoader::Singleton.ExpectShutdown(true); } else { void (*fXRE_SetAndroidChildFds)(int, int); diff --git a/toolkit/xre/CreateAppData.cpp b/toolkit/xre/CreateAppData.cpp index 8c91ddc87444..ccb8e323af7a 100644 --- a/toolkit/xre/CreateAppData.cpp +++ b/toolkit/xre/CreateAppData.cpp @@ -7,55 +7,18 @@ #include "nsINIParser.h" #include "nsIFile.h" #include "nsAutoPtr.h" -#include "mozilla/AppData.h" +#include "mozilla/XREAppData.h" using namespace mozilla; -nsresult -XRE_CreateAppData(nsIFile* aINIFile, nsXREAppData **aAppData) -{ - NS_ENSURE_ARG(aINIFile && aAppData); - - nsAutoPtr 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 appDir; - rv = aINIFile->GetParent(getter_AddRefs(appDir)); - if (NS_FAILED(rv)) - return rv; - - appDir.forget(&data->directory); - } - - *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) +ReadString(nsINIParser &parser, const char* section, + const char* key, XREAppData::CharPtr& result) { - nsresult rv; nsCString str; - - while (reads->section) { - rv = parser.GetString(reads->section, reads->key, str); - if (NS_SUCCEEDED(rv)) { - SetAllocatedString(*reads->buffer, str); - } - - ++reads; + nsresult rv = parser.GetString(section, key, str); + if (NS_SUCCEEDED(rv)) { + result = str.get(); } } @@ -66,30 +29,25 @@ struct ReadFlag { }; static void -ReadFlags(nsINIParser &parser, const ReadFlag *reads, uint32_t *buffer) +ReadFlag(nsINIParser &parser, const char* section, + const char* key, uint32_t flag, uint32_t& result) { - 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; - } + nsresult rv = parser.GetString(section, 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') { + result |= flag; + } + if (buf[0] == '0' || buf[0] == 'f' || buf[0] == 'F') { + result &= ~flag; } - - ++reads; } } nsresult -XRE_ParseAppData(nsIFile* aINIFile, nsXREAppData *aAppData) +XRE_ParseAppData(nsIFile* aINIFile, XREAppData& aAppData) { - NS_ENSURE_ARG(aINIFile && aAppData); + NS_ENSURE_ARG(aINIFile); nsresult rv; @@ -98,68 +56,22 @@ XRE_ParseAppData(nsIFile* aINIFile, nsXREAppData *aAppData) if (NS_FAILED(rv)) return rv; - nsCString str; - - ReadString strings[] = { - { "App", "Vendor", &aAppData->vendor }, - { "App", "Name", &aAppData->name }, - { "App", "RemotingName", &aAppData->remotingName }, - { "App", "Version", &aAppData->version }, - { "App", "BuildID", &aAppData->buildID }, - { "App", "ID", &aAppData->ID }, - { "App", "Copyright", &aAppData->copyright }, - { "App", "Profile", &aAppData->profile }, - { nullptr } - }; - ReadStrings(parser, strings); - - ReadFlag flags[] = { - { "XRE", "EnableProfileMigrator", NS_XRE_ENABLE_PROFILE_MIGRATOR }, - { nullptr } - }; - ReadFlags(parser, flags, &aAppData->flags); - - if (aAppData->size > offsetof(nsXREAppData, xreDirectory)) { - ReadString strings2[] = { - { "Gecko", "MinVersion", &aAppData->minVersion }, - { "Gecko", "MaxVersion", &aAppData->maxVersion }, - { nullptr } - }; - ReadStrings(parser, strings2); - } - - if (aAppData->size > offsetof(nsXREAppData, crashReporterURL)) { - ReadString strings3[] = { - { "Crash Reporter", "ServerURL", &aAppData->crashReporterURL }, - { nullptr } - }; - ReadStrings(parser, strings3); - ReadFlag flags2[] = { - { "Crash Reporter", "Enabled", NS_XRE_ENABLE_CRASH_REPORTER }, - { nullptr } - }; - ReadFlags(parser, flags2, &aAppData->flags); - } - - if (aAppData->size > offsetof(nsXREAppData, UAName)) { - ReadString strings4[] = { - { "App", "UAName", &aAppData->UAName }, - { nullptr } - }; - ReadStrings(parser, strings4); - } + ReadString(parser, "App", "Vendor", aAppData.vendor); + ReadString(parser, "App", "Name", aAppData.name), + ReadString(parser, "App", "RemotingName", aAppData.remotingName); + ReadString(parser, "App", "Version", aAppData.version); + ReadString(parser, "App", "BuildID", aAppData.buildID); + ReadString(parser, "App", "ID", aAppData.ID); + ReadString(parser, "App", "Copyright", aAppData.copyright); + ReadString(parser, "App", "Profile", aAppData.profile); + ReadString(parser, "Gecko", "MinVersion", aAppData.minVersion); + ReadString(parser, "Gecko", "MaxVersion", aAppData.maxVersion); + ReadString(parser, "Crash Reporter", "ServerURL", aAppData.crashReporterURL); + ReadString(parser, "App", "UAName", aAppData.UAName); + ReadFlag(parser, "XRE", "EnableProfileMigrator", + NS_XRE_ENABLE_PROFILE_MIGRATOR, aAppData.flags); + ReadFlag(parser, "Crash Reporter", "Enabled", + NS_XRE_ENABLE_CRASH_REPORTER, aAppData.flags); return NS_OK; } - -void -XRE_FreeAppData(nsXREAppData *aAppData) -{ - if (!aAppData) { - NS_ERROR("Invalid arg"); - return; - } - - ScopedAppData* sad = static_cast(aAppData); - delete sad; -} diff --git a/toolkit/xre/ProfileReset.cpp b/toolkit/xre/ProfileReset.cpp index aef2d7746d53..eec684bab258 100644 --- a/toolkit/xre/ProfileReset.cpp +++ b/toolkit/xre/ProfileReset.cpp @@ -15,14 +15,17 @@ #include "nsDirectoryServiceUtils.h" #include "nsPIDOMWindow.h" #include "nsPrintfCString.h" +#include "nsString.h" #include "nsToolkitCompsCID.h" #include "nsXPCOMCIDInternal.h" -#include "nsXREAppData.h" +#include "mozilla/XREAppData.h" #include "mozilla/Services.h" #include "prtime.h" -extern const nsXREAppData* gAppData; +using namespace mozilla; + +extern const XREAppData* gAppData; static const char kProfileProperties[] = "chrome://mozapps/locale/profile/profileSelection.properties"; diff --git a/toolkit/xre/nsAndroidStartup.cpp b/toolkit/xre/nsAndroidStartup.cpp index cd35b97ea8ce..2800a0695638 100644 --- a/toolkit/xre/nsAndroidStartup.cpp +++ b/toolkit/xre/nsAndroidStartup.cpp @@ -21,8 +21,10 @@ #define LOG(args...) __android_log_print(ANDROID_LOG_INFO, MOZ_APP_NAME, args) +using namespace mozilla; + extern "C" NS_EXPORT void -GeckoStart(JNIEnv* env, char** argv, int argc, const nsXREAppData* appData) +GeckoStart(JNIEnv* env, char** argv, int argc, const StaticXREAppData& aAppData) { mozilla::jni::SetGeckoThreadEnv(env); @@ -40,6 +42,9 @@ GeckoStart(JNIEnv* env, char** argv, int argc, const nsXREAppData* appData) return; } + XREAppData appData; + appData = aAppData; + int result = XRE_main(argc, argv, appData, 0); if (result) diff --git a/toolkit/xre/nsAppRunner.cpp b/toolkit/xre/nsAppRunner.cpp index af5d6d6246cf..51c6bbb23cf2 100644 --- a/toolkit/xre/nsAppRunner.cpp +++ b/toolkit/xre/nsAppRunner.cpp @@ -21,7 +21,7 @@ #include "mozilla/Telemetry.h" #include "nsAppRunner.h" -#include "mozilla/AppData.h" +#include "mozilla/XREAppData.h" #if defined(MOZ_UPDATER) && !defined(MOZ_WIDGET_ANDROID) #include "nsUpdateDriver.h" #endif @@ -1608,7 +1608,7 @@ DumpHelp() #endif #ifdef XP_UNIX printf(" --g-fatal-warnings Make all warnings fatal\n" - "\n%s options\n", gAppData->name); + "\n%s options\n", (const char*) gAppData->name); #endif printf(" -h or --help Print this message.\n" @@ -1621,10 +1621,10 @@ DumpHelp() " --new-instance.\n" " --new-instance Open new instance, not a new window in running instance.\n" " --UILocale Start with resources as UI Locale.\n" - " --safe-mode Disables extensions and themes for this session.\n", gAppData->name); + " --safe-mode Disables extensions and themes for this session.\n", (const char*) gAppData->name); #if defined(XP_WIN) - printf(" --console Start %s with a debugging console.\n", gAppData->name); + printf(" --console Start %s with a debugging console.\n", (const char*) gAppData->name); #endif // this works, but only after the components have registered. so if you drop in a new command line handler, --help @@ -1679,10 +1679,10 @@ static inline void DumpVersion() { if (gAppData->vendor) - printf("%s ", gAppData->vendor); - printf("%s %s", gAppData->name, gAppData->version); + printf("%s ", (const char*) gAppData->vendor); + printf("%s %s", (const char*) gAppData->name, (const char*) gAppData->version); if (gAppData->copyright) - printf(", %s", gAppData->copyright); + printf(", %s", (const char*) gAppData->copyright); printf("\n"); } @@ -2803,7 +2803,7 @@ static void MakeOrSetMinidumpPath(nsIFile* profD) } #endif -const nsXREAppData* gAppData = nullptr; +const XREAppData* gAppData = nullptr; #ifdef MOZ_WIDGET_GTK static void MOZ_gdk_display_close(GdkDisplay *display) @@ -3020,7 +3020,7 @@ public: mAppData = nullptr; } - int XRE_main(int argc, char* argv[], const nsXREAppData* aAppData); + int XRE_main(int argc, char* argv[], const XREAppData& aAppData); int XRE_mainInit(bool* aExitFlag); int XRE_mainStartup(bool* aExitFlag); nsresult XRE_mainRun(); @@ -3037,7 +3037,7 @@ public: #endif UniquePtr mScopedXPCOM; - nsAutoPtr mAppData; + UniquePtr mAppData; nsXREDirProvider mDirProvider; nsAutoCString mProfileName; @@ -3159,7 +3159,7 @@ XREMain::XRE_mainInit(bool* aExitFlag) return 1; } - rv = XRE_ParseAppData(overrideLF, mAppData.get()); + rv = XRE_ParseAppData(overrideLF, *mAppData); if (NS_FAILED(rv)) { Output(true, "Couldn't read override.ini"); return 1; @@ -3198,33 +3198,31 @@ XREMain::XRE_mainInit(bool* aExitFlag) greDir->AppendNative(NS_LITERAL_CSTRING("Resources")); #endif - greDir.forget(&mAppData->xreDirectory); + mAppData->xreDirectory = greDir; } if (!mAppData->directory) { - NS_IF_ADDREF(mAppData->directory = mAppData->xreDirectory); + mAppData->directory = mAppData->xreDirectory; } - if (mAppData->size > offsetof(nsXREAppData, minVersion)) { - if (!mAppData->minVersion) { - Output(true, "Error: Gecko:MinVersion not specified in application.ini\n"); - return 1; - } + if (!mAppData->minVersion) { + Output(true, "Error: Gecko:MinVersion not specified in application.ini\n"); + return 1; + } - if (!mAppData->maxVersion) { - // If no maxVersion is specified, we assume the app is only compatible - // with the initial preview release. Do not increment this number ever! - SetAllocatedString(mAppData->maxVersion, "1.*"); - } + if (!mAppData->maxVersion) { + // If no maxVersion is specified, we assume the app is only compatible + // with the initial preview release. Do not increment this number ever! + mAppData->maxVersion = "1.*"; + } - if (mozilla::Version(mAppData->minVersion) > gToolkitVersion || - mozilla::Version(mAppData->maxVersion) < gToolkitVersion) { - Output(true, "Error: Platform version '%s' is not compatible with\n" - "minVersion >= %s\nmaxVersion <= %s\n", - gToolkitVersion, - mAppData->minVersion, mAppData->maxVersion); - return 1; - } + if (mozilla::Version(mAppData->minVersion) > gToolkitVersion || + mozilla::Version(mAppData->maxVersion) < gToolkitVersion) { + Output(true, "Error: Platform version '%s' is not compatible with\n" + "minVersion >= %s\nmaxVersion <= %s\n", + (const char*) gToolkitVersion, (const char*) mAppData->minVersion, + (const char*) mAppData->maxVersion); + return 1; } rv = mDirProvider.Initialize(mAppData->directory, mAppData->xreDirectory); @@ -4532,7 +4530,7 @@ XRE_CreateStatsObject() * .app/Contents/Resources. */ int -XREMain::XRE_main(int argc, char* argv[], const nsXREAppData* aAppData) +XREMain::XRE_main(int argc, char* argv[], const XREAppData& aAppData) { ScopedLogging log; @@ -4561,16 +4559,12 @@ XREMain::XRE_main(int argc, char* argv[], const nsXREAppData* aAppData) gArgc = argc; gArgv = argv; - NS_ENSURE_TRUE(aAppData, 2); - - mAppData = new ScopedAppData(aAppData); - if (!mAppData) - return 1; + mAppData = MakeUnique(aAppData); if (!mAppData->remotingName) { - SetAllocatedString(mAppData->remotingName, mAppData->name); + mAppData->remotingName = mAppData->name; } // used throughout this file - gAppData = mAppData; + gAppData = mAppData.get(); nsCOMPtr binFile; rv = XRE_GetBinaryPath(argv[0], getter_AddRefs(binFile)); @@ -4702,7 +4696,7 @@ XRE_StopLateWriteChecks(void) { } int -XRE_main(int argc, char* argv[], const nsXREAppData* aAppData, uint32_t aFlags) +XRE_main(int argc, char* argv[], const XREAppData& aAppData, uint32_t aFlags) { XREMain main; diff --git a/toolkit/xre/nsAppRunner.h b/toolkit/xre/nsAppRunner.h index b8d955319042..470ecd8a37ba 100644 --- a/toolkit/xre/nsAppRunner.h +++ b/toolkit/xre/nsAppRunner.h @@ -42,10 +42,8 @@ class nsString; extern nsXREDirProvider* gDirServiceProvider; -// NOTE: gAppData will be null in embedded contexts. The "size" parameter -// will be the size of the original structure passed to XRE_main, but the -// structure will have all of the members available. -extern const nsXREAppData* gAppData; +// NOTE: gAppData will be null in embedded contexts. +extern const mozilla::XREAppData* gAppData; extern bool gSafeMode; extern int gArgc; diff --git a/toolkit/xre/nsNativeAppSupportWin.cpp b/toolkit/xre/nsNativeAppSupportWin.cpp index 37cad0783a8e..a92e7e4bec15 100644 --- a/toolkit/xre/nsNativeAppSupportWin.cpp +++ b/toolkit/xre/nsNativeAppSupportWin.cpp @@ -685,7 +685,7 @@ nsNativeAppSupportWin::StartDDE() { NS_ERROR_FAILURE ); // Allocate DDE strings. - NS_ENSURE_TRUE( ( mApplication = DdeCreateStringHandleA( mInstance, (char*) gAppData->name, CP_WINANSI ) ) && InitTopicStrings(), + NS_ENSURE_TRUE( ( mApplication = DdeCreateStringHandleA( mInstance, (char*)(const char*) gAppData->name, CP_WINANSI ) ) && InitTopicStrings(), NS_ERROR_FAILURE ); // Next step is to register a DDE service. diff --git a/xpcom/build/nsXREAppData.h b/xpcom/build/XREAppData.h similarity index 67% rename from xpcom/build/nsXREAppData.h rename to xpcom/build/XREAppData.h index fbc7adb8fcd6..c6e6b96a2fa8 100644 --- a/xpcom/build/nsXREAppData.h +++ b/xpcom/build/XREAppData.h @@ -9,8 +9,9 @@ #include #include "mozilla/Attributes.h" - -class nsIFile; +#include "nsCOMPtr.h" +#include "nsCRTGlue.h" +#include "nsIFile.h" #if defined(XP_WIN) && defined(MOZ_SANDBOX) namespace sandbox { @@ -18,60 +19,108 @@ class BrokerServices; } #endif +namespace mozilla { + +struct StaticXREAppData; + /** * Application-specific data needed to start the apprunner. - * - * @note When this structure is allocated and manipulated by XRE_CreateAppData, - * string fields will be allocated with moz_xmalloc, and interface pointers - * are strong references. */ -struct nsXREAppData +class XREAppData { - /** - * This should be set to sizeof(nsXREAppData). This structure may be - * extended in future releases, and this ensures that binary compatibility - * is maintained. - */ - uint32_t size; +public: + XREAppData() { } + ~XREAppData() { } + XREAppData(const XREAppData& aOther) + { + *this = aOther; + } + + XREAppData& operator=(const StaticXREAppData& aOther); + XREAppData& operator=(const XREAppData& aOther); + XREAppData& operator=(XREAppData&& aOther) = default; + + struct NSFreePolicy + { + void operator()(const void* ptr) { + NS_Free(const_cast(ptr)); + } + }; + + // Lots of code reads these fields directly like a struct, so rather + // than using UniquePtr directly, use an auto-converting wrapper. + class CharPtr + { + public: + explicit CharPtr() = default; + explicit CharPtr(const char* v) + { + *this = v; + } + CharPtr(CharPtr&&) = default; + ~CharPtr() = default; + + CharPtr& operator=(const char* v) + { + if (v) { + mValue.reset(NS_strdup(v)); + } else { + mValue = nullptr; + } + return *this; + } + CharPtr& operator=(const CharPtr& v) + { + *this = (const char*) v; + return *this; + } + + operator const char*() const { + return mValue.get(); + } + + private: + UniquePtr mValue; + }; /** * The directory of the application to be run. May be null if the * xulrunner and the app are installed into the same directory. */ - nsIFile* MOZ_NON_OWNING_REF directory; + nsCOMPtr directory; /** * The name of the application vendor. This must be ASCII, and is normally * mixed-case, e.g. "Mozilla". Optional (may be null), but highly * recommended. Must not be the empty string. */ - const char* vendor; + CharPtr vendor; /** * The name of the application. This must be ASCII, and is normally * mixed-case, e.g. "Firefox". Required (must not be null or an empty * string). */ - const char* name; + CharPtr name; /** * The internal name of the application for remoting purposes. When left * unspecified, "name" is used instead. This must be ASCII, and is normally * lowercase, e.g. "firefox". Optional (may be null but not an empty string). */ - const char* remotingName; + CharPtr remotingName; /** * The major version, e.g. "0.8.0+". Optional (may be null), but * required for advanced application features such as the extension * manager and update service. Must not be the empty string. */ - const char* version; + CharPtr version; /** * The application's build identifier, e.g. "2004051604" */ - const char* buildID; + CharPtr buildID; /** * The application's UUID. Used by the extension manager to determine @@ -83,35 +132,35 @@ struct nsXREAppData * a more readable form is encouraged: "appname@vendor.tld". Only * the following characters are allowed: a-z A-Z 0-9 - . @ _ { } * */ - const char* ID; + CharPtr ID; /** * The copyright information to print for the -h commandline flag, * e.g. "Copyright (c) 2003 mozilla.org". */ - const char* copyright; + CharPtr copyright; /** * Combination of NS_XRE_ prefixed flags (defined below). */ - uint32_t flags; + uint32_t flags = 0; /** * The location of the XRE. XRE_main may not be able to figure this out * programatically. */ - nsIFile* MOZ_NON_OWNING_REF xreDirectory; + nsCOMPtr xreDirectory; /** * The minimum/maximum compatible XRE version. */ - const char* minVersion; - const char* maxVersion; + CharPtr minVersion; + CharPtr maxVersion; /** * The server URL to send crash reports to. */ - const char* crashReporterURL; + CharPtr crashReporterURL; /** * The profile directory that will be used. Optional (may be null). Must not @@ -128,18 +177,18 @@ struct nsXREAppData * * UAppData = $HOME/$profile */ - const char* profile; + CharPtr profile; /** * The application name to use in the User Agent string. */ - const char* UAName; + CharPtr UAName; #if defined(XP_WIN) && defined(MOZ_SANDBOX) /** * Chromium sandbox BrokerServices. */ - sandbox::BrokerServices* sandboxBrokerServices; + sandbox::BrokerServices* sandboxBrokerServices = nullptr; #endif }; @@ -161,4 +210,29 @@ struct nsXREAppData */ #define NS_XRE_ENABLE_CRASH_REPORTER (1 << 3) -#endif // nsXREAppData_h +/** + * A static version of the XRE app data is compiled into the application + * so that it is not necessary to read application.ini at startup. + * + * This structure is initialized into and matches nsXREAppData + */ +struct StaticXREAppData +{ + const char* vendor; + const char* name; + const char* remotingName; + const char* version; + const char* buildID; + const char* ID; + const char* copyright; + uint32_t flags; + const char* minVersion; + const char* maxVersion; + const char* crashReporterURL; + const char* profile; + const char* UAName; +}; + +} // namespace mozilla + +#endif // XREAppData_h diff --git a/xpcom/build/moz.build b/xpcom/build/moz.build index 68bd001a2e0a..b9c0cd1c2793 100644 --- a/xpcom/build/moz.build +++ b/xpcom/build/moz.build @@ -8,7 +8,6 @@ EXPORTS += [ 'nsXPCOM.h', 'nsXPCOMCID.h', 'nsXPCOMCIDInternal.h', - 'nsXREAppData.h', 'nsXULAppAPI.h', 'XREChildData.h', 'xrecore.h', @@ -24,6 +23,7 @@ EXPORTS.mozilla += [ 'ServiceList.h', 'Services.h', 'XPCOM.h', + 'XREAppData.h', ] if CONFIG['OS_ARCH'] == 'WINNT': diff --git a/xpcom/build/nsXULAppAPI.h b/xpcom/build/nsXULAppAPI.h index 758174e161d4..26475dc08dad 100644 --- a/xpcom/build/nsXULAppAPI.h +++ b/xpcom/build/nsXULAppAPI.h @@ -12,7 +12,7 @@ #include "nsXPCOM.h" #include "nsISupports.h" #include "mozilla/Logging.h" -#include "nsXREAppData.h" +#include "mozilla/XREAppData.h" #include "js/TypeDecls.h" #include "mozilla/ArrayUtils.h" @@ -204,7 +204,7 @@ * XPCOMGlueStartup() should be called before this method. */ XRE_API(int, - XRE_main, (int argc, char* argv[], const nsXREAppData* aAppData, + XRE_main, (int argc, char* argv[], const mozilla::XREAppData& aAppData, uint32_t aFlags)) /** @@ -361,18 +361,6 @@ XRE_API(void, XRE_API(void, XRE_TermEmbedding, ()) -/** - * Create a new nsXREAppData structure from an application.ini file. - * - * @param aINIFile The application.ini file to parse. - * @param aAppData A newly-allocated nsXREAppData structure. The caller is - * responsible for freeing this structure using - * XRE_FreeAppData. - */ -XRE_API(nsresult, - XRE_CreateAppData, (nsIFile* aINIFile, - nsXREAppData** aAppData)) - /** * Parse an INI file (application.ini or override.ini) into an existing * nsXREAppData structure. @@ -382,13 +370,7 @@ XRE_API(nsresult, */ XRE_API(nsresult, XRE_ParseAppData, (nsIFile* aINIFile, - nsXREAppData* aAppData)) - -/** - * Free a nsXREAppData structure that was allocated with XRE_CreateAppData. - */ -XRE_API(void, - XRE_FreeAppData, (nsXREAppData* aAppData)) + mozilla::XREAppData& aAppData)) enum GeckoProcessType { diff --git a/xpcom/glue/AppData.cpp b/xpcom/glue/AppData.cpp deleted file mode 100644 index 845267e60d66..000000000000 --- a/xpcom/glue/AppData.cpp +++ /dev/null @@ -1,95 +0,0 @@ -/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ -/* vim: set ts=8 sts=2 et sw=2 tw=80: */ -/* 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*& aStr, const char* aNewValue) -{ - NS_Free(const_cast(aStr)); - if (aNewValue) { - aStr = NS_strdup(aNewValue); - } else { - aStr = nullptr; - } -} - -void -SetAllocatedString(const char*& aStr, const nsACString& aNewValue) -{ - NS_Free(const_cast(aStr)); - if (aNewValue.IsEmpty()) { - aStr = nullptr; - } else { - aStr = ToNewCString(aNewValue); - } -} - -ScopedAppData::ScopedAppData(const nsXREAppData* aAppData) -{ - Zero(); - - this->size = aAppData->size; - - SetAllocatedString(this->vendor, aAppData->vendor); - SetAllocatedString(this->name, aAppData->name); - SetAllocatedString(this->remotingName, aAppData->remotingName); - 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); - } - -#if defined(XP_WIN) && defined(MOZ_SANDBOX) - sandboxBrokerServices = aAppData->sandboxBrokerServices; -#endif -} - -ScopedAppData::~ScopedAppData() -{ - SetAllocatedString(this->vendor, nullptr); - SetAllocatedString(this->name, nullptr); - SetAllocatedString(this->remotingName, nullptr); - SetAllocatedString(this->version, nullptr); - SetAllocatedString(this->buildID, nullptr); - SetAllocatedString(this->ID, nullptr); - SetAllocatedString(this->copyright, nullptr); - SetAllocatedString(this->profile, nullptr); - - NS_IF_RELEASE(this->directory); - - SetStrongPtr(this->xreDirectory, (nsIFile*)nullptr); - SetAllocatedString(this->minVersion, nullptr); - SetAllocatedString(this->maxVersion, nullptr); - - SetAllocatedString(this->crashReporterURL, nullptr); - SetAllocatedString(this->UAName, nullptr); -} - -} // namespace mozilla diff --git a/xpcom/glue/AppData.h b/xpcom/glue/AppData.h deleted file mode 100644 index 0134df32ca14..000000000000 --- a/xpcom/glue/AppData.h +++ /dev/null @@ -1,63 +0,0 @@ -/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ -/* vim: set ts=8 sts=2 et sw=2 tw=80: */ -/* 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" -#include "nsISupportsUtils.h" - -namespace mozilla { - -// Like nsXREAppData, but releases all strong refs/allocated memory -// in the destructor. -class ScopedAppData : public nsXREAppData -{ -public: - ScopedAppData() - { - Zero(); - this->size = sizeof(*this); - } - - explicit ScopedAppData(const nsXREAppData* aAppData); - - void Zero() { memset(this, 0, sizeof(*this)); } - - ~ScopedAppData(); -}; - -/** - * Given |aStr| is holding a string allocated with NS_Alloc, or null: - * replace the value in |aStr| with a new value. - * - * @param aNewValue Null is permitted. The string is cloned with NS_strdup. - */ -void SetAllocatedString(const char*& aStr, const char* aNewValue); - -/** - * Given "str" is holding a string allocated with NS_Alloc, or null: - * replace the value in "str" with a new value. - * - * @param aNewValue If |aNewValue| is the empty string, |aStr| will be set - * to null. - */ -void SetAllocatedString(const char*& aStr, const nsACString& aNewValue); - -template -void -SetStrongPtr(T*& aPtr, T* aNewValue) -{ - NS_IF_RELEASE(aPtr); - aPtr = aNewValue; - NS_IF_ADDREF(aPtr); -} - -} // namespace mozilla - -#endif diff --git a/xpcom/glue/XREAppData.cpp b/xpcom/glue/XREAppData.cpp new file mode 100644 index 000000000000..8aef2e16c043 --- /dev/null +++ b/xpcom/glue/XREAppData.cpp @@ -0,0 +1,56 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ +/* 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/XREAppData.h" +#include "nsCRTGlue.h" + +namespace mozilla { + +XREAppData& +XREAppData::operator=(const StaticXREAppData& aOther) +{ + vendor = aOther.vendor; + name = aOther.name; + remotingName = aOther.remotingName; + version = aOther.version; + buildID = aOther.buildID; + ID = aOther.ID; + copyright = aOther.copyright; + flags = aOther.flags; + minVersion = aOther.minVersion; + maxVersion = aOther.maxVersion; + crashReporterURL = aOther.crashReporterURL; + profile = aOther.profile; + UAName = aOther.UAName; + + return *this; +} + +XREAppData& +XREAppData::operator=(const XREAppData& aOther) +{ + directory = aOther.directory; + vendor = aOther.vendor; + name = aOther.name; + remotingName = aOther.remotingName; + version = aOther.version; + buildID = aOther.buildID; + ID = aOther.ID; + copyright = aOther.copyright; + flags = aOther.flags; + xreDirectory = aOther.xreDirectory; + minVersion = aOther.minVersion; + maxVersion = aOther.maxVersion; + crashReporterURL = aOther.crashReporterURL; + profile = aOther.profile; + UAName = aOther.UAName; +#if defined(XP_WIN) && defined(MOZ_SANDBOX) + sandboxBrokerServices = aOther.sandboxBrokerServices; +#endif + return *this; +} + +} // namespace mozilla diff --git a/xpcom/glue/moz.build b/xpcom/glue/moz.build index 95c18b273ed1..e1a22dbe6c44 100644 --- a/xpcom/glue/moz.build +++ b/xpcom/glue/moz.build @@ -67,7 +67,6 @@ EXPORTS += [ ] EXPORTS.mozilla += [ - 'AppData.h', 'AutoRestore.h', 'BlockingResourceBase.h', 'CondVar.h', diff --git a/xpcom/glue/objs.mozbuild b/xpcom/glue/objs.mozbuild index 8161e1ebce92..64fc2851b230 100644 --- a/xpcom/glue/objs.mozbuild +++ b/xpcom/glue/objs.mozbuild @@ -5,7 +5,6 @@ # file, You can obtain one at http://mozilla.org/MPL/2.0/. xpcom_glue_src_lcppsrcs = [ - 'AppData.cpp', 'FileUtils.cpp', 'nsArrayEnumerator.cpp', 'nsArrayUtils.cpp', @@ -30,6 +29,7 @@ xpcom_glue_src_lcppsrcs = [ 'nsVersionComparator.cpp', 'nsWeakReference.cpp', 'PLDHashTable.cpp', + 'XREAppData.cpp', ] xpcom_glue_src_cppsrcs = [ diff --git a/xpcom/system/nsIXULAppInfo.idl b/xpcom/system/nsIXULAppInfo.idl index 1ea9208a3491..43debf7c1692 100644 --- a/xpcom/system/nsIXULAppInfo.idl +++ b/xpcom/system/nsIXULAppInfo.idl @@ -13,19 +13,19 @@ interface nsIXULAppInfo : nsIPlatformInfo { /** - * @see nsXREAppData.vendor - * @returns an empty string if nsXREAppData.vendor is not set. + * @see XREAppData.vendor + * @returns an empty string if XREAppData.vendor is not set. */ readonly attribute ACString vendor; /** - * @see nsXREAppData.name + * @see XREAppData.name */ readonly attribute ACString name; /** - * @see nsXREAppData.ID - * @returns an empty string if nsXREAppData.ID is not set. + * @see XREAppData.ID + * @returns an empty string if XREAppData.ID is not set. */ readonly attribute ACString ID; @@ -33,8 +33,8 @@ interface nsIXULAppInfo : nsIPlatformInfo * The version of the XUL application. It is different than the * version of the XULRunner platform. Be careful about which one you want. * - * @see nsXREAppData.version - * @returns an empty string if nsXREAppData.version is not set. + * @see XREAppData.version + * @returns an empty string if XREAppData.version is not set. */ readonly attribute ACString version; @@ -46,8 +46,8 @@ interface nsIXULAppInfo : nsIPlatformInfo readonly attribute ACString appBuildID; /** - * @see nsXREAppData.UAName - * @returns an empty string if nsXREAppData.UAName is not set. + * @see XREAppData.UAName + * @returns an empty string if XREAppData.UAName is not set. */ readonly attribute ACString UAName; };