|
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 #include "nsXULAppAPI.h" |
|
7 #include "nsINIParser.h" |
|
8 #include "nsIFile.h" |
|
9 #include "nsAutoPtr.h" |
|
10 #include "mozilla/AppData.h" |
|
11 |
|
12 using namespace mozilla; |
|
13 |
|
14 nsresult |
|
15 XRE_CreateAppData(nsIFile* aINIFile, nsXREAppData **aAppData) |
|
16 { |
|
17 NS_ENSURE_ARG(aINIFile && aAppData); |
|
18 |
|
19 nsAutoPtr<ScopedAppData> data(new ScopedAppData()); |
|
20 if (!data) |
|
21 return NS_ERROR_OUT_OF_MEMORY; |
|
22 |
|
23 nsresult rv = XRE_ParseAppData(aINIFile, data); |
|
24 if (NS_FAILED(rv)) |
|
25 return rv; |
|
26 |
|
27 if (!data->directory) { |
|
28 nsCOMPtr<nsIFile> appDir; |
|
29 rv = aINIFile->GetParent(getter_AddRefs(appDir)); |
|
30 if (NS_FAILED(rv)) |
|
31 return rv; |
|
32 |
|
33 appDir.forget(&data->directory); |
|
34 } |
|
35 |
|
36 *aAppData = data.forget(); |
|
37 return NS_OK; |
|
38 } |
|
39 |
|
40 struct ReadString { |
|
41 const char *section; |
|
42 const char *key; |
|
43 const char **buffer; |
|
44 }; |
|
45 |
|
46 static void |
|
47 ReadStrings(nsINIParser &parser, const ReadString *reads) |
|
48 { |
|
49 nsresult rv; |
|
50 nsCString str; |
|
51 |
|
52 while (reads->section) { |
|
53 rv = parser.GetString(reads->section, reads->key, str); |
|
54 if (NS_SUCCEEDED(rv)) { |
|
55 SetAllocatedString(*reads->buffer, str); |
|
56 } |
|
57 |
|
58 ++reads; |
|
59 } |
|
60 } |
|
61 |
|
62 struct ReadFlag { |
|
63 const char *section; |
|
64 const char *key; |
|
65 uint32_t flag; |
|
66 }; |
|
67 |
|
68 static void |
|
69 ReadFlags(nsINIParser &parser, const ReadFlag *reads, uint32_t *buffer) |
|
70 { |
|
71 nsresult rv; |
|
72 char buf[6]; // large enough to hold "false" |
|
73 |
|
74 while (reads->section) { |
|
75 rv = parser.GetString(reads->section, reads->key, buf, sizeof(buf)); |
|
76 if (NS_SUCCEEDED(rv) || rv == NS_ERROR_LOSS_OF_SIGNIFICANT_DATA) { |
|
77 if (buf[0] == '1' || buf[0] == 't' || buf[0] == 'T') { |
|
78 *buffer |= reads->flag; |
|
79 } |
|
80 if (buf[0] == '0' || buf[0] == 'f' || buf[0] == 'F') { |
|
81 *buffer &= ~reads->flag; |
|
82 } |
|
83 } |
|
84 |
|
85 ++reads; |
|
86 } |
|
87 } |
|
88 |
|
89 nsresult |
|
90 XRE_ParseAppData(nsIFile* aINIFile, nsXREAppData *aAppData) |
|
91 { |
|
92 NS_ENSURE_ARG(aINIFile && aAppData); |
|
93 |
|
94 nsresult rv; |
|
95 |
|
96 nsINIParser parser; |
|
97 rv = parser.Init(aINIFile); |
|
98 if (NS_FAILED(rv)) |
|
99 return rv; |
|
100 |
|
101 nsCString str; |
|
102 |
|
103 ReadString strings[] = { |
|
104 { "App", "Vendor", &aAppData->vendor }, |
|
105 { "App", "Name", &aAppData->name }, |
|
106 { "App", "Version", &aAppData->version }, |
|
107 { "App", "BuildID", &aAppData->buildID }, |
|
108 { "App", "ID", &aAppData->ID }, |
|
109 { "App", "Copyright", &aAppData->copyright }, |
|
110 { "App", "Profile", &aAppData->profile }, |
|
111 { nullptr } |
|
112 }; |
|
113 ReadStrings(parser, strings); |
|
114 |
|
115 ReadFlag flags[] = { |
|
116 { "XRE", "EnableProfileMigrator", NS_XRE_ENABLE_PROFILE_MIGRATOR }, |
|
117 { "XRE", "EnableExtensionManager", NS_XRE_ENABLE_EXTENSION_MANAGER }, |
|
118 { nullptr } |
|
119 }; |
|
120 ReadFlags(parser, flags, &aAppData->flags); |
|
121 |
|
122 if (aAppData->size > offsetof(nsXREAppData, xreDirectory)) { |
|
123 ReadString strings2[] = { |
|
124 { "Gecko", "MinVersion", &aAppData->minVersion }, |
|
125 { "Gecko", "MaxVersion", &aAppData->maxVersion }, |
|
126 { nullptr } |
|
127 }; |
|
128 ReadStrings(parser, strings2); |
|
129 } |
|
130 |
|
131 if (aAppData->size > offsetof(nsXREAppData, crashReporterURL)) { |
|
132 ReadString strings3[] = { |
|
133 { "Crash Reporter", "ServerURL", &aAppData->crashReporterURL }, |
|
134 { nullptr } |
|
135 }; |
|
136 ReadStrings(parser, strings3); |
|
137 ReadFlag flags2[] = { |
|
138 { "Crash Reporter", "Enabled", NS_XRE_ENABLE_CRASH_REPORTER }, |
|
139 { nullptr } |
|
140 }; |
|
141 ReadFlags(parser, flags2, &aAppData->flags); |
|
142 } |
|
143 |
|
144 if (aAppData->size > offsetof(nsXREAppData, UAName)) { |
|
145 ReadString strings4[] = { |
|
146 { "App", "UAName", &aAppData->UAName }, |
|
147 { nullptr } |
|
148 }; |
|
149 ReadStrings(parser, strings4); |
|
150 } |
|
151 |
|
152 return NS_OK; |
|
153 } |
|
154 |
|
155 void |
|
156 XRE_FreeAppData(nsXREAppData *aAppData) |
|
157 { |
|
158 if (!aAppData) { |
|
159 NS_ERROR("Invalid arg"); |
|
160 return; |
|
161 } |
|
162 |
|
163 ScopedAppData* sad = static_cast<ScopedAppData*>(aAppData); |
|
164 delete sad; |
|
165 } |