OLD | NEW |
---|---|
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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-autofill-section' is the section containing saved | 6 * @fileoverview 'settings-autofill-section' is the section containing saved |
7 * addresses and credit cards for use in autofill. | 7 * addresses and credit cards for use in autofill. |
8 */ | 8 */ |
9 | |
10 /** | |
11 * Interface for all callbacks to the autofill API. | |
12 * @interface | |
13 */ | |
14 function AutofillManager() {} | |
15 | |
16 /** @typedef {chrome.autofillPrivate.AddressEntry} */ | |
17 AutofillManager.AddressEntry; | |
18 | |
19 /** @typedef {chrome.autofillPrivate.CreditCardEntry} */ | |
20 AutofillManager.CreditCardEntry; | |
21 | |
22 AutofillManager.prototype = { | |
23 /** | |
24 * Add an observer to the list of addresses. | |
25 * @param {function(!Array<!AutofillManager.AddressEntry>):void} listener | |
26 */ | |
27 addAddressListChangedListener: assertNotReached, | |
28 | |
29 /** | |
30 * Remove an observer from the list of addresses. | |
31 * @param {function(!Array<!AutofillManager.AddressEntry>):void} listener | |
32 */ | |
33 removeAddressListChangedListener: assertNotReached, | |
34 | |
35 /** | |
36 * Request the list of addresses. | |
37 * @param {function(!Array<!AutofillManager.AddressEntry>):void} callback | |
38 */ | |
39 getAddressList: assertNotReached, | |
40 | |
41 /** | |
42 * Saves the given address. | |
43 * @param {!AutofillManager.AddressEntry} address | |
44 */ | |
45 saveAddress: assertNotReached, | |
46 | |
47 /** @param {string} guid The guid of the address to remove. */ | |
48 removeAddress: assertNotReached, | |
49 | |
50 /** | |
51 * Add an observer to the list of credit cards. | |
52 * @param {function(!Array<!AutofillManager.CreditCardEntry>):void} listener | |
53 */ | |
54 addCreditCardListChangedListener: assertNotReached, | |
55 | |
56 /** | |
57 * Remove an observer from the list of credit cards. | |
58 * @param {function(!Array<!AutofillManager.CreditCardEntry>):void} listener | |
59 */ | |
60 removeCreditCardListChangedListener: assertNotReached, | |
61 | |
62 /** | |
63 * Request the list of credit cards. | |
64 * @param {function(!Array<!AutofillManager.CreditCardEntry>):void} callback | |
65 */ | |
66 getCreditCardList: assertNotReached, | |
67 | |
68 /** @param {string} guid The GUID of the credit card to remove. */ | |
69 removeCreditCard: assertNotReached, | |
70 | |
71 /** @param {string} guid The GUID to credit card to remove from the cache. */ | |
72 clearCachedCreditCard: assertNotReached, | |
73 | |
74 /** | |
75 * Saves the given credit card. | |
76 * @param {!AutofillManager.CreditCardEntry} creditCard | |
77 */ | |
78 saveCreditCard: assertNotReached, | |
79 }; | |
80 | |
81 /** | |
82 * Implementation that accesses the private API. | |
83 * @implements {AutofillManager} | |
84 * @constructor | |
85 */ | |
86 function AutofillManagerImpl() {} | |
87 cr.addSingletonGetter(AutofillManagerImpl); | |
88 | |
89 AutofillManagerImpl.prototype = { | |
90 __proto__: AutofillManager, | |
91 | |
92 /** @override */ | |
93 addAddressListChangedListener: function(listener) { | |
94 chrome.autofillPrivate.onAddressListChanged.addListener(listener); | |
95 }, | |
96 | |
97 /** @override */ | |
98 removeAddressListChangedListener: function(listener) { | |
99 chrome.autofillPrivate.onAddressListChanged.removeListener(listener); | |
100 }, | |
101 | |
102 /** @override */ | |
103 getAddressList: function(callback) { | |
104 chrome.autofillPrivate.getAddressList(callback); | |
105 }, | |
106 | |
107 /** @override */ | |
108 saveAddress: function(address) { | |
109 chrome.autofillPrivate.saveAddress(address); | |
110 }, | |
111 | |
112 /** @override */ | |
113 removeAddress: function(guid) { | |
114 assert(guid); | |
115 chrome.autofillPrivate.removeEntry(guid); | |
Dan Beam
2017/02/15 23:19:39
nit: assert() returns whatever it's passsed, so yo
hcarmona
2017/02/16 00:00:01
Done.
| |
116 }, | |
117 | |
118 /** @override */ | |
119 addCreditCardListChangedListener: function(listener) { | |
120 chrome.autofillPrivate.onCreditCardListChanged.addListener(listener); | |
121 }, | |
122 | |
123 /** @override */ | |
124 removeCreditCardListChangedListener: function(listener) { | |
125 chrome.autofillPrivate.onCreditCardListChanged.removeListener(listener); | |
126 }, | |
127 | |
128 /** @override */ | |
129 getCreditCardList: function(callback) { | |
130 chrome.autofillPrivate.getCreditCardList(callback); | |
131 }, | |
132 | |
133 /** @override */ | |
134 removeCreditCard: function(guid) { | |
135 assert(guid); | |
136 chrome.autofillPrivate.removeEntry(guid); | |
137 }, | |
138 | |
139 /** @override */ | |
140 clearCachedCreditCard: function(guid) { | |
141 assert(guid); | |
142 chrome.autofillPrivate.maskCreditCard(guid); | |
143 }, | |
144 | |
145 /** @override */ | |
146 saveCreditCard: function(creditCard) { | |
147 chrome.autofillPrivate.saveCreditCard(creditCard); | |
148 } | |
149 }; | |
150 | |
9 (function() { | 151 (function() { |
10 'use strict'; | 152 'use strict'; |
11 | 153 |
12 Polymer({ | 154 Polymer({ |
13 is: 'settings-autofill-section', | 155 is: 'settings-autofill-section', |
14 | 156 |
15 behaviors: [I18nBehavior], | 157 behaviors: [I18nBehavior], |
16 | 158 |
17 properties: { | 159 properties: { |
18 /** | 160 /** |
19 * An array of saved addresses. | 161 * An array of saved addresses. |
20 * @type {!Array<!chrome.autofillPrivate.AddressEntry>} | 162 * @type {!Array<!AutofillManager.AddressEntry>} |
21 */ | 163 */ |
22 addresses: Array, | 164 addresses: Array, |
23 | 165 |
24 /** | 166 /** |
25 * The model for any address related action menus or dialogs. | 167 * The model for any address related action menus or dialogs. |
26 * @private {?chrome.autofillPrivate.AddressEntry} | 168 * @private {?chrome.autofillPrivate.AddressEntry} |
27 */ | 169 */ |
28 activeAddress: Object, | 170 activeAddress: Object, |
29 | 171 |
30 /** @private */ | 172 /** @private */ |
31 showAddressDialog_: Boolean, | 173 showAddressDialog_: Boolean, |
32 | 174 |
33 /** | 175 /** |
34 * An array of saved credit cards. | 176 * An array of saved credit cards. |
35 * @type {!Array<!chrome.autofillPrivate.CreditCardEntry>} | 177 * @type {!Array<!AutofillManager.CreditCardEntry>} |
36 */ | 178 */ |
37 creditCards: Array, | 179 creditCards: Array, |
38 | 180 |
39 /** | 181 /** |
40 * The model for any credit card related action menus or dialogs. | 182 * The model for any credit card related action menus or dialogs. |
41 * @private {?chrome.autofillPrivate.CreditCardEntry} | 183 * @private {?chrome.autofillPrivate.CreditCardEntry} |
42 */ | 184 */ |
43 activeCreditCard: Object, | 185 activeCreditCard: Object, |
44 | 186 |
45 /** @private */ | 187 /** @private */ |
46 showCreditCardDialog_: Boolean, | 188 showCreditCardDialog_: Boolean, |
47 }, | 189 }, |
48 | 190 |
191 listeners: { | |
192 'save-address': 'saveAddress_', | |
193 'save-credit-card': 'saveCreditCard_', | |
194 }, | |
195 | |
196 /** | |
197 * @type {AutofillManager} | |
198 * @private | |
199 */ | |
200 autofillManager_: null, | |
201 | |
202 /** | |
203 * @type {?function(!Array<!AutofillManager.AddressEntry>)} | |
204 * @private | |
205 */ | |
206 setAddressesListener_: null, | |
207 | |
208 /** | |
209 * @type {?function(!Array<!AutofillManager.CreditCardEntry>)} | |
210 * @private | |
211 */ | |
212 setCreditCardsListener_: null, | |
213 | |
214 /** @override */ | |
215 ready: function() { | |
216 // Create listener functions. | |
217 /** @type {function(!Array<!AutofillManager.AddressEntry>)} */ | |
218 var setAddressesListener = function(list) { | |
219 this.addresses = list; | |
220 }.bind(this); | |
221 | |
222 /** @type {function(!Array<!AutofillManager.CreditCardEntry>)} */ | |
223 var setCreditCardsListener = function(list) { | |
224 this.creditCards = list; | |
225 }.bind(this); | |
226 | |
227 // Remember the bound reference in order to detach. | |
228 this.setAddressesListener_ = setAddressesListener; | |
229 this.setCreditCardsListener_ = setCreditCardsListener; | |
230 | |
231 // Set the managers. These can be overridden by tests. | |
232 this.autofillManager_ = AutofillManagerImpl.getInstance(); | |
233 | |
234 // Request initial data. | |
235 this.autofillManager_.getAddressList(setAddressesListener); | |
236 this.autofillManager_.getCreditCardList(setCreditCardsListener); | |
237 | |
238 // Listen for changes. | |
239 this.autofillManager_.addAddressListChangedListener(setAddressesListener); | |
240 this.autofillManager_.addCreditCardListChangedListener( | |
241 setCreditCardsListener); | |
242 }, | |
243 | |
244 /** @override */ | |
245 detached: function() { | |
246 this.autofillManager_.removeAddressListChangedListener( | |
247 /** @type {function(!Array<!AutofillManager.AddressEntry>)} */( | |
248 this.setAddressesListener_)); | |
249 this.autofillManager_.removeCreditCardListChangedListener( | |
250 /** @type {function(!Array<!AutofillManager.CreditCardEntry>)} */( | |
251 this.setCreditCardsListener_)); | |
252 }, | |
253 | |
49 /** | 254 /** |
50 * Formats the expiration date so it's displayed as MM/YYYY. | 255 * Formats the expiration date so it's displayed as MM/YYYY. |
51 * @param {!chrome.autofillPrivate.CreditCardEntry} item | 256 * @param {!chrome.autofillPrivate.CreditCardEntry} item |
52 * @return {string} | 257 * @return {string} |
258 * @private | |
53 */ | 259 */ |
54 expiration_: function(item) { | 260 expiration_: function(item) { |
55 return item.expirationMonth + '/' + item.expirationYear; | 261 return item.expirationMonth + '/' + item.expirationYear; |
56 }, | 262 }, |
57 | 263 |
58 /** | 264 /** |
59 * Open the address action menu. | 265 * Open the address action menu. |
60 * @param {!Event} e The polymer event. | 266 * @param {!Event} e The polymer event. |
61 * @private | 267 * @private |
62 */ | 268 */ |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
106 /** @private */ | 312 /** @private */ |
107 onRemoteEditAddressTap_: function() { | 313 onRemoteEditAddressTap_: function() { |
108 window.open(this.i18n('manageAddressesUrl')); | 314 window.open(this.i18n('manageAddressesUrl')); |
109 }, | 315 }, |
110 | 316 |
111 /** | 317 /** |
112 * Handles tapping on the "Remove" address button. | 318 * Handles tapping on the "Remove" address button. |
113 * @private | 319 * @private |
114 */ | 320 */ |
115 onMenuRemoveAddressTap_: function() { | 321 onMenuRemoveAddressTap_: function() { |
116 this.fire('remove-address', this.activeAddress); | 322 this.autofillManager_.removeAddress( |
323 /** @type {string} */(this.activeAddress.guid)); | |
117 this.$.addressSharedMenu.close(); | 324 this.$.addressSharedMenu.close(); |
118 }, | 325 }, |
119 | 326 |
120 /** | 327 /** |
121 * Opens the credit card action menu. | 328 * Opens the credit card action menu. |
122 * @param {!Event} e The polymer event. | 329 * @param {!Event} e The polymer event. |
123 * @private | 330 * @private |
124 */ | 331 */ |
125 onCreditCardMenuTap_: function(e) { | 332 onCreditCardMenuTap_: function(e) { |
126 var menuEvent = /** @type {!{model: !{item: !Object}}} */(e); | 333 var menuEvent = /** @type {!{model: !{item: !Object}}} */(e); |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
174 /** @private */ | 381 /** @private */ |
175 onRemoteEditCreditCardTap_: function() { | 382 onRemoteEditCreditCardTap_: function() { |
176 window.open(this.i18n('manageCreditCardsUrl')); | 383 window.open(this.i18n('manageCreditCardsUrl')); |
177 }, | 384 }, |
178 | 385 |
179 /** | 386 /** |
180 * Handles tapping on the "Remove" credit card button. | 387 * Handles tapping on the "Remove" credit card button. |
181 * @private | 388 * @private |
182 */ | 389 */ |
183 onMenuRemoveCreditCardTap_: function() { | 390 onMenuRemoveCreditCardTap_: function() { |
184 this.fire('remove-credit-card', this.activeCreditCard); | 391 this.autofillManager_.removeCreditCard( |
392 /** @type {string} */(this.activeCreditCard.guid)); | |
185 this.$.creditCardSharedMenu.close(); | 393 this.$.creditCardSharedMenu.close(); |
186 }, | 394 }, |
187 | 395 |
188 /** | 396 /** |
189 * Handles tapping on the "Clear copy" button for cached credit cards. | 397 * Handles tapping on the "Clear copy" button for cached credit cards. |
190 * @private | 398 * @private |
191 */ | 399 */ |
192 onMenuClearCreditCardTap_: function() { | 400 onMenuClearCreditCardTap_: function() { |
193 this.fire('clear-credit-card', this.activeCreditCard); | 401 this.autofillManager_.clearCachedCreditCard( |
402 /** @type {string} */(this.activeCreditCard.guid)); | |
194 this.$.creditCardSharedMenu.close(); | 403 this.$.creditCardSharedMenu.close(); |
195 }, | 404 }, |
196 | 405 |
197 /** | 406 /** |
198 * Returns true if the list exists and has items. | 407 * Returns true if the list exists and has items. |
199 * @param {Array<Object>} list | 408 * @param {Array<Object>} list |
200 * @return {boolean} | 409 * @return {boolean} |
201 * @private | 410 * @private |
202 */ | 411 */ |
203 hasSome_: function(list) { | 412 hasSome_: function(list) { |
204 return !!(list && list.length); | 413 return !!(list && list.length); |
205 }, | 414 }, |
415 | |
416 /** | |
417 * Listens for the save-address event, and calls the private API. | |
418 * @param {!Event} event | |
419 * @private | |
420 */ | |
421 saveAddress_: function(event) { | |
422 this.autofillManager_.saveAddress(event.detail); | |
423 }, | |
424 | |
425 /** | |
426 * Listens for the save-credit-card event, and calls the private API. | |
427 * @param {!Event} event | |
428 * @private | |
429 */ | |
430 saveCreditCard_: function(event) { | |
431 this.autofillManager_.saveCreditCard(event.detail); | |
432 }, | |
206 }); | 433 }); |
207 })(); | 434 })(); |
OLD | NEW |