Wed, 31 Dec 2014 06:09:35 +0100
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 |
michael@0 | 4 | * file, 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 | function ItemPinHelper(aUnpinnedPrefName) { |
michael@0 | 9 | this._prefKey = aUnpinnedPrefName; |
michael@0 | 10 | } |
michael@0 | 11 | |
michael@0 | 12 | // Cache preferences on a static variable shared |
michael@0 | 13 | // by all instances registered to the same pref key. |
michael@0 | 14 | ItemPinHelper._prefValue = {}; |
michael@0 | 15 | |
michael@0 | 16 | ItemPinHelper.prototype = { |
michael@0 | 17 | _getPrefValue: function _getPrefValue() { |
michael@0 | 18 | if (ItemPinHelper._prefValue[this._prefKey]) |
michael@0 | 19 | return ItemPinHelper._prefValue[this._prefKey]; |
michael@0 | 20 | |
michael@0 | 21 | try { |
michael@0 | 22 | // getComplexValue throws if pref never set. Really. |
michael@0 | 23 | let prefValue = Services.prefs.getComplexValue(this._prefKey, Ci.nsISupportsString); |
michael@0 | 24 | ItemPinHelper._prefValue[this._prefKey] = JSON.parse(prefValue.data); |
michael@0 | 25 | } catch(e) { |
michael@0 | 26 | ItemPinHelper._prefValue[this._prefKey] = []; |
michael@0 | 27 | } |
michael@0 | 28 | |
michael@0 | 29 | return ItemPinHelper._prefValue[this._prefKey]; |
michael@0 | 30 | }, |
michael@0 | 31 | |
michael@0 | 32 | _setPrefValue: function _setPrefValue(aNewValue) { |
michael@0 | 33 | let stringified = Cc["@mozilla.org/supports-string;1"].createInstance(Ci.nsISupportsString); |
michael@0 | 34 | stringified.data = JSON.stringify(aNewValue); |
michael@0 | 35 | |
michael@0 | 36 | Services.prefs.setComplexValue(this._prefKey, Ci.nsISupportsString, stringified); |
michael@0 | 37 | ItemPinHelper._prefValue[this._prefKey] = aNewValue; |
michael@0 | 38 | }, |
michael@0 | 39 | |
michael@0 | 40 | isPinned: function isPinned(aItemId) { |
michael@0 | 41 | // Bookmarks are visible on StartUI (pinned) by default |
michael@0 | 42 | return this._getPrefValue().indexOf(aItemId) === -1; |
michael@0 | 43 | }, |
michael@0 | 44 | |
michael@0 | 45 | setUnpinned: function setPinned(aItemId) { |
michael@0 | 46 | let unpinned = this._getPrefValue(); |
michael@0 | 47 | unpinned.push(aItemId); |
michael@0 | 48 | this._setPrefValue(unpinned); |
michael@0 | 49 | }, |
michael@0 | 50 | |
michael@0 | 51 | setPinned: function unsetPinned(aItemId) { |
michael@0 | 52 | let unpinned = this._getPrefValue(); |
michael@0 | 53 | |
michael@0 | 54 | let index = unpinned.indexOf(aItemId); |
michael@0 | 55 | unpinned.splice(index, 1); |
michael@0 | 56 | |
michael@0 | 57 | this._setPrefValue(unpinned); |
michael@0 | 58 | }, |
michael@0 | 59 | } |