Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Support promises in privacy namespace #133

Merged
merged 1 commit into from Jul 11, 2018

Conversation

Rob--W
Copy link
Member

@Rob--W Rob--W commented Jun 19, 2018

This PR adds promise support for the common subset of methods in the privacy namespace that are supported by both Chrome and Firefox.
In the future, we can consider extending the promise support to all objects in the privacy namespace, even if they are only supported by one browser.

Note that CI will currently fail because of bug #81 (which will be fixed by #132).

Supersedes and closes #44.

Copy link
Member

@rpl rpl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @Rob--W this looks good to me, there is only a small fix that I'd really like to apply before merging this, that is the small one on the fixture described below (the other two comments are only small super-optional nits).

@@ -0,0 +1,10 @@
console.log(name, "background page loaded");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

name doesn't seem to be defined in this background script, this was meant to be the extension name retrieved from the manifest, like in the some of the other test extensions in the test/fixtures dir, e.g.

const {name} = browser.runtime.getManifest();

To be fair, it is fine with me if we opt to define name and leave the console.log, but it is also absolutely fine with me if we opt to remove these console.log (from line 1, 4 and 10).

@@ -0,0 +1,48 @@
test("privacy API should be unavailable in the content script", (t) => {
t.deepEqual(browser.privacy, undefined, "browser.privacy is not available in a content script");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit, s/is not/should not/ (only to make it clear what we expect)

// Invokes: browser.privacy.services.passwordSavingEnabled[method](...args);
let {type, result} = await browser.runtime.sendMessage({method, args});
// In Chrome `undefined` values are serialized to `null`, so use the
// pre-determined type instead.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit, how about rephrase this comment a bit? e.g. "so check the type as determined in the background page"

if (navigator.userAgent.includes("Firefox/")) {
t.equal(res, true, "passwordSavingEnabled.set() resolves to true");
} else {
t.equal(res, undefined, "passwordSavingEnabled.set() resolves to a void value");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note to self: this different behavior of set and clear seems something that we should list in the known incompatibilities in the updated README (iirc on Firefox set and clear returns true if the setting has been changes, false otherwise, on the contrary Chrome doesn't pass any parameter to the callback that is called once the operation has been completed).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like something that should be documented on MDN: https://developer.mozilla.org/en-US/Add-ons/WebExtensions/API/types/BrowserSetting

@Rob--W
Copy link
Member Author

Rob--W commented Jul 11, 2018

Done:

diff --git a/test/fixtures/privacy-setting-extension/background.js b/test/fixtures/privacy-setting-extension/background.js
index 37aab05..ef7bf14 100644
--- a/test/fixtures/privacy-setting-extension/background.js
+++ b/test/fixtures/privacy-setting-extension/background.js
@@ -1,10 +1,5 @@
-console.log(name, "background page loaded");
-
 browser.runtime.onMessage.addListener(async (msg) => {
-  console.log(name, "background received msg", {msg});
   let {method, args} = msg;
   let result = await browser.privacy.services.passwordSavingEnabled[method](...args);
   return {result, type: typeof result};
 });
-
-console.log(name, "background page ready to receive a content script message...");
diff --git a/test/fixtures/privacy-setting-extension/content.js b/test/fixtures/privacy-setting-extension/content.js
index 5a8ce46..78cc4ef 100644
--- a/test/fixtures/privacy-setting-extension/content.js
+++ b/test/fixtures/privacy-setting-extension/content.js
@@ -1,13 +1,13 @@
 test("privacy API should be unavailable in the content script", (t) => {
-  t.deepEqual(browser.privacy, undefined, "browser.privacy is not available in a content script");
+  t.deepEqual(browser.privacy, undefined, "browser.privacy should not be available in a content script");
 });
 
 test("privacy API should support promises", async (t) => {
   async function callSettingAPI(method, ...args) {
     // Invokes: browser.privacy.services.passwordSavingEnabled[method](...args);
     let {type, result} = await browser.runtime.sendMessage({method, args});
-    // In Chrome `undefined` values are serialized to `null`, so use the
-    // pre-determined type instead.
+    // In Chrome `undefined` values are serialized to `null`, so check the type
+    // as determined in the background page.
     return type === "undefined" ? undefined : result;
   }
 

@rpl rpl merged commit d77a418 into mozilla:master Jul 11, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants