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
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.
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"
46 SetAllocatedString(const char *&str
, const char *newvalue
)
48 NS_Free(const_cast<char*>(str
));
50 str
= NS_strdup(newvalue
);
58 SetAllocatedString(const char *&str
, const nsACString
&newvalue
)
60 NS_Free(const_cast<char*>(str
));
61 if (newvalue
.IsEmpty()) {
65 str
= ToNewCString(newvalue
);
69 ScopedAppData::ScopedAppData(const nsXREAppData
* aAppData
)
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
);
116 XRE_CreateAppData(nsILocalFile
* aINIFile
, nsXREAppData
**aAppData
)
118 NS_ENSURE_ARG(aINIFile
&& aAppData
);
120 nsAutoPtr
<ScopedAppData
> data(new ScopedAppData());
122 return NS_ERROR_OUT_OF_MEMORY
;
124 nsresult rv
= XRE_ParseAppData(aINIFile
, data
);
128 if (!data
->directory
) {
129 nsCOMPtr
<nsIFile
> appDir
;
130 rv
= aINIFile
->GetParent(getter_AddRefs(appDir
));
134 rv
= CallQueryInterface(appDir
, &data
->directory
);
139 *aAppData
= data
.forget();
150 ReadStrings(nsINIParser
&parser
, const ReadString
*reads
)
155 while (reads
->section
) {
156 rv
= parser
.GetString(reads
->section
, reads
->key
, str
);
157 if (NS_SUCCEEDED(rv
)) {
158 SetAllocatedString(*reads
->buffer
, str
);
172 ReadFlags(nsINIParser
&parser
, const ReadFlag
*reads
, PRUint32
*buffer
)
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
;
193 XRE_ParseAppData(nsILocalFile
* aINIFile
, nsXREAppData
*aAppData
)
195 NS_ENSURE_ARG(aINIFile
&& aAppData
);
200 rv
= parser
.Init(aINIFile
);
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
},
216 ReadStrings(parser
, strings
);
219 { "XRE", "EnableProfileMigrator", NS_XRE_ENABLE_PROFILE_MIGRATOR
},
220 { "XRE", "EnableExtensionManager", NS_XRE_ENABLE_EXTENSION_MANAGER
},
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
},
231 ReadStrings(parser
, strings2
);
234 if (aAppData
->size
> offsetof(nsXREAppData
, crashReporterURL
)) {
235 ReadString strings3
[] = {
236 { "Crash Reporter", "ServerURL", &aAppData
->crashReporterURL
},
239 ReadStrings(parser
, strings3
);
240 ReadFlag flags2
[] = {
241 { "Crash Reporter", "Enabled", NS_XRE_ENABLE_CRASH_REPORTER
},
244 ReadFlags(parser
, flags2
, &aAppData
->flags
);
251 XRE_FreeAppData(nsXREAppData
*aAppData
)
254 NS_ERROR("Invalid arg");
258 ScopedAppData
* sad
= static_cast<ScopedAppData
*>(aAppData
);