DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Mercurial (409f3966645a)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
/* 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";

var EXPORTED_SYMBOLS = ["LoginManagerContextMenu"];

ChromeUtils.import("resource://gre/modules/XPCOMUtils.jsm");
ChromeUtils.import("resource://gre/modules/Services.jsm");

ChromeUtils.defineModuleGetter(this, "LoginHelper",
                               "resource://gre/modules/LoginHelper.jsm");
ChromeUtils.defineModuleGetter(this, "LoginManagerParent",
                               "resource://gre/modules/LoginManagerParent.jsm");

/*
 * Password manager object for the browser contextual menu.
 */
var LoginManagerContextMenu = {
  /**
   * Look for login items and add them to the contextual menu.
   *
   * @param {HTMLInputElement} inputElement
   *        The target input element of the context menu click.
   * @param {xul:browser} browser
   *        The browser for the document the context menu was open on.
   * @param {nsIURI} documentURI
   *        The URI of the document that the context menu was activated from.
   *        This isn't the same as the browser's top-level document URI
   *        when subframes are involved.
   * @returns {DocumentFragment} a document fragment with all the login items.
   */
  addLoginsToMenu(inputElement, browser, documentURI) {
    let foundLogins = this._findLogins(documentURI);

    if (!foundLogins.length) {
      return null;
    }

    let fragment = browser.ownerDocument.createDocumentFragment();
    let duplicateUsernames = this._findDuplicates(foundLogins);
    for (let login of foundLogins) {
        let item = fragment.ownerDocument.createXULElement("menuitem");

        let username = login.username;
        // If login is empty or duplicated we want to append a modification date to it.
        if (!username || duplicateUsernames.has(username)) {
          if (!username) {
            username = this._getLocalizedString("noUsername");
          }
          let meta = login.QueryInterface(Ci.nsILoginMetaInfo);
          let time = this.dateAndTimeFormatter.format(new Date(meta.timePasswordChanged));
          username = this._getLocalizedString("loginHostAge", [username, time]);
        }
        item.setAttribute("label", username);
        item.setAttribute("class", "context-login-item");

        // login is bound so we can keep the reference to each object.
        item.addEventListener("command", function(login, event) {
          this._fillTargetField(login, inputElement, browser, documentURI);
        }.bind(this, login));

        fragment.appendChild(item);
    }

    return fragment;
  },

  /**
   * Undoes the work of addLoginsToMenu for the same menu.
   *
   * @param {Document}
   *        The context menu owner document.
   */
  clearLoginsFromMenu(document) {
    let loginItems = document.getElementsByClassName("context-login-item");
    while (loginItems.item(0)) {
      loginItems.item(0).remove();
    }
  },

  /**
   * Find logins for the current URI.
   *
   * @param {nsIURI} documentURI
   *        URI object with the hostname of the logins we want to find.
   *        This isn't the same as the browser's top-level document URI
   *        when subframes are involved.
   *
   * @returns {nsILoginInfo[]} a login list
   */
  _findLogins(documentURI) {
    let searchParams = {
      hostname: documentURI.displayPrePath,
      schemeUpgrades: LoginHelper.schemeUpgrades,
    };
    let logins = LoginHelper.searchLoginsWithObject(searchParams);
    let resolveBy = [
      "scheme",
      "timePasswordChanged",
    ];
    logins = LoginHelper.dedupeLogins(logins, ["username", "password"], resolveBy, documentURI.displayPrePath);

    // Sort logins in alphabetical order and by date.
    logins.sort((loginA, loginB) => {
      // Sort alphabetically
      let result = loginA.username.localeCompare(loginB.username);
      if (result) {
        // Forces empty logins to be at the end
        if (!loginA.username) {
          return 1;
        }
        if (!loginB.username) {
          return -1;
        }
        return result;
      }

      // Same username logins are sorted by last change date
      let metaA = loginA.QueryInterface(Ci.nsILoginMetaInfo);
      let metaB = loginB.QueryInterface(Ci.nsILoginMetaInfo);
      return metaB.timePasswordChanged - metaA.timePasswordChanged;
    });

    return logins;
  },

  /**
   * Find duplicate usernames in a login list.
   *
   * @param {nsILoginInfo[]} loginList
   *        A list of logins we want to look for duplicate usernames.
   *
   * @returns {Set} a set with the duplicate usernames.
   */
  _findDuplicates(loginList) {
    let seen = new Set();
    let duplicates = new Set();
    for (let login of loginList) {
      if (seen.has(login.username)) {
        duplicates.add(login.username);
      }
      seen.add(login.username);
    }
    return duplicates;
  },

  /**
   * @param {nsILoginInfo} login
   *        The login we want to fill the form with.
   * @param {Element} inputElement
   *        The target input element we want to fill.
   * @param {xul:browser} browser
   *        The target tab browser.
   * @param {nsIURI} documentURI
   *        URI of the document owning the form we want to fill.
   *        This isn't the same as the browser's top-level
   *        document URI when subframes are involved.
   */
  _fillTargetField(login, inputElement, browser, documentURI) {
    LoginManagerParent.fillForm({
      browser,
      loginFormOrigin: documentURI.displayPrePath,
      login,
      inputElement,
    }).catch(Cu.reportError);
  },

  /**
   * @param {string} key
   *        The localized string key
   * @param {string[]} formatArgs
   *        An array of formatting argument string
   *
   * @returns {string} the localized string for the specified key,
   *          formatted with arguments if required.
   */
  _getLocalizedString(key, formatArgs) {
    if (formatArgs) {
      return this._stringBundle.formatStringFromName(key, formatArgs, formatArgs.length);
    }
    return this._stringBundle.GetStringFromName(key);
  },
};

XPCOMUtils.defineLazyGetter(LoginManagerContextMenu, "_stringBundle", function() {
  return Services.strings.
         createBundle("chrome://passwordmgr/locale/passwordmgr.properties");
});

XPCOMUtils.defineLazyGetter(LoginManagerContextMenu, "dateAndTimeFormatter", function() {
  return new Services.intl.DateTimeFormat(undefined, {
    dateStyle: "medium",
  });
});