Bug 729485 - Add "Don't ask again for this site" option to notifications created by ContentPermissionPrompt.js. r=mbrubeck
authorMargaret Leibovic <margaret.leibovic@gmail.com>
Wed, 16 May 2012 12:00:40 -0700
changeset 95596 62b0be6431ad638ed3c5267d1871de918818e259
parent 95595 553bea8ea04e0940b02a7838e4d881b543337aa7
child 95597 023c65a66288e5d828647bb81a0cc2954d027eb3
push idunknown
push userunknown
push dateunknown
reviewersmbrubeck
bugs729485
milestone15.0a1
Bug 729485 - Add "Don't ask again for this site" option to notifications created by ContentPermissionPrompt.js. r=mbrubeck
mobile/android/components/ContentPermissionPrompt.js
mobile/android/locales/en-US/chrome/browser.properties
--- a/mobile/android/components/ContentPermissionPrompt.js
+++ b/mobile/android/components/ContentPermissionPrompt.js
@@ -38,49 +38,18 @@
 const Ci = Components.interfaces;
 const Cr = Components.results;
 const Cu = Components.utils;
 const Cc = Components.classes;
 
 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
 Cu.import("resource://gre/modules/Services.jsm");
 
-const kCountBeforeWeRemember = 5;
-
-function dump(a) {
-    Cc["@mozilla.org/consoleservice;1"]
-        .getService(Ci.nsIConsoleService)
-        .logStringMessage(a);
-}
-
-function setPagePermission(type, uri, allow) {
-  let pm = Services.perms;
-  let contentPrefs = Services.contentPrefs;
-  let contentPrefName = type + ".request.remember";
-
-  if (!contentPrefs.hasPref(uri, contentPrefName))
-      contentPrefs.setPref(uri, contentPrefName, 0);
-
-  let count = contentPrefs.getPref(uri, contentPrefName);
-
-  if (allow == false)
-    count--;
-  else
-    count++;
-    
-  contentPrefs.setPref(uri, contentPrefName, count);
-  if (count == kCountBeforeWeRemember)
-    pm.add(uri, type, Ci.nsIPermissionManager.ALLOW_ACTION);
-  else if (count == -kCountBeforeWeRemember)
-    pm.add(uri, type, Ci.nsIPermissionManager.DENY_ACTION);
-}
-
-const kEntities = { "geolocation": "geolocation", "desktop-notification": "desktopNotification",
-                    "indexedDB": "offlineApps", "indexedDBQuota": "indexedDBQuota",
-                    "openWebappsManage": "openWebappsManage" };
+const kEntities = { "geolocation": "geolocation",
+                    "desktop-notification": "desktopNotification" };
 
 function ContentPermissionPrompt() {}
 
 ContentPermissionPrompt.prototype = {
   classID: Components.ID("{C6E8C44D-9F39-4AF7-BCC0-76E38A8310F5}"),
 
   QueryInterface: XPCOMUtils.generateQI([Ci.nsIContentPermissionPrompt]),
 
@@ -111,61 +80,55 @@ ContentPermissionPrompt.prototype = {
   getChromeForRequest: function getChromeForRequest(request) {
     if (request.window) {
       let requestingWindow = request.window.top;
       return this.getChromeWindow(requestingWindow).wrappedJSObject;
     }
     return request.element.ownerDocument.defaultView;
   },
 
-  getTabForRequest: function getTabForRequest(request) {
-    let chromeWin = this.getChromeForRequest(request);
-    if (request.window) {
-      let browser = chromeWin.BrowserApp.getBrowserForWindow(request.window);
-      let tabID = chromeWin.BrowserApp.getTabForBrowser(browser).id;
-      return tabID;
-    }
-    // Fix this if e10s is needed again
-    return null;
-  },
-
   prompt: function(request) {
-    // returns true if the request was handled
+    // Returns true if the request was handled
     if (this.handleExistingPermission(request))
        return;
 
-    let pm = Services.perms;
+    let chromeWin = this.getChromeForRequest(request);
+    let tab = chromeWin.BrowserApp.getTabForWindow(request.window);
+    if (!tab)
+      return;
+
     let browserBundle = Services.strings.createBundle("chrome://browser/locale/browser.properties");
-
     let entityName = kEntities[request.type];
 
-    let tabID = this.getTabForRequest(request);
-    let chromeWin = this.getChromeForRequest(request);
-
     let buttons = [{
       label: browserBundle.GetStringFromName(entityName + ".allow"),
-      accessKey: null,
-      callback: function(notification) {
-        setPagePermission(request.type, request.uri, true);
+      callback: function(aChecked) {
+        // If the user checked "Don't ask again", make a permanent exception
+        if (aChecked)
+          Services.perms.add(request.uri, request.type, Ci.nsIPermissionManager.ALLOW_ACTION);
+
         request.allow();
       }
     },
     {
       label: browserBundle.GetStringFromName(entityName + ".dontAllow"),
-      accessKey: null,
-      callback: function(notification) {
-        setPagePermission(request.type, request.uri, false);
+      callback: function(aChecked) {
+        // If the user checked "Don't ask again", make a permanent exception
+        if (aChecked)
+          Services.perms.add(request.uri, request.type, Ci.nsIPermissionManager.DENY_ACTION);
+
         request.cancel();
       }
     }];
 
     let message = browserBundle.formatStringFromName(entityName + ".wantsTo",
                                                      [request.uri.host], 1);
+    let options = { checkbox: browserBundle.GetStringFromName(entityName + ".dontAskAgain") };
 
     chromeWin.NativeWindow.doorhanger.show(message,
                                            entityName + request.uri.host,
-                                           buttons, tabID);
+                                           buttons, tab.id, options);
   }
 };
 
 
 //module initialization
 const NSGetFactory = XPCOMUtils.generateNSGetFactory([ContentPermissionPrompt]);
--- a/mobile/android/locales/en-US/chrome/browser.properties
+++ b/mobile/android/locales/en-US/chrome/browser.properties
@@ -81,24 +81,30 @@ identity.ownerUnknown2=(unknown)
 geolocation.allow=Share
 geolocation.dontAllow=Don't share
 geolocation.alwaysAllow=Always Share
 geolocation.neverAllow=Never Share
 geolocation.wantsTo=%S wants your location.
 # LOCALIZATION NOTE (geolocation.shareLocation): Label that will be used in
 # site settings dialog.
 geolocation.shareLocation=Share Location
+# LOCALIZATION NOTE (geolocation.dontAskAgain): This label appears next to a
+# checkbox to indicate whether or not the user wants to make a permanent decision.
+geolocation.dontAskAgain=Don't ask again for this site
 
 # Desktop notification UI
 desktopNotification.allow=Allow
 desktopNotification.dontAllow=Don't allow
 desktopNotification.wantsTo=%S wants to use notifications.
 # LOCALIZATION NOTE (desktopNotification.useNotifications): Label that will be
 # used in site settings dialog.
 desktopNotification.useNotifications=Use Notifications
+# LOCALIZATION NOTE (desktopNotification.dontAskAgain): This label appears next to a
+# checkbox to indicate whether or not the user wants to make a permanent decision.
+desktopNotification.dontAskAgain=Don't ask again for this site
 
 # New Tab Popup
 # LOCALIZATION NOTE (newtabpopup): Semi-colon list of plural forms.
 # See: http://developer.mozilla.org/en/docs/Localization_and_Plurals
 # #1 number of tabs
 newtabpopup.opened=New tab opened;#1 new tabs opened
 
 # Error Console