Bug 1330467 - part 12. FPI isolation of translation permissions; r=johannh
authorLiang-Heng Chen <xeonchen@gmail.com>
Wed, 08 May 2019 22:05:53 +0000
changeset 473402 0229d5353d503441ed11643c8e5e4e90011261b8
parent 473401 2f2308fe574799b90705b7f55c402b9dd578369d
child 473403 1760c6913c094a2364658cbd73131383e452d890
push id35996
push userdvarga@mozilla.com
push dateFri, 10 May 2019 21:46:48 +0000
treeherdermozilla-central@362df4629f8f [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjohannh
bugs1330467
milestone68.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1330467 - part 12. FPI isolation of translation permissions; r=johannh Differential Revision: https://phabricator.services.mozilla.com/D19933
browser/components/translation/Translation.jsm
browser/components/translation/content/translation-notification.js
browser/components/translation/test/browser_translation_exceptions.js
--- a/browser/components/translation/Translation.jsm
+++ b/browser/components/translation/Translation.jsm
@@ -63,17 +63,17 @@ var Translation = {
                                                  : aData.state;
     trUI.detectedLanguage = aData.detectedLanguage;
     trUI.translatedFrom = aData.translatedFrom;
     trUI.translatedTo = aData.translatedTo;
     trUI.originalShown = aData.originalShown;
 
     trUI.showURLBarIcon();
 
-    if (trUI.shouldShowInfoBar(aBrowser.currentURI))
+    if (trUI.shouldShowInfoBar(aBrowser.contentPrincipal))
       trUI.showTranslationInfoBar();
   },
 
   openProviderAttribution() {
     let attribution = this.supportedEngines[this.translationEngine];
     const {BrowserWindowTracker} = ChromeUtils.import("resource:///modules/BrowserWindowTracker.jsm");
     BrowserWindowTracker.getTopWindow().openWebLinkIn(attribution, "tab");
   },
@@ -229,33 +229,33 @@ TranslationUI.prototype = {
     let notificationBox = this.notificationBox;
     let notif = notificationBox.appendNotification("", "translation", null,
       notificationBox.PRIORITY_INFO_HIGH, null, null,
       "translation-notification");
     notif.init(this);
     return notif;
   },
 
-  shouldShowInfoBar(aURI) {
+  shouldShowInfoBar(aPrincipal) {
     // Never show the infobar automatically while the translation
     // service is temporarily unavailable.
     if (Translation.serviceUnavailable)
       return false;
 
     // Check if we should never show the infobar for this language.
     let neverForLangs =
       Services.prefs.getCharPref("browser.translation.neverForLanguages");
     if (neverForLangs.split(",").includes(this.detectedLanguage)) {
       TranslationTelemetry.recordAutoRejectedTranslationOffer();
       return false;
     }
 
     // or if we should never show the infobar for this domain.
     let perms = Services.perms;
-    if (perms.testExactPermission(aURI, "translate") == perms.DENY_ACTION) {
+    if (perms.testExactPermissionFromPrincipal(aPrincipal, "translate") == perms.DENY_ACTION) {
       TranslationTelemetry.recordAutoRejectedTranslationOffer();
       return false;
     }
 
     return true;
   },
 
   receiveMessage(msg) {
--- a/browser/components/translation/content/translation-notification.js
+++ b/browser/components/translation/content/translation-notification.js
@@ -307,40 +307,40 @@ class MozTranslationNotification extends
 
     // We may need to disable the menuitems if they have already been used.
     // Check if translation is already disabled for this language:
     let neverForLangs =
       Services.prefs.getCharPref("browser.translation.neverForLanguages");
     item.disabled = neverForLangs.split(",").includes(lang);
 
     // Check if translation is disabled for the domain:
-    let uri = this.translation.browser.currentURI;
+    let principal = this.translation.browser.contentPrincipal;
     let perms = Services.perms;
     item = this._getAnonElt("neverForSite");
     item.disabled =
-      perms.testExactPermission(uri, "translate") == perms.DENY_ACTION;
+      perms.testExactPermissionFromPrincipal(principal, "translate") == perms.DENY_ACTION;
   }
 
   neverForLanguage() {
     const kPrefName = "browser.translation.neverForLanguages";
 
     let val = Services.prefs.getCharPref(kPrefName);
     if (val)
       val += ",";
     val += this._getAnonElt("neverForLanguage").langCode;
 
     Services.prefs.setCharPref(kPrefName, val);
 
     this.closeCommand();
   }
 
   neverForSite() {
-    let uri = this.translation.browser.currentURI;
+    let principal = this.translation.browser.contentPrincipal;
     let perms = Services.perms;
-    perms.add(uri, "translate", perms.DENY_ACTION);
+    perms.addFromPrincipal(principal, "translate", perms.DENY_ACTION);
 
     this.closeCommand();
   }
 
   openProviderAttribution() {
     Translation.openProviderAttribution();
   }
 }
--- a/browser/components/translation/test/browser_translation_exceptions.js
+++ b/browser/components/translation/test/browser_translation_exceptions.js
@@ -107,18 +107,18 @@ var gTests = [
     // Show the infobar for example.com and fr.
     Translation.documentStateReceived(gBrowser.selectedBrowser,
                                       {state: Translation.STATE_OFFER,
                                        originalShown: true,
                                        detectedLanguage: "fr"});
     let notif = await getInfoBar();
     ok(notif, "the infobar is visible");
     let ui = gBrowser.selectedBrowser.translationUI;
-    let uri = gBrowser.selectedBrowser.currentURI;
-    ok(ui.shouldShowInfoBar(uri, "fr"),
+    let principal = gBrowser.selectedBrowser.contentPrincipal;
+    ok(ui.shouldShowInfoBar(principal, "fr"),
        "check shouldShowInfoBar initially returns true");
 
     // Open the "options" drop down.
     await openPopup(notif._getAnonElt("options"));
     ok(notif._getAnonElt("options").getAttribute("open"),
        "the options menu is open");
 
     // Check that the item is not disabled.
@@ -129,17 +129,17 @@ var gTests = [
     notif._getAnonElt("neverForLanguage").click();
     notif = await getInfoBar();
     ok(!notif, "infobar hidden");
 
     // Check this has been saved to the exceptions list.
     let langs = getLanguageExceptions();
     is(langs.length, 1, "one language in the exception list");
     is(langs[0], "fr", "correct language in the exception list");
-    ok(!ui.shouldShowInfoBar(uri, "fr"),
+    ok(!ui.shouldShowInfoBar(principal, "fr"),
        "the infobar wouldn't be shown anymore");
 
     // Reopen the infobar.
     PopupNotifications.getNotification("translate").anchorElement.click();
     notif = await getInfoBar();
     // Open the "options" drop down.
     await openPopup(notif._getAnonElt("options"));
     ok(notif._getAnonElt("neverForLanguage").disabled,
@@ -157,18 +157,18 @@ var gTests = [
     // Show the infobar for example.com and fr.
     Translation.documentStateReceived(gBrowser.selectedBrowser,
                                       {state: Translation.STATE_OFFER,
                                        originalShown: true,
                                        detectedLanguage: "fr"});
     let notif = await getInfoBar();
     ok(notif, "the infobar is visible");
     let ui = gBrowser.selectedBrowser.translationUI;
-    let uri = gBrowser.selectedBrowser.currentURI;
-    ok(ui.shouldShowInfoBar(uri, "fr"),
+    let principal = gBrowser.selectedBrowser.contentPrincipal;
+    ok(ui.shouldShowInfoBar(principal, "fr"),
        "check shouldShowInfoBar initially returns true");
 
     // Open the "options" drop down.
     await openPopup(notif._getAnonElt("options"));
     ok(notif._getAnonElt("options").getAttribute("open"),
        "the options menu is open");
 
     // Check that the item is not disabled.
@@ -179,17 +179,17 @@ var gTests = [
     notif._getAnonElt("neverForSite").click();
     notif = await getInfoBar();
     ok(!notif, "infobar hidden");
 
     // Check this has been saved to the exceptions list.
     let sites = getDomainExceptions();
     is(sites.length, 1, "one site in the exception list");
     is(sites[0].origin, "http://example.com", "correct site in the exception list");
-    ok(!ui.shouldShowInfoBar(uri, "fr"),
+    ok(!ui.shouldShowInfoBar(principal, "fr"),
        "the infobar wouldn't be shown anymore");
 
     // Reopen the infobar.
     PopupNotifications.getNotification("translate").anchorElement.click();
     notif = await getInfoBar();
     // Open the "options" drop down.
     await openPopup(notif._getAnonElt("options"));
     ok(notif._getAnonElt("neverForSite").disabled,