1/* vim: set ts=2 sw=2 sts=2 et tw=80: */
2/* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5
6"use strict";
7
8var EXPORTED_SYMBOLS = [
9  "ContentPref",
10  "cbHandleResult",
11  "cbHandleError",
12  "cbHandleCompletion",
13  "safeCallback",
14  "_methodsCallableFromChild",
15];
16
17function ContentPref(domain, name, value) {
18  this.domain = domain;
19  this.name = name;
20  this.value = value;
21}
22
23ContentPref.prototype = {
24  QueryInterface: ChromeUtils.generateQI(["nsIContentPref"]),
25};
26
27function cbHandleResult(callback, pref) {
28  safeCallback(callback, "handleResult", [pref]);
29}
30
31function cbHandleCompletion(callback, reason) {
32  safeCallback(callback, "handleCompletion", [reason]);
33}
34
35function cbHandleError(callback, nsresult) {
36  safeCallback(callback, "handleError", [nsresult]);
37}
38
39function safeCallback(callbackObj, methodName, args) {
40  if (!callbackObj || typeof callbackObj[methodName] != "function") {
41    return;
42  }
43  try {
44    callbackObj[methodName].apply(callbackObj, args);
45  } catch (err) {
46    Cu.reportError(err);
47  }
48}
49
50const _methodsCallableFromChild = Object.freeze([
51  ["getByName", ["name", "context", "callback"]],
52  ["getByDomainAndName", ["domain", "name", "context", "callback"]],
53  ["getBySubdomainAndName", ["domain", "name", "context", "callback"]],
54  ["getGlobal", ["name", "context", "callback"]],
55  ["set", ["domain", "name", "value", "context", "callback"]],
56  ["setGlobal", ["name", "value", "context", "callback"]],
57  ["removeByDomainAndName", ["domain", "name", "context", "callback"]],
58  ["removeBySubdomainAndName", ["domain", "name", "context", "callback"]],
59  ["removeGlobal", ["name", "context", "callback"]],
60  ["removeByDomain", ["domain", "context", "callback"]],
61  ["removeBySubdomain", ["domain", "context", "callback"]],
62  ["removeByName", ["name", "context", "callback"]],
63  ["removeAllDomains", ["context", "callback"]],
64  ["removeAllGlobals", ["context", "callback"]],
65]);
66