2018-11-20 17:00:06 +03:00
|
|
|
/* 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";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Interface to a dedicated thread handling for Remote Settings heavy operations.
|
|
|
|
*/
|
2019-05-16 12:10:34 +03:00
|
|
|
const { XPCOMUtils } = ChromeUtils.import(
|
|
|
|
"resource://gre/modules/XPCOMUtils.jsm"
|
|
|
|
);
|
|
|
|
const { setTimeout, clearTimeout } = ChromeUtils.import(
|
|
|
|
"resource://gre/modules/Timer.jsm"
|
|
|
|
);
|
2018-11-20 17:00:06 +03:00
|
|
|
|
|
|
|
var EXPORTED_SYMBOLS = ["RemoteSettingsWorker"];
|
|
|
|
|
2019-05-16 12:10:34 +03:00
|
|
|
XPCOMUtils.defineLazyPreferenceGetter(
|
|
|
|
this,
|
|
|
|
"gMaxIdleMilliseconds",
|
|
|
|
"services.settings.worker_idle_max_milliseconds",
|
|
|
|
30 * 1000 // Default of 30 seconds.
|
|
|
|
);
|
|
|
|
|
2018-11-20 17:00:06 +03:00
|
|
|
class Worker {
|
|
|
|
constructor(source) {
|
2019-05-16 12:10:34 +03:00
|
|
|
this.source = source;
|
|
|
|
this.worker = null;
|
2018-11-20 17:00:06 +03:00
|
|
|
|
|
|
|
this.callbacks = new Map();
|
|
|
|
this.lastCallbackId = 0;
|
2019-05-16 12:10:34 +03:00
|
|
|
this.idleTimeoutId = null;
|
2018-11-20 17:00:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
async _execute(method, args = []) {
|
2019-05-16 12:10:34 +03:00
|
|
|
// (Re)instantiate the worker if it was terminated.
|
|
|
|
if (!this.worker) {
|
|
|
|
this.worker = new ChromeWorker(this.source);
|
|
|
|
this.worker.onmessage = this._onWorkerMessage.bind(this);
|
|
|
|
}
|
|
|
|
// New activity: reset the idle timer.
|
|
|
|
if (this.idleTimeoutId) {
|
|
|
|
clearTimeout(this.idleTimeoutId);
|
|
|
|
}
|
|
|
|
return new Promise((resolve, reject) => {
|
2018-11-20 17:00:06 +03:00
|
|
|
const callbackId = ++this.lastCallbackId;
|
|
|
|
this.callbacks.set(callbackId, [resolve, reject]);
|
|
|
|
this.worker.postMessage({ callbackId, method, args });
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
_onWorkerMessage(event) {
|
|
|
|
const { callbackId, result, error } = event.data;
|
|
|
|
const [resolve, reject] = this.callbacks.get(callbackId);
|
|
|
|
if (error) {
|
|
|
|
reject(new Error(error));
|
|
|
|
} else {
|
|
|
|
resolve(result);
|
|
|
|
}
|
|
|
|
this.callbacks.delete(callbackId);
|
2019-05-16 12:10:34 +03:00
|
|
|
|
|
|
|
// Terminate the worker when it's unused for some time.
|
|
|
|
// But don't terminate it if an operation is pending.
|
|
|
|
if (this.callbacks.length == 0) {
|
|
|
|
this.idleTimeoutId = setTimeout(() => {
|
|
|
|
this.worker.terminate();
|
|
|
|
this.worker = null;
|
|
|
|
this.idleTimeoutId = null;
|
|
|
|
}, gMaxIdleMilliseconds);
|
|
|
|
}
|
2018-11-20 17:00:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
async canonicalStringify(localRecords, remoteRecords, timestamp) {
|
|
|
|
return this._execute("canonicalStringify", [
|
|
|
|
localRecords,
|
|
|
|
remoteRecords,
|
|
|
|
timestamp,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
async importJSONDump(bucket, collection) {
|
|
|
|
return this._execute("importJSONDump", [bucket, collection]);
|
|
|
|
}
|
2019-05-11 01:57:40 +03:00
|
|
|
|
|
|
|
async checkFileHash(filepath, size, hash) {
|
|
|
|
return this._execute("checkFileHash", [filepath, size, hash]);
|
|
|
|
}
|
2018-11-20 17:00:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var RemoteSettingsWorker = new Worker(
|
|
|
|
"resource://services-settings/RemoteSettingsWorker.js"
|
|
|
|
);
|