Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
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 | |
michael@0 | 8 | #include "nsGSettingsService.h" |
michael@0 | 9 | #include "nsStringAPI.h" |
michael@0 | 10 | #include "nsCOMPtr.h" |
michael@0 | 11 | #include "nsMemory.h" |
michael@0 | 12 | #include "prlink.h" |
michael@0 | 13 | #include "nsComponentManagerUtils.h" |
michael@0 | 14 | #include "nsIMutableArray.h" |
michael@0 | 15 | #include "nsISupportsPrimitives.h" |
michael@0 | 16 | |
michael@0 | 17 | #include <glib.h> |
michael@0 | 18 | #include <glib-object.h> |
michael@0 | 19 | |
michael@0 | 20 | using namespace mozilla; |
michael@0 | 21 | |
michael@0 | 22 | typedef struct _GSettings GSettings; |
michael@0 | 23 | typedef struct _GVariantType GVariantType; |
michael@0 | 24 | typedef struct _GVariant GVariant; |
michael@0 | 25 | |
michael@0 | 26 | #ifndef G_VARIANT_TYPE_INT32 |
michael@0 | 27 | # define G_VARIANT_TYPE_INT32 ((const GVariantType *) "i") |
michael@0 | 28 | # define G_VARIANT_TYPE_BOOLEAN ((const GVariantType *) "b") |
michael@0 | 29 | # define G_VARIANT_TYPE_STRING ((const GVariantType *) "s") |
michael@0 | 30 | # define G_VARIANT_TYPE_OBJECT_PATH ((const GVariantType *) "o") |
michael@0 | 31 | # define G_VARIANT_TYPE_SIGNATURE ((const GVariantType *) "g") |
michael@0 | 32 | #endif |
michael@0 | 33 | #ifndef G_VARIANT_TYPE_STRING_ARRAY |
michael@0 | 34 | # define G_VARIANT_TYPE_STRING_ARRAY ((const GVariantType *) "as") |
michael@0 | 35 | #endif |
michael@0 | 36 | |
michael@0 | 37 | #define GSETTINGS_FUNCTIONS \ |
michael@0 | 38 | FUNC(g_settings_new, GSettings *, (const char* schema)) \ |
michael@0 | 39 | FUNC(g_settings_list_schemas, const char * const *, (void)) \ |
michael@0 | 40 | FUNC(g_settings_list_keys, char **, (GSettings* settings)) \ |
michael@0 | 41 | FUNC(g_settings_get_value, GVariant *, (GSettings* settings, const char* key)) \ |
michael@0 | 42 | FUNC(g_settings_set_value, gboolean, (GSettings* settings, const char* key, GVariant* value)) \ |
michael@0 | 43 | FUNC(g_settings_range_check, gboolean, (GSettings* settings, const char* key, GVariant* value)) \ |
michael@0 | 44 | FUNC(g_variant_get_int32, gint32, (GVariant* variant)) \ |
michael@0 | 45 | FUNC(g_variant_get_boolean, gboolean, (GVariant* variant)) \ |
michael@0 | 46 | FUNC(g_variant_get_string, const char *, (GVariant* value, gsize* length)) \ |
michael@0 | 47 | FUNC(g_variant_get_strv, const char **, (GVariant* value, gsize* length)) \ |
michael@0 | 48 | FUNC(g_variant_is_of_type, gboolean, (GVariant* value, const GVariantType* type)) \ |
michael@0 | 49 | FUNC(g_variant_new_int32, GVariant *, (gint32 value)) \ |
michael@0 | 50 | FUNC(g_variant_new_boolean, GVariant *, (gboolean value)) \ |
michael@0 | 51 | FUNC(g_variant_new_string, GVariant *, (const char* string)) \ |
michael@0 | 52 | FUNC(g_variant_unref, void, (GVariant* value)) |
michael@0 | 53 | |
michael@0 | 54 | #define FUNC(name, type, params) \ |
michael@0 | 55 | typedef type (*_##name##_fn) params; \ |
michael@0 | 56 | static _##name##_fn _##name; |
michael@0 | 57 | |
michael@0 | 58 | GSETTINGS_FUNCTIONS |
michael@0 | 59 | |
michael@0 | 60 | #undef FUNC |
michael@0 | 61 | |
michael@0 | 62 | #define g_settings_new _g_settings_new |
michael@0 | 63 | #define g_settings_list_schemas _g_settings_list_schemas |
michael@0 | 64 | #define g_settings_list_keys _g_settings_list_keys |
michael@0 | 65 | #define g_settings_get_value _g_settings_get_value |
michael@0 | 66 | #define g_settings_set_value _g_settings_set_value |
michael@0 | 67 | #define g_settings_range_check _g_settings_range_check |
michael@0 | 68 | #define g_variant_get_int32 _g_variant_get_int32 |
michael@0 | 69 | #define g_variant_get_boolean _g_variant_get_boolean |
michael@0 | 70 | #define g_variant_get_string _g_variant_get_string |
michael@0 | 71 | #define g_variant_get_strv _g_variant_get_strv |
michael@0 | 72 | #define g_variant_is_of_type _g_variant_is_of_type |
michael@0 | 73 | #define g_variant_new_int32 _g_variant_new_int32 |
michael@0 | 74 | #define g_variant_new_boolean _g_variant_new_boolean |
michael@0 | 75 | #define g_variant_new_string _g_variant_new_string |
michael@0 | 76 | #define g_variant_unref _g_variant_unref |
michael@0 | 77 | |
michael@0 | 78 | static PRLibrary *gioLib = nullptr; |
michael@0 | 79 | |
michael@0 | 80 | class nsGSettingsCollection MOZ_FINAL : public nsIGSettingsCollection |
michael@0 | 81 | { |
michael@0 | 82 | public: |
michael@0 | 83 | NS_DECL_ISUPPORTS |
michael@0 | 84 | NS_DECL_NSIGSETTINGSCOLLECTION |
michael@0 | 85 | |
michael@0 | 86 | nsGSettingsCollection(GSettings* aSettings) : mSettings(aSettings), |
michael@0 | 87 | mKeys(nullptr) {} |
michael@0 | 88 | ~nsGSettingsCollection(); |
michael@0 | 89 | |
michael@0 | 90 | private: |
michael@0 | 91 | bool KeyExists(const nsACString& aKey); |
michael@0 | 92 | bool SetValue(const nsACString& aKey, |
michael@0 | 93 | GVariant *aValue); |
michael@0 | 94 | |
michael@0 | 95 | GSettings *mSettings; |
michael@0 | 96 | char **mKeys; |
michael@0 | 97 | }; |
michael@0 | 98 | |
michael@0 | 99 | nsGSettingsCollection::~nsGSettingsCollection() |
michael@0 | 100 | { |
michael@0 | 101 | g_strfreev(mKeys); |
michael@0 | 102 | g_object_unref(mSettings); |
michael@0 | 103 | } |
michael@0 | 104 | |
michael@0 | 105 | bool |
michael@0 | 106 | nsGSettingsCollection::KeyExists(const nsACString& aKey) |
michael@0 | 107 | { |
michael@0 | 108 | if (!mKeys) |
michael@0 | 109 | mKeys = g_settings_list_keys(mSettings); |
michael@0 | 110 | |
michael@0 | 111 | for (uint32_t i = 0; mKeys[i] != nullptr; i++) { |
michael@0 | 112 | if (aKey.Equals(mKeys[i])) |
michael@0 | 113 | return true; |
michael@0 | 114 | } |
michael@0 | 115 | |
michael@0 | 116 | return false; |
michael@0 | 117 | } |
michael@0 | 118 | |
michael@0 | 119 | bool |
michael@0 | 120 | nsGSettingsCollection::SetValue(const nsACString& aKey, |
michael@0 | 121 | GVariant *aValue) |
michael@0 | 122 | { |
michael@0 | 123 | if (!KeyExists(aKey) || |
michael@0 | 124 | !g_settings_range_check(mSettings, |
michael@0 | 125 | PromiseFlatCString(aKey).get(), |
michael@0 | 126 | aValue)) { |
michael@0 | 127 | g_variant_unref(aValue); |
michael@0 | 128 | return false; |
michael@0 | 129 | } |
michael@0 | 130 | |
michael@0 | 131 | return g_settings_set_value(mSettings, |
michael@0 | 132 | PromiseFlatCString(aKey).get(), |
michael@0 | 133 | aValue); |
michael@0 | 134 | } |
michael@0 | 135 | |
michael@0 | 136 | NS_IMPL_ISUPPORTS(nsGSettingsCollection, nsIGSettingsCollection) |
michael@0 | 137 | |
michael@0 | 138 | NS_IMETHODIMP |
michael@0 | 139 | nsGSettingsCollection::SetString(const nsACString& aKey, |
michael@0 | 140 | const nsACString& aValue) |
michael@0 | 141 | { |
michael@0 | 142 | GVariant *value = g_variant_new_string(PromiseFlatCString(aValue).get()); |
michael@0 | 143 | if (!value) |
michael@0 | 144 | return NS_ERROR_OUT_OF_MEMORY; |
michael@0 | 145 | |
michael@0 | 146 | bool res = SetValue(aKey, value); |
michael@0 | 147 | |
michael@0 | 148 | return res ? NS_OK : NS_ERROR_FAILURE; |
michael@0 | 149 | } |
michael@0 | 150 | |
michael@0 | 151 | NS_IMETHODIMP |
michael@0 | 152 | nsGSettingsCollection::SetBoolean(const nsACString& aKey, |
michael@0 | 153 | bool aValue) |
michael@0 | 154 | { |
michael@0 | 155 | GVariant *value = g_variant_new_boolean(aValue); |
michael@0 | 156 | if (!value) |
michael@0 | 157 | return NS_ERROR_OUT_OF_MEMORY; |
michael@0 | 158 | |
michael@0 | 159 | bool res = SetValue(aKey, value); |
michael@0 | 160 | |
michael@0 | 161 | return res ? NS_OK : NS_ERROR_FAILURE; |
michael@0 | 162 | } |
michael@0 | 163 | |
michael@0 | 164 | NS_IMETHODIMP |
michael@0 | 165 | nsGSettingsCollection::SetInt(const nsACString& aKey, |
michael@0 | 166 | int32_t aValue) |
michael@0 | 167 | { |
michael@0 | 168 | GVariant *value = g_variant_new_int32(aValue); |
michael@0 | 169 | if (!value) |
michael@0 | 170 | return NS_ERROR_OUT_OF_MEMORY; |
michael@0 | 171 | |
michael@0 | 172 | bool res = SetValue(aKey, value); |
michael@0 | 173 | |
michael@0 | 174 | return res ? NS_OK : NS_ERROR_FAILURE; |
michael@0 | 175 | } |
michael@0 | 176 | |
michael@0 | 177 | NS_IMETHODIMP |
michael@0 | 178 | nsGSettingsCollection::GetString(const nsACString& aKey, |
michael@0 | 179 | nsACString& aResult) |
michael@0 | 180 | { |
michael@0 | 181 | if (!KeyExists(aKey)) |
michael@0 | 182 | return NS_ERROR_INVALID_ARG; |
michael@0 | 183 | |
michael@0 | 184 | GVariant *value = g_settings_get_value(mSettings, |
michael@0 | 185 | PromiseFlatCString(aKey).get()); |
michael@0 | 186 | if (!g_variant_is_of_type(value, G_VARIANT_TYPE_STRING) && |
michael@0 | 187 | !g_variant_is_of_type(value, G_VARIANT_TYPE_OBJECT_PATH) && |
michael@0 | 188 | !g_variant_is_of_type(value, G_VARIANT_TYPE_SIGNATURE)) { |
michael@0 | 189 | g_variant_unref(value); |
michael@0 | 190 | return NS_ERROR_FAILURE; |
michael@0 | 191 | } |
michael@0 | 192 | |
michael@0 | 193 | aResult.Assign(g_variant_get_string(value, nullptr)); |
michael@0 | 194 | g_variant_unref(value); |
michael@0 | 195 | |
michael@0 | 196 | return NS_OK; |
michael@0 | 197 | } |
michael@0 | 198 | |
michael@0 | 199 | NS_IMETHODIMP |
michael@0 | 200 | nsGSettingsCollection::GetBoolean(const nsACString& aKey, |
michael@0 | 201 | bool* aResult) |
michael@0 | 202 | { |
michael@0 | 203 | NS_ENSURE_ARG_POINTER(aResult); |
michael@0 | 204 | |
michael@0 | 205 | if (!KeyExists(aKey)) |
michael@0 | 206 | return NS_ERROR_INVALID_ARG; |
michael@0 | 207 | |
michael@0 | 208 | GVariant *value = g_settings_get_value(mSettings, |
michael@0 | 209 | PromiseFlatCString(aKey).get()); |
michael@0 | 210 | if (!g_variant_is_of_type(value, G_VARIANT_TYPE_BOOLEAN)) { |
michael@0 | 211 | g_variant_unref(value); |
michael@0 | 212 | return NS_ERROR_FAILURE; |
michael@0 | 213 | } |
michael@0 | 214 | |
michael@0 | 215 | gboolean res = g_variant_get_boolean(value); |
michael@0 | 216 | *aResult = res ? true : false; |
michael@0 | 217 | g_variant_unref(value); |
michael@0 | 218 | |
michael@0 | 219 | return NS_OK; |
michael@0 | 220 | } |
michael@0 | 221 | |
michael@0 | 222 | NS_IMETHODIMP |
michael@0 | 223 | nsGSettingsCollection::GetInt(const nsACString& aKey, |
michael@0 | 224 | int32_t* aResult) |
michael@0 | 225 | { |
michael@0 | 226 | NS_ENSURE_ARG_POINTER(aResult); |
michael@0 | 227 | |
michael@0 | 228 | if (!KeyExists(aKey)) |
michael@0 | 229 | return NS_ERROR_INVALID_ARG; |
michael@0 | 230 | |
michael@0 | 231 | GVariant *value = g_settings_get_value(mSettings, |
michael@0 | 232 | PromiseFlatCString(aKey).get()); |
michael@0 | 233 | if (!g_variant_is_of_type(value, G_VARIANT_TYPE_INT32)) { |
michael@0 | 234 | g_variant_unref(value); |
michael@0 | 235 | return NS_ERROR_FAILURE; |
michael@0 | 236 | } |
michael@0 | 237 | |
michael@0 | 238 | *aResult = g_variant_get_int32(value); |
michael@0 | 239 | g_variant_unref(value); |
michael@0 | 240 | |
michael@0 | 241 | return NS_OK; |
michael@0 | 242 | } |
michael@0 | 243 | |
michael@0 | 244 | // These types are local to nsGSettingsService::Init, but ISO C++98 doesn't |
michael@0 | 245 | // allow a template (ArrayLength) to be instantiated based on a local type. |
michael@0 | 246 | // Boo-urns! |
michael@0 | 247 | typedef void (*nsGSettingsFunc)(); |
michael@0 | 248 | struct nsGSettingsDynamicFunction { |
michael@0 | 249 | const char *functionName; |
michael@0 | 250 | nsGSettingsFunc *function; |
michael@0 | 251 | }; |
michael@0 | 252 | |
michael@0 | 253 | NS_IMETHODIMP |
michael@0 | 254 | nsGSettingsCollection::GetStringList(const nsACString& aKey, nsIArray** aResult) |
michael@0 | 255 | { |
michael@0 | 256 | if (!KeyExists(aKey)) |
michael@0 | 257 | return NS_ERROR_INVALID_ARG; |
michael@0 | 258 | |
michael@0 | 259 | nsCOMPtr<nsIMutableArray> items(do_CreateInstance(NS_ARRAY_CONTRACTID)); |
michael@0 | 260 | if (!items) { |
michael@0 | 261 | return NS_ERROR_OUT_OF_MEMORY; |
michael@0 | 262 | } |
michael@0 | 263 | |
michael@0 | 264 | GVariant *value = g_settings_get_value(mSettings, |
michael@0 | 265 | PromiseFlatCString(aKey).get()); |
michael@0 | 266 | |
michael@0 | 267 | if (!g_variant_is_of_type(value, G_VARIANT_TYPE_STRING_ARRAY)) { |
michael@0 | 268 | g_variant_unref(value); |
michael@0 | 269 | return NS_ERROR_FAILURE; |
michael@0 | 270 | } |
michael@0 | 271 | |
michael@0 | 272 | const gchar ** gs_strings = g_variant_get_strv(value, nullptr); |
michael@0 | 273 | if (!gs_strings) { |
michael@0 | 274 | // empty array |
michael@0 | 275 | NS_ADDREF(*aResult = items); |
michael@0 | 276 | g_variant_unref(value); |
michael@0 | 277 | return NS_OK; |
michael@0 | 278 | } |
michael@0 | 279 | |
michael@0 | 280 | const gchar** p_gs_strings = gs_strings; |
michael@0 | 281 | while (*p_gs_strings != nullptr) |
michael@0 | 282 | { |
michael@0 | 283 | nsCOMPtr<nsISupportsCString> obj(do_CreateInstance(NS_SUPPORTS_CSTRING_CONTRACTID)); |
michael@0 | 284 | if (obj) { |
michael@0 | 285 | obj->SetData(nsDependentCString(*p_gs_strings)); |
michael@0 | 286 | items->AppendElement(obj, false); |
michael@0 | 287 | } |
michael@0 | 288 | p_gs_strings++; |
michael@0 | 289 | } |
michael@0 | 290 | g_free(gs_strings); |
michael@0 | 291 | NS_ADDREF(*aResult = items); |
michael@0 | 292 | g_variant_unref(value); |
michael@0 | 293 | return NS_OK; |
michael@0 | 294 | } |
michael@0 | 295 | |
michael@0 | 296 | nsresult |
michael@0 | 297 | nsGSettingsService::Init() |
michael@0 | 298 | { |
michael@0 | 299 | #define FUNC(name, type, params) { #name, (nsGSettingsFunc *)&_##name }, |
michael@0 | 300 | static const nsGSettingsDynamicFunction kGSettingsSymbols[] = { |
michael@0 | 301 | GSETTINGS_FUNCTIONS |
michael@0 | 302 | }; |
michael@0 | 303 | #undef FUNC |
michael@0 | 304 | |
michael@0 | 305 | if (!gioLib) { |
michael@0 | 306 | gioLib = PR_LoadLibrary("libgio-2.0.so.0"); |
michael@0 | 307 | if (!gioLib) |
michael@0 | 308 | return NS_ERROR_FAILURE; |
michael@0 | 309 | } |
michael@0 | 310 | |
michael@0 | 311 | for (uint32_t i = 0; i < ArrayLength(kGSettingsSymbols); i++) { |
michael@0 | 312 | *kGSettingsSymbols[i].function = |
michael@0 | 313 | PR_FindFunctionSymbol(gioLib, kGSettingsSymbols[i].functionName); |
michael@0 | 314 | if (!*kGSettingsSymbols[i].function) { |
michael@0 | 315 | return NS_ERROR_FAILURE; |
michael@0 | 316 | } |
michael@0 | 317 | } |
michael@0 | 318 | |
michael@0 | 319 | return NS_OK; |
michael@0 | 320 | } |
michael@0 | 321 | |
michael@0 | 322 | NS_IMPL_ISUPPORTS(nsGSettingsService, nsIGSettingsService) |
michael@0 | 323 | |
michael@0 | 324 | nsGSettingsService::~nsGSettingsService() |
michael@0 | 325 | { |
michael@0 | 326 | if (gioLib) { |
michael@0 | 327 | PR_UnloadLibrary(gioLib); |
michael@0 | 328 | gioLib = nullptr; |
michael@0 | 329 | } |
michael@0 | 330 | } |
michael@0 | 331 | |
michael@0 | 332 | NS_IMETHODIMP |
michael@0 | 333 | nsGSettingsService::GetCollectionForSchema(const nsACString& schema, |
michael@0 | 334 | nsIGSettingsCollection** collection) |
michael@0 | 335 | { |
michael@0 | 336 | NS_ENSURE_ARG_POINTER(collection); |
michael@0 | 337 | |
michael@0 | 338 | const char * const *schemas = g_settings_list_schemas(); |
michael@0 | 339 | |
michael@0 | 340 | for (uint32_t i = 0; schemas[i] != nullptr; i++) { |
michael@0 | 341 | if (schema.Equals(schemas[i])) { |
michael@0 | 342 | GSettings *settings = g_settings_new(PromiseFlatCString(schema).get()); |
michael@0 | 343 | nsGSettingsCollection *mozGSettings = new nsGSettingsCollection(settings); |
michael@0 | 344 | NS_ADDREF(*collection = mozGSettings); |
michael@0 | 345 | return NS_OK; |
michael@0 | 346 | } |
michael@0 | 347 | } |
michael@0 | 348 | |
michael@0 | 349 | return NS_ERROR_FAILURE; |
michael@0 | 350 | } |