Skip to content

Instantly share code, notes, and snippets.

@leplatrem
Last active July 5, 2019 11:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leplatrem/35ec71460e5567ced2ce88c6ad3127f9 to your computer and use it in GitHub Desktop.
Save leplatrem/35ec71460e5567ced2ce88c6ad3127f9 to your computer and use it in GitHub Desktop.
notes for Bug 1563226

Some tests to write:

  • On initialization, if the record is already in the local DB, the signal is sent with the URI
  • When the sync event has a created record, its attachment is downloaded
  • When the sync event has an updated record, its attachment is downloaded
  • When the sync event downloads an attachment, a signal is sent with the URI

https://firefox-source-docs.mozilla.org/services/common/services/RemoteSettings.html#unit-tests

In order to verify that a signal was sent:

const promiseSignal = TestUtils.topicObserved("psl-updated");

client.emit("sync", {...});

const observed = await promiseSignal;
console.log(observed); // --> fileURI somewhere, [0] ? :)
/* 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";
const { RemoteSettings } = ChromeUtils.import("resource://services-settings/remote-settings.js");
const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
const EXPORTED_SYMBOLS = ["PublicSuffixList"];
const RECORD_ID = "tld-dafsa";
var PublicSuffixList = {
init() {
const client = RemoteSettings("public-suffix-list");
client.on("sync", this.onUpdate.bind(this));
// We have a single record for this collection. Let's see if we already have it locally.
// Note that on startup, we don't need to synchronize immediately on new profiles.
client.get({ syncIfEmpty: false, filters: { id: RECORD_ID } })
.then((records) => {
if (records.length == 1) {
const { attachment } = records[0];
// Download (or do nothing if already downloaded)
client.attachments.download(attachment)
.then((fileURI) => {
// Send a signal so that the C++ code loads the updated list on startup.
this.notifyUpdate(fileURI);
});
}
});
},
notifyUpdate(fileURI) {
Services.obs.notifyObservers(null, "public-suffic-list-updated", fileURI);
},
onUpdate({ data: { created, updated, deleted } }) {
// In theory, this will never happen, we will never delete the record.
if (deleted.length == 1) {
await client.attachments.delete(deleted[0].attachment);
}
// Handle creation and update the same way :)
const changed = created.concat(updated.map(u => u.new));
// In theory, we should never have more than one record. And if we receive
// this event, it's because the single record was updated.
if (changed.length != 1) {
console.warn("Unsupported sync event for Public Suffic List");
return;
}
// Download the updated file.
const fileURL = await client.attachments.download(changed[0].attachment);
// Notify the C++ part to reload it from disk.
this.notifyUpdate(fileURL);
},
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment