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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
|
/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim: set sts=2 sw=2 et tw=80: */
/* 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";
var EXPORTED_SYMBOLS = ["ExtensionStorage"];
const { XPCOMUtils } = ChromeUtils.importESModule(
"resource://gre/modules/XPCOMUtils.sys.mjs"
);
const lazy = {};
ChromeUtils.defineModuleGetter(
lazy,
"ExtensionUtils",
"resource://gre/modules/ExtensionUtils.jsm"
);
ChromeUtils.defineESModuleGetters(lazy, {
JSONFile: "resource://gre/modules/JSONFile.sys.mjs",
});
ChromeUtils.defineModuleGetter(lazy, "OS", "resource://gre/modules/osfile.jsm");
function isStructuredCloneHolder(value) {
return (
value &&
typeof value === "object" &&
Cu.getClassName(value, true) === "StructuredCloneHolder"
);
}
class SerializeableMap extends Map {
toJSON() {
let result = {};
for (let [key, value] of this) {
if (isStructuredCloneHolder(value)) {
value = value.deserialize(globalThis);
this.set(key, value);
}
result[key] = value;
}
return result;
}
/**
* Like toJSON, but attempts to serialize every value separately, and
* elides any which fail to serialize. Should only be used if initial
* JSON serialization fails.
*
* @returns {object}
*/
toJSONSafe() {
let result = {};
for (let [key, value] of this) {
try {
void JSON.stringify(value);
result[key] = value;
} catch (e) {
Cu.reportError(
new Error(`Failed to serialize browser.storage key "${key}": ${e}`)
);
}
}
return result;
}
}
/**
* Serializes an arbitrary value into a StructuredCloneHolder, if
* appropriate. Existing StructuredCloneHolders are returned unchanged.
* Non-object values are also returned unchanged. Anything else is
* serialized, and a new StructuredCloneHolder returned.
*
* This allows us to avoid a second structured clone operation after
* sending a storage value across a message manager, before cloning it
* into an extension scope.
*
* @param {StructuredCloneHolder|*} value
* A value to serialize.
* @returns {*}
*/
function serialize(value) {
if (value && typeof value === "object" && !isStructuredCloneHolder(value)) {
return new StructuredCloneHolder(value);
}
return value;
}
var ExtensionStorage = {
// Map<extension-id, Promise<JSONFile>>
jsonFilePromises: new Map(),
listeners: new Map(),
/**
* Asynchronously reads the storage file for the given extension ID
* and returns a Promise for its initialized JSONFile object.
*
* @param {string} extensionId
* The ID of the extension for which to return a file.
* @returns {Promise<JSONFile>}
*/
async _readFile(extensionId) {
lazy.OS.File.makeDir(this.getExtensionDir(extensionId), {
ignoreExisting: true,
from: lazy.OS.Constants.Path.profileDir,
});
let jsonFile = new lazy.JSONFile({
path: this.getStorageFile(extensionId),
});
await jsonFile.load();
jsonFile.data = this._serializableMap(jsonFile.data);
return jsonFile;
},
_serializableMap(data) {
return new SerializeableMap(Object.entries(data));
},
/**
* Returns a Promise for initialized JSONFile instance for the
* extension's storage file.
*
* @param {string} extensionId
* The ID of the extension for which to return a file.
* @returns {Promise<JSONFile>}
*/
getFile(extensionId) {
let promise = this.jsonFilePromises.get(extensionId);
if (!promise) {
promise = this._readFile(extensionId);
this.jsonFilePromises.set(extensionId, promise);
}
return promise;
},
/**
* Clear the cached jsonFilePromise for a given extensionId
* (used by ExtensionStorageIDB to free the jsonFile once the data migration
* has been completed).
*
* @param {string} extensionId
* The ID of the extension for which to return a file.
*/
async clearCachedFile(extensionId) {
let promise = this.jsonFilePromises.get(extensionId);
if (promise) {
this.jsonFilePromises.delete(extensionId);
await promise.then(jsonFile => jsonFile.finalize());
}
},
/**
* Sanitizes the given value, and returns a JSON-compatible
* representation of it, based on the privileges of the given global.
*
* @param {value} value
* The value to sanitize.
* @param {Context} context
* The extension context in which to sanitize the value
* @returns {value}
* The sanitized value.
*/
sanitize(value, context) {
let json = context.jsonStringify(value === undefined ? null : value);
if (json == undefined) {
throw new lazy.ExtensionUtils.ExtensionError(
"DataCloneError: The object could not be cloned."
);
}
return JSON.parse(json);
},
/**
* Returns the path to the storage directory within the profile for
* the given extension ID.
*
* @param {string} extensionId
* The ID of the extension for which to return a directory path.
* @returns {string}
*/
getExtensionDir(extensionId) {
return lazy.OS.Path.join(this.extensionDir, extensionId);
},
/**
* Returns the path to the JSON storage file for the given extension
* ID.
*
* @param {string} extensionId
* The ID of the extension for which to return a file path.
* @returns {string}
*/
getStorageFile(extensionId) {
return lazy.OS.Path.join(this.extensionDir, extensionId, "storage.js");
},
/**
* Asynchronously sets the values of the given storage items for the
* given extension.
*
* @param {string} extensionId
* The ID of the extension for which to set storage values.
* @param {object} items
* The storage items to set. For each property in the object,
* the storage value for that property is set to its value in
* said object. Any values which are StructuredCloneHolder
* instances are deserialized before being stored.
* @returns {Promise<void>}
*/
async set(extensionId, items) {
let jsonFile = await this.getFile(extensionId);
let changes = {};
for (let prop in items) {
let item = items[prop];
changes[prop] = {
oldValue: serialize(jsonFile.data.get(prop)),
newValue: serialize(item),
};
jsonFile.data.set(prop, item);
}
this.notifyListeners(extensionId, changes);
jsonFile.saveSoon();
return null;
},
/**
* Asynchronously removes the given storage items for the given
* extension ID.
*
* @param {string} extensionId
* The ID of the extension for which to remove storage values.
* @param {Array<string>} items
* A list of storage items to remove.
* @returns {Promise<void>}
*/
async remove(extensionId, items) {
let jsonFile = await this.getFile(extensionId);
let changed = false;
let changes = {};
for (let prop of [].concat(items)) {
if (jsonFile.data.has(prop)) {
changes[prop] = { oldValue: serialize(jsonFile.data.get(prop)) };
jsonFile.data.delete(prop);
changed = true;
}
}
if (changed) {
this.notifyListeners(extensionId, changes);
jsonFile.saveSoon();
}
return null;
},
/**
* Asynchronously clears all storage entries for the given extension
* ID.
*
* @param {string} extensionId
* The ID of the extension for which to clear storage.
* @param {object} options
* @param {boolean} [options.shouldNotifyListeners = true]
* Whether or not collect and send the changes to the listeners,
* used when the extension data is being cleared on uninstall.
* @returns {Promise<void>}
*/
async clear(extensionId, { shouldNotifyListeners = true } = {}) {
let jsonFile = await this.getFile(extensionId);
let changed = false;
let changes = {};
for (let [prop, oldValue] of jsonFile.data.entries()) {
if (shouldNotifyListeners) {
changes[prop] = { oldValue: serialize(oldValue) };
}
jsonFile.data.delete(prop);
changed = true;
}
if (changed) {
if (shouldNotifyListeners) {
this.notifyListeners(extensionId, changes);
}
jsonFile.saveSoon();
}
return null;
},
/**
* Asynchronously retrieves the values for the given storage items for
* the given extension ID.
*
* @param {string} extensionId
* The ID of the extension for which to get storage values.
* @param {Array<string>|object|null} [keys]
* The storage items to get. If an array, the value of each key
* in the array is returned. If null, the values of all items
* are returned. If an object, the value for each key in the
* object is returned, or that key's value if the item is not
* set.
* @returns {Promise<object>}
* An object which a property for each requested key,
* containing that key's storage value. Values are
* StructuredCloneHolder objects which can be deserialized to
* the original storage value.
*/
async get(extensionId, keys) {
let jsonFile = await this.getFile(extensionId);
return this._filterProperties(jsonFile.data, keys);
},
async _filterProperties(data, keys) {
let result = {};
if (keys === null) {
Object.assign(result, data.toJSON());
} else if (typeof keys == "object" && !Array.isArray(keys)) {
for (let prop in keys) {
if (data.has(prop)) {
result[prop] = serialize(data.get(prop));
} else {
result[prop] = keys[prop];
}
}
} else {
for (let prop of [].concat(keys)) {
if (data.has(prop)) {
result[prop] = serialize(data.get(prop));
}
}
}
return result;
},
addOnChangedListener(extensionId, listener) {
let listeners = this.listeners.get(extensionId) || new Set();
listeners.add(listener);
this.listeners.set(extensionId, listeners);
},
removeOnChangedListener(extensionId, listener) {
let listeners = this.listeners.get(extensionId);
listeners.delete(listener);
},
notifyListeners(extensionId, changes) {
let listeners = this.listeners.get(extensionId);
if (listeners) {
for (let listener of listeners) {
listener(changes);
}
}
},
init() {
if (Services.appinfo.processType != Services.appinfo.PROCESS_TYPE_DEFAULT) {
return;
}
Services.obs.addObserver(this, "extension-invalidate-storage-cache");
Services.obs.addObserver(this, "xpcom-shutdown");
},
observe(subject, topic, data) {
if (topic == "xpcom-shutdown") {
Services.obs.removeObserver(this, "extension-invalidate-storage-cache");
Services.obs.removeObserver(this, "xpcom-shutdown");
} else if (topic == "extension-invalidate-storage-cache") {
for (let promise of this.jsonFilePromises.values()) {
promise.then(jsonFile => {
jsonFile.finalize();
});
}
this.jsonFilePromises.clear();
}
},
// Serializes an arbitrary value into a StructuredCloneHolder, if appropriate.
serialize,
/**
* Serializes the given storage items for transporting between processes.
*
* @param {BaseContext} context
* The context to use for the created StructuredCloneHolder
* objects.
* @param {Array<string>|object} items
* The items to serialize. If an object is provided, its
* values are serialized to StructuredCloneHolder objects.
* Otherwise, it is returned as-is.
* @returns {Array<string>|object}
*/
serializeForContext(context, items) {
if (items && typeof items === "object" && !Array.isArray(items)) {
let result = {};
for (let [key, value] of Object.entries(items)) {
try {
result[key] = new StructuredCloneHolder(value, context.cloneScope);
} catch (e) {
throw new lazy.ExtensionUtils.ExtensionError(String(e));
}
}
return result;
}
return items;
},
/**
* Deserializes the given storage items into the given extension context.
*
* @param {BaseContext} context
* The context to use to deserialize the StructuredCloneHolder objects.
* @param {object} items
* The items to deserialize. Any property of the object which
* is a StructuredCloneHolder instance is deserialized into
* the extension scope. Any other object is cloned into the
* extension scope directly.
* @returns {object}
*/
deserializeForContext(context, items) {
let result = new context.cloneScope.Object();
for (let [key, value] of Object.entries(items)) {
if (
value &&
typeof value === "object" &&
Cu.getClassName(value, true) === "StructuredCloneHolder"
) {
value = value.deserialize(context.cloneScope, true);
} else {
value = Cu.cloneInto(value, context.cloneScope);
}
result[key] = value;
}
return result;
},
};
XPCOMUtils.defineLazyGetter(ExtensionStorage, "extensionDir", () =>
lazy.OS.Path.join(lazy.OS.Constants.Path.profileDir, "browser-extension-data")
);
ExtensionStorage.init();
|