toolkit/xre/CreateAppData.cpp

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

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

mercurial