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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
|
/* 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/. */
"use strict";
const {Cc, Ci} = require("chrome");
const protocol = require("devtools/shared/protocol");
const {DebuggerServer} = require("devtools/server/main");
const promise = require("promise");
const Services = require("Services");
const { settingsSpec } = require("devtools/shared/specs/settings");
const { FileUtils} = require("resource://gre/modules/FileUtils.jsm");
const { NetUtil} = require("resource://gre/modules/NetUtil.jsm");
var defaultSettings = {};
var settingsFile;
exports.register = function (handle) {
handle.addGlobalActor(SettingsActor, "settingsActor");
};
exports.unregister = function (handle) {
};
function getDefaultSettings() {
let chan = NetUtil.newChannel({
uri: NetUtil.newURI(settingsFile),
loadUsingSystemPrincipal: true});
let stream = chan.open2();
// Obtain a converter to read from a UTF-8 encoded input stream.
let converter = Cc["@mozilla.org/intl/scriptableunicodeconverter"]
.createInstance(Ci.nsIScriptableUnicodeConverter);
converter.charset = "UTF-8";
let rawstr = converter.ConvertToUnicode(NetUtil.readInputStreamToString(
stream,
stream.available()) || "");
try {
defaultSettings = JSON.parse(rawstr);
} catch (e) { }
stream.close();
}
function loadSettingsFile() {
// Loading resource://app/defaults/settings.json doesn't work because
// settings.json is not in the omnijar.
// So we look for the app dir instead and go from here...
if (settingsFile) {
return;
}
settingsFile = FileUtils.getFile("DefRt", ["settings.json"], false);
if (!settingsFile || (settingsFile && !settingsFile.exists())) {
// On b2g desktop builds the settings.json file is moved in the
// profile directory by the build system.
settingsFile = FileUtils.getFile("ProfD", ["settings.json"], false);
if (!settingsFile || (settingsFile && !settingsFile.exists())) {
console.log("settings.json file does not exist");
}
}
if (settingsFile.exists()) {
getDefaultSettings();
}
}
var SettingsActor = exports.SettingsActor = protocol.ActorClassWithSpec(settingsSpec, {
_getSettingsService: function () {
let win = Services.wm.getMostRecentWindow(DebuggerServer.chromeWindowType);
return win.navigator.mozSettings;
},
getSetting: function (name) {
let deferred = promise.defer();
let lock = this._getSettingsService().createLock();
let req = lock.get(name);
req.onsuccess = function () {
deferred.resolve(req.result[name]);
};
req.onerror = function () {
deferred.reject(req.error);
};
return deferred.promise;
},
setSetting: function (name, value) {
let deferred = promise.defer();
let data = {};
data[name] = value;
let lock = this._getSettingsService().createLock();
let req = lock.set(data);
req.onsuccess = function () {
deferred.resolve(true);
};
req.onerror = function () {
deferred.reject(req.error);
};
return deferred.promise;
},
_hasUserSetting: function (name, value) {
if (typeof value === "object") {
return JSON.stringify(defaultSettings[name]) !== JSON.stringify(value);
}
return (defaultSettings[name] !== value);
},
getAllSettings: function () {
loadSettingsFile();
let settings = {};
let self = this;
let deferred = promise.defer();
let lock = this._getSettingsService().createLock();
let req = lock.get("*");
req.onsuccess = function () {
for (var name in req.result) {
settings[name] = {
value: req.result[name],
hasUserValue: self._hasUserSetting(name, req.result[name])
};
}
deferred.resolve(settings);
};
req.onfailure = function () {
deferred.reject(req.error);
};
return deferred.promise;
},
clearUserSetting: function (name) {
loadSettingsFile();
try {
this.setSetting(name, defaultSettings[name]);
} catch (e) {
console.log(e);
}
}
});
// For tests
exports._setDefaultSettings = function (settings) {
defaultSettings = settings || {};
};
|