Merge mozilla-central to tracemonkey.
[mozilla-central.git] / toolkit / xre / nsAppData.cpp
blobe7a5f6b55cbe9c8dd75e6401b0704505d10d757c
1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
12 * License.
14 * The Original Code is mozilla.org code.
16 * The Initial Developer of the Original Code is
17 * the Mozilla Foundation <http://www.mozilla.org/>
18 * Portions created by the Initial Developer are Copyright (C) 2006
19 * the Initial Developer. All Rights Reserved.
21 * Contributor(s):
22 * Benjamin Smedberg <benjamin@smedberg.us> (Original Author)
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
38 #include "nsXULAppAPI.h"
39 #include "nsINIParser.h"
40 #include "nsILocalFile.h"
41 #include "nsAppRunner.h"
42 #include "nsCRTGlue.h"
43 #include "nsAutoPtr.h"
45 void
46 SetAllocatedString(const char *&str, const char *newvalue)
48 NS_Free(const_cast<char*>(str));
49 if (newvalue) {
50 str = NS_strdup(newvalue);
52 else {
53 str = nsnull;
57 void
58 SetAllocatedString(const char *&str, const nsACString &newvalue)
60 NS_Free(const_cast<char*>(str));
61 if (newvalue.IsEmpty()) {
62 str = nsnull;
64 else {
65 str = ToNewCString(newvalue);
69 ScopedAppData::ScopedAppData(const nsXREAppData* aAppData)
71 Zero();
73 this->size = aAppData->size;
75 SetAllocatedString(this->vendor, aAppData->vendor);
76 SetAllocatedString(this->name, aAppData->name);
77 SetAllocatedString(this->version, aAppData->version);
78 SetAllocatedString(this->buildID, aAppData->buildID);
79 SetAllocatedString(this->ID, aAppData->ID);
80 SetAllocatedString(this->copyright, aAppData->copyright);
81 SetAllocatedString(this->profile, aAppData->profile);
82 SetStrongPtr(this->directory, aAppData->directory);
83 this->flags = aAppData->flags;
85 if (aAppData->size > offsetof(nsXREAppData, xreDirectory)) {
86 SetStrongPtr(this->xreDirectory, aAppData->xreDirectory);
87 SetAllocatedString(this->minVersion, aAppData->minVersion);
88 SetAllocatedString(this->maxVersion, aAppData->maxVersion);
91 if (aAppData->size > offsetof(nsXREAppData, crashReporterURL)) {
92 SetAllocatedString(this->crashReporterURL, aAppData->crashReporterURL);
96 ScopedAppData::~ScopedAppData()
98 SetAllocatedString(this->vendor, nsnull);
99 SetAllocatedString(this->name, nsnull);
100 SetAllocatedString(this->version, nsnull);
101 SetAllocatedString(this->buildID, nsnull);
102 SetAllocatedString(this->ID, nsnull);
103 SetAllocatedString(this->copyright, nsnull);
104 SetAllocatedString(this->profile, nsnull);
106 NS_IF_RELEASE(this->directory);
108 SetStrongPtr(this->xreDirectory, (nsILocalFile*) nsnull);
109 SetAllocatedString(this->minVersion, nsnull);
110 SetAllocatedString(this->maxVersion, nsnull);
112 SetAllocatedString(this->crashReporterURL, nsnull);
115 nsresult
116 XRE_CreateAppData(nsILocalFile* aINIFile, nsXREAppData **aAppData)
118 NS_ENSURE_ARG(aINIFile && aAppData);
120 nsAutoPtr<ScopedAppData> data(new ScopedAppData());
121 if (!data)
122 return NS_ERROR_OUT_OF_MEMORY;
124 nsresult rv = XRE_ParseAppData(aINIFile, data);
125 if (NS_FAILED(rv))
126 return rv;
128 if (!data->directory) {
129 nsCOMPtr<nsIFile> appDir;
130 rv = aINIFile->GetParent(getter_AddRefs(appDir));
131 if (NS_FAILED(rv))
132 return rv;
134 rv = CallQueryInterface(appDir, &data->directory);
135 if (NS_FAILED(rv))
136 return rv;
139 *aAppData = data.forget();
140 return NS_OK;
143 struct ReadString {
144 const char *section;
145 const char *key;
146 const char **buffer;
149 static void
150 ReadStrings(nsINIParser &parser, const ReadString *reads)
152 nsresult rv;
153 nsCString str;
155 while (reads->section) {
156 rv = parser.GetString(reads->section, reads->key, str);
157 if (NS_SUCCEEDED(rv)) {
158 SetAllocatedString(*reads->buffer, str);
161 ++reads;
165 struct ReadFlag {
166 const char *section;
167 const char *key;
168 PRUint32 flag;
171 static void
172 ReadFlags(nsINIParser &parser, const ReadFlag *reads, PRUint32 *buffer)
174 nsresult rv;
175 char buf[6]; // large enough to hold "false"
177 while (reads->section) {
178 rv = parser.GetString(reads->section, reads->key, buf, sizeof(buf));
179 if (NS_SUCCEEDED(rv) || rv == NS_ERROR_LOSS_OF_SIGNIFICANT_DATA) {
180 if (buf[0] == '1' || buf[0] == 't' || buf[0] == 'T') {
181 *buffer |= reads->flag;
183 if (buf[0] == '0' || buf[0] == 'f' || buf[0] == 'F') {
184 *buffer &= ~reads->flag;
188 ++reads;
192 nsresult
193 XRE_ParseAppData(nsILocalFile* aINIFile, nsXREAppData *aAppData)
195 NS_ENSURE_ARG(aINIFile && aAppData);
197 nsresult rv;
199 nsINIParser parser;
200 rv = parser.Init(aINIFile);
201 if (NS_FAILED(rv))
202 return rv;
204 nsCString str;
206 ReadString strings[] = {
207 { "App", "Vendor", &aAppData->vendor },
208 { "App", "Name", &aAppData->name },
209 { "App", "Version", &aAppData->version },
210 { "App", "BuildID", &aAppData->buildID },
211 { "App", "ID", &aAppData->ID },
212 { "App", "Copyright", &aAppData->copyright },
213 { "App", "Profile", &aAppData->profile },
214 { nsnull }
216 ReadStrings(parser, strings);
218 ReadFlag flags[] = {
219 { "XRE", "EnableProfileMigrator", NS_XRE_ENABLE_PROFILE_MIGRATOR },
220 { "XRE", "EnableExtensionManager", NS_XRE_ENABLE_EXTENSION_MANAGER },
221 { nsnull }
223 ReadFlags(parser, flags, &aAppData->flags);
225 if (aAppData->size > offsetof(nsXREAppData, xreDirectory)) {
226 ReadString strings2[] = {
227 { "Gecko", "MinVersion", &aAppData->minVersion },
228 { "Gecko", "MaxVersion", &aAppData->maxVersion },
229 { nsnull }
231 ReadStrings(parser, strings2);
234 if (aAppData->size > offsetof(nsXREAppData, crashReporterURL)) {
235 ReadString strings3[] = {
236 { "Crash Reporter", "ServerURL", &aAppData->crashReporterURL },
237 { nsnull }
239 ReadStrings(parser, strings3);
240 ReadFlag flags2[] = {
241 { "Crash Reporter", "Enabled", NS_XRE_ENABLE_CRASH_REPORTER },
242 { nsnull }
244 ReadFlags(parser, flags2, &aAppData->flags);
247 return NS_OK;
250 void
251 XRE_FreeAppData(nsXREAppData *aAppData)
253 if (!aAppData) {
254 NS_ERROR("Invalid arg");
255 return;
258 ScopedAppData* sad = static_cast<ScopedAppData*>(aAppData);
259 delete sad;