summaryrefslogtreecommitdiffstats
path: root/services/sync/tps/extensions/tps/resource/modules/prefs.sys.mjs
blob: 9f6c423a4092f61f5e9b81c92df09e80bda76923 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

/* This is a JavaScript module (JSM) to be imported via
   ChromeUtils.import() and acts as a singleton.
   Only the following listed symbols will exposed on import, and only when
   and where imported. */

const WEAVE_PREF_PREFIX = "services.sync.prefs.sync.";

import { Logger } from "resource://tps/logger.sys.mjs";

/**
 * Preference class constructor
 *
 * Initializes instance properties.
 */
export function Preference(props) {
  Logger.AssertTrue(
    "name" in props && "value" in props,
    "Preference must have both name and value"
  );

  this.name = props.name;
  this.value = props.value;
}

/**
 * Preference instance methods
 */
Preference.prototype = {
  /**
   * Modify
   *
   * Sets the value of the preference this.name to this.value.
   * Throws on error.
   *
   * @return nothing
   */
  Modify() {
    // Determine if this pref is actually something Weave even looks at.
    let weavepref = WEAVE_PREF_PREFIX + this.name;
    try {
      let syncPref = Services.prefs.getBoolPref(weavepref);
      if (!syncPref) {
        Services.prefs.setBoolPref(weavepref, true);
      }
    } catch (e) {
      Logger.AssertTrue(false, "Weave doesn't sync pref " + this.name);
    }

    // Modify the pref; throw an exception if the pref type is different
    // than the value type specified in the test.
    let prefType = Services.prefs.getPrefType(this.name);
    switch (prefType) {
      case Ci.nsIPrefBranch.PREF_INT:
        Logger.AssertEqual(
          typeof this.value,
          "number",
          "Wrong type used for preference value"
        );
        Services.prefs.setIntPref(this.name, this.value);
        break;
      case Ci.nsIPrefBranch.PREF_STRING:
        Logger.AssertEqual(
          typeof this.value,
          "string",
          "Wrong type used for preference value"
        );
        Services.prefs.setCharPref(this.name, this.value);
        break;
      case Ci.nsIPrefBranch.PREF_BOOL:
        Logger.AssertEqual(
          typeof this.value,
          "boolean",
          "Wrong type used for preference value"
        );
        Services.prefs.setBoolPref(this.name, this.value);
        break;
    }
  },

  /**
   * Find
   *
   * Verifies that the preference this.name has the value
   * this.value. Throws on error, or if the pref's type or value
   * doesn't match.
   *
   * @return nothing
   */
  Find() {
    // Read the pref value.
    let value;
    try {
      let prefType = Services.prefs.getPrefType(this.name);
      switch (prefType) {
        case Ci.nsIPrefBranch.PREF_INT:
          value = Services.prefs.getIntPref(this.name);
          break;
        case Ci.nsIPrefBranch.PREF_STRING:
          value = Services.prefs.getCharPref(this.name);
          break;
        case Ci.nsIPrefBranch.PREF_BOOL:
          value = Services.prefs.getBoolPref(this.name);
          break;
      }
    } catch (e) {
      Logger.AssertTrue(false, "Error accessing pref " + this.name);
    }

    // Throw an exception if the current and expected values aren't of
    // the same type, or don't have the same values.
    Logger.AssertEqual(
      typeof value,
      typeof this.value,
      "Value types don't match"
    );
    Logger.AssertEqual(value, this.value, "Preference values don't match");
  },
};