Bug 1454970 - Use list of RemoteSettings collection from server f?glasserc draft
authorMathieu Leplatre <mathieu@mozilla.com>
Tue, 08 May 2018 19:25:07 +0200
changeset 792593 bc8a2adf0800aeabe159e2327a7d4a2a14b43204
parent 792086 2d95d70298e248161b629ef1d0d57049c0b62d71
push id109155
push usermleplatre@mozilla.com
push dateTue, 08 May 2018 17:25:34 +0000
bugs1454970
milestone62.0a1
Bug 1454970 - Use list of RemoteSettings collection from server f?glasserc MozReview-Commit-ID: 4g9Vog0sPtx
services/common/remote-settings.js
--- a/services/common/remote-settings.js
+++ b/services/common/remote-settings.js
@@ -420,23 +420,23 @@ class RemoteSettingsClient {
 }
 
 
 function remoteSettingsFunction() {
   const _clients = new Map();
 
   // If not explicitly specified, use the default bucket name and signer.
   const mainBucket = Services.prefs.getCharPref(PREF_SETTINGS_DEFAULT_BUCKET);
-  const defaultSigner = Services.prefs.getCharPref(PREF_SETTINGS_DEFAULT_SIGNER);
+  const mainSigner = Services.prefs.getCharPref(PREF_SETTINGS_DEFAULT_SIGNER);
 
   const remoteSettings = function(collectionName, options) {
     // Get or instantiate a remote settings client.
     const rsOptions = {
       bucketName: mainBucket,
-      signerName: defaultSigner,
+      signerName: mainSigner,
       ...options
     };
     const { bucketName } = rsOptions;
     const key = `${bucketName}/${collectionName}`;
     if (!_clients.has(key)) {
       const c = new RemoteSettingsClient(collectionName, rsOptions);
       _clients.set(key, c);
     }
@@ -458,16 +458,18 @@ function remoteSettingsFunction() {
       } else {
         Services.prefs.clearUserPref(PREF_SETTINGS_SERVER_BACKOFF);
       }
     }
 
     // Right now, we only use the collection name and the last modified info
     const kintoBase = Services.prefs.getCharPref(PREF_SETTINGS_SERVER);
     const changesEndpoint = kintoBase + Services.prefs.getCharPref(PREF_SETTINGS_CHANGES_PATH);
+    const defaultBucket = Services.prefs.getCharPref(PREF_SETTINGS_DEFAULT_BUCKET);
+    const defaultSigner = Services.prefs.getCharPref(PREF_SETTINGS_DEFAULT_SIGNER);
 
     let lastEtag;
     if (Services.prefs.prefHasUserValue(PREF_SETTINGS_LAST_ETAG)) {
       lastEtag = Services.prefs.getCharPref(PREF_SETTINGS_LAST_ETAG);
     }
 
     let pollResult;
     try {
@@ -509,23 +511,39 @@ function remoteSettingsFunction() {
 
     const loadDump = Services.prefs.getBoolPref(PREF_SETTINGS_LOAD_DUMP, true);
     // Iterate through the collections version info and initiate a synchronization
     // on the related remote settings client.
     let firstError;
     for (const change of changes) {
       const {bucket, collection, last_modified: lastModified} = change;
       const key = `${bucket}/${collection}`;
-      if (!_clients.has(key)) {
+      let client;
+      if (_clients.has(key)) {
+        // A client was already instantiated (eg. with specific options), reuse it.
+        client = _clients.get(key);
+        if (client.bucketName != bucket) {
+          // If the client bucketName attribute was modified after its instantiation
+          // then it should be ignored.
+          continue;
+        }
+
+      } else if (bucket == defaultBucket) {
+        // Clients in the default bucket all share the same signer. We will thus
+        // synchronize every remote collection with default options.
+        client = new RemoteSettings(collection, {
+          bucketName: defaultBucket,
+          signerName: defaultSigner
+        });
+
+      } else {
+        // Ignore entries for unknown buckets/collections.
         continue;
       }
-      const client = _clients.get(key);
-      if (client.bucketName != bucket) {
-        continue;
-      }
+
       try {
         await client.maybeSync(lastModified, serverTimeMillis, {loadDump});
       } catch (e) {
         if (!firstError) {
           firstError = e;
         }
       }
     }