Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | |
michael@0 | 6 | const Cc = Components.classes; |
michael@0 | 7 | const Ci = Components.interfaces; |
michael@0 | 8 | |
michael@0 | 9 | Components.utils.import("resource://gre/modules/XPCOMUtils.jsm"); |
michael@0 | 10 | Components.utils.import("resource://gre/modules/Services.jsm"); |
michael@0 | 11 | Components.utils.import("resource://gre/modules/PrivateBrowsingUtils.jsm"); |
michael@0 | 12 | |
michael@0 | 13 | XPCOMUtils.defineLazyModuleGetter(this, |
michael@0 | 14 | "LoginManagerContent", "resource://gre/modules/LoginManagerContent.jsm"); |
michael@0 | 15 | |
michael@0 | 16 | var debug = false; |
michael@0 | 17 | function log(...pieces) { |
michael@0 | 18 | function generateLogMessage(args) { |
michael@0 | 19 | let strings = ['Login Manager:']; |
michael@0 | 20 | |
michael@0 | 21 | args.forEach(function(arg) { |
michael@0 | 22 | if (typeof arg === 'string') { |
michael@0 | 23 | strings.push(arg); |
michael@0 | 24 | } else if (typeof arg === 'undefined') { |
michael@0 | 25 | strings.push('undefined'); |
michael@0 | 26 | } else if (arg === null) { |
michael@0 | 27 | strings.push('null'); |
michael@0 | 28 | } else { |
michael@0 | 29 | try { |
michael@0 | 30 | strings.push(JSON.stringify(arg, null, 2)); |
michael@0 | 31 | } catch(err) { |
michael@0 | 32 | strings.push("<<something>>"); |
michael@0 | 33 | } |
michael@0 | 34 | } |
michael@0 | 35 | }); |
michael@0 | 36 | return strings.join(' '); |
michael@0 | 37 | } |
michael@0 | 38 | |
michael@0 | 39 | if (!debug) |
michael@0 | 40 | return; |
michael@0 | 41 | |
michael@0 | 42 | let message = generateLogMessage(pieces); |
michael@0 | 43 | dump(message + "\n"); |
michael@0 | 44 | Services.console.logStringMessage(message); |
michael@0 | 45 | } |
michael@0 | 46 | |
michael@0 | 47 | function LoginManager() { |
michael@0 | 48 | this.init(); |
michael@0 | 49 | } |
michael@0 | 50 | |
michael@0 | 51 | LoginManager.prototype = { |
michael@0 | 52 | |
michael@0 | 53 | classID: Components.ID("{cb9e0de8-3598-4ed7-857b-827f011ad5d8}"), |
michael@0 | 54 | QueryInterface : XPCOMUtils.generateQI([Ci.nsILoginManager, |
michael@0 | 55 | Ci.nsISupportsWeakReference, |
michael@0 | 56 | Ci.nsIInterfaceRequestor]), |
michael@0 | 57 | getInterface : function(aIID) { |
michael@0 | 58 | if (aIID.equals(Ci.mozIStorageConnection) && this._storage) { |
michael@0 | 59 | let ir = this._storage.QueryInterface(Ci.nsIInterfaceRequestor); |
michael@0 | 60 | return ir.getInterface(aIID); |
michael@0 | 61 | } |
michael@0 | 62 | |
michael@0 | 63 | throw Cr.NS_ERROR_NO_INTERFACE; |
michael@0 | 64 | }, |
michael@0 | 65 | |
michael@0 | 66 | |
michael@0 | 67 | /* ---------- private memebers ---------- */ |
michael@0 | 68 | |
michael@0 | 69 | |
michael@0 | 70 | __formFillService : null, // FormFillController, for username autocompleting |
michael@0 | 71 | get _formFillService() { |
michael@0 | 72 | if (!this.__formFillService) |
michael@0 | 73 | this.__formFillService = |
michael@0 | 74 | Cc["@mozilla.org/satchel/form-fill-controller;1"]. |
michael@0 | 75 | getService(Ci.nsIFormFillController); |
michael@0 | 76 | return this.__formFillService; |
michael@0 | 77 | }, |
michael@0 | 78 | |
michael@0 | 79 | |
michael@0 | 80 | __storage : null, // Storage component which contains the saved logins |
michael@0 | 81 | get _storage() { |
michael@0 | 82 | if (!this.__storage) { |
michael@0 | 83 | |
michael@0 | 84 | var contractID = "@mozilla.org/login-manager/storage/mozStorage;1"; |
michael@0 | 85 | try { |
michael@0 | 86 | var catMan = Cc["@mozilla.org/categorymanager;1"]. |
michael@0 | 87 | getService(Ci.nsICategoryManager); |
michael@0 | 88 | contractID = catMan.getCategoryEntry("login-manager-storage", |
michael@0 | 89 | "nsILoginManagerStorage"); |
michael@0 | 90 | log("Found alternate nsILoginManagerStorage with contract ID:", contractID); |
michael@0 | 91 | } catch (e) { |
michael@0 | 92 | log("No alternate nsILoginManagerStorage registered"); |
michael@0 | 93 | } |
michael@0 | 94 | |
michael@0 | 95 | this.__storage = Cc[contractID]. |
michael@0 | 96 | createInstance(Ci.nsILoginManagerStorage); |
michael@0 | 97 | try { |
michael@0 | 98 | this.__storage.init(); |
michael@0 | 99 | } catch (e) { |
michael@0 | 100 | log("Initialization of storage component failed:", e); |
michael@0 | 101 | this.__storage = null; |
michael@0 | 102 | } |
michael@0 | 103 | } |
michael@0 | 104 | |
michael@0 | 105 | return this.__storage; |
michael@0 | 106 | }, |
michael@0 | 107 | |
michael@0 | 108 | _prefBranch : null, // Preferences service |
michael@0 | 109 | _remember : true, // mirrors signon.rememberSignons preference |
michael@0 | 110 | |
michael@0 | 111 | |
michael@0 | 112 | /* |
michael@0 | 113 | * init |
michael@0 | 114 | * |
michael@0 | 115 | * Initialize the Login Manager. Automatically called when service |
michael@0 | 116 | * is created. |
michael@0 | 117 | * |
michael@0 | 118 | * Note: Service created in /browser/base/content/browser.js, |
michael@0 | 119 | * delayedStartup() |
michael@0 | 120 | */ |
michael@0 | 121 | init : function () { |
michael@0 | 122 | |
michael@0 | 123 | // Cache references to current |this| in utility objects |
michael@0 | 124 | this._observer._pwmgr = this; |
michael@0 | 125 | |
michael@0 | 126 | // Preferences. Add observer so we get notified of changes. |
michael@0 | 127 | this._prefBranch = Services.prefs.getBranch("signon."); |
michael@0 | 128 | this._prefBranch.addObserver("", this._observer, false); |
michael@0 | 129 | |
michael@0 | 130 | // Get current preference values. |
michael@0 | 131 | debug = this._prefBranch.getBoolPref("debug"); |
michael@0 | 132 | |
michael@0 | 133 | this._remember = this._prefBranch.getBoolPref("rememberSignons"); |
michael@0 | 134 | |
michael@0 | 135 | // Form submit observer checks forms for new logins and pw changes. |
michael@0 | 136 | Services.obs.addObserver(this._observer, "xpcom-shutdown", false); |
michael@0 | 137 | |
michael@0 | 138 | // XXX gross hacky workaround for bug 881996. The WPL does nothing. |
michael@0 | 139 | var progress = Cc["@mozilla.org/docloaderservice;1"]. |
michael@0 | 140 | getService(Ci.nsIWebProgress); |
michael@0 | 141 | progress.addProgressListener(this._webProgressListener, |
michael@0 | 142 | Ci.nsIWebProgress.NOTIFY_STATE_DOCUMENT); |
michael@0 | 143 | }, |
michael@0 | 144 | |
michael@0 | 145 | |
michael@0 | 146 | /* ---------- Utility objects ---------- */ |
michael@0 | 147 | |
michael@0 | 148 | |
michael@0 | 149 | /* |
michael@0 | 150 | * _observer object |
michael@0 | 151 | * |
michael@0 | 152 | * Internal utility object, implements the nsIObserver interface. |
michael@0 | 153 | * Used to receive notification for: form submission, preference changes. |
michael@0 | 154 | */ |
michael@0 | 155 | _observer : { |
michael@0 | 156 | _pwmgr : null, |
michael@0 | 157 | |
michael@0 | 158 | QueryInterface : XPCOMUtils.generateQI([Ci.nsIObserver, |
michael@0 | 159 | Ci.nsISupportsWeakReference]), |
michael@0 | 160 | |
michael@0 | 161 | // nsObserver |
michael@0 | 162 | observe : function (subject, topic, data) { |
michael@0 | 163 | |
michael@0 | 164 | if (topic == "nsPref:changed") { |
michael@0 | 165 | var prefName = data; |
michael@0 | 166 | log("got change to", prefName, "preference"); |
michael@0 | 167 | |
michael@0 | 168 | if (prefName == "debug") { |
michael@0 | 169 | debug = this._pwmgr._prefBranch.getBoolPref("debug"); |
michael@0 | 170 | } else if (prefName == "rememberSignons") { |
michael@0 | 171 | this._pwmgr._remember = |
michael@0 | 172 | this._pwmgr._prefBranch.getBoolPref("rememberSignons"); |
michael@0 | 173 | } else { |
michael@0 | 174 | log("Oops! Pref not handled, change ignored."); |
michael@0 | 175 | } |
michael@0 | 176 | } else if (topic == "xpcom-shutdown") { |
michael@0 | 177 | for (let i in this._pwmgr) { |
michael@0 | 178 | try { |
michael@0 | 179 | this._pwmgr[i] = null; |
michael@0 | 180 | } catch(ex) {} |
michael@0 | 181 | } |
michael@0 | 182 | this._pwmgr = null; |
michael@0 | 183 | } else { |
michael@0 | 184 | log("Oops! Unexpected notification:", topic); |
michael@0 | 185 | } |
michael@0 | 186 | } |
michael@0 | 187 | }, |
michael@0 | 188 | |
michael@0 | 189 | |
michael@0 | 190 | _webProgressListener : { |
michael@0 | 191 | QueryInterface : XPCOMUtils.generateQI([Ci.nsIWebProgressListener, |
michael@0 | 192 | Ci.nsISupportsWeakReference]), |
michael@0 | 193 | onStateChange : function() { /* NOP */ }, |
michael@0 | 194 | onProgressChange : function() { throw "Unexpected onProgressChange"; }, |
michael@0 | 195 | onLocationChange : function() { throw "Unexpected onLocationChange"; }, |
michael@0 | 196 | onStatusChange : function() { throw "Unexpected onStatusChange"; }, |
michael@0 | 197 | onSecurityChange : function() { throw "Unexpected onSecurityChange"; } |
michael@0 | 198 | }, |
michael@0 | 199 | |
michael@0 | 200 | |
michael@0 | 201 | |
michael@0 | 202 | |
michael@0 | 203 | /* ---------- Primary Public interfaces ---------- */ |
michael@0 | 204 | |
michael@0 | 205 | |
michael@0 | 206 | |
michael@0 | 207 | |
michael@0 | 208 | /* |
michael@0 | 209 | * addLogin |
michael@0 | 210 | * |
michael@0 | 211 | * Add a new login to login storage. |
michael@0 | 212 | */ |
michael@0 | 213 | addLogin : function (login) { |
michael@0 | 214 | // Sanity check the login |
michael@0 | 215 | if (login.hostname == null || login.hostname.length == 0) |
michael@0 | 216 | throw "Can't add a login with a null or empty hostname."; |
michael@0 | 217 | |
michael@0 | 218 | // For logins w/o a username, set to "", not null. |
michael@0 | 219 | if (login.username == null) |
michael@0 | 220 | throw "Can't add a login with a null username."; |
michael@0 | 221 | |
michael@0 | 222 | if (login.password == null || login.password.length == 0) |
michael@0 | 223 | throw "Can't add a login with a null or empty password."; |
michael@0 | 224 | |
michael@0 | 225 | if (login.formSubmitURL || login.formSubmitURL == "") { |
michael@0 | 226 | // We have a form submit URL. Can't have a HTTP realm. |
michael@0 | 227 | if (login.httpRealm != null) |
michael@0 | 228 | throw "Can't add a login with both a httpRealm and formSubmitURL."; |
michael@0 | 229 | } else if (login.httpRealm) { |
michael@0 | 230 | // We have a HTTP realm. Can't have a form submit URL. |
michael@0 | 231 | if (login.formSubmitURL != null) |
michael@0 | 232 | throw "Can't add a login with both a httpRealm and formSubmitURL."; |
michael@0 | 233 | } else { |
michael@0 | 234 | // Need one or the other! |
michael@0 | 235 | throw "Can't add a login without a httpRealm or formSubmitURL."; |
michael@0 | 236 | } |
michael@0 | 237 | |
michael@0 | 238 | |
michael@0 | 239 | // Look for an existing entry. |
michael@0 | 240 | var logins = this.findLogins({}, login.hostname, login.formSubmitURL, |
michael@0 | 241 | login.httpRealm); |
michael@0 | 242 | |
michael@0 | 243 | if (logins.some(function(l) login.matches(l, true))) |
michael@0 | 244 | throw "This login already exists."; |
michael@0 | 245 | |
michael@0 | 246 | log("Adding login"); |
michael@0 | 247 | return this._storage.addLogin(login); |
michael@0 | 248 | }, |
michael@0 | 249 | |
michael@0 | 250 | |
michael@0 | 251 | /* |
michael@0 | 252 | * removeLogin |
michael@0 | 253 | * |
michael@0 | 254 | * Remove the specified login from the stored logins. |
michael@0 | 255 | */ |
michael@0 | 256 | removeLogin : function (login) { |
michael@0 | 257 | log("Removing login"); |
michael@0 | 258 | return this._storage.removeLogin(login); |
michael@0 | 259 | }, |
michael@0 | 260 | |
michael@0 | 261 | |
michael@0 | 262 | /* |
michael@0 | 263 | * modifyLogin |
michael@0 | 264 | * |
michael@0 | 265 | * Change the specified login to match the new login. |
michael@0 | 266 | */ |
michael@0 | 267 | modifyLogin : function (oldLogin, newLogin) { |
michael@0 | 268 | log("Modifying login"); |
michael@0 | 269 | return this._storage.modifyLogin(oldLogin, newLogin); |
michael@0 | 270 | }, |
michael@0 | 271 | |
michael@0 | 272 | |
michael@0 | 273 | /* |
michael@0 | 274 | * getAllLogins |
michael@0 | 275 | * |
michael@0 | 276 | * Get a dump of all stored logins. Used by the login manager UI. |
michael@0 | 277 | * |
michael@0 | 278 | * |count| is only needed for XPCOM. |
michael@0 | 279 | * |
michael@0 | 280 | * Returns an array of logins. If there are no logins, the array is empty. |
michael@0 | 281 | */ |
michael@0 | 282 | getAllLogins : function (count) { |
michael@0 | 283 | log("Getting a list of all logins"); |
michael@0 | 284 | return this._storage.getAllLogins(count); |
michael@0 | 285 | }, |
michael@0 | 286 | |
michael@0 | 287 | |
michael@0 | 288 | /* |
michael@0 | 289 | * removeAllLogins |
michael@0 | 290 | * |
michael@0 | 291 | * Remove all stored logins. |
michael@0 | 292 | */ |
michael@0 | 293 | removeAllLogins : function () { |
michael@0 | 294 | log("Removing all logins"); |
michael@0 | 295 | this._storage.removeAllLogins(); |
michael@0 | 296 | }, |
michael@0 | 297 | |
michael@0 | 298 | /* |
michael@0 | 299 | * getAllDisabledHosts |
michael@0 | 300 | * |
michael@0 | 301 | * Get a list of all hosts for which logins are disabled. |
michael@0 | 302 | * |
michael@0 | 303 | * |count| is only needed for XPCOM. |
michael@0 | 304 | * |
michael@0 | 305 | * Returns an array of disabled logins. If there are no disabled logins, |
michael@0 | 306 | * the array is empty. |
michael@0 | 307 | */ |
michael@0 | 308 | getAllDisabledHosts : function (count) { |
michael@0 | 309 | log("Getting a list of all disabled hosts"); |
michael@0 | 310 | return this._storage.getAllDisabledHosts(count); |
michael@0 | 311 | }, |
michael@0 | 312 | |
michael@0 | 313 | |
michael@0 | 314 | /* |
michael@0 | 315 | * findLogins |
michael@0 | 316 | * |
michael@0 | 317 | * Search for the known logins for entries matching the specified criteria. |
michael@0 | 318 | */ |
michael@0 | 319 | findLogins : function (count, hostname, formSubmitURL, httpRealm) { |
michael@0 | 320 | log("Searching for logins matching host:", hostname, |
michael@0 | 321 | "formSubmitURL:", formSubmitURL, "httpRealm:", httpRealm); |
michael@0 | 322 | |
michael@0 | 323 | return this._storage.findLogins(count, hostname, formSubmitURL, |
michael@0 | 324 | httpRealm); |
michael@0 | 325 | }, |
michael@0 | 326 | |
michael@0 | 327 | |
michael@0 | 328 | /* |
michael@0 | 329 | * searchLogins |
michael@0 | 330 | * |
michael@0 | 331 | * Public wrapper around _searchLogins to convert the nsIPropertyBag to a |
michael@0 | 332 | * JavaScript object and decrypt the results. |
michael@0 | 333 | * |
michael@0 | 334 | * Returns an array of decrypted nsILoginInfo. |
michael@0 | 335 | */ |
michael@0 | 336 | searchLogins : function(count, matchData) { |
michael@0 | 337 | log("Searching for logins"); |
michael@0 | 338 | |
michael@0 | 339 | return this._storage.searchLogins(count, matchData); |
michael@0 | 340 | }, |
michael@0 | 341 | |
michael@0 | 342 | |
michael@0 | 343 | /* |
michael@0 | 344 | * countLogins |
michael@0 | 345 | * |
michael@0 | 346 | * Search for the known logins for entries matching the specified criteria, |
michael@0 | 347 | * returns only the count. |
michael@0 | 348 | */ |
michael@0 | 349 | countLogins : function (hostname, formSubmitURL, httpRealm) { |
michael@0 | 350 | log("Counting logins matching host:", hostname, |
michael@0 | 351 | "formSubmitURL:", formSubmitURL, "httpRealm:", httpRealm); |
michael@0 | 352 | |
michael@0 | 353 | return this._storage.countLogins(hostname, formSubmitURL, httpRealm); |
michael@0 | 354 | }, |
michael@0 | 355 | |
michael@0 | 356 | |
michael@0 | 357 | /* |
michael@0 | 358 | * uiBusy |
michael@0 | 359 | */ |
michael@0 | 360 | get uiBusy() { |
michael@0 | 361 | return this._storage.uiBusy; |
michael@0 | 362 | }, |
michael@0 | 363 | |
michael@0 | 364 | |
michael@0 | 365 | /* |
michael@0 | 366 | * isLoggedIn |
michael@0 | 367 | */ |
michael@0 | 368 | get isLoggedIn() { |
michael@0 | 369 | return this._storage.isLoggedIn; |
michael@0 | 370 | }, |
michael@0 | 371 | |
michael@0 | 372 | |
michael@0 | 373 | /* |
michael@0 | 374 | * getLoginSavingEnabled |
michael@0 | 375 | * |
michael@0 | 376 | * Check to see if user has disabled saving logins for the host. |
michael@0 | 377 | */ |
michael@0 | 378 | getLoginSavingEnabled : function (host) { |
michael@0 | 379 | log("Checking if logins to", host, "can be saved."); |
michael@0 | 380 | if (!this._remember) |
michael@0 | 381 | return false; |
michael@0 | 382 | |
michael@0 | 383 | return this._storage.getLoginSavingEnabled(host); |
michael@0 | 384 | }, |
michael@0 | 385 | |
michael@0 | 386 | |
michael@0 | 387 | /* |
michael@0 | 388 | * setLoginSavingEnabled |
michael@0 | 389 | * |
michael@0 | 390 | * Enable or disable storing logins for the specified host. |
michael@0 | 391 | */ |
michael@0 | 392 | setLoginSavingEnabled : function (hostname, enabled) { |
michael@0 | 393 | // Nulls won't round-trip with getAllDisabledHosts(). |
michael@0 | 394 | if (hostname.indexOf("\0") != -1) |
michael@0 | 395 | throw "Invalid hostname"; |
michael@0 | 396 | |
michael@0 | 397 | log("Login saving for", hostname, "now enabled?", enabled); |
michael@0 | 398 | return this._storage.setLoginSavingEnabled(hostname, enabled); |
michael@0 | 399 | }, |
michael@0 | 400 | |
michael@0 | 401 | |
michael@0 | 402 | /* |
michael@0 | 403 | * autoCompleteSearch |
michael@0 | 404 | * |
michael@0 | 405 | * Yuck. This is called directly by satchel: |
michael@0 | 406 | * nsFormFillController::StartSearch() |
michael@0 | 407 | * [toolkit/components/satchel/src/nsFormFillController.cpp] |
michael@0 | 408 | * |
michael@0 | 409 | * We really ought to have a simple way for code to register an |
michael@0 | 410 | * auto-complete provider, and not have satchel calling pwmgr directly. |
michael@0 | 411 | */ |
michael@0 | 412 | autoCompleteSearch : function (aSearchString, aPreviousResult, aElement) { |
michael@0 | 413 | // aPreviousResult & aResult are nsIAutoCompleteResult, |
michael@0 | 414 | // aElement is nsIDOMHTMLInputElement |
michael@0 | 415 | |
michael@0 | 416 | if (!this._remember) |
michael@0 | 417 | return null; |
michael@0 | 418 | |
michael@0 | 419 | log("AutoCompleteSearch invoked. Search is:", aSearchString); |
michael@0 | 420 | |
michael@0 | 421 | var result = null; |
michael@0 | 422 | |
michael@0 | 423 | if (aPreviousResult && |
michael@0 | 424 | aSearchString.substr(0, aPreviousResult.searchString.length) == aPreviousResult.searchString) { |
michael@0 | 425 | log("Using previous autocomplete result"); |
michael@0 | 426 | result = aPreviousResult; |
michael@0 | 427 | result.wrappedJSObject.searchString = aSearchString; |
michael@0 | 428 | |
michael@0 | 429 | // We have a list of results for a shorter search string, so just |
michael@0 | 430 | // filter them further based on the new search string. |
michael@0 | 431 | // Count backwards, because result.matchCount is decremented |
michael@0 | 432 | // when we remove an entry. |
michael@0 | 433 | for (var i = result.matchCount - 1; i >= 0; i--) { |
michael@0 | 434 | var match = result.getValueAt(i); |
michael@0 | 435 | |
michael@0 | 436 | // Remove results that are too short, or have different prefix. |
michael@0 | 437 | if (aSearchString.length > match.length || |
michael@0 | 438 | aSearchString.toLowerCase() != |
michael@0 | 439 | match.substr(0, aSearchString.length).toLowerCase()) |
michael@0 | 440 | { |
michael@0 | 441 | log("Removing autocomplete entry:", match); |
michael@0 | 442 | result.removeValueAt(i, false); |
michael@0 | 443 | } |
michael@0 | 444 | } |
michael@0 | 445 | } else { |
michael@0 | 446 | log("Creating new autocomplete search result."); |
michael@0 | 447 | |
michael@0 | 448 | var doc = aElement.ownerDocument; |
michael@0 | 449 | var origin = this._getPasswordOrigin(doc.documentURI); |
michael@0 | 450 | var actionOrigin = this._getActionOrigin(aElement.form); |
michael@0 | 451 | |
michael@0 | 452 | // This shouldn't trigger a master password prompt, because we |
michael@0 | 453 | // don't attach to the input until after we successfully obtain |
michael@0 | 454 | // logins for the form. |
michael@0 | 455 | var logins = this.findLogins({}, origin, actionOrigin, null); |
michael@0 | 456 | var matchingLogins = []; |
michael@0 | 457 | |
michael@0 | 458 | // Filter out logins that don't match the search prefix. Also |
michael@0 | 459 | // filter logins without a username, since that's confusing to see |
michael@0 | 460 | // in the dropdown and we can't autocomplete them anyway. |
michael@0 | 461 | for (i = 0; i < logins.length; i++) { |
michael@0 | 462 | var username = logins[i].username.toLowerCase(); |
michael@0 | 463 | if (username && |
michael@0 | 464 | aSearchString.length <= username.length && |
michael@0 | 465 | aSearchString.toLowerCase() == |
michael@0 | 466 | username.substr(0, aSearchString.length)) |
michael@0 | 467 | { |
michael@0 | 468 | matchingLogins.push(logins[i]); |
michael@0 | 469 | } |
michael@0 | 470 | } |
michael@0 | 471 | log(matchingLogins.length, "autocomplete logins avail."); |
michael@0 | 472 | result = new UserAutoCompleteResult(aSearchString, matchingLogins); |
michael@0 | 473 | } |
michael@0 | 474 | |
michael@0 | 475 | return result; |
michael@0 | 476 | }, |
michael@0 | 477 | |
michael@0 | 478 | |
michael@0 | 479 | |
michael@0 | 480 | |
michael@0 | 481 | /* ------- Internal methods / callbacks for document integration ------- */ |
michael@0 | 482 | |
michael@0 | 483 | |
michael@0 | 484 | |
michael@0 | 485 | |
michael@0 | 486 | /* |
michael@0 | 487 | * _getPasswordOrigin |
michael@0 | 488 | * |
michael@0 | 489 | * Get the parts of the URL we want for identification. |
michael@0 | 490 | */ |
michael@0 | 491 | _getPasswordOrigin : function (uriString, allowJS) { |
michael@0 | 492 | var realm = ""; |
michael@0 | 493 | try { |
michael@0 | 494 | var uri = Services.io.newURI(uriString, null, null); |
michael@0 | 495 | |
michael@0 | 496 | if (allowJS && uri.scheme == "javascript") |
michael@0 | 497 | return "javascript:" |
michael@0 | 498 | |
michael@0 | 499 | realm = uri.scheme + "://" + uri.host; |
michael@0 | 500 | |
michael@0 | 501 | // If the URI explicitly specified a port, only include it when |
michael@0 | 502 | // it's not the default. (We never want "http://foo.com:80") |
michael@0 | 503 | var port = uri.port; |
michael@0 | 504 | if (port != -1) { |
michael@0 | 505 | var handler = Services.io.getProtocolHandler(uri.scheme); |
michael@0 | 506 | if (port != handler.defaultPort) |
michael@0 | 507 | realm += ":" + port; |
michael@0 | 508 | } |
michael@0 | 509 | |
michael@0 | 510 | } catch (e) { |
michael@0 | 511 | // bug 159484 - disallow url types that don't support a hostPort. |
michael@0 | 512 | // (although we handle "javascript:..." as a special case above.) |
michael@0 | 513 | log("Couldn't parse origin for", uriString); |
michael@0 | 514 | realm = null; |
michael@0 | 515 | } |
michael@0 | 516 | |
michael@0 | 517 | return realm; |
michael@0 | 518 | }, |
michael@0 | 519 | |
michael@0 | 520 | _getActionOrigin : function (form) { |
michael@0 | 521 | var uriString = form.action; |
michael@0 | 522 | |
michael@0 | 523 | // A blank or missing action submits to where it came from. |
michael@0 | 524 | if (uriString == "") |
michael@0 | 525 | uriString = form.baseURI; // ala bug 297761 |
michael@0 | 526 | |
michael@0 | 527 | return this._getPasswordOrigin(uriString, true); |
michael@0 | 528 | }, |
michael@0 | 529 | |
michael@0 | 530 | |
michael@0 | 531 | /* |
michael@0 | 532 | * fillForm |
michael@0 | 533 | * |
michael@0 | 534 | * Fill the form with login information if we can find it. |
michael@0 | 535 | */ |
michael@0 | 536 | fillForm : function (form) { |
michael@0 | 537 | log("fillForm processing form[ id:", form.id, "]"); |
michael@0 | 538 | return LoginManagerContent._fillForm(form, true, true, false, null)[0]; |
michael@0 | 539 | }, |
michael@0 | 540 | |
michael@0 | 541 | }; // end of LoginManager implementation |
michael@0 | 542 | |
michael@0 | 543 | |
michael@0 | 544 | |
michael@0 | 545 | |
michael@0 | 546 | // nsIAutoCompleteResult implementation |
michael@0 | 547 | function UserAutoCompleteResult (aSearchString, matchingLogins) { |
michael@0 | 548 | function loginSort(a,b) { |
michael@0 | 549 | var userA = a.username.toLowerCase(); |
michael@0 | 550 | var userB = b.username.toLowerCase(); |
michael@0 | 551 | |
michael@0 | 552 | if (userA < userB) |
michael@0 | 553 | return -1; |
michael@0 | 554 | |
michael@0 | 555 | if (userB > userA) |
michael@0 | 556 | return 1; |
michael@0 | 557 | |
michael@0 | 558 | return 0; |
michael@0 | 559 | }; |
michael@0 | 560 | |
michael@0 | 561 | this.searchString = aSearchString; |
michael@0 | 562 | this.logins = matchingLogins.sort(loginSort); |
michael@0 | 563 | this.matchCount = matchingLogins.length; |
michael@0 | 564 | |
michael@0 | 565 | if (this.matchCount > 0) { |
michael@0 | 566 | this.searchResult = Ci.nsIAutoCompleteResult.RESULT_SUCCESS; |
michael@0 | 567 | this.defaultIndex = 0; |
michael@0 | 568 | } |
michael@0 | 569 | } |
michael@0 | 570 | |
michael@0 | 571 | UserAutoCompleteResult.prototype = { |
michael@0 | 572 | QueryInterface : XPCOMUtils.generateQI([Ci.nsIAutoCompleteResult, |
michael@0 | 573 | Ci.nsISupportsWeakReference]), |
michael@0 | 574 | |
michael@0 | 575 | // private |
michael@0 | 576 | logins : null, |
michael@0 | 577 | |
michael@0 | 578 | // Allow autoCompleteSearch to get at the JS object so it can |
michael@0 | 579 | // modify some readonly properties for internal use. |
michael@0 | 580 | get wrappedJSObject() { |
michael@0 | 581 | return this; |
michael@0 | 582 | }, |
michael@0 | 583 | |
michael@0 | 584 | // Interfaces from idl... |
michael@0 | 585 | searchString : null, |
michael@0 | 586 | searchResult : Ci.nsIAutoCompleteResult.RESULT_NOMATCH, |
michael@0 | 587 | defaultIndex : -1, |
michael@0 | 588 | errorDescription : "", |
michael@0 | 589 | matchCount : 0, |
michael@0 | 590 | |
michael@0 | 591 | getValueAt : function (index) { |
michael@0 | 592 | if (index < 0 || index >= this.logins.length) |
michael@0 | 593 | throw "Index out of range."; |
michael@0 | 594 | |
michael@0 | 595 | return this.logins[index].username; |
michael@0 | 596 | }, |
michael@0 | 597 | |
michael@0 | 598 | getLabelAt: function(index) { |
michael@0 | 599 | return this.getValueAt(index); |
michael@0 | 600 | }, |
michael@0 | 601 | |
michael@0 | 602 | getCommentAt : function (index) { |
michael@0 | 603 | return ""; |
michael@0 | 604 | }, |
michael@0 | 605 | |
michael@0 | 606 | getStyleAt : function (index) { |
michael@0 | 607 | return ""; |
michael@0 | 608 | }, |
michael@0 | 609 | |
michael@0 | 610 | getImageAt : function (index) { |
michael@0 | 611 | return ""; |
michael@0 | 612 | }, |
michael@0 | 613 | |
michael@0 | 614 | getFinalCompleteValueAt : function (index) { |
michael@0 | 615 | return this.getValueAt(index); |
michael@0 | 616 | }, |
michael@0 | 617 | |
michael@0 | 618 | removeValueAt : function (index, removeFromDB) { |
michael@0 | 619 | if (index < 0 || index >= this.logins.length) |
michael@0 | 620 | throw "Index out of range."; |
michael@0 | 621 | |
michael@0 | 622 | var [removedLogin] = this.logins.splice(index, 1); |
michael@0 | 623 | |
michael@0 | 624 | this.matchCount--; |
michael@0 | 625 | if (this.defaultIndex > this.logins.length) |
michael@0 | 626 | this.defaultIndex--; |
michael@0 | 627 | |
michael@0 | 628 | if (removeFromDB) { |
michael@0 | 629 | var pwmgr = Cc["@mozilla.org/login-manager;1"]. |
michael@0 | 630 | getService(Ci.nsILoginManager); |
michael@0 | 631 | pwmgr.removeLogin(removedLogin); |
michael@0 | 632 | } |
michael@0 | 633 | } |
michael@0 | 634 | }; |
michael@0 | 635 | |
michael@0 | 636 | this.NSGetFactory = XPCOMUtils.generateNSGetFactory([LoginManager]); |