toolkit/system/gnome/nsGConfService.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: 2; 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 "mozilla/ArrayUtils.h"
michael@0 7 #include "nsGConfService.h"
michael@0 8 #include "nsStringAPI.h"
michael@0 9 #include "nsCOMPtr.h"
michael@0 10 #include "nsComponentManagerUtils.h"
michael@0 11 #include "nsISupportsPrimitives.h"
michael@0 12 #include "nsIMutableArray.h"
michael@0 13 #include "prlink.h"
michael@0 14
michael@0 15 #include <gconf/gconf-client.h>
michael@0 16
michael@0 17 using namespace mozilla;
michael@0 18
michael@0 19 #define GCONF_FUNCTIONS \
michael@0 20 FUNC(gconf_client_get_default, GConfClient*, (void)) \
michael@0 21 FUNC(gconf_client_get_bool, gboolean, (GConfClient*, const gchar*, GError**)) \
michael@0 22 FUNC(gconf_client_get_string, gchar*, (GConfClient*, const gchar*, GError**)) \
michael@0 23 FUNC(gconf_client_get_int, gint, (GConfClient*, const gchar*, GError**)) \
michael@0 24 FUNC(gconf_client_get_float, gdouble, (GConfClient*, const gchar*, GError**)) \
michael@0 25 FUNC(gconf_client_get_list, GSList*, (GConfClient*, const gchar*, GConfValueType, GError**)) \
michael@0 26 FUNC(gconf_client_set_bool, gboolean, (GConfClient*, const gchar*, gboolean, GError**)) \
michael@0 27 FUNC(gconf_client_set_string, gboolean, (GConfClient*, const gchar*, const gchar*, GError**)) \
michael@0 28 FUNC(gconf_client_set_int, gboolean, (GConfClient*, const gchar*, gint, GError**)) \
michael@0 29 FUNC(gconf_client_set_float, gboolean, (GConfClient*, const gchar*, gdouble, GError**)) \
michael@0 30 FUNC(gconf_client_unset, gboolean, (GConfClient*, const gchar*, GError**))
michael@0 31
michael@0 32 #define FUNC(name, type, params) \
michael@0 33 typedef type (*_##name##_fn) params; \
michael@0 34 static _##name##_fn _##name;
michael@0 35
michael@0 36 GCONF_FUNCTIONS
michael@0 37
michael@0 38 #undef FUNC
michael@0 39
michael@0 40 #define gconf_client_get_default _gconf_client_get_default
michael@0 41 #define gconf_client_get_bool _gconf_client_get_bool
michael@0 42 #define gconf_client_get_string _gconf_client_get_string
michael@0 43 #define gconf_client_get_int _gconf_client_get_int
michael@0 44 #define gconf_client_get_float _gconf_client_get_float
michael@0 45 #define gconf_client_get_list _gconf_client_get_list
michael@0 46 #define gconf_client_set_bool _gconf_client_set_bool
michael@0 47 #define gconf_client_set_string _gconf_client_set_string
michael@0 48 #define gconf_client_set_int _gconf_client_set_int
michael@0 49 #define gconf_client_set_float _gconf_client_set_float
michael@0 50 #define gconf_client_unset _gconf_client_unset
michael@0 51
michael@0 52 static PRLibrary *gconfLib = nullptr;
michael@0 53
michael@0 54 typedef void (*nsGConfFunc)();
michael@0 55 struct nsGConfDynamicFunction {
michael@0 56 const char *functionName;
michael@0 57 nsGConfFunc *function;
michael@0 58 };
michael@0 59
michael@0 60 nsGConfService::~nsGConfService()
michael@0 61 {
michael@0 62 if (mClient)
michael@0 63 g_object_unref(mClient);
michael@0 64
michael@0 65 // We don't unload gconf here because liborbit uses atexit(). In addition to
michael@0 66 // this, it's not a good idea to unload any gobject based library, as it
michael@0 67 // leaves types registered in glib's type system
michael@0 68 }
michael@0 69
michael@0 70 nsresult
michael@0 71 nsGConfService::Init()
michael@0 72 {
michael@0 73 #define FUNC(name, type, params) { #name, (nsGConfFunc *)&_##name },
michael@0 74 static const nsGConfDynamicFunction kGConfSymbols[] = {
michael@0 75 GCONF_FUNCTIONS
michael@0 76 };
michael@0 77 #undef FUNC
michael@0 78
michael@0 79 if (!gconfLib) {
michael@0 80 gconfLib = PR_LoadLibrary("libgconf-2.so.4");
michael@0 81 if (!gconfLib)
michael@0 82 return NS_ERROR_FAILURE;
michael@0 83 }
michael@0 84
michael@0 85 for (uint32_t i = 0; i < ArrayLength(kGConfSymbols); i++) {
michael@0 86 *kGConfSymbols[i].function =
michael@0 87 PR_FindFunctionSymbol(gconfLib, kGConfSymbols[i].functionName);
michael@0 88 if (!*kGConfSymbols[i].function) {
michael@0 89 return NS_ERROR_FAILURE;
michael@0 90 }
michael@0 91 }
michael@0 92
michael@0 93 mClient = gconf_client_get_default();
michael@0 94 return mClient ? NS_OK : NS_ERROR_FAILURE;
michael@0 95 }
michael@0 96
michael@0 97 NS_IMPL_ISUPPORTS(nsGConfService, nsIGConfService)
michael@0 98
michael@0 99 NS_IMETHODIMP
michael@0 100 nsGConfService::GetBool(const nsACString &aKey, bool *aResult)
michael@0 101 {
michael@0 102 GError* error = nullptr;
michael@0 103 *aResult = gconf_client_get_bool(mClient, PromiseFlatCString(aKey).get(),
michael@0 104 &error);
michael@0 105
michael@0 106 if (error) {
michael@0 107 g_error_free(error);
michael@0 108 return NS_ERROR_FAILURE;
michael@0 109 }
michael@0 110
michael@0 111 return NS_OK;
michael@0 112 }
michael@0 113
michael@0 114 NS_IMETHODIMP
michael@0 115 nsGConfService::GetString(const nsACString &aKey, nsACString &aResult)
michael@0 116 {
michael@0 117 GError* error = nullptr;
michael@0 118 gchar *result = gconf_client_get_string(mClient,
michael@0 119 PromiseFlatCString(aKey).get(),
michael@0 120 &error);
michael@0 121
michael@0 122 if (error) {
michael@0 123 g_error_free(error);
michael@0 124 return NS_ERROR_FAILURE;
michael@0 125 }
michael@0 126
michael@0 127 // We do a string copy here so that the caller doesn't need to worry about
michael@0 128 // freeing the string with g_free().
michael@0 129
michael@0 130 aResult.Assign(result);
michael@0 131 g_free(result);
michael@0 132
michael@0 133 return NS_OK;
michael@0 134 }
michael@0 135
michael@0 136 NS_IMETHODIMP
michael@0 137 nsGConfService::GetInt(const nsACString &aKey, int32_t* aResult)
michael@0 138 {
michael@0 139 GError* error = nullptr;
michael@0 140 *aResult = gconf_client_get_int(mClient, PromiseFlatCString(aKey).get(),
michael@0 141 &error);
michael@0 142
michael@0 143 if (error) {
michael@0 144 g_error_free(error);
michael@0 145 return NS_ERROR_FAILURE;
michael@0 146 }
michael@0 147
michael@0 148 return NS_OK;
michael@0 149 }
michael@0 150
michael@0 151 NS_IMETHODIMP
michael@0 152 nsGConfService::GetFloat(const nsACString &aKey, float* aResult)
michael@0 153 {
michael@0 154 GError* error = nullptr;
michael@0 155 *aResult = gconf_client_get_float(mClient, PromiseFlatCString(aKey).get(),
michael@0 156 &error);
michael@0 157
michael@0 158 if (error) {
michael@0 159 g_error_free(error);
michael@0 160 return NS_ERROR_FAILURE;
michael@0 161 }
michael@0 162
michael@0 163 return NS_OK;
michael@0 164 }
michael@0 165
michael@0 166 NS_IMETHODIMP
michael@0 167 nsGConfService::GetStringList(const nsACString &aKey, nsIArray** aResult)
michael@0 168 {
michael@0 169 nsCOMPtr<nsIMutableArray> items(do_CreateInstance(NS_ARRAY_CONTRACTID));
michael@0 170 if (!items)
michael@0 171 return NS_ERROR_OUT_OF_MEMORY;
michael@0 172
michael@0 173 GError* error = nullptr;
michael@0 174 GSList* list = gconf_client_get_list(mClient, PromiseFlatCString(aKey).get(),
michael@0 175 GCONF_VALUE_STRING, &error);
michael@0 176 if (error) {
michael@0 177 g_error_free(error);
michael@0 178 return NS_ERROR_FAILURE;
michael@0 179 }
michael@0 180
michael@0 181 for (GSList* l = list; l; l = l->next) {
michael@0 182 nsCOMPtr<nsISupportsString> obj(do_CreateInstance(NS_SUPPORTS_STRING_CONTRACTID));
michael@0 183 if (!obj) {
michael@0 184 g_slist_free(list);
michael@0 185 return NS_ERROR_OUT_OF_MEMORY;
michael@0 186 }
michael@0 187 obj->SetData(NS_ConvertUTF8toUTF16((const char*)l->data));
michael@0 188 items->AppendElement(obj, false);
michael@0 189 g_free(l->data);
michael@0 190 }
michael@0 191
michael@0 192 g_slist_free(list);
michael@0 193 NS_ADDREF(*aResult = items);
michael@0 194 return NS_OK;
michael@0 195 }
michael@0 196
michael@0 197 NS_IMETHODIMP
michael@0 198 nsGConfService::SetBool(const nsACString &aKey, bool aValue)
michael@0 199 {
michael@0 200 bool res = gconf_client_set_bool(mClient, PromiseFlatCString(aKey).get(),
michael@0 201 aValue, nullptr);
michael@0 202
michael@0 203 return res ? NS_OK : NS_ERROR_FAILURE;
michael@0 204 }
michael@0 205
michael@0 206 NS_IMETHODIMP
michael@0 207 nsGConfService::SetString(const nsACString &aKey, const nsACString &aValue)
michael@0 208 {
michael@0 209 bool res = gconf_client_set_string(mClient, PromiseFlatCString(aKey).get(),
michael@0 210 PromiseFlatCString(aValue).get(),
michael@0 211 nullptr);
michael@0 212
michael@0 213 return res ? NS_OK : NS_ERROR_FAILURE;
michael@0 214 }
michael@0 215
michael@0 216 NS_IMETHODIMP
michael@0 217 nsGConfService::SetInt(const nsACString &aKey, int32_t aValue)
michael@0 218 {
michael@0 219 bool res = gconf_client_set_int(mClient, PromiseFlatCString(aKey).get(),
michael@0 220 aValue, nullptr);
michael@0 221
michael@0 222 return res ? NS_OK : NS_ERROR_FAILURE;
michael@0 223 }
michael@0 224
michael@0 225 NS_IMETHODIMP
michael@0 226 nsGConfService::SetFloat(const nsACString &aKey, float aValue)
michael@0 227 {
michael@0 228 bool res = gconf_client_set_float(mClient, PromiseFlatCString(aKey).get(),
michael@0 229 aValue, nullptr);
michael@0 230
michael@0 231 return res ? NS_OK : NS_ERROR_FAILURE;
michael@0 232 }
michael@0 233
michael@0 234 NS_IMETHODIMP
michael@0 235 nsGConfService::GetAppForProtocol(const nsACString &aScheme, bool *aEnabled,
michael@0 236 nsACString &aHandler)
michael@0 237 {
michael@0 238 nsAutoCString key("/desktop/gnome/url-handlers/");
michael@0 239 key.Append(aScheme);
michael@0 240 key.Append("/command");
michael@0 241
michael@0 242 GError *err = nullptr;
michael@0 243 gchar *command = gconf_client_get_string(mClient, key.get(), &err);
michael@0 244 if (!err && command) {
michael@0 245 key.Replace(key.Length() - 7, 7, NS_LITERAL_CSTRING("enabled"));
michael@0 246 *aEnabled = gconf_client_get_bool(mClient, key.get(), &err);
michael@0 247 } else {
michael@0 248 *aEnabled = false;
michael@0 249 }
michael@0 250
michael@0 251 aHandler.Assign(command);
michael@0 252 g_free(command);
michael@0 253
michael@0 254 if (err) {
michael@0 255 g_error_free(err);
michael@0 256 return NS_ERROR_FAILURE;
michael@0 257 }
michael@0 258
michael@0 259 return NS_OK;
michael@0 260 }
michael@0 261
michael@0 262 NS_IMETHODIMP
michael@0 263 nsGConfService::HandlerRequiresTerminal(const nsACString &aScheme,
michael@0 264 bool *aResult)
michael@0 265 {
michael@0 266 nsAutoCString key("/desktop/gnome/url-handlers/");
michael@0 267 key.Append(aScheme);
michael@0 268 key.Append("/requires_terminal");
michael@0 269
michael@0 270 GError *err = nullptr;
michael@0 271 *aResult = gconf_client_get_bool(mClient, key.get(), &err);
michael@0 272 if (err) {
michael@0 273 g_error_free(err);
michael@0 274 return NS_ERROR_FAILURE;
michael@0 275 }
michael@0 276
michael@0 277 return NS_OK;
michael@0 278 }
michael@0 279
michael@0 280 NS_IMETHODIMP
michael@0 281 nsGConfService::SetAppForProtocol(const nsACString &aScheme,
michael@0 282 const nsACString &aCommand)
michael@0 283 {
michael@0 284 nsAutoCString key("/desktop/gnome/url-handlers/");
michael@0 285 key.Append(aScheme);
michael@0 286 key.Append("/command");
michael@0 287
michael@0 288 bool res = gconf_client_set_string(mClient, key.get(),
michael@0 289 PromiseFlatCString(aCommand).get(),
michael@0 290 nullptr);
michael@0 291 if (res) {
michael@0 292 key.Replace(key.Length() - 7, 7, NS_LITERAL_CSTRING("enabled"));
michael@0 293 res = gconf_client_set_bool(mClient, key.get(), true, nullptr);
michael@0 294 if (res) {
michael@0 295 key.Replace(key.Length() - 7, 7, NS_LITERAL_CSTRING("needs_terminal"));
michael@0 296 res = gconf_client_set_bool(mClient, key.get(), false, nullptr);
michael@0 297 if (res) {
michael@0 298 key.Replace(key.Length() - 14, 14, NS_LITERAL_CSTRING("command-id"));
michael@0 299 res = gconf_client_unset(mClient, key.get(), nullptr);
michael@0 300 }
michael@0 301 }
michael@0 302 }
michael@0 303
michael@0 304 return res ? NS_OK : NS_ERROR_FAILURE;
michael@0 305 }

mercurial