Bug 788829 - Call SetSizeConstraints even if a popup is not open. r=enndeakin
[gecko.git] / toolkit / xre / CreateAppData.cpp
bloba7f13a317b1dbe89122944510c143336b6c69c2a
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 { "XRE", "EnableExtensionManager", NS_XRE_ENABLE_EXTENSION_MANAGER },
118 { nullptr }
120 ReadFlags(parser, flags, &aAppData->flags);
122 if (aAppData->size > offsetof(nsXREAppData, xreDirectory)) {
123 ReadString strings2[] = {
124 { "Gecko", "MinVersion", &aAppData->minVersion },
125 { "Gecko", "MaxVersion", &aAppData->maxVersion },
126 { nullptr }
128 ReadStrings(parser, strings2);
131 if (aAppData->size > offsetof(nsXREAppData, crashReporterURL)) {
132 ReadString strings3[] = {
133 { "Crash Reporter", "ServerURL", &aAppData->crashReporterURL },
134 { nullptr }
136 ReadStrings(parser, strings3);
137 ReadFlag flags2[] = {
138 { "Crash Reporter", "Enabled", NS_XRE_ENABLE_CRASH_REPORTER },
139 { nullptr }
141 ReadFlags(parser, flags2, &aAppData->flags);
144 if (aAppData->size > offsetof(nsXREAppData, UAName)) {
145 ReadString strings4[] = {
146 { "App", "UAName", &aAppData->UAName },
147 { nullptr }
149 ReadStrings(parser, strings4);
152 return NS_OK;
155 void
156 XRE_FreeAppData(nsXREAppData *aAppData)
158 if (!aAppData) {
159 NS_ERROR("Invalid arg");
160 return;
163 ScopedAppData* sad = static_cast<ScopedAppData*>(aAppData);
164 delete sad;