mobile/android/modules/SharedPreferences.jsm

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 // -*- Mode: js2; tab-width: 2; indent-tabs-mode: nil; js2-basic-offset: 2; js2-skip-preprocessor-directives: t; -*-
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 file,
michael@0 4 * You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 "use strict";
michael@0 7
michael@0 8 this.EXPORTED_SYMBOLS = ["SharedPreferences"];
michael@0 9
michael@0 10 const { classes: Cc, interfaces: Ci, utils: Cu } = Components;
michael@0 11
michael@0 12 // For adding observers.
michael@0 13 Cu.import("resource://gre/modules/Services.jsm");
michael@0 14 Cu.import("resource://gre/modules/Messaging.jsm");
michael@0 15
michael@0 16 /**
michael@0 17 * Create an interface to an Android SharedPreferences branch.
michael@0 18 *
michael@0 19 * branch {String} should be a string describing a preferences branch,
michael@0 20 * like "UpdateService" or "background.data", or null to access the
michael@0 21 * default preferences branch for the application.
michael@0 22 */
michael@0 23 function SharedPreferences(branch) {
michael@0 24 if (!(this instanceof SharedPreferences)) {
michael@0 25 return new SharedPreferences(branch);
michael@0 26 }
michael@0 27 this._branch = branch || null;
michael@0 28 this._observers = {};
michael@0 29 };
michael@0 30
michael@0 31 SharedPreferences.prototype = Object.freeze({
michael@0 32 _set: function _set(prefs) {
michael@0 33 sendMessageToJava({
michael@0 34 type: "SharedPreferences:Set",
michael@0 35 preferences: prefs,
michael@0 36 branch: this._branch,
michael@0 37 });
michael@0 38 },
michael@0 39
michael@0 40 _setOne: function _setOne(prefName, value, type) {
michael@0 41 let prefs = [];
michael@0 42 prefs.push({
michael@0 43 name: prefName,
michael@0 44 value: value,
michael@0 45 type: type,
michael@0 46 });
michael@0 47 this._set(prefs);
michael@0 48 },
michael@0 49
michael@0 50 setBoolPref: function setBoolPref(prefName, value) {
michael@0 51 this._setOne(prefName, value, "bool");
michael@0 52 },
michael@0 53
michael@0 54 setCharPref: function setCharPref(prefName, value) {
michael@0 55 this._setOne(prefName, value, "string");
michael@0 56 },
michael@0 57
michael@0 58 setIntPref: function setIntPref(prefName, value) {
michael@0 59 this._setOne(prefName, value, "int");
michael@0 60 },
michael@0 61
michael@0 62 _get: function _get(prefs, callback) {
michael@0 63 let result = null;
michael@0 64 sendMessageToJava({
michael@0 65 type: "SharedPreferences:Get",
michael@0 66 preferences: prefs,
michael@0 67 branch: this._branch,
michael@0 68 }, (data) => {
michael@0 69 result = data.values;
michael@0 70 });
michael@0 71
michael@0 72 let thread = Services.tm.currentThread;
michael@0 73 while (result == null)
michael@0 74 thread.processNextEvent(true);
michael@0 75
michael@0 76 return result;
michael@0 77 },
michael@0 78
michael@0 79 _getOne: function _getOne(prefName, type) {
michael@0 80 let prefs = [];
michael@0 81 prefs.push({
michael@0 82 name: prefName,
michael@0 83 type: type,
michael@0 84 });
michael@0 85 let values = this._get(prefs);
michael@0 86 if (values.length != 1) {
michael@0 87 throw new Error("Got too many values: " + values.length);
michael@0 88 }
michael@0 89 return values[0].value;
michael@0 90 },
michael@0 91
michael@0 92 getBoolPref: function getBoolPref(prefName) {
michael@0 93 return this._getOne(prefName, "bool");
michael@0 94 },
michael@0 95
michael@0 96 getCharPref: function getCharPref(prefName) {
michael@0 97 return this._getOne(prefName, "string");
michael@0 98 },
michael@0 99
michael@0 100 getIntPref: function getIntPref(prefName) {
michael@0 101 return this._getOne(prefName, "int");
michael@0 102 },
michael@0 103
michael@0 104 /**
michael@0 105 * Invoke `observer` after a change to the preference `domain` in
michael@0 106 * the current branch.
michael@0 107 *
michael@0 108 * `observer` should implement the nsIObserver.observe interface.
michael@0 109 */
michael@0 110 addObserver: function addObserver(domain, observer, holdWeak) {
michael@0 111 if (!domain)
michael@0 112 throw new Error("domain must not be null");
michael@0 113 if (!observer)
michael@0 114 throw new Error("observer must not be null");
michael@0 115 if (holdWeak)
michael@0 116 throw new Error("Weak references not yet implemented.");
michael@0 117
michael@0 118 if (!this._observers.hasOwnProperty(domain))
michael@0 119 this._observers[domain] = [];
michael@0 120 if (this._observers[domain].indexOf(observer) > -1)
michael@0 121 return;
michael@0 122
michael@0 123 this._observers[domain].push(observer);
michael@0 124
michael@0 125 this._updateAndroidListener();
michael@0 126 },
michael@0 127
michael@0 128 /**
michael@0 129 * Do not invoke `observer` after a change to the preference
michael@0 130 * `domain` in the current branch.
michael@0 131 */
michael@0 132 removeObserver: function removeObserver(domain, observer) {
michael@0 133 if (!this._observers.hasOwnProperty(domain))
michael@0 134 return;
michael@0 135 let index = this._observers[domain].indexOf(observer);
michael@0 136 if (index < 0)
michael@0 137 return;
michael@0 138
michael@0 139 this._observers[domain].splice(index, 1);
michael@0 140 if (this._observers[domain].length < 1)
michael@0 141 delete this._observers[domain];
michael@0 142
michael@0 143 this._updateAndroidListener();
michael@0 144 },
michael@0 145
michael@0 146 _updateAndroidListener: function _updateAndroidListener() {
michael@0 147 if (this._listening && Object.keys(this._observers).length < 1)
michael@0 148 this._uninstallAndroidListener();
michael@0 149 if (!this._listening && Object.keys(this._observers).length > 0)
michael@0 150 this._installAndroidListener();
michael@0 151 },
michael@0 152
michael@0 153 _installAndroidListener: function _installAndroidListener() {
michael@0 154 if (this._listening)
michael@0 155 return;
michael@0 156 this._listening = true;
michael@0 157
michael@0 158 Services.obs.addObserver(this, "SharedPreferences:Changed", false);
michael@0 159 sendMessageToJava({
michael@0 160 type: "SharedPreferences:Observe",
michael@0 161 enable: true,
michael@0 162 branch: this._branch,
michael@0 163 });
michael@0 164 },
michael@0 165
michael@0 166 observe: function observe(subject, topic, data) {
michael@0 167 if (topic != "SharedPreferences:Changed") {
michael@0 168 return;
michael@0 169 }
michael@0 170
michael@0 171 let msg = JSON.parse(data);
michael@0 172 if (msg.branch != this._branch) {
michael@0 173 return;
michael@0 174 }
michael@0 175
michael@0 176 if (!this._observers.hasOwnProperty(msg.key)) {
michael@0 177 return;
michael@0 178 }
michael@0 179
michael@0 180 let observers = this._observers[msg.key];
michael@0 181 for (let obs of observers) {
michael@0 182 obs.observe(obs, msg.key, msg.value);
michael@0 183 }
michael@0 184 },
michael@0 185
michael@0 186 _uninstallAndroidListener: function _uninstallAndroidListener() {
michael@0 187 if (!this._listening)
michael@0 188 return;
michael@0 189 this._listening = false;
michael@0 190
michael@0 191 Services.obs.removeObserver(this, "SharedPreferences:Changed");
michael@0 192 sendMessageToJava({
michael@0 193 type: "SharedPreferences:Observe",
michael@0 194 enable: false,
michael@0 195 branch: this._branch,
michael@0 196 });
michael@0 197 },
michael@0 198 });

mercurial