1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/xre/CreateAppData.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,165 @@ 1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#include "nsXULAppAPI.h" 1.10 +#include "nsINIParser.h" 1.11 +#include "nsIFile.h" 1.12 +#include "nsAutoPtr.h" 1.13 +#include "mozilla/AppData.h" 1.14 + 1.15 +using namespace mozilla; 1.16 + 1.17 +nsresult 1.18 +XRE_CreateAppData(nsIFile* aINIFile, nsXREAppData **aAppData) 1.19 +{ 1.20 + NS_ENSURE_ARG(aINIFile && aAppData); 1.21 + 1.22 + nsAutoPtr<ScopedAppData> data(new ScopedAppData()); 1.23 + if (!data) 1.24 + return NS_ERROR_OUT_OF_MEMORY; 1.25 + 1.26 + nsresult rv = XRE_ParseAppData(aINIFile, data); 1.27 + if (NS_FAILED(rv)) 1.28 + return rv; 1.29 + 1.30 + if (!data->directory) { 1.31 + nsCOMPtr<nsIFile> appDir; 1.32 + rv = aINIFile->GetParent(getter_AddRefs(appDir)); 1.33 + if (NS_FAILED(rv)) 1.34 + return rv; 1.35 + 1.36 + appDir.forget(&data->directory); 1.37 + } 1.38 + 1.39 + *aAppData = data.forget(); 1.40 + return NS_OK; 1.41 +} 1.42 + 1.43 +struct ReadString { 1.44 + const char *section; 1.45 + const char *key; 1.46 + const char **buffer; 1.47 +}; 1.48 + 1.49 +static void 1.50 +ReadStrings(nsINIParser &parser, const ReadString *reads) 1.51 +{ 1.52 + nsresult rv; 1.53 + nsCString str; 1.54 + 1.55 + while (reads->section) { 1.56 + rv = parser.GetString(reads->section, reads->key, str); 1.57 + if (NS_SUCCEEDED(rv)) { 1.58 + SetAllocatedString(*reads->buffer, str); 1.59 + } 1.60 + 1.61 + ++reads; 1.62 + } 1.63 +} 1.64 + 1.65 +struct ReadFlag { 1.66 + const char *section; 1.67 + const char *key; 1.68 + uint32_t flag; 1.69 +}; 1.70 + 1.71 +static void 1.72 +ReadFlags(nsINIParser &parser, const ReadFlag *reads, uint32_t *buffer) 1.73 +{ 1.74 + nsresult rv; 1.75 + char buf[6]; // large enough to hold "false" 1.76 + 1.77 + while (reads->section) { 1.78 + rv = parser.GetString(reads->section, reads->key, buf, sizeof(buf)); 1.79 + if (NS_SUCCEEDED(rv) || rv == NS_ERROR_LOSS_OF_SIGNIFICANT_DATA) { 1.80 + if (buf[0] == '1' || buf[0] == 't' || buf[0] == 'T') { 1.81 + *buffer |= reads->flag; 1.82 + } 1.83 + if (buf[0] == '0' || buf[0] == 'f' || buf[0] == 'F') { 1.84 + *buffer &= ~reads->flag; 1.85 + } 1.86 + } 1.87 + 1.88 + ++reads; 1.89 + } 1.90 +} 1.91 + 1.92 +nsresult 1.93 +XRE_ParseAppData(nsIFile* aINIFile, nsXREAppData *aAppData) 1.94 +{ 1.95 + NS_ENSURE_ARG(aINIFile && aAppData); 1.96 + 1.97 + nsresult rv; 1.98 + 1.99 + nsINIParser parser; 1.100 + rv = parser.Init(aINIFile); 1.101 + if (NS_FAILED(rv)) 1.102 + return rv; 1.103 + 1.104 + nsCString str; 1.105 + 1.106 + ReadString strings[] = { 1.107 + { "App", "Vendor", &aAppData->vendor }, 1.108 + { "App", "Name", &aAppData->name }, 1.109 + { "App", "Version", &aAppData->version }, 1.110 + { "App", "BuildID", &aAppData->buildID }, 1.111 + { "App", "ID", &aAppData->ID }, 1.112 + { "App", "Copyright", &aAppData->copyright }, 1.113 + { "App", "Profile", &aAppData->profile }, 1.114 + { nullptr } 1.115 + }; 1.116 + ReadStrings(parser, strings); 1.117 + 1.118 + ReadFlag flags[] = { 1.119 + { "XRE", "EnableProfileMigrator", NS_XRE_ENABLE_PROFILE_MIGRATOR }, 1.120 + { "XRE", "EnableExtensionManager", NS_XRE_ENABLE_EXTENSION_MANAGER }, 1.121 + { nullptr } 1.122 + }; 1.123 + ReadFlags(parser, flags, &aAppData->flags); 1.124 + 1.125 + if (aAppData->size > offsetof(nsXREAppData, xreDirectory)) { 1.126 + ReadString strings2[] = { 1.127 + { "Gecko", "MinVersion", &aAppData->minVersion }, 1.128 + { "Gecko", "MaxVersion", &aAppData->maxVersion }, 1.129 + { nullptr } 1.130 + }; 1.131 + ReadStrings(parser, strings2); 1.132 + } 1.133 + 1.134 + if (aAppData->size > offsetof(nsXREAppData, crashReporterURL)) { 1.135 + ReadString strings3[] = { 1.136 + { "Crash Reporter", "ServerURL", &aAppData->crashReporterURL }, 1.137 + { nullptr } 1.138 + }; 1.139 + ReadStrings(parser, strings3); 1.140 + ReadFlag flags2[] = { 1.141 + { "Crash Reporter", "Enabled", NS_XRE_ENABLE_CRASH_REPORTER }, 1.142 + { nullptr } 1.143 + }; 1.144 + ReadFlags(parser, flags2, &aAppData->flags); 1.145 + } 1.146 + 1.147 + if (aAppData->size > offsetof(nsXREAppData, UAName)) { 1.148 + ReadString strings4[] = { 1.149 + { "App", "UAName", &aAppData->UAName }, 1.150 + { nullptr } 1.151 + }; 1.152 + ReadStrings(parser, strings4); 1.153 + } 1.154 + 1.155 + return NS_OK; 1.156 +} 1.157 + 1.158 +void 1.159 +XRE_FreeAppData(nsXREAppData *aAppData) 1.160 +{ 1.161 + if (!aAppData) { 1.162 + NS_ERROR("Invalid arg"); 1.163 + return; 1.164 + } 1.165 + 1.166 + ScopedAppData* sad = static_cast<ScopedAppData*>(aAppData); 1.167 + delete sad; 1.168 +}