Bumping manifests a=b2g-bump
[gecko.git] / toolkit / xre / CreateAppData.cpp
blob48120f61da27d9d55a7eab2ad68792ddf19d3687
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/. */
6 #include "nsXULAppAPI.h"
7 #include "nsINIParser.h"
8 #include "nsIFile.h"
9 #include "nsAutoPtr.h"
10 #include "mozilla/AppData.h"
12 using namespace mozilla;
14 nsresult
15 XRE_CreateAppData(nsIFile* aINIFile, nsXREAppData **aAppData)
17 NS_ENSURE_ARG(aINIFile && aAppData);
19 nsAutoPtr<ScopedAppData> data(new ScopedAppData());
20 if (!data)
21 return NS_ERROR_OUT_OF_MEMORY;
23 nsresult rv = XRE_ParseAppData(aINIFile, data);
24 if (NS_FAILED(rv))
25 return rv;
27 if (!data->directory) {
28 nsCOMPtr<nsIFile> appDir;
29 rv = aINIFile->GetParent(getter_AddRefs(appDir));
30 if (NS_FAILED(rv))
31 return rv;
33 appDir.forget(&data->directory);
36 *aAppData = data.forget();
37 return NS_OK;
40 struct ReadString {
41 const char *section;
42 const char *key;
43 const char **buffer;
46 static void
47 ReadStrings(nsINIParser &parser, const ReadString *reads)
49 nsresult rv;
50 nsCString str;
52 while (reads->section) {
53 rv = parser.GetString(reads->section, reads->key, str);
54 if (NS_SUCCEEDED(rv)) {
55 SetAllocatedString(*reads->buffer, str);
58 ++reads;
62 struct ReadFlag {
63 const char *section;
64 const char *key;
65 uint32_t flag;
68 static void
69 ReadFlags(nsINIParser &parser, const ReadFlag *reads, uint32_t *buffer)
71 nsresult rv;
72 char buf[6]; // large enough to hold "false"
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;
80 if (buf[0] == '0' || buf[0] == 'f' || buf[0] == 'F') {
81 *buffer &= ~reads->flag;
85 ++reads;
89 nsresult
90 XRE_ParseAppData(nsIFile* aINIFile, nsXREAppData *aAppData)
92 NS_ENSURE_ARG(aINIFile && aAppData);
94 nsresult rv;
96 nsINIParser parser;
97 rv = parser.Init(aINIFile);
98 if (NS_FAILED(rv))
99 return rv;
101 nsCString str;
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 }
113 ReadStrings(parser, strings);
115 ReadFlag flags[] = {
116 { "XRE", "EnableProfileMigrator", NS_XRE_ENABLE_PROFILE_MIGRATOR },
117 { nullptr }
119 ReadFlags(parser, flags, &aAppData->flags);
121 if (aAppData->size > offsetof(nsXREAppData, xreDirectory)) {
122 ReadString strings2[] = {
123 { "Gecko", "MinVersion", &aAppData->minVersion },
124 { "Gecko", "MaxVersion", &aAppData->maxVersion },
125 { nullptr }
127 ReadStrings(parser, strings2);
130 if (aAppData->size > offsetof(nsXREAppData, crashReporterURL)) {
131 ReadString strings3[] = {
132 { "Crash Reporter", "ServerURL", &aAppData->crashReporterURL },
133 { nullptr }
135 ReadStrings(parser, strings3);
136 ReadFlag flags2[] = {
137 { "Crash Reporter", "Enabled", NS_XRE_ENABLE_CRASH_REPORTER },
138 { nullptr }
140 ReadFlags(parser, flags2, &aAppData->flags);
143 if (aAppData->size > offsetof(nsXREAppData, UAName)) {
144 ReadString strings4[] = {
145 { "App", "UAName", &aAppData->UAName },
146 { nullptr }
148 ReadStrings(parser, strings4);
151 return NS_OK;
154 void
155 XRE_FreeAppData(nsXREAppData *aAppData)
157 if (!aAppData) {
158 NS_ERROR("Invalid arg");
159 return;
162 ScopedAppData* sad = static_cast<ScopedAppData*>(aAppData);
163 delete sad;