addon-sdk/source/lib/sdk/preferences/event-target.js

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.

     1 /* This Source Code Form is subject to the terms of the Mozilla Public
     2  * License, v. 2.0. If a copy of the MPL was not distributed with this
     3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     4 'use strict';
     6 module.metadata = {
     7   "stability": "unstable"
     8 };
    10 const { Cc, Ci } = require('chrome');
    11 const { Class } = require('../core/heritage');
    12 const { EventTarget } = require('../event/target');
    13 const { Branch } = require('./service');
    14 const { emit, off } = require('../event/core');
    15 const { when: unload } = require('../system/unload');
    17 const prefTargetNS = require('../core/namespace').ns();
    19 const PrefsTarget = Class({
    20   extends: EventTarget,
    21   initialize: function(options) {
    22     options = options || {};
    23     EventTarget.prototype.initialize.call(this, options);
    25     let branchName = options.branchName || '';
    26     let branch = Cc["@mozilla.org/preferences-service;1"].
    27         getService(Ci.nsIPrefService).
    28         getBranch(branchName).
    29         QueryInterface(Ci.nsIPrefBranch2);
    30     prefTargetNS(this).branch = branch;
    32     // provides easy access to preference values
    33     this.prefs = Branch(branchName);
    35     // start listening to preference changes
    36     let observer = prefTargetNS(this).observer = onChange.bind(this);
    37     branch.addObserver('', observer, false);
    39     // Make sure to destroy this on unload
    40     unload(destroy.bind(this));
    41   }
    42 });
    43 exports.PrefsTarget = PrefsTarget;
    45 /* HELPERS */
    47 function onChange(subject, topic, name) {
    48   if (topic === 'nsPref:changed') {
    49     emit(this, name, name);
    50     emit(this, '', name);
    51   }
    52 }
    54 function destroy() {
    55   off(this);
    57   // stop listening to preference changes
    58   let branch = prefTargetNS(this).branch;
    59   branch.removeObserver('', prefTargetNS(this).observer, false);
    60   prefTargetNS(this).observer = null;
    61 }

mercurial