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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
|
/* 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/. */
var EXPORTED_SYMBOLS = ["SearchSettings"];
const { XPCOMUtils } = ChromeUtils.import(
"resource://gre/modules/XPCOMUtils.jsm"
);
XPCOMUtils.defineLazyModuleGetters(this, {
DeferredTask: "resource://gre/modules/DeferredTask.jsm",
OS: "resource://gre/modules/osfile.jsm",
SearchUtils: "resource://gre/modules/SearchUtils.jsm",
Services: "resource://gre/modules/Services.jsm",
});
XPCOMUtils.defineLazyGetter(this, "logConsole", () => {
return console.createInstance({
prefix: "SearchSettings",
maxLogLevel: SearchUtils.loggingEnabled ? "Debug" : "Warn",
});
});
// A text encoder to UTF8, used whenever we commit the settings to disk.
XPCOMUtils.defineLazyGetter(this, "gEncoder", function() {
return new TextEncoder();
});
const SETTINGS_FILENAME = "search.json.mozlz4";
/**
* This class manages the saves search settings.
*
* Global settings can be saved and obtained from this class via the
* `*Attribute` methods.
*/
class SearchSettings {
constructor(searchService) {
this._searchService = searchService;
}
QueryInterface = ChromeUtils.generateQI([Ci.nsIObserver]);
// Delay for batching invalidation of the JSON settings (ms)
static SETTINGS_INVALIDATION_DELAY = 1000;
/**
* A reference to the pending DeferredTask, if there is one.
*/
_batchTask = null;
/**
* The current metadata stored in the settings. This stores:
* - current
* The current user-set default engine. The associated hash is called
* 'hash'.
* - private
* The current user-set private engine. The associated hash is called
* 'privateHash'.
*
* All of the above have associated hash fields to validate the value is set
* by the application.
*/
_metaData = {};
/**
* A reference to the search service so that we can save the engines list.
*/
_searchService = null;
/*
* A copy of the settings so we can persist metadata for engines that
* are not currently active.
*/
_currentSettings = null;
addObservers() {
Services.obs.addObserver(this, SearchUtils.TOPIC_ENGINE_MODIFIED);
Services.obs.addObserver(this, SearchUtils.TOPIC_SEARCH_SERVICE);
}
/**
* Cleans up, removing observers.
*/
removeObservers() {
Services.obs.removeObserver(this, SearchUtils.TOPIC_ENGINE_MODIFIED);
Services.obs.removeObserver(this, SearchUtils.TOPIC_SEARCH_SERVICE);
}
/**
* Reads the settings file.
*
* @param {string} origin
* If this parameter is "test", then the settings will not be written. As
* some tests manipulate the settings directly, we allow turning off writing to
* avoid writing stale settings data.
* @returns {object}
* Returns the settings file data.
*/
async get(origin = "") {
let json;
await this._ensurePendingWritesCompleted(origin);
try {
let settingsFilePath = OS.Path.join(
OS.Constants.Path.profileDir,
SETTINGS_FILENAME
);
let bytes = await OS.File.read(settingsFilePath, { compression: "lz4" });
json = JSON.parse(new TextDecoder().decode(bytes));
if (!json.engines || !json.engines.length) {
throw new Error("no engine in the file");
}
} catch (ex) {
logConsole.warn("get: No settings file exists, new profile?", ex);
json = {};
}
if (json.metaData) {
this._metaData = json.metaData;
}
// Versions of gecko older than 82 stored the order flag as a preference.
// This was changed in version 6 of the settings file.
if (json.version < 6 || !("useSavedOrder" in this._metaData)) {
const prefName = SearchUtils.BROWSER_SEARCH_PREF + "useDBForOrder";
let useSavedOrder = Services.prefs.getBoolPref(prefName, false);
this.setAttribute("useSavedOrder", useSavedOrder);
// Clear the old pref so it isn't lying around.
Services.prefs.clearUserPref(prefName);
}
this._currentSettings = json;
return json;
}
/**
* Queues writing the settings until after SETTINGS_INVALIDATION_DELAY. If there
* is a currently queued task then it will be restarted.
*/
_delayedWrite() {
if (this._batchTask) {
this._batchTask.disarm();
} else {
let task = async () => {
if (
!this._searchService.isInitialized ||
this._searchService._reloadingEngines
) {
// Re-arm the task as we don't want to save potentially incomplete
// information during the middle of (re-)initializing.
this._batchTask.arm();
return;
}
logConsole.debug("batchTask: Invalidating engine settings");
await this._write();
};
this._batchTask = new DeferredTask(
task,
SearchSettings.SETTINGS_INVALIDATION_DELAY
);
}
this._batchTask.arm();
}
/**
* Ensures any pending writes of the settings are completed.
*
* @param {string} origin
* If this parameter is "test", then the settings will not be written. As
* some tests manipulate the settings directly, we allow turning off writing to
* avoid writing stale settings data.
*/
async _ensurePendingWritesCompleted(origin = "") {
// Before we read the settings file, first make sure all pending tasks are clear.
if (!this._batchTask) {
return;
}
logConsole.debug("finalizing batch task");
let task = this._batchTask;
this._batchTask = null;
// Tests manipulate the settings directly, so let's not double-write with
// stale settings data here.
if (origin == "test") {
task.disarm();
} else {
await task.finalize();
}
}
/**
* Writes the settings to disk (no delay).
*/
async _write() {
if (this._batchTask) {
this._batchTask.disarm();
}
let settings = {};
// Allows us to force a settings refresh should the settings format change.
settings.version = SearchUtils.SETTINGS_VERSION;
settings.engines = [...this._searchService._engines.values()];
settings.metaData = this._metaData;
// Persist metadata for AppProvided engines even if they aren't currently
// active, this means if they become active again their settings
// will be restored.
if (this._currentSettings?.engines) {
for (let engine of this._currentSettings.engines) {
let included = settings.engines.some(e => e._name == engine._name);
if (engine._isAppProvided && !included) {
settings.engines.push(engine);
}
}
}
// Update the local copy.
this._currentSettings = settings;
try {
if (!settings.engines.length) {
throw new Error("cannot write without any engine.");
}
logConsole.debug("_write: Writing to settings file.");
let path = OS.Path.join(OS.Constants.Path.profileDir, SETTINGS_FILENAME);
let data = gEncoder.encode(JSON.stringify(settings));
await OS.File.writeAtomic(path, data, {
compression: "lz4",
tmpPath: path + ".tmp",
});
logConsole.debug("_write: settings file written to disk.");
Services.obs.notifyObservers(
null,
SearchUtils.TOPIC_SEARCH_SERVICE,
"write-settings-to-disk-complete"
);
} catch (ex) {
logConsole.error("_write: Could not write to settings file:", ex);
}
}
/**
* Sets an attribute without verification.
*
* @param {string} name
* The name of the attribute to set.
* @param {*} val
* The value to set.
*/
setAttribute(name, val) {
this._metaData[name] = val;
this._delayedWrite();
}
/**
* Sets a verified attribute. This will save an additional hash
* value, that can be verified when reading back.
*
* @param {string} name
* The name of the attribute to set.
* @param {*} val
* The value to set.
*/
setVerifiedAttribute(name, val) {
this._metaData[name] = val;
this._metaData[this.getHashName(name)] = SearchUtils.getVerificationHash(
val
);
this._delayedWrite();
}
/**
* Gets an attribute without verification.
*
* @param {string} name
* The name of the attribute to get.
* @returns {*}
* The value of the attribute, or undefined if not known.
*/
getAttribute(name) {
return this._metaData[name] ?? undefined;
}
/**
* Gets a verified attribute.
*
* @param {string} name
* The name of the attribute to get.
* @returns {*}
* The value of the attribute, or undefined if not known or an empty strings
* if it does not match the verification hash.
*/
getVerifiedAttribute(name) {
let val = this.getAttribute(name);
if (
val &&
this.getAttribute(this.getHashName(name)) !=
SearchUtils.getVerificationHash(val)
) {
logConsole.warn("getVerifiedGlobalAttr, invalid hash for", name);
return undefined;
}
return val;
}
/**
* Returns the name for the hash for a particular attribute. This is
* necessary because the normal default engine is named `current` with
* its hash as `hash`. All other hashes are in the `<name>Hash` format.
*
* @param {string} name
* The name of the attribute to get the hash name for.
* @returns {string}
* The hash name to use.
*/
getHashName(name) {
if (name == "current") {
return "hash";
}
return name + "Hash";
}
/**
* Handles shutdown; writing the settings if necessary.
*
* @param {object} state
* The shutdownState object that is used to help analyzing the shutdown
* state in case of a crash or shutdown timeout.
*/
async shutdown(state) {
if (!this._batchTask) {
return;
}
state.step = "Finalizing batched task";
try {
await this._batchTask.finalize();
state.step = "Batched task finalized";
} catch (ex) {
state.step = "Batched task failed to finalize";
state.latestError.message = "" + ex;
if (ex && typeof ex == "object") {
state.latestError.stack = ex.stack || undefined;
}
}
}
// nsIObserver
observe(engine, topic, verb) {
switch (topic) {
case SearchUtils.TOPIC_ENGINE_MODIFIED:
switch (verb) {
case SearchUtils.MODIFIED_TYPE.ADDED:
case SearchUtils.MODIFIED_TYPE.CHANGED:
case SearchUtils.MODIFIED_TYPE.REMOVED:
this._delayedWrite();
break;
}
break;
case SearchUtils.TOPIC_SEARCH_SERVICE:
switch (verb) {
case "init-complete":
case "engines-reloaded":
this._delayedWrite();
break;
}
break;
}
}
}
|