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 (5b81998bb7ab)

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 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
/* 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";

const EXPORTED_SYMBOLS = ["SignInToWebsiteUX"];

const Cc = Components.classes;
const Ci = Components.interfaces;
const Cu = Components.utils;

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

XPCOMUtils.defineLazyModuleGetter(this, "IdentityService",
                                  "resource://gre/modules/identity/Identity.jsm");

XPCOMUtils.defineLazyModuleGetter(this, "Logger",
                                  "resource://gre/modules/identity/LogUtils.jsm");

function log(...aMessageArgs) {
  Logger.log.apply(Logger, ["SignInToWebsiteUX"].concat(aMessageArgs));
}

let SignInToWebsiteUX = {

  init: function SignInToWebsiteUX_init() {
    Services.obs.addObserver(this, "identity-request", false);
    Services.obs.addObserver(this, "identity-auth", false);
    Services.obs.addObserver(this, "identity-auth-complete", false);
    Services.obs.addObserver(this, "identity-login-state-changed", false);
  },

  uninit: function SignInToWebsiteUX_uninit() {
    Services.obs.removeObserver(this, "identity-request");
    Services.obs.removeObserver(this, "identity-auth");
    Services.obs.removeObserver(this, "identity-auth-complete");
    Services.obs.removeObserver(this, "identity-login-state-changed");
  },

  observe: function SignInToWebsiteUX_observe(aSubject, aTopic, aData) {
    log("observe: received", aTopic, "with", aData, "for", aSubject);
    let options = null;
    if (aSubject) {
      options = aSubject.wrappedJSObject;
    }
    switch(aTopic) {
      case "identity-request":
        this.requestLogin(options);
        break;
      case "identity-auth":
        this._openAuthenticationUI(aData, options);
        break;
      case "identity-auth-complete":
        this._closeAuthenticationUI(aData);
        break;
      case "identity-login-state-changed":
        let emailAddress = aData;
        if (emailAddress) {
          this._removeRequestUI(options);
          this._showLoggedInUI(emailAddress, options);
        } else {
          this._removeLoggedInUI(options);
        }
        break;
      default:
        Logger.reportError("SignInToWebsiteUX", "Unknown observer notification:", aTopic);
        break;
    }
  },

  /**
   * The website is requesting login so the user must choose an identity to use.
   */
  requestLogin: function SignInToWebsiteUX_requestLogin(aOptions) {
    let windowID = aOptions.rpId;
    log("requestLogin", aOptions);
    let [chromeWin, browserEl] = this._getUIForWindowID(windowID);

    // message is not shown in the UI but is required
    let message = aOptions.origin;
    let mainAction = {
      label: chromeWin.gNavigatorBundle.getString("identity.next.label"),
      accessKey: chromeWin.gNavigatorBundle.getString("identity.next.accessKey"),
      callback: function() {}, // required
    };
    let options = {
      identity: {
        origin: aOptions.origin,
      },
    };
    let secondaryActions = [];

    // add some extra properties to the notification to store some identity-related state
    for (let opt in aOptions) {
      options.identity[opt] = aOptions[opt];
    }
    log("requestLogin: rpId: ", options.identity.rpId);

    chromeWin.PopupNotifications.show(browserEl, "identity-request", message,
                                      "identity-notification-icon", mainAction,
                                      [], options);
  },

  /**
   * Get the list of possible identities to login to the given origin.
   */
  getIdentitiesForSite: function SignInToWebsiteUX_getIdentitiesForSite(aOrigin) {
    return IdentityService.RP.getIdentitiesForSite(aOrigin);
  },

  /**
   * User chose a new or existing identity from the doorhanger after a request() call
   */
  selectIdentity: function SignInToWebsiteUX_selectIdentity(aRpId, aIdentity) {
    log("selectIdentity: rpId: ", aRpId, " identity: ", aIdentity);
    IdentityService.selectIdentity(aRpId, aIdentity);
  },

  // Private

  /**
   * Return the chrome window and <browser> for the given outer window ID.
   */
  _getUIForWindowID: function(aWindowID) {
    let someWindow = Services.wm.getMostRecentWindow("navigator:browser");
    if (!someWindow) {
      Logger.reportError("SignInToWebsiteUX", "no window");
      return [null, null];
    }

    let windowUtils = someWindow.QueryInterface(Ci.nsIInterfaceRequestor)
                                .getInterface(Ci.nsIDOMWindowUtils);
    let content = windowUtils.getOuterWindowWithId(aWindowID);

    if (content) {
      let browser = content.QueryInterface(Ci.nsIInterfaceRequestor)
                           .getInterface(Ci.nsIWebNavigation)
                           .QueryInterface(Ci.nsIDocShell).chromeEventHandler;
      let chromeWin = browser.ownerDocument.defaultView;
      return [chromeWin, browser];
    }
    Logger.reportError("SignInToWebsiteUX", "no content");

    return [null, null];
  },

  /**
   * Open UI with a content frame displaying aAuthURI so that the user can authenticate with their
   * IDP.  Then tell Identity.jsm the identifier for the window so that it knows that the DOM API
   * calls are for this authentication flow.
   */
  _openAuthenticationUI: function _openAuthenticationUI(aAuthURI, aContext) {
    // Open a tab/window with aAuthURI with an identifier (aID) attached so that the DOM APIs know this is an auth. window.
    let chromeWin = Services.wm.getMostRecentWindow('navigator:browser');
    let features = "chrome=false,width=640,height=480,centerscreen,location=yes,resizable=yes,scrollbars=yes,status=yes";
    log("aAuthURI: ", aAuthURI);
    let authWin = Services.ww.openWindow(chromeWin, "about:blank", "", features, null);
    let windowID = authWin.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindowUtils).outerWindowID;
    log("authWin outer id: ", windowID);

    let provId = aContext.provId;
    // Tell the ID service about the id before loading the url
    IdentityService.IDP.setAuthenticationFlow(windowID, provId);

    authWin.location = aAuthURI;
  },

  _closeAuthenticationUI: function _closeAuthenticationUI(aAuthId) {
    log("_closeAuthenticationUI:", aAuthId);
    let [chromeWin, browserEl] = this._getUIForWindowID(aAuthId);
    if (chromeWin)
      chromeWin.close();
    else
      Logger.reportError("SignInToWebsite", "Could not close window with ID", aAuthId);
  },

  /**
   * Show a doorhanger indicating the currently logged-in user.
   */
  _showLoggedInUI: function _showLoggedInUI(aIdentity, aContext) {
    let windowID = aContext.rpId;
    log("_showLoggedInUI for ", windowID);
    let [chromeWin, browserEl] = this._getUIForWindowID(windowID);

    let message = chromeWin.gNavigatorBundle.getFormattedString("identity.loggedIn.description",
                                                          [aIdentity]);
    let mainAction = {
      label: chromeWin.gNavigatorBundle.getString("identity.loggedIn.signOut.label"),
      accessKey: chromeWin.gNavigatorBundle.getString("identity.loggedIn.signOut.accessKey"),
      callback: function() {
        log("sign out callback fired");
        IdentityService.RP.logout(windowID);
      },
    };
    let secondaryActions = [];
    let options = {
      dismissed: true,
    };
    let loggedInNot = chromeWin.PopupNotifications.show(browserEl, "identity-logged-in", message,
                                                  "identity-notification-icon", mainAction,
                                                  secondaryActions, options);
    loggedInNot.rpId = windowID;
  },

  /**
   * Remove the doorhanger indicating the currently logged-in user.
   */
  _removeLoggedInUI: function _removeLoggedInUI(aContext) {
    let windowID = aContext.rpId;
    log("_removeLoggedInUI for ", windowID);
    if (!windowID)
      throw "_removeLoggedInUI: Invalid RP ID";
    let [chromeWin, browserEl] = this._getUIForWindowID(windowID);

    let loggedInNot = chromeWin.PopupNotifications.getNotification("identity-logged-in", browserEl);
    if (loggedInNot)
      chromeWin.PopupNotifications.remove(loggedInNot);
  },

  /**
   * Remove the doorhanger indicating the currently logged-in user.
   */
  _removeRequestUI: function _removeRequestUI(aContext) {
    let windowID = aContext.rpId;
    log("_removeRequestUI for ", windowID);
    let [chromeWin, browserEl] = this._getUIForWindowID(windowID);

    let requestNot = chromeWin.PopupNotifications.getNotification("identity-request", browserEl);
    if (requestNot)
      chromeWin.PopupNotifications.remove(requestNot);
  },

};