| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 /** | 5 /** |
| 6 * @fileoverview 'settings-passwords-and-forms-page' is the settings page | 6 * @fileoverview 'settings-passwords-and-forms-page' is the settings page |
| 7 * for passwords and auto fill. | 7 * for passwords and auto fill. |
| 8 */ | 8 */ |
| 9 | 9 |
| 10 /** | |
| 11 * Interface for all callbacks to the password API. | |
| 12 * @interface | |
| 13 */ | |
| 14 function PasswordManager() {} | |
| 15 | |
| 16 /** @typedef {chrome.passwordsPrivate.PasswordUiEntry} */ | |
| 17 PasswordManager.PasswordUiEntry; | |
| 18 | |
| 19 /** @typedef {chrome.passwordsPrivate.LoginPair} */ | |
| 20 PasswordManager.LoginPair; | |
| 21 | |
| 22 /** @typedef {chrome.passwordsPrivate.ExceptionPair} */ | |
| 23 PasswordManager.ExceptionPair; | |
| 24 | |
| 25 /** @typedef {chrome.passwordsPrivate.PlaintextPasswordEventParameters} */ | |
| 26 PasswordManager.PlaintextPasswordEvent; | |
| 27 | |
| 28 PasswordManager.prototype = { | |
| 29 /** | |
| 30 * Add an observer to the list of saved passwords. | |
| 31 * @param {function(!Array<!PasswordManager.PasswordUiEntry>):void} listener | |
| 32 */ | |
| 33 addSavedPasswordListChangedListener: assertNotReached, | |
| 34 | |
| 35 /** | |
| 36 * Remove an observer from the list of saved passwords. | |
| 37 * @param {function(!Array<!PasswordManager.PasswordUiEntry>):void} listener | |
| 38 */ | |
| 39 removeSavedPasswordListChangedListener: assertNotReached, | |
| 40 | |
| 41 /** | |
| 42 * Request the list of saved passwords. | |
| 43 * @param {function(!Array<!PasswordManager.PasswordUiEntry>):void} callback | |
| 44 */ | |
| 45 getSavedPasswordList: assertNotReached, | |
| 46 | |
| 47 /** | |
| 48 * Should remove the saved password and notify that the list has changed. | |
| 49 * @param {!PasswordManager.LoginPair} loginPair The saved password that | |
| 50 * should be removed from the list. No-op if |loginPair| is not found. | |
| 51 */ | |
| 52 removeSavedPassword: assertNotReached, | |
| 53 | |
| 54 /** | |
| 55 * Add an observer to the list of password exceptions. | |
| 56 * @param {function(!Array<!PasswordManager.ExceptionPair>):void} listener | |
| 57 */ | |
| 58 addExceptionListChangedListener: assertNotReached, | |
| 59 | |
| 60 /** | |
| 61 * Remove an observer from the list of password exceptions. | |
| 62 * @param {function(!Array<!PasswordManager.ExceptionPair>):void} listener | |
| 63 */ | |
| 64 removeExceptionListChangedListener: assertNotReached, | |
| 65 | |
| 66 /** | |
| 67 * Request the list of password exceptions. | |
| 68 * @param {function(!Array<!PasswordManager.ExceptionPair>):void} callback | |
| 69 */ | |
| 70 getExceptionList: assertNotReached, | |
| 71 | |
| 72 /** | |
| 73 * Should remove the password exception and notify that the list has changed. | |
| 74 * @param {string} exception The exception that should be removed from the | |
| 75 * list. No-op if |exception| is not in the list. | |
| 76 */ | |
| 77 removeException: assertNotReached, | |
| 78 | |
| 79 /** | |
| 80 * Gets the saved password for a given login pair. | |
| 81 * @param {!PasswordManager.LoginPair} loginPair The saved password that | |
| 82 * should be retrieved. | |
| 83 * @param {function(!PasswordManager.PlaintextPasswordEvent):void} callback | |
| 84 */ | |
| 85 getPlaintextPassword: assertNotReached, | |
| 86 }; | |
| 87 | |
| 88 /** | |
| 89 * Interface for all callbacks to the autofill API. | |
| 90 * @interface | |
| 91 */ | |
| 92 function AutofillManager() {} | |
| 93 | |
| 94 /** @typedef {chrome.autofillPrivate.AddressEntry} */ | |
| 95 AutofillManager.AddressEntry; | |
| 96 | |
| 97 /** @typedef {chrome.autofillPrivate.CreditCardEntry} */ | |
| 98 AutofillManager.CreditCardEntry; | |
| 99 | |
| 100 AutofillManager.prototype = { | |
| 101 /** | |
| 102 * Add an observer to the list of addresses. | |
| 103 * @param {function(!Array<!AutofillManager.AddressEntry>):void} listener | |
| 104 */ | |
| 105 addAddressListChangedListener: assertNotReached, | |
| 106 | |
| 107 /** | |
| 108 * Remove an observer from the list of addresses. | |
| 109 * @param {function(!Array<!AutofillManager.AddressEntry>):void} listener | |
| 110 */ | |
| 111 removeAddressListChangedListener: assertNotReached, | |
| 112 | |
| 113 /** | |
| 114 * Request the list of addresses. | |
| 115 * @param {function(!Array<!AutofillManager.AddressEntry>):void} callback | |
| 116 */ | |
| 117 getAddressList: assertNotReached, | |
| 118 | |
| 119 /** | |
| 120 * Saves the given address. | |
| 121 * @param {!AutofillManager.AddressEntry} address | |
| 122 */ | |
| 123 saveAddress: assertNotReached, | |
| 124 | |
| 125 /** @param {string} guid The guid of the address to remove. */ | |
| 126 removeAddress: assertNotReached, | |
| 127 | |
| 128 /** | |
| 129 * Add an observer to the list of credit cards. | |
| 130 * @param {function(!Array<!AutofillManager.CreditCardEntry>):void} listener | |
| 131 */ | |
| 132 addCreditCardListChangedListener: assertNotReached, | |
| 133 | |
| 134 /** | |
| 135 * Remove an observer from the list of credit cards. | |
| 136 * @param {function(!Array<!AutofillManager.CreditCardEntry>):void} listener | |
| 137 */ | |
| 138 removeCreditCardListChangedListener: assertNotReached, | |
| 139 | |
| 140 /** | |
| 141 * Request the list of credit cards. | |
| 142 * @param {function(!Array<!AutofillManager.CreditCardEntry>):void} callback | |
| 143 */ | |
| 144 getCreditCardList: assertNotReached, | |
| 145 | |
| 146 /** @param {string} guid The GUID of the credit card to remove. */ | |
| 147 removeCreditCard: assertNotReached, | |
| 148 | |
| 149 /** @param {string} guid The GUID to credit card to remove from the cache. */ | |
| 150 clearCachedCreditCard: assertNotReached, | |
| 151 | |
| 152 /** | |
| 153 * Saves the given credit card. | |
| 154 * @param {!AutofillManager.CreditCardEntry} creditCard | |
| 155 */ | |
| 156 saveCreditCard: assertNotReached, | |
| 157 }; | |
| 158 | |
| 159 /** | |
| 160 * Implementation that accesses the private API. | |
| 161 * @implements {PasswordManager} | |
| 162 * @constructor | |
| 163 */ | |
| 164 function PasswordManagerImpl() {} | |
| 165 cr.addSingletonGetter(PasswordManagerImpl); | |
| 166 | |
| 167 PasswordManagerImpl.prototype = { | |
| 168 __proto__: PasswordManager, | |
| 169 | |
| 170 /** @override */ | |
| 171 addSavedPasswordListChangedListener: function(listener) { | |
| 172 chrome.passwordsPrivate.onSavedPasswordsListChanged.addListener(listener); | |
| 173 }, | |
| 174 | |
| 175 /** @override */ | |
| 176 removeSavedPasswordListChangedListener: function(listener) { | |
| 177 chrome.passwordsPrivate.onSavedPasswordsListChanged.removeListener( | |
| 178 listener); | |
| 179 }, | |
| 180 | |
| 181 /** @override */ | |
| 182 getSavedPasswordList: function(callback) { | |
| 183 chrome.passwordsPrivate.getSavedPasswordList(callback); | |
| 184 }, | |
| 185 | |
| 186 /** @override */ | |
| 187 removeSavedPassword: function(loginPair) { | |
| 188 chrome.passwordsPrivate.removeSavedPassword(loginPair); | |
| 189 }, | |
| 190 | |
| 191 /** @override */ | |
| 192 addExceptionListChangedListener: function(listener) { | |
| 193 chrome.passwordsPrivate.onPasswordExceptionsListChanged.addListener( | |
| 194 listener); | |
| 195 }, | |
| 196 | |
| 197 /** @override */ | |
| 198 removeExceptionListChangedListener: function(listener) { | |
| 199 chrome.passwordsPrivate.onPasswordExceptionsListChanged.removeListener( | |
| 200 listener); | |
| 201 }, | |
| 202 | |
| 203 /** @override */ | |
| 204 getExceptionList: function(callback) { | |
| 205 chrome.passwordsPrivate.getPasswordExceptionList(callback); | |
| 206 }, | |
| 207 | |
| 208 /** @override */ | |
| 209 removeException: function(exception) { | |
| 210 chrome.passwordsPrivate.removePasswordException(exception); | |
| 211 }, | |
| 212 | |
| 213 /** @override */ | |
| 214 getPlaintextPassword: function(loginPair, callback) { | |
| 215 var listener = function(reply) { | |
| 216 // Only handle the reply for our loginPair request. | |
| 217 if (reply.loginPair.originUrl == loginPair.originUrl && | |
| 218 reply.loginPair.username == loginPair.username) { | |
| 219 chrome.passwordsPrivate.onPlaintextPasswordRetrieved.removeListener( | |
| 220 listener); | |
| 221 callback(reply); | |
| 222 } | |
| 223 }; | |
| 224 chrome.passwordsPrivate.onPlaintextPasswordRetrieved.addListener(listener); | |
| 225 chrome.passwordsPrivate.requestPlaintextPassword(loginPair); | |
| 226 }, | |
| 227 }; | |
| 228 | |
| 229 /** | |
| 230 * Implementation that accesses the private API. | |
| 231 * @implements {AutofillManager} | |
| 232 * @constructor | |
| 233 */ | |
| 234 function AutofillManagerImpl() {} | |
| 235 cr.addSingletonGetter(AutofillManagerImpl); | |
| 236 | |
| 237 AutofillManagerImpl.prototype = { | |
| 238 __proto__: AutofillManager, | |
| 239 | |
| 240 /** @override */ | |
| 241 addAddressListChangedListener: function(listener) { | |
| 242 chrome.autofillPrivate.onAddressListChanged.addListener(listener); | |
| 243 }, | |
| 244 | |
| 245 /** @override */ | |
| 246 removeAddressListChangedListener: function(listener) { | |
| 247 chrome.autofillPrivate.onAddressListChanged.removeListener(listener); | |
| 248 }, | |
| 249 | |
| 250 /** @override */ | |
| 251 getAddressList: function(callback) { | |
| 252 chrome.autofillPrivate.getAddressList(callback); | |
| 253 }, | |
| 254 | |
| 255 /** @override */ | |
| 256 saveAddress: function(address) { | |
| 257 chrome.autofillPrivate.saveAddress(address); | |
| 258 }, | |
| 259 | |
| 260 /** @override */ | |
| 261 removeAddress: function(guid) { | |
| 262 assert(guid); | |
| 263 chrome.autofillPrivate.removeEntry(guid); | |
| 264 }, | |
| 265 | |
| 266 /** @override */ | |
| 267 addCreditCardListChangedListener: function(listener) { | |
| 268 chrome.autofillPrivate.onCreditCardListChanged.addListener(listener); | |
| 269 }, | |
| 270 | |
| 271 /** @override */ | |
| 272 removeCreditCardListChangedListener: function(listener) { | |
| 273 chrome.autofillPrivate.onCreditCardListChanged.removeListener(listener); | |
| 274 }, | |
| 275 | |
| 276 /** @override */ | |
| 277 getCreditCardList: function(callback) { | |
| 278 chrome.autofillPrivate.getCreditCardList(callback); | |
| 279 }, | |
| 280 | |
| 281 /** @override */ | |
| 282 removeCreditCard: function(guid) { | |
| 283 assert(guid); | |
| 284 chrome.autofillPrivate.removeEntry(guid); | |
| 285 }, | |
| 286 | |
| 287 /** @override */ | |
| 288 clearCachedCreditCard: function(guid) { | |
| 289 assert(guid); | |
| 290 chrome.autofillPrivate.maskCreditCard(guid); | |
| 291 }, | |
| 292 | |
| 293 /** @override */ | |
| 294 saveCreditCard: function(creditCard) { | |
| 295 chrome.autofillPrivate.saveCreditCard(creditCard); | |
| 296 } | |
| 297 }; | |
| 298 | |
| 299 (function() { | 10 (function() { |
| 300 'use strict'; | 11 'use strict'; |
| 301 | 12 |
| 302 Polymer({ | 13 Polymer({ |
| 303 is: 'settings-passwords-and-forms-page', | 14 is: 'settings-passwords-and-forms-page', |
| 304 | 15 |
| 305 behaviors: [PrefsBehavior], | 16 behaviors: [PrefsBehavior], |
| 306 | 17 |
| 307 properties: { | 18 properties: { |
| 308 /** | |
| 309 * An array of passwords to display. | |
| 310 * @type {!Array<!PasswordManager.PasswordUiEntry>} | |
| 311 */ | |
| 312 savedPasswords: Array, | |
| 313 | |
| 314 /** | |
| 315 * An array of sites to display. | |
| 316 * @type {!Array<!PasswordManager.ExceptionPair>} | |
| 317 */ | |
| 318 passwordExceptions: Array, | |
| 319 | |
| 320 /** @private Filter applied to passwords and password exceptions. */ | 19 /** @private Filter applied to passwords and password exceptions. */ |
| 321 passwordFilter_: String, | 20 passwordFilter_: String, |
| 322 | |
| 323 /** | |
| 324 * An array of saved addresses. | |
| 325 * @type {!Array<!AutofillManager.AddressEntry>} | |
| 326 */ | |
| 327 addresses: Array, | |
| 328 | |
| 329 /** | |
| 330 * An array of saved addresses. | |
| 331 * @type {!Array<!AutofillManager.CreditCardEntry>} | |
| 332 */ | |
| 333 creditCards: Array, | |
| 334 }, | |
| 335 | |
| 336 listeners: { | |
| 337 'clear-credit-card': 'clearCreditCard_', | |
| 338 'remove-address': 'removeAddress_', | |
| 339 'remove-credit-card': 'removeCreditCard_', | |
| 340 'remove-password-exception': 'removePasswordException_', | |
| 341 'remove-saved-password': 'removeSavedPassword_', | |
| 342 'save-address': 'saveAddress_', | |
| 343 'save-credit-card': 'saveCreditCard_', | |
| 344 'show-password': 'showPassword_', | |
| 345 }, | |
| 346 | |
| 347 /** @type {?function(!Array<PasswordManager.PasswordUiEntry>):void} */ | |
| 348 setSavedPasswordsListener_: null, | |
| 349 | |
| 350 /** @type {?function(!Array<PasswordManager.ExceptionPair>):void} */ | |
| 351 setPasswordExceptionsListener_: null, | |
| 352 | |
| 353 /** @type {?function(!Array<!AutofillManager.AddressEntry>)} */ | |
| 354 setAddressesListener_: null, | |
| 355 | |
| 356 /** @type {?function(!Array<!AutofillManager.CreditCardEntry>)} */ | |
| 357 setCreditCardsListener_: null, | |
| 358 | |
| 359 /** @override */ | |
| 360 ready: function() { | |
| 361 // Create listener functions. | |
| 362 this.setSavedPasswordsListener_ = function(list) { | |
| 363 this.savedPasswords = list; | |
| 364 }.bind(this); | |
| 365 | |
| 366 this.setPasswordExceptionsListener_ = function(list) { | |
| 367 this.passwordExceptions = list; | |
| 368 }.bind(this); | |
| 369 | |
| 370 this.setAddressesListener_ = function(list) { | |
| 371 this.addresses = list; | |
| 372 }.bind(this); | |
| 373 | |
| 374 this.setCreditCardsListener_ = function(list) { | |
| 375 this.creditCards = list; | |
| 376 }.bind(this); | |
| 377 | |
| 378 // Set the managers. These can be overridden by tests. | |
| 379 this.passwordManager_ = PasswordManagerImpl.getInstance(); | |
| 380 this.autofillManager_ = AutofillManagerImpl.getInstance(); | |
| 381 | |
| 382 // Request initial data. | |
| 383 this.passwordManager_.getSavedPasswordList(this.setSavedPasswordsListener_); | |
| 384 this.passwordManager_.getExceptionList(this.setPasswordExceptionsListener_); | |
| 385 this.autofillManager_.getAddressList(this.setAddressesListener_); | |
| 386 this.autofillManager_.getCreditCardList(this.setCreditCardsListener_); | |
| 387 | |
| 388 // Listen for changes. | |
| 389 this.passwordManager_.addSavedPasswordListChangedListener( | |
| 390 this.setSavedPasswordsListener_); | |
| 391 this.passwordManager_.addExceptionListChangedListener( | |
| 392 this.setPasswordExceptionsListener_); | |
| 393 this.autofillManager_.addAddressListChangedListener( | |
| 394 this.setAddressesListener_); | |
| 395 this.autofillManager_.addCreditCardListChangedListener( | |
| 396 this.setCreditCardsListener_); | |
| 397 }, | |
| 398 | |
| 399 /** @override */ | |
| 400 detached: function() { | |
| 401 this.passwordManager_.removeSavedPasswordListChangedListener( | |
| 402 this.setSavedPasswordsListener_); | |
| 403 this.passwordManager_.removeExceptionListChangedListener( | |
| 404 this.setPasswordExceptionsListener_); | |
| 405 this.autofillManager_.removeAddressListChangedListener( | |
| 406 this.setAddressesListener_); | |
| 407 this.autofillManager_.removeCreditCardListChangedListener( | |
| 408 this.setCreditCardsListener_); | |
| 409 }, | 21 }, |
| 410 | 22 |
| 411 /** | 23 /** |
| 412 * Listens for the remove-password-exception event, and calls the private API. | |
| 413 * @param {!Event} event | |
| 414 * @private | |
| 415 */ | |
| 416 removePasswordException_: function(event) { | |
| 417 this.passwordManager_.removeException(event.detail); | |
| 418 }, | |
| 419 | |
| 420 /** | |
| 421 * Listens for the remove-saved-password event, and calls the private API. | |
| 422 * @param {!Event} event | |
| 423 * @private | |
| 424 */ | |
| 425 removeSavedPassword_: function(event) { | |
| 426 this.passwordManager_.removeSavedPassword(event.detail); | |
| 427 }, | |
| 428 | |
| 429 /** | |
| 430 * Listens for the remove-address event, and calls the private API. | |
| 431 * @param {!Event} event | |
| 432 * @private | |
| 433 */ | |
| 434 removeAddress_: function(event) { | |
| 435 this.autofillManager_.removeAddress(event.detail.guid); | |
| 436 }, | |
| 437 | |
| 438 /** | |
| 439 * Listens for the remove-credit-card event, and calls the private API. | |
| 440 * @param {!Event} event | |
| 441 * @private | |
| 442 */ | |
| 443 removeCreditCard_: function(event) { | |
| 444 this.autofillManager_.removeCreditCard(event.detail.guid); | |
| 445 }, | |
| 446 | |
| 447 /** | |
| 448 * Listens for the clear-credit-card event, and calls the private API. | |
| 449 * @param {!Event} event | |
| 450 * @private | |
| 451 */ | |
| 452 clearCreditCard_: function(event) { | |
| 453 this.autofillManager_.clearCachedCreditCard(event.detail.guid); | |
| 454 }, | |
| 455 | |
| 456 /** | |
| 457 * Shows the manage autofill sub page. | 24 * Shows the manage autofill sub page. |
| 458 * @param {!Event} event | 25 * @param {!Event} event |
| 459 * @private | 26 * @private |
| 460 */ | 27 */ |
| 461 onAutofillTap_: function(event) { | 28 onAutofillTap_: function(event) { |
| 462 // Ignore clicking on the toggle button and verify autofill is enabled. | 29 // Ignore clicking on the toggle button and verify autofill is enabled. |
| 463 if (Polymer.dom(event).localTarget != this.$.autofillToggle && | 30 if (Polymer.dom(event).localTarget != this.$.autofillToggle && |
| 464 this.getPref('autofill.enabled').value) { | 31 this.getPref('autofill.enabled').value) { |
| 465 settings.navigateTo(settings.Route.AUTOFILL); | 32 settings.navigateTo(settings.Route.AUTOFILL); |
| 466 } | 33 } |
| 467 }, | 34 }, |
| 468 | 35 |
| 469 /** | 36 /** |
| 470 * Shows the manage passwords sub page. | 37 * Shows the manage passwords sub page. |
| 471 * @param {!Event} event | 38 * @param {!Event} event |
| 472 * @private | 39 * @private |
| 473 */ | 40 */ |
| 474 onPasswordsTap_: function(event) { | 41 onPasswordsTap_: function(event) { |
| 475 // Ignore clicking on the toggle button and only expand if the manager is | 42 // Ignore clicking on the toggle button and only expand if the manager is |
| 476 // enabled. | 43 // enabled. |
| 477 if (Polymer.dom(event).localTarget != this.$.passwordToggle && | 44 if (Polymer.dom(event).localTarget != this.$.passwordToggle && |
| 478 this.getPref('profile.password_manager_enabled').value) { | 45 this.getPref('profile.password_manager_enabled').value) { |
| 479 settings.navigateTo(settings.Route.MANAGE_PASSWORDS); | 46 settings.navigateTo(settings.Route.MANAGE_PASSWORDS); |
| 480 } | 47 } |
| 481 }, | 48 }, |
| 482 | |
| 483 /** | |
| 484 * Listens for the save-address event, and calls the private API. | |
| 485 * @param {!Event} event | |
| 486 * @private | |
| 487 */ | |
| 488 saveAddress_: function(event) { | |
| 489 this.autofillManager_.saveAddress(event.detail); | |
| 490 }, | |
| 491 | |
| 492 /** | |
| 493 * Listens for the save-credit-card event, and calls the private API. | |
| 494 * @param {!Event} event | |
| 495 * @private | |
| 496 */ | |
| 497 saveCreditCard_: function(event) { | |
| 498 this.autofillManager_.saveCreditCard(event.detail); | |
| 499 }, | |
| 500 | |
| 501 /** | |
| 502 * Listens for the show-password event, and calls the private API. | |
| 503 * @param {!Event} event | |
| 504 * @private | |
| 505 */ | |
| 506 showPassword_: function(event) { | |
| 507 this.passwordManager_.getPlaintextPassword(event.detail, function(e) { | |
| 508 this.$$('#passwordSection').setPassword(e.loginPair, e.plaintextPassword); | |
| 509 }.bind(this)); | |
| 510 }, | |
| 511 }); | 49 }); |
| 512 })(); | 50 })(); |
| OLD | NEW |