1 : /* ***** BEGIN LICENSE BLOCK *****
2 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3 : *
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/
8 : *
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.
13 : *
14 : * The Original Code is mozilla.org code.
15 : *
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.
20 : *
21 : * Contributor(s):
22 : * Benjamin Smedberg <benjamin@smedberg.us> (Original Author)
23 : *
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.
35 : *
36 : * ***** END LICENSE BLOCK ***** */
37 :
38 : #include "nsXULAppAPI.h"
39 : #include "nsINIParser.h"
40 : #include "nsILocalFile.h"
41 : #include "nsAppRunner.h"
42 : #include "nsCRTGlue.h"
43 : #include "nsAutoPtr.h"
44 :
45 : void
46 460 : SetAllocatedString(const char *&str, const char *newvalue)
47 : {
48 460 : NS_Free(const_cast<char*>(str));
49 460 : if (newvalue) {
50 184 : str = NS_strdup(newvalue);
51 : }
52 : else {
53 276 : str = nsnull;
54 : }
55 460 : }
56 :
57 : void
58 0 : SetAllocatedString(const char *&str, const nsACString &newvalue)
59 : {
60 0 : NS_Free(const_cast<char*>(str));
61 0 : if (newvalue.IsEmpty()) {
62 0 : str = nsnull;
63 : }
64 : else {
65 0 : str = ToNewCString(newvalue);
66 : }
67 0 : }
68 :
69 23 : ScopedAppData::ScopedAppData(const nsXREAppData* aAppData)
70 : {
71 23 : Zero();
72 :
73 23 : this->size = aAppData->size;
74 :
75 23 : SetAllocatedString(this->vendor, aAppData->vendor);
76 23 : SetAllocatedString(this->name, aAppData->name);
77 23 : SetAllocatedString(this->version, aAppData->version);
78 23 : SetAllocatedString(this->buildID, aAppData->buildID);
79 23 : SetAllocatedString(this->ID, aAppData->ID);
80 23 : SetAllocatedString(this->copyright, aAppData->copyright);
81 23 : SetAllocatedString(this->profile, aAppData->profile);
82 23 : SetStrongPtr(this->directory, aAppData->directory);
83 23 : this->flags = aAppData->flags;
84 :
85 23 : if (aAppData->size > offsetof(nsXREAppData, xreDirectory)) {
86 23 : SetStrongPtr(this->xreDirectory, aAppData->xreDirectory);
87 23 : SetAllocatedString(this->minVersion, aAppData->minVersion);
88 23 : SetAllocatedString(this->maxVersion, aAppData->maxVersion);
89 : }
90 :
91 23 : if (aAppData->size > offsetof(nsXREAppData, crashReporterURL)) {
92 23 : SetAllocatedString(this->crashReporterURL, aAppData->crashReporterURL);
93 : }
94 23 : }
95 :
96 23 : ScopedAppData::~ScopedAppData()
97 : {
98 23 : SetAllocatedString(this->vendor, nsnull);
99 23 : SetAllocatedString(this->name, nsnull);
100 23 : SetAllocatedString(this->version, nsnull);
101 23 : SetAllocatedString(this->buildID, nsnull);
102 23 : SetAllocatedString(this->ID, nsnull);
103 23 : SetAllocatedString(this->copyright, nsnull);
104 23 : SetAllocatedString(this->profile, nsnull);
105 :
106 23 : NS_IF_RELEASE(this->directory);
107 :
108 23 : SetStrongPtr(this->xreDirectory, (nsILocalFile*) nsnull);
109 23 : SetAllocatedString(this->minVersion, nsnull);
110 23 : SetAllocatedString(this->maxVersion, nsnull);
111 :
112 23 : SetAllocatedString(this->crashReporterURL, nsnull);
113 23 : }
114 :
115 : nsresult
116 0 : XRE_CreateAppData(nsILocalFile* aINIFile, nsXREAppData **aAppData)
117 : {
118 0 : NS_ENSURE_ARG(aINIFile && aAppData);
119 :
120 0 : nsAutoPtr<ScopedAppData> data(new ScopedAppData());
121 0 : if (!data)
122 0 : return NS_ERROR_OUT_OF_MEMORY;
123 :
124 0 : nsresult rv = XRE_ParseAppData(aINIFile, data);
125 0 : if (NS_FAILED(rv))
126 0 : return rv;
127 :
128 0 : if (!data->directory) {
129 0 : nsCOMPtr<nsIFile> appDir;
130 0 : rv = aINIFile->GetParent(getter_AddRefs(appDir));
131 0 : if (NS_FAILED(rv))
132 0 : return rv;
133 :
134 0 : rv = CallQueryInterface(appDir, &data->directory);
135 0 : if (NS_FAILED(rv))
136 0 : return rv;
137 : }
138 :
139 0 : *aAppData = data.forget();
140 0 : return NS_OK;
141 : }
142 :
143 : struct ReadString {
144 : const char *section;
145 : const char *key;
146 : const char **buffer;
147 : };
148 :
149 : static void
150 0 : ReadStrings(nsINIParser &parser, const ReadString *reads)
151 : {
152 : nsresult rv;
153 0 : nsCString str;
154 :
155 0 : while (reads->section) {
156 0 : rv = parser.GetString(reads->section, reads->key, str);
157 0 : if (NS_SUCCEEDED(rv)) {
158 0 : SetAllocatedString(*reads->buffer, str);
159 : }
160 :
161 0 : ++reads;
162 : }
163 0 : }
164 :
165 : struct ReadFlag {
166 : const char *section;
167 : const char *key;
168 : PRUint32 flag;
169 : };
170 :
171 : static void
172 0 : ReadFlags(nsINIParser &parser, const ReadFlag *reads, PRUint32 *buffer)
173 : {
174 : nsresult rv;
175 : char buf[6]; // large enough to hold "false"
176 :
177 0 : while (reads->section) {
178 0 : rv = parser.GetString(reads->section, reads->key, buf, sizeof(buf));
179 0 : if (NS_SUCCEEDED(rv) || rv == NS_ERROR_LOSS_OF_SIGNIFICANT_DATA) {
180 0 : if (buf[0] == '1' || buf[0] == 't' || buf[0] == 'T') {
181 0 : *buffer |= reads->flag;
182 : }
183 0 : if (buf[0] == '0' || buf[0] == 'f' || buf[0] == 'F') {
184 0 : *buffer &= ~reads->flag;
185 : }
186 : }
187 :
188 0 : ++reads;
189 : }
190 0 : }
191 :
192 : nsresult
193 0 : XRE_ParseAppData(nsILocalFile* aINIFile, nsXREAppData *aAppData)
194 : {
195 0 : NS_ENSURE_ARG(aINIFile && aAppData);
196 :
197 : nsresult rv;
198 :
199 0 : nsINIParser parser;
200 0 : rv = parser.Init(aINIFile);
201 0 : if (NS_FAILED(rv))
202 0 : return rv;
203 :
204 0 : nsCString str;
205 :
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 }
215 0 : };
216 0 : ReadStrings(parser, strings);
217 :
218 : ReadFlag flags[] = {
219 : { "XRE", "EnableProfileMigrator", NS_XRE_ENABLE_PROFILE_MIGRATOR },
220 : { "XRE", "EnableExtensionManager", NS_XRE_ENABLE_EXTENSION_MANAGER },
221 : { nsnull }
222 0 : };
223 0 : ReadFlags(parser, flags, &aAppData->flags);
224 :
225 0 : if (aAppData->size > offsetof(nsXREAppData, xreDirectory)) {
226 : ReadString strings2[] = {
227 : { "Gecko", "MinVersion", &aAppData->minVersion },
228 : { "Gecko", "MaxVersion", &aAppData->maxVersion },
229 : { nsnull }
230 0 : };
231 0 : ReadStrings(parser, strings2);
232 : }
233 :
234 0 : if (aAppData->size > offsetof(nsXREAppData, crashReporterURL)) {
235 : ReadString strings3[] = {
236 : { "Crash Reporter", "ServerURL", &aAppData->crashReporterURL },
237 : { nsnull }
238 0 : };
239 0 : ReadStrings(parser, strings3);
240 : ReadFlag flags2[] = {
241 : { "Crash Reporter", "Enabled", NS_XRE_ENABLE_CRASH_REPORTER },
242 : { nsnull }
243 0 : };
244 0 : ReadFlags(parser, flags2, &aAppData->flags);
245 : }
246 :
247 0 : return NS_OK;
248 : }
249 :
250 : void
251 0 : XRE_FreeAppData(nsXREAppData *aAppData)
252 : {
253 0 : if (!aAppData) {
254 0 : NS_ERROR("Invalid arg");
255 0 : return;
256 : }
257 :
258 0 : ScopedAppData* sad = static_cast<ScopedAppData*>(aAppData);
259 0 : delete sad;
260 : }
|