Bug 1058438 - Delegate disabledHost APIs to the permission manager. r=MattN draft
authorKapeel Sable <kapeels42@gmail.com>
Wed, 06 Jan 2016 17:57:40 +0530
changeset 386847 147ce7b518177076e019fab349a27e59fddcb3f8
parent 386302 214884d507ee369c1cf14edb26527c4f9a97bf48
child 386848 2aea4bd6c346195b737e37bd7e303c39c19ebccd
child 386858 b7f2d13288ac08e79aa1d90467fdeb0374927e4d
push id22825
push usersaad@saadquadri.com
push dateTue, 12 Jul 2016 21:59:20 +0000
reviewersMattN
bugs1058438
milestone50.0a1
Bug 1058438 - Delegate disabledHost APIs to the permission manager. r=MattN MozReview-Commit-ID: 2qaUCZjqc47
toolkit/components/passwordmgr/LoginStore.jsm
toolkit/components/passwordmgr/storage-json.js
--- a/toolkit/components/passwordmgr/LoginStore.jsm
+++ b/toolkit/components/passwordmgr/LoginStore.jsm
@@ -263,16 +263,18 @@ LoginStore.prototype = {
    * Synchronously work on the data just loaded into memory.
    */
   _processLoadedData: function ()
   {
     // Create any arrays that are not present in the saved file.
     if (!this.data.logins) {
       this.data.logins = [];
     }
+
+    // Stub needed for login imports before data has been migrated.
     if (!this.data.disabledHosts) {
       this.data.disabledHosts = [];
     }
 
     // Indicate that the current version of the code has touched the file.
     this.data.version = kDataVersion;
 
     this.dataReady = true;
--- a/toolkit/components/passwordmgr/storage-json.js
+++ b/toolkit/components/passwordmgr/storage-json.js
@@ -22,16 +22,19 @@ XPCOMUtils.defineLazyModuleGetter(this, 
                                   "resource://gre/modules/LoginStore.jsm");
 XPCOMUtils.defineLazyModuleGetter(this, "OS",
                                   "resource://gre/modules/osfile.jsm");
 
 XPCOMUtils.defineLazyServiceGetter(this, "gUUIDGenerator",
                                    "@mozilla.org/uuid-generator;1",
                                    "nsIUUIDGenerator");
 
+// The permission type we store in the permission manager.
+const PERMISSION_TYPE_LOGIN = "login-saving";
+
 this.LoginManagerStorage_json = function () {};
 
 this.LoginManagerStorage_json.prototype = {
   classID: Components.ID("{c00c432d-a0c9-46d7-bef6-9c45b4d07341}"),
   QueryInterface: XPCOMUtils.generateQI([Ci.nsILoginManagerStorage]),
 
   __crypto: null,  // nsILoginManagerCrypto service
   get _crypto() {
@@ -79,18 +82,33 @@ this.LoginManagerStorage_json.prototype 
           // prevent us from marking the operation as completed.
           // At the next startup, we will not try the import again.
           yield loginImport.import().catch(Cu.reportError);
           this._store.saveSoon();
         }
 
         // We won't attempt import again on next startup.
         Services.prefs.setBoolPref("signon.importedFromSqlite", true);
-      }.bind(this)).catch(Cu.reportError);
+      }.bind(this)).catch(Cu.reportError)
+      .then(Task.spawn(function () {
+        // If the storage has a disabledHosts entry we migrate them
+        // to the permissions manager (bug 1058438)
+        if (!this._store.data || !this._store.data.disabledHosts) {
+          return; // already migrated.
+        }
+        for (let host of this._store.data.disabledHosts) {
+          let uri = Services.io.newURI(host);
+          Services.perms.add(uri, PERMISSION_TYPE_LOGIN,
+                             Ci.nsIPermissionManager.DENY_ACTION);
+        }
+        delete this._store.data.disabledHosts;
+        this._store.saveSoon();
+      }.bind(this))).catch(Cu.reportError);
     } catch (e) {
+      dump("Initialization failed: " + e + "\n");
       this.log("Initialization failed:", e);
       throw new Error("Initialization failed");
     }
   },
 
   /**
    * Internal method used by regression tests only.  It is called before
    * replacing this storage module with a new instance.
@@ -361,68 +379,67 @@ this.LoginManagerStorage_json.prototype 
 
     this.log("_searchLogins: returning", foundLogins.length, "logins for", matchData,
              "with options", aOptions);
     return [foundLogins, foundIds];
   },
 
   /**
    * Removes all logins from storage.
-   *
-   * Disabled hosts are kept, as one presumably doesn't want to erase those.
    */
   removeAllLogins() {
     this._store.ensureDataReady();
 
     this.log("Removing all logins");
     this._store.data.logins = [];
     this._store.saveSoon();
 
     this._sendNotification("removeAllLogins", null);
   },
 
