toolkit/components/contentprefs/ContentPrefStore.jsm

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/components/contentprefs/ContentPrefStore.jsm	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,124 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this file,
     1.6 + * You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +let EXPORTED_SYMBOLS = [
     1.9 +  "ContentPrefStore",
    1.10 +];
    1.11 +
    1.12 +function ContentPrefStore() {
    1.13 +  this._groups = {};
    1.14 +  this._globalNames = {};
    1.15 +}
    1.16 +
    1.17 +ContentPrefStore.prototype = {
    1.18 +
    1.19 +  set: function CPS_set(group, name, val) {
    1.20 +    if (group) {
    1.21 +      if (!this._groups.hasOwnProperty(group))
    1.22 +        this._groups[group] = {};
    1.23 +      this._groups[group][name] = val;
    1.24 +    }
    1.25 +    else {
    1.26 +      this._globalNames[name] = val;
    1.27 +    }
    1.28 +  },
    1.29 +
    1.30 +  setWithCast: function CPS_setWithCast(group, name, val) {
    1.31 +    if (typeof(val) == "boolean")
    1.32 +      val = val ? 1 : 0;
    1.33 +    else if (val === undefined)
    1.34 +      val = null;
    1.35 +    this.set(group, name, val);
    1.36 +  },
    1.37 +
    1.38 +  has: function CPS_has(group, name) {
    1.39 +    return (group &&
    1.40 +            this._groups.hasOwnProperty(group) &&
    1.41 +            this._groups[group].hasOwnProperty(name)) ||
    1.42 +           (!group &&
    1.43 +            this._globalNames.hasOwnProperty(name));
    1.44 +  },
    1.45 +
    1.46 +  get: function CPS_get(group, name) {
    1.47 +    if (group) {
    1.48 +      if (this._groups.hasOwnProperty(group) &&
    1.49 +          this._groups[group].hasOwnProperty(name))
    1.50 +        return this._groups[group][name];
    1.51 +    }
    1.52 +    else if (this._globalNames.hasOwnProperty(name)) {
    1.53 +      return this._globalNames[name];
    1.54 +    }
    1.55 +    return undefined;
    1.56 +  },
    1.57 +
    1.58 +  remove: function CPS_remove(group, name) {
    1.59 +    if (group) {
    1.60 +      if (this._groups.hasOwnProperty(group)) {
    1.61 +        delete this._groups[group][name];
    1.62 +        if (!Object.keys(this._groups[group]).length)
    1.63 +          delete this._groups[group];
    1.64 +      }
    1.65 +    }
    1.66 +    else {
    1.67 +      delete this._globalNames[name];
    1.68 +    }
    1.69 +  },
    1.70 +
    1.71 +  removeGroup: function CPS_removeGroup(group) {
    1.72 +    if (group) {
    1.73 +      delete this._groups[group];
    1.74 +    }
    1.75 +    else {
    1.76 +      this._globalNames = {};
    1.77 +    }
    1.78 +  },
    1.79 +
    1.80 +  removeAllGroups: function CPS_removeAllGroups() {
    1.81 +    this._groups = {};
    1.82 +  },
    1.83 +
    1.84 +  removeAll: function CPS_removeAll() {
    1.85 +    this.removeAllGroups();
    1.86 +    this._globalNames = {};
    1.87 +  },
    1.88 +
    1.89 +  __iterator__: function CPS___iterator__() {
    1.90 +    for (let [group, names] in Iterator(this._groups)) {
    1.91 +      for (let [name, val] in Iterator(names)) {
    1.92 +        yield [group, name, val];
    1.93 +      }
    1.94 +    }
    1.95 +    for (let [name, val] in Iterator(this._globalNames)) {
    1.96 +      yield [null, name, val];
    1.97 +    }
    1.98 +  },
    1.99 +
   1.100 +  match: function CPS_match(group, name, includeSubdomains) {
   1.101 +    for (let sgroup in this.matchGroups(group, includeSubdomains)) {
   1.102 +      if (this.has(sgroup, name))
   1.103 +        yield [sgroup, this.get(sgroup, name)];
   1.104 +    }
   1.105 +  },
   1.106 +
   1.107 +  matchGroups: function CPS_matchGroups(group, includeSubdomains) {
   1.108 +    if (group) {
   1.109 +      if (includeSubdomains) {
   1.110 +        for (let [sgroup, , ] in this) {
   1.111 +          if (sgroup) {
   1.112 +            let idx = sgroup.indexOf(group);
   1.113 +            if (idx == sgroup.length - group.length &&
   1.114 +                (idx == 0 || sgroup[idx - 1] == "."))
   1.115 +              yield sgroup;
   1.116 +          }
   1.117 +        }
   1.118 +      }
   1.119 +      else if (this._groups.hasOwnProperty(group)) {
   1.120 +        yield group;
   1.121 +      }
   1.122 +    }
   1.123 +    else if (Object.keys(this._globalNames).length) {
   1.124 +      yield null;
   1.125 +    }
   1.126 +  },
   1.127 +};

mercurial