michael@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "nsXULAppAPI.h" michael@0: #include "nsINIParser.h" michael@0: #include "nsIFile.h" michael@0: #include "nsAutoPtr.h" michael@0: #include "mozilla/AppData.h" michael@0: michael@0: using namespace mozilla; michael@0: michael@0: nsresult michael@0: XRE_CreateAppData(nsIFile* aINIFile, nsXREAppData **aAppData) michael@0: { michael@0: NS_ENSURE_ARG(aINIFile && aAppData); michael@0: michael@0: nsAutoPtr data(new ScopedAppData()); michael@0: if (!data) michael@0: return NS_ERROR_OUT_OF_MEMORY; michael@0: michael@0: nsresult rv = XRE_ParseAppData(aINIFile, data); michael@0: if (NS_FAILED(rv)) michael@0: return rv; michael@0: michael@0: if (!data->directory) { michael@0: nsCOMPtr appDir; michael@0: rv = aINIFile->GetParent(getter_AddRefs(appDir)); michael@0: if (NS_FAILED(rv)) michael@0: return rv; michael@0: michael@0: appDir.forget(&data->directory); michael@0: } michael@0: michael@0: *aAppData = data.forget(); michael@0: return NS_OK; michael@0: } michael@0: michael@0: struct ReadString { michael@0: const char *section; michael@0: const char *key; michael@0: const char **buffer; michael@0: }; michael@0: michael@0: static void michael@0: ReadStrings(nsINIParser &parser, const ReadString *reads) michael@0: { michael@0: nsresult rv; michael@0: nsCString str; michael@0: michael@0: while (reads->section) { michael@0: rv = parser.GetString(reads->section, reads->key, str); michael@0: if (NS_SUCCEEDED(rv)) { michael@0: SetAllocatedString(*reads->buffer, str); michael@0: } michael@0: michael@0: ++reads; michael@0: } michael@0: } michael@0: michael@0: struct ReadFlag { michael@0: const char *section; michael@0: const char *key; michael@0: uint32_t flag; michael@0: }; michael@0: michael@0: static void michael@0: ReadFlags(nsINIParser &parser, const ReadFlag *reads, uint32_t *buffer) michael@0: { michael@0: nsresult rv; michael@0: char buf[6]; // large enough to hold "false" michael@0: michael@0: while (reads->section) { michael@0: rv = parser.GetString(reads->section, reads->key, buf, sizeof(buf)); michael@0: if (NS_SUCCEEDED(rv) || rv == NS_ERROR_LOSS_OF_SIGNIFICANT_DATA) { michael@0: if (buf[0] == '1' || buf[0] == 't' || buf[0] == 'T') { michael@0: *buffer |= reads->flag; michael@0: } michael@0: if (buf[0] == '0' || buf[0] == 'f' || buf[0] == 'F') { michael@0: *buffer &= ~reads->flag; michael@0: } michael@0: } michael@0: michael@0: ++reads; michael@0: } michael@0: } michael@0: michael@0: nsresult michael@0: XRE_ParseAppData(nsIFile* aINIFile, nsXREAppData *aAppData) michael@0: { michael@0: NS_ENSURE_ARG(aINIFile && aAppData); michael@0: michael@0: nsresult rv; michael@0: michael@0: nsINIParser parser; michael@0: rv = parser.Init(aINIFile); michael@0: if (NS_FAILED(rv)) michael@0: return rv; michael@0: michael@0: nsCString str; michael@0: michael@0: ReadString strings[] = { michael@0: { "App", "Vendor", &aAppData->vendor }, michael@0: { "App", "Name", &aAppData->name }, michael@0: { "App", "Version", &aAppData->version }, michael@0: { "App", "BuildID", &aAppData->buildID }, michael@0: { "App", "ID", &aAppData->ID }, michael@0: { "App", "Copyright", &aAppData->copyright }, michael@0: { "App", "Profile", &aAppData->profile }, michael@0: { nullptr } michael@0: }; michael@0: ReadStrings(parser, strings); michael@0: michael@0: ReadFlag flags[] = { michael@0: { "XRE", "EnableProfileMigrator", NS_XRE_ENABLE_PROFILE_MIGRATOR }, michael@0: { "XRE", "EnableExtensionManager", NS_XRE_ENABLE_EXTENSION_MANAGER }, michael@0: { nullptr } michael@0: }; michael@0: ReadFlags(parser, flags, &aAppData->flags); michael@0: michael@0: if (aAppData->size > offsetof(nsXREAppData, xreDirectory)) { michael@0: ReadString strings2[] = { michael@0: { "Gecko", "MinVersion", &aAppData->minVersion }, michael@0: { "Gecko", "MaxVersion", &aAppData->maxVersion }, michael@0: { nullptr } michael@0: }; michael@0: ReadStrings(parser, strings2); michael@0: } michael@0: michael@0: if (aAppData->size > offsetof(nsXREAppData, crashReporterURL)) { michael@0: ReadString strings3[] = { michael@0: { "Crash Reporter", "ServerURL", &aAppData->crashReporterURL }, michael@0: { nullptr } michael@0: }; michael@0: ReadStrings(parser, strings3); michael@0: ReadFlag flags2[] = { michael@0: { "Crash Reporter", "Enabled", NS_XRE_ENABLE_CRASH_REPORTER }, michael@0: { nullptr } michael@0: }; michael@0: ReadFlags(parser, flags2, &aAppData->flags); michael@0: } michael@0: michael@0: if (aAppData->size > offsetof(nsXREAppData, UAName)) { michael@0: ReadString strings4[] = { michael@0: { "App", "UAName", &aAppData->UAName }, michael@0: { nullptr } michael@0: }; michael@0: ReadStrings(parser, strings4); michael@0: } michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: void michael@0: XRE_FreeAppData(nsXREAppData *aAppData) michael@0: { michael@0: if (!aAppData) { michael@0: NS_ERROR("Invalid arg"); michael@0: return; michael@0: } michael@0: michael@0: ScopedAppData* sad = static_cast(aAppData); michael@0: delete sad; michael@0: }