+  // All disabledHost APIs are now delegated to the permission manager
+  // after issuing a deprecation warning.
   getAllDisabledHosts(count) {
-    this._store.ensureDataReady();
+    let disabledHosts = [];
+    let enumerator = Services.perms.enumerator;
 
-    let disabledHosts = this._store.data.disabledHosts.slice(0);
+    while (enumerator.hasMoreElements()) {
+      let perm = enumerator.getNext();
+      if (perm.type == PERMISSION_TYPE_LOGIN
+          && perm.capability == Services.perms.DENY_ACTION) {
+        disabledHosts.push(perm.principal.URI.prePath);
+      }
+    }
+
+    if (count)
+      count.value = disabledHosts.length; // needed for XPCOM
 
     this.log("_getAllDisabledHosts: returning", disabledHosts.length, "disabled hosts.");
-    if (count)
-      count.value = disabledHosts.length; // needed for XPCOM
     return disabledHosts;
   },
 
-  getLoginSavingEnabled(hostname) {
-    this._store.ensureDataReady();
-
-    this.log("Getting login saving is enabled for", hostname);
-    return this._store.data.disabledHosts.indexOf(hostname) == -1;
+  getLoginSavingEnabled(origin) {
+    let uri = Services.io.newURI(origin, null, null);
+    this.log("Getting login saving is enabled for", origin);
+    return Services.perms.testPermission(uri, PERMISSION_TYPE_LOGIN) != Services.perms.DENY_ACTION;
   },
 
-  setLoginSavingEnabled(hostname, enabled) {
-    this._store.ensureDataReady();
-
+  setLoginSavingEnabled(origin, enabled) {
     // Throws if there are bogus values.
-    LoginHelper.checkHostnameValue(hostname);
+    LoginHelper.checkHostnameValue(origin);
 
-    this.log("Setting login saving enabled for", hostname, "to", enabled);
-    let foundIndex = this._store.data.disabledHosts.indexOf(hostname);
+    this.log("Setting login saving enabled for", origin, "to", enabled);
+    let uri = Services.io.newURI(origin, null, null);
     if (enabled) {
-      if (foundIndex != -1) {
-        this._store.data.disabledHosts.splice(foundIndex, 1);
-        this._store.saveSoon();
-      }
+      Services.perms.remove(uri, PERMISSION_TYPE_LOGIN);
     } else {
-      if (foundIndex == -1) {
-        this._store.data.disabledHosts.push(hostname);
-        this._store.saveSoon();
-      }
+      Services.perms.add(uri, PERMISSION_TYPE_LOGIN, Services.perms.DENY_ACTION);
     }
 
-    this._sendNotification(enabled ? "hostSavingEnabled" : "hostSavingDisabled", hostname);
+    this._sendNotification(enabled ? "hostSavingEnabled" : "hostSavingDisabled", origin);
   },
 
   findLogins(count, hostname, formSubmitURL, httpRealm) {
     let loginData = {
       hostname: hostname,
       formSubmitURL: formSubmitURL,
       httpRealm: httpRealm
     };