2018-03-13 18:23:57 +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/. */
|
|
|
|
|
2018-05-08 18:53:06 +03:00
|
|
|
/* global __URI__ */
|
|
|
|
|
2018-03-13 18:23:57 +03:00
|
|
|
"use strict";
|
|
|
|
|
2018-05-11 18:09:44 +03:00
|
|
|
var EXPORTED_SYMBOLS = [
|
|
|
|
"RemoteSettings",
|
2018-05-08 18:53:06 +03:00
|
|
|
"jexlFilterFunc",
|
|
|
|
"remoteSettingsBroadcastHandler",
|
2018-05-11 18:09:44 +03:00
|
|
|
];
|
2018-03-13 18:23:57 +03:00
|
|
|
|
2019-01-17 21:18:31 +03:00
|
|
|
const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
|
|
|
|
const { XPCOMUtils } = ChromeUtils.import(
|
|
|
|
"resource://gre/modules/XPCOMUtils.jsm"
|
|
|
|
);
|
2019-07-05 11:58:22 +03:00
|
|
|
|
2020-02-21 21:18:26 +03:00
|
|
|
ChromeUtils.defineModuleGetter(
|
|
|
|
this,
|
|
|
|
"UptakeTelemetry",
|
|
|
|
"resource://services-common/uptake-telemetry.js"
|
|
|
|
);
|
|
|
|
ChromeUtils.defineModuleGetter(
|
|
|
|
this,
|
|
|
|
"pushBroadcastService",
|
|
|
|
"resource://gre/modules/PushBroadcastService.jsm"
|
|
|
|
);
|
|
|
|
ChromeUtils.defineModuleGetter(
|
|
|
|
this,
|
|
|
|
"RemoteSettingsClient",
|
|
|
|
"resource://services-settings/RemoteSettingsClient.jsm"
|
|
|
|
);
|
|
|
|
ChromeUtils.defineModuleGetter(
|
|
|
|
this,
|
|
|
|
"Utils",
|
|
|
|
"resource://services-settings/Utils.jsm"
|
|
|
|
);
|
|
|
|
ChromeUtils.defineModuleGetter(
|
|
|
|
this,
|
|
|
|
"FilterExpressions",
|
|
|
|
"resource://gre/modules/components-utils/FilterExpressions.jsm"
|
|
|
|
);
|
2018-11-27 13:34:16 +03:00
|
|
|
|
|
|
|
XPCOMUtils.defineLazyGlobalGetters(this, ["fetch"]);
|
2018-03-13 18:23:57 +03:00
|
|
|
|
2018-03-30 00:38:16 +03:00
|
|
|
const PREF_SETTINGS_DEFAULT_BUCKET = "services.settings.default_bucket";
|
2018-09-18 09:46:06 +03:00
|
|
|
const PREF_SETTINGS_BRANCH = "services.settings.";
|
|
|
|
const PREF_SETTINGS_DEFAULT_SIGNER = "default_signer";
|
|
|
|
const PREF_SETTINGS_SERVER_BACKOFF = "server.backoff";
|
|
|
|
const PREF_SETTINGS_LAST_UPDATE = "last_update_seconds";
|
|
|
|
const PREF_SETTINGS_LAST_ETAG = "last_etag";
|
|
|
|
const PREF_SETTINGS_CLOCK_SKEW_SECONDS = "clock_skew_seconds";
|
|
|
|
const PREF_SETTINGS_LOAD_DUMP = "load_dump";
|
2018-03-13 18:23:57 +03:00
|
|
|
|
2019-02-15 12:38:18 +03:00
|
|
|
// Telemetry identifiers.
|
|
|
|
const TELEMETRY_COMPONENT = "remotesettings";
|
2019-04-01 23:04:25 +03:00
|
|
|
const TELEMETRY_SOURCE_POLL = "settings-changes-monitoring";
|
|
|
|
const TELEMETRY_SOURCE_SYNC = "settings-sync";
|
2019-02-15 12:38:18 +03:00
|
|
|
|
2018-11-27 13:34:16 +03:00
|
|
|
// Push broadcast id.
|
|
|
|
const BROADCAST_ID = "remote-settings/monitor_changes";
|
2018-03-13 18:23:57 +03:00
|
|
|
|
2019-05-14 23:45:03 +03:00
|
|
|
// Signer to be used when not specified (see Ci.nsIContentSignatureVerifier).
|
|
|
|
const DEFAULT_SIGNER = "remote-settings.content-signature.mozilla.org";
|
|
|
|
|
2018-09-18 09:46:06 +03:00
|
|
|
XPCOMUtils.defineLazyGetter(this, "gPrefs", () => {
|
|
|
|
return Services.prefs.getBranch(PREF_SETTINGS_BRANCH);
|
|
|
|
});
|
2019-05-22 17:01:53 +03:00
|
|
|
XPCOMUtils.defineLazyGetter(this, "console", () => Utils.log);
|
2018-09-18 09:46:06 +03:00
|
|
|
|
2018-05-11 18:09:44 +03:00
|
|
|
/**
|
|
|
|
* Default entry filtering function, in charge of excluding remote settings entries
|
|
|
|
* where the JEXL expression evaluates into a falsy value.
|
2018-11-27 13:34:16 +03:00
|
|
|
* @param {Object} entry The Remote Settings entry to be excluded or kept.
|
|
|
|
* @param {ClientEnvironment} environment Information about version, language, platform etc.
|
|
|
|
* @returns {?Object} the entry or null if excluded.
|
2018-05-11 18:09:44 +03:00
|
|
|
*/
|
|
|
|
async function jexlFilterFunc(entry, environment) {
|
2018-05-28 14:45:27 +03:00
|
|
|
const { filter_expression } = entry;
|
|
|
|
if (!filter_expression) {
|
2018-05-11 18:09:44 +03:00
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
let result;
|
|
|
|
try {
|
|
|
|
const context = {
|
2019-01-09 02:52:35 +03:00
|
|
|
env: environment,
|
2018-05-11 18:09:44 +03:00
|
|
|
};
|
2018-05-28 14:45:27 +03:00
|
|
|
result = await FilterExpressions.eval(filter_expression, context);
|
2018-05-11 18:09:44 +03:00
|
|
|
} catch (e) {
|
|
|
|
Cu.reportError(e);
|
|
|
|
}
|
|
|
|
return result ? entry : null;
|
|
|
|
}
|
|
|
|
|
2018-03-30 00:38:16 +03:00
|
|
|
function remoteSettingsFunction() {
|
|
|
|
const _clients = new Map();
|
2019-04-09 20:43:47 +03:00
|
|
|
let _invalidatePolling = false;
|
2018-03-30 00:38:16 +03:00
|
|
|
|
2018-09-18 09:46:06 +03:00
|
|
|
// If not explicitly specified, use the default signer.
|
|
|
|
const defaultOptions = {
|
|
|
|
bucketNamePref: PREF_SETTINGS_DEFAULT_BUCKET,
|
2019-05-14 23:45:03 +03:00
|
|
|
signerName: DEFAULT_SIGNER,
|
2018-11-27 13:34:16 +03:00
|
|
|
filterFunc: jexlFilterFunc,
|
2018-09-18 09:46:06 +03:00
|
|
|
};
|
2018-03-30 00:38:16 +03:00
|
|
|
|
2018-06-08 18:08:33 +03:00
|
|
|
/**
|
|
|
|
* RemoteSettings constructor.
|
|
|
|
*
|
|
|
|
* @param {String} collectionName The remote settings identifier
|
|
|
|
* @param {Object} options Advanced options
|
|
|
|
* @returns {RemoteSettingsClient} An instance of a Remote Settings client.
|
|
|
|
*/
|
2018-03-30 00:38:16 +03:00
|
|
|
const remoteSettings = function(collectionName, options) {
|
|
|
|
// Get or instantiate a remote settings client.
|
2018-09-18 09:46:06 +03:00
|
|
|
if (!_clients.has(collectionName)) {
|
2018-07-25 18:43:08 +03:00
|
|
|
// Register a new client!
|
2018-09-18 09:46:06 +03:00
|
|
|
const c = new RemoteSettingsClient(collectionName, {
|
|
|
|
...defaultOptions,
|
|
|
|
...options,
|
|
|
|
});
|
|
|
|
// Store instance for later call.
|
|
|
|
_clients.set(collectionName, c);
|
2018-07-25 18:43:08 +03:00
|
|
|
// Invalidate the polling status, since we want the new collection to
|
|
|
|
// be taken into account.
|
2019-04-09 20:43:47 +03:00
|
|
|
_invalidatePolling = true;
|
2019-05-22 17:01:53 +03:00
|
|
|
console.debug(`Instantiated new client ${c.identifier}`);
|
2018-03-30 00:38:16 +03:00
|
|
|
}
|
2018-09-18 09:46:06 +03:00
|
|
|
return _clients.get(collectionName);
|
2018-03-30 00:38:16 +03:00
|
|
|
};
|
|
|
|
|
2018-06-08 18:08:33 +03:00
|
|
|
/**
|
|
|
|
* Internal helper to retrieve existing instances of clients or new instances
|
|
|
|
* with default options if possible, or `null` if bucket/collection are unknown.
|
|
|
|
*/
|
|
|
|
async function _client(bucketName, collectionName) {
|
|
|
|
// Check if a client was registered for this bucket/collection. Potentially
|
|
|
|
// with some specific options like signer, filter function etc.
|
2018-09-18 09:46:06 +03:00
|
|
|
const client = _clients.get(collectionName);
|
2018-09-19 13:16:41 +03:00
|
|
|
if (client && client.bucketName == bucketName) {
|
|
|
|
return client;
|
|
|
|
}
|
|
|
|
// There was no client registered for this collection, but it's the main bucket,
|
2018-06-08 18:08:33 +03:00
|
|
|
// therefore we can instantiate a client with the default options.
|
|
|
|
// So if we have a local database or if we ship a JSON dump, then it means that
|
|
|
|
// this client is known but it was not registered yet (eg. calling module not "imported" yet).
|
2018-09-19 13:16:41 +03:00
|
|
|
if (
|
|
|
|
bucketName == Services.prefs.getCharPref(PREF_SETTINGS_DEFAULT_BUCKET)
|
|
|
|
) {
|
|
|
|
const c = new RemoteSettingsClient(collectionName, defaultOptions);
|
2018-06-08 18:08:33 +03:00
|
|
|
const [dbExists, localDump] = await Promise.all([
|
2018-11-29 20:35:20 +03:00
|
|
|
Utils.hasLocalData(c),
|
|
|
|
Utils.hasLocalDump(bucketName, collectionName),
|
2018-06-08 18:08:33 +03:00
|
|
|
]);
|
|
|
|
if (dbExists || localDump) {
|
2018-09-19 13:16:41 +03:00
|
|
|
return c;
|
2018-06-08 18:08:33 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Else, we cannot return a client insttance because we are not able to synchronize data in specific buckets.
|
|
|
|
// Mainly because we cannot guess which `signerName` has to be used for example.
|
|
|
|
// And we don't want to synchronize data for collections in the main bucket that are
|
|
|
|
// completely unknown (ie. no database and no JSON dump).
|
2019-05-22 17:01:53 +03:00
|
|
|
console.debug(`No known client for ${bucketName}/${collectionName}`);
|
2018-06-08 18:08:33 +03:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Main polling method, called by the ping mechanism.
|
|
|
|
*
|
2018-10-09 17:05:01 +03:00
|
|
|
* @param {Object} options
|
|
|
|
. * @param {Object} options.expectedTimestamp (optional) The expected timestamp to be received — used by servers for cache busting.
|
2019-03-08 22:57:46 +03:00
|
|
|
* @param {string} options.trigger (optional) label to identify what triggered this sync (eg. ``"timer"``, default: `"manual"`)
|
2018-06-08 18:08:33 +03:00
|
|
|
* @returns {Promise} or throws error if something goes wrong.
|
|
|
|
*/
|
2019-03-08 22:57:46 +03:00
|
|
|
remoteSettings.pollChanges = async ({
|
|
|
|
expectedTimestamp,
|
|
|
|
trigger = "manual",
|
|
|
|
} = {}) => {
|
2019-04-01 23:04:25 +03:00
|
|
|
let pollTelemetryArgs = {
|
|
|
|
source: TELEMETRY_SOURCE_POLL,
|
2019-03-07 17:44:52 +03:00
|
|
|
trigger,
|
|
|
|
};
|
|
|
|
|
2020-02-17 13:52:47 +03:00
|
|
|
if (Utils.isOffline) {
|
|
|
|
console.info("Network is offline. Give up.");
|
|
|
|
await UptakeTelemetry.report(
|
|
|
|
TELEMETRY_COMPONENT,
|
|
|
|
UptakeTelemetry.STATUS.NETWORK_OFFLINE_ERROR,
|
|
|
|
pollTelemetryArgs
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const startedAt = new Date();
|
|
|
|
|
2018-03-30 00:38:16 +03:00
|
|
|
// Check if the server backoff time is elapsed.
|
2018-09-18 09:46:06 +03:00
|
|
|
if (gPrefs.prefHasUserValue(PREF_SETTINGS_SERVER_BACKOFF)) {
|
|
|
|
const backoffReleaseTime = gPrefs.getCharPref(
|
|
|
|
PREF_SETTINGS_SERVER_BACKOFF
|
|
|
|
);
|
2018-03-30 00:38:16 +03:00
|
|
|
const remainingMilliseconds =
|
|
|
|
parseInt(backoffReleaseTime, 10) - Date.now();
|
|
|
|
if (remainingMilliseconds > 0) {
|
|
|
|
// Backoff time has not elapsed yet.
|
2019-04-01 23:04:25 +03:00
|
|
|
await UptakeTelemetry.report(
|
|
|
|
TELEMETRY_COMPONENT,
|
|
|
|
UptakeTelemetry.STATUS.BACKOFF,
|
|
|
|
pollTelemetryArgs
|
|
|
|
);
|
2018-03-30 00:38:16 +03:00
|
|
|
throw new Error(
|
|
|
|
`Server is asking clients to back off; retry in ${Math.ceil(
|
|
|
|
remainingMilliseconds / 1000
|
|
|
|
)}s.`
|
|
|
|
);
|
|
|
|
} else {
|
2018-09-18 09:46:06 +03:00
|
|
|
gPrefs.clearUserPref(PREF_SETTINGS_SERVER_BACKOFF);
|
2018-03-30 00:38:16 +03:00
|
|
|
}
|
2018-03-13 18:23:57 +03:00
|
|
|
}
|
|
|
|
|
2019-05-22 17:01:53 +03:00
|
|
|
console.info("Start polling for changes");
|
2019-01-16 17:19:27 +03:00
|
|
|
Services.obs.notifyObservers(
|
|
|
|
null,
|
|
|
|
"remote-settings:changes-poll-start",
|
|
|
|
JSON.stringify({ expectedTimestamp })
|
|
|
|
);
|
|
|
|
|
2019-04-09 20:43:47 +03:00
|
|
|
// Do we have the latest version already?
|
|
|
|
// Every time we register a new client, we have to fetch the whole list again.
|
|
|
|
const lastEtag = _invalidatePolling
|
|
|
|
? ""
|
|
|
|
: gPrefs.getCharPref(PREF_SETTINGS_LAST_ETAG, "");
|
2018-03-13 18:23:57 +03:00
|
|
|
|
2018-03-30 00:38:16 +03:00
|
|
|
let pollResult;
|
|
|
|
try {
|
2019-12-09 15:07:46 +03:00
|
|
|
pollResult = await Utils.fetchLatestChanges(Utils.SERVER_URL, {
|
2019-05-14 23:45:03 +03:00
|
|
|
expectedTimestamp,
|
|
|
|
lastEtag,
|
|
|
|
});
|
2018-03-30 00:38:16 +03:00
|
|
|
} catch (e) {
|
|
|
|
// Report polling error to Uptake Telemetry.
|
2019-02-15 12:38:18 +03:00
|
|
|
let reportStatus;
|
2019-02-25 23:22:16 +03:00
|
|
|
if (/JSON\.parse/.test(e.message)) {
|
|
|
|
reportStatus = UptakeTelemetry.STATUS.PARSE_ERROR;
|
|
|
|
} else if (/content-type/.test(e.message)) {
|
|
|
|
reportStatus = UptakeTelemetry.STATUS.CONTENT_ERROR;
|
|
|
|
} else if (/Server/.test(e.message)) {
|
2019-02-15 12:38:18 +03:00
|
|
|
reportStatus = UptakeTelemetry.STATUS.SERVER_ERROR;
|
2019-02-25 23:22:16 +03:00
|
|
|
} else if (/Timeout/.test(e.message)) {
|
|
|
|
reportStatus = UptakeTelemetry.STATUS.TIMEOUT_ERROR;
|
2018-03-30 00:38:16 +03:00
|
|
|
} else if (/NetworkError/.test(e.message)) {
|
2019-02-15 12:38:18 +03:00
|
|
|
reportStatus = UptakeTelemetry.STATUS.NETWORK_ERROR;
|
2018-03-30 00:38:16 +03:00
|
|
|
} else {
|
2019-02-15 12:38:18 +03:00
|
|
|
reportStatus = UptakeTelemetry.STATUS.UNKNOWN_ERROR;
|
2018-03-30 00:38:16 +03:00
|
|
|
}
|
2019-04-01 23:04:25 +03:00
|
|
|
await UptakeTelemetry.report(
|
|
|
|
TELEMETRY_COMPONENT,
|
|
|
|
reportStatus,
|
|
|
|
pollTelemetryArgs
|
|
|
|
);
|
2018-03-30 00:38:16 +03:00
|
|
|
// No need to go further.
|
|
|
|
throw new Error(`Polling for changes failed: ${e.message}.`);
|
2018-03-13 18:23:57 +03:00
|
|
|
}
|
|
|
|
|
2019-03-12 18:13:31 +03:00
|
|
|
const {
|
|
|
|
serverTimeMillis,
|
|
|
|
changes,
|
|
|
|
currentEtag,
|
|
|
|
backoffSeconds,
|
|
|
|
ageSeconds,
|
|
|
|
} = pollResult;
|
|
|
|
|
|
|
|
// Report age of server data in Telemetry.
|
2019-04-01 23:04:25 +03:00
|
|
|
pollTelemetryArgs = { age: ageSeconds, ...pollTelemetryArgs };
|
2018-03-13 18:23:57 +03:00
|
|
|
|
2018-03-30 00:38:16 +03:00
|
|
|
// Report polling success to Uptake Telemetry.
|
2019-02-15 12:38:18 +03:00
|
|
|
const reportStatus =
|
|
|
|
changes.length === 0
|
|
|
|
? UptakeTelemetry.STATUS.UP_TO_DATE
|
|
|
|
: UptakeTelemetry.STATUS.SUCCESS;
|
2019-04-01 23:04:25 +03:00
|
|
|
await UptakeTelemetry.report(
|
|
|
|
TELEMETRY_COMPONENT,
|
|
|
|
reportStatus,
|
|
|
|
pollTelemetryArgs
|
|
|
|
);
|
2018-03-13 18:23:57 +03:00
|
|
|
|
2018-03-30 00:38:16 +03:00
|
|
|
// Check if the server asked the clients to back off (for next poll).
|
|
|
|
if (backoffSeconds) {
|
2019-05-22 17:01:53 +03:00
|
|
|
console.info(
|
|
|
|
"Server asks clients to backoff for ${backoffSeconds} seconds"
|
|
|
|
);
|
2018-03-30 00:38:16 +03:00
|
|
|
const backoffReleaseTime = Date.now() + backoffSeconds * 1000;
|
2018-09-18 09:46:06 +03:00
|
|
|
gPrefs.setCharPref(PREF_SETTINGS_SERVER_BACKOFF, backoffReleaseTime);
|
2018-03-30 00:38:16 +03:00
|
|
|
}
|
2018-03-13 18:23:57 +03:00
|
|
|
|
2018-03-30 00:38:16 +03:00
|
|
|
// Record new update time and the difference between local and server time.
|
|
|
|
// Negative clockDifference means local time is behind server time
|
|
|
|
// by the absolute of that value in seconds (positive means it's ahead)
|
|
|
|
const clockDifference = Math.floor((Date.now() - serverTimeMillis) / 1000);
|
2018-09-18 09:46:06 +03:00
|
|
|
gPrefs.setIntPref(PREF_SETTINGS_CLOCK_SKEW_SECONDS, clockDifference);
|
2018-11-29 20:35:20 +03:00
|
|
|
const checkedServerTimeInSeconds = Math.round(serverTimeMillis / 1000);
|
|
|
|
gPrefs.setIntPref(PREF_SETTINGS_LAST_UPDATE, checkedServerTimeInSeconds);
|
|
|
|
|
2019-03-08 22:57:46 +03:00
|
|
|
// Should the clients try to load JSON dump? (mainly disabled in tests)
|
2018-09-18 09:46:06 +03:00
|
|
|
const loadDump = gPrefs.getBoolPref(PREF_SETTINGS_LOAD_DUMP, true);
|
2018-06-08 18:08:33 +03:00
|
|
|
|
2018-03-30 00:38:16 +03:00
|
|
|
// Iterate through the collections version info and initiate a synchronization
|
2019-04-01 23:04:25 +03:00
|
|
|
// on the related remote settings clients.
|
2018-03-30 00:38:16 +03:00
|
|
|
let firstError;
|
|
|
|
for (const change of changes) {
|
2018-06-08 18:08:33 +03:00
|
|
|
const { bucket, collection, last_modified } = change;
|
2018-05-25 00:55:23 +03:00
|
|
|
|
2018-06-08 18:08:33 +03:00
|
|
|
const client = await _client(bucket, collection);
|
|
|
|
if (!client) {
|
2019-04-01 23:04:25 +03:00
|
|
|
// This collection has no associated client (eg. preview, other platform...)
|
2018-03-30 00:38:16 +03:00
|
|
|
continue;
|
|
|
|
}
|
2018-05-25 00:55:23 +03:00
|
|
|
// Start synchronization! It will be a no-op if the specified `lastModified` equals
|
|
|
|
// the one in the local database.
|
2018-03-30 00:38:16 +03:00
|
|
|
try {
|
2019-03-07 17:44:52 +03:00
|
|
|
await client.maybeSync(last_modified, { loadDump, trigger });
|
2019-01-16 17:19:27 +03:00
|
|
|
|
2018-11-29 20:35:20 +03:00
|
|
|
// Save last time this client was successfully synced.
|
|
|
|
Services.prefs.setIntPref(
|
|
|
|
client.lastCheckTimePref,
|
|
|
|
checkedServerTimeInSeconds
|
|
|
|
);
|
2018-03-30 00:38:16 +03:00
|
|
|
} catch (e) {
|
2019-05-22 17:01:53 +03:00
|
|
|
console.error(e);
|
2018-03-30 00:38:16 +03:00
|
|
|
if (!firstError) {
|
|
|
|
firstError = e;
|
2018-05-25 00:55:23 +03:00
|
|
|
firstError.details = change;
|
2018-03-30 00:38:16 +03:00
|
|
|
}
|
|
|
|
}
|
2018-03-13 18:23:57 +03:00
|
|
|
}
|
2019-04-09 20:43:47 +03:00
|
|
|
|
|
|
|
// Polling is done.
|
|
|
|
_invalidatePolling = false;
|
|
|
|
|
2019-04-01 23:04:25 +03:00
|
|
|
// Report total synchronization duration to Telemetry.
|
|
|
|
const durationMilliseconds = new Date() - startedAt;
|
|
|
|
const syncTelemetryArgs = {
|
|
|
|
source: TELEMETRY_SOURCE_SYNC,
|
|
|
|
duration: durationMilliseconds,
|
2020-02-11 20:10:38 +03:00
|
|
|
timestamp: `${currentEtag}`,
|
2019-04-01 23:04:25 +03:00
|
|
|
trigger,
|
|
|
|
};
|
|
|
|
|
2018-03-30 00:38:16 +03:00
|
|
|
if (firstError) {
|
2019-04-01 23:04:25 +03:00
|
|
|
// Report the global synchronization failure. Individual uptake reports will also have been sent for each collection.
|
|
|
|
await UptakeTelemetry.report(
|
|
|
|
TELEMETRY_COMPONENT,
|
|
|
|
UptakeTelemetry.STATUS.SYNC_ERROR,
|
|
|
|
syncTelemetryArgs
|
|
|
|
);
|
|
|
|
// Rethrow the first observed error
|
2018-03-30 00:38:16 +03:00
|
|
|
throw firstError;
|
2018-03-13 18:23:57 +03:00
|
|
|
}
|
2018-03-30 00:38:16 +03:00
|
|
|
|
|
|
|
// Save current Etag for next poll.
|
|
|
|
if (currentEtag) {
|
2018-09-18 09:46:06 +03:00
|
|
|
gPrefs.setCharPref(PREF_SETTINGS_LAST_ETAG, currentEtag);
|
2018-03-13 18:23:57 +03:00
|
|
|
}
|
|
|
|
|
2019-04-01 23:04:25 +03:00
|
|
|
// Report the global synchronization success.
|
|
|
|
await UptakeTelemetry.report(
|
|
|
|
TELEMETRY_COMPONENT,
|
|
|
|
UptakeTelemetry.STATUS.SUCCESS,
|
|
|
|
syncTelemetryArgs
|
|
|
|
);
|
|
|
|
|
2019-05-22 17:01:53 +03:00
|
|
|
console.info("Polling for changes done");
|
2019-01-16 17:19:27 +03:00
|
|
|
Services.obs.notifyObservers(null, "remote-settings:changes-poll-end");
|
2018-03-30 00:38:16 +03:00
|
|
|
};
|
2018-03-13 18:23:57 +03:00
|
|
|
|
2018-06-08 18:08:33 +03:00
|
|
|
/**
|
|
|
|
* Returns an object with polling status information and the list of
|
|
|
|
* known remote settings collections.
|
|
|
|
*/
|
|
|
|
remoteSettings.inspect = async () => {
|
2019-05-14 23:45:03 +03:00
|
|
|
const {
|
|
|
|
changes,
|
|
|
|
currentEtag: serverTimestamp,
|
2019-12-09 15:07:46 +03:00
|
|
|
} = await Utils.fetchLatestChanges(Utils.SERVER_URL);
|
2019-07-05 11:58:22 +03:00
|
|
|
|
2018-06-08 18:08:33 +03:00
|
|
|
const collections = await Promise.all(
|
|
|
|
changes.map(async change => {
|
|
|
|
const { bucket, collection, last_modified: serverTimestamp } = change;
|
|
|
|
const client = await _client(bucket, collection);
|
|
|
|
if (!client) {
|
|
|
|
return null;
|
|
|
|
}
|
2020-02-21 21:18:26 +03:00
|
|
|
const kintoCol = await client.openCollection();
|
|
|
|
const localTimestamp = await kintoCol.db.getLastModified();
|
2018-06-08 18:08:33 +03:00
|
|
|
const lastCheck = Services.prefs.getIntPref(
|
|
|
|
client.lastCheckTimePref,
|
|
|
|
0
|
|
|
|
);
|
|
|
|
return {
|
|
|
|
bucket,
|
|
|
|
collection,
|
|
|
|
localTimestamp,
|
|
|
|
serverTimestamp,
|
|
|
|
lastCheck,
|
2018-08-31 08:59:17 +03:00
|
|
|
signerName: client.signerName,
|
2018-06-08 18:08:33 +03:00
|
|
|
};
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
return {
|
2019-12-09 15:07:46 +03:00
|
|
|
serverURL: Utils.SERVER_URL,
|
|
|
|
pollingEndpoint: Utils.SERVER_URL + Utils.CHANGES_PATH,
|
2018-06-08 18:08:33 +03:00
|
|
|
serverTimestamp,
|
2018-09-18 09:46:06 +03:00
|
|
|
localTimestamp: gPrefs.getCharPref(PREF_SETTINGS_LAST_ETAG, null),
|
|
|
|
lastCheck: gPrefs.getIntPref(PREF_SETTINGS_LAST_UPDATE, 0),
|
|
|
|
mainBucket: Services.prefs.getCharPref(PREF_SETTINGS_DEFAULT_BUCKET),
|
2019-05-14 23:45:03 +03:00
|
|
|
defaultSigner: DEFAULT_SIGNER,
|
2018-08-31 08:59:17 +03:00
|
|
|
collections: collections.filter(c => !!c),
|
2018-06-08 18:08:33 +03:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2018-06-14 21:54:19 +03:00
|
|
|
/**
|
|
|
|
* Startup function called from nsBrowserGlue.
|
|
|
|
*/
|
|
|
|
remoteSettings.init = () => {
|
2019-05-22 17:01:53 +03:00
|
|
|
console.info("Initialize Remote Settings");
|
2018-06-14 21:54:19 +03:00
|
|
|
// Hook the Push broadcast and RemoteSettings polling.
|
|
|
|
// When we start on a new profile there will be no ETag stored.
|
|
|
|
// Use an arbitrary ETag that is guaranteed not to occur.
|
|
|
|
// This will trigger a broadcast message but that's fine because we
|
|
|
|
// will check the changes on each collection and retrieve only the
|
|
|
|
// changes (e.g. nothing if we have a dump with the same data).
|
2018-09-18 09:46:06 +03:00
|
|
|
const currentVersion = gPrefs.getStringPref(PREF_SETTINGS_LAST_ETAG, '"0"');
|
2018-06-14 21:54:19 +03:00
|
|
|
const moduleInfo = {
|
|
|
|
moduleURI: __URI__,
|
|
|
|
symbolName: "remoteSettingsBroadcastHandler",
|
|
|
|
};
|
2018-11-27 13:34:16 +03:00
|
|
|
pushBroadcastService.addListener(BROADCAST_ID, currentVersion, moduleInfo);
|
2018-05-08 18:53:06 +03:00
|
|
|
};
|
|
|
|
|
2018-03-30 00:38:16 +03:00
|
|
|
return remoteSettings;
|
2018-03-13 18:23:57 +03:00
|
|
|
}
|
2018-03-30 00:38:16 +03:00
|
|
|
|
|
|
|
var RemoteSettings = remoteSettingsFunction();
|
2018-05-08 18:53:06 +03:00
|
|
|
|
|
|
|
var remoteSettingsBroadcastHandler = {
|
2019-04-10 19:55:52 +03:00
|
|
|
async receivedBroadcastMessage(version, broadcastID, context) {
|
|
|
|
const { phase } = context;
|
|
|
|
const isStartup = [
|
|
|
|
pushBroadcastService.PHASES.HELLO,
|
|
|
|
pushBroadcastService.PHASES.REGISTER,
|
|
|
|
].includes(phase);
|
|
|
|
|
2019-05-22 17:01:53 +03:00
|
|
|
console.info(
|
|
|
|
`Push notification received (version=${version} phase=${phase})`
|
|
|
|
);
|
|
|
|
|
2019-04-10 19:55:52 +03:00
|
|
|
return RemoteSettings.pollChanges({
|
|
|
|
expectedTimestamp: version,
|
|
|
|
trigger: isStartup ? "startup" : "broadcast",
|
|
|
|
});
|
2018-08-31 08:59:17 +03:00
|
|
|
},
|
2018-05-08 18:53:06 +03:00
|
|
|
};
|