From dd3efea1da106b004bc186811c9b9bc66bac64be Mon Sep 17 00:00:00 2001 From: Anthony Tseng Date: Thu, 22 Sep 2016 14:52:25 +0800 Subject: [PATCH] Autofill data add/edit method refactoring 1. add/edit data on default session 2. Apply changes to https://github.com/brave/electron/pull/61 requires https://github.com/brave/electron/pull/61 fix #4155 fix #3725 Auditors: @bridiver, @bbondy Test Plan: STR in #4155 and #3725 --- app/filtering.js | 68 ++++++++++++++++-------------------------- docs/state.md | 12 +++----- js/components/frame.js | 20 ++++++------- js/stores/appStore.js | 42 +++++++++++--------------- 4 files changed, 56 insertions(+), 86 deletions(-) diff --git a/app/filtering.js b/app/filtering.js index 28214e8b411..b5b9611fda9 100644 --- a/app/filtering.js +++ b/app/filtering.js @@ -627,57 +627,39 @@ module.exports.setDefaultZoomLevel = (zoom) => { } } -module.exports.addAutofillAddress = (detail) => { - let guidMap = {} - for (let partition in registeredSessions) { - let ses = registeredSessions[partition] - let guid = ses.autofill.addProfile({ - full_name: detail.name, - company_name: detail.organization, - street_address: detail.streetAddress, - city: detail.city, - state: detail.state, - postal_code: detail.postalCode, - country_code: detail.country, - phone: detail.phone, - email: detail.email - }) - guidMap[partition] = guid - } - return guidMap +module.exports.addAutofillAddress = (detail, oldGuid) => { + let guid = session.defaultSession.autofill.addProfile({ + full_name: detail.name, + company_name: detail.organization, + street_address: detail.streetAddress, + city: detail.city, + state: detail.state, + postal_code: detail.postalCode, + country_code: detail.country, + phone: detail.phone, + email: detail.email, + guid: oldGuid + }) + return guid } module.exports.removeAutofillAddress = (guid) => { - for (let partition in registeredSessions) { - let ses = registeredSessions[partition] - if (guid[partition] !== undefined) { - ses.autofill.removeProfile(guid[partition]) - } - } + session.defaultSession.autofill.removeProfile(guid) } -module.exports.addAutofillCreditCard = (detail) => { - let guidMap = {} - for (let partition in registeredSessions) { - let ses = registeredSessions[partition] - let guid = ses.autofill.addCreditCard({ - name: detail.name, - card_number: detail.card, - expiration_month: detail.month, - expiration_year: detail.year - }) - guidMap[partition] = guid - } - return guidMap +module.exports.addAutofillCreditCard = (detail, oldGuid) => { + let guid = session.defaultSession.autofill.addCreditCard({ + name: detail.name, + card_number: detail.card, + expiration_month: detail.month, + expiration_year: detail.year, + guid: oldGuid + }) + return guid } module.exports.removeAutofillCreditCard = (guid) => { - for (let partition in registeredSessions) { - let ses = registeredSessions[partition] - if (guid[partition] !== undefined) { - ses.autofill.removeCreditCard(guid[partition]) - } - } + session.defaultSession.autofill.removeCreditCard(guid) } module.exports.clearAutocompleteData = () => { diff --git a/docs/state.md b/docs/state.md index 3dbf4b7ef1f..debe4a0a1ea 100644 --- a/docs/state.md +++ b/docs/state.md @@ -204,15 +204,11 @@ AppStore }, autofill: { addresses: { - guid: [{ - Object.> // map of (partition, id) used to access the autofill entry in database - }], + guid: Array, // List of id used to access the autofill entry in database timestamp: number }, creditCards: { - guid: [{ - Object.> // map of (partition, id) used to access the autofill entry in database - }], + guid: Array, // List of id used to access the autofill entry in database timestamp: number } }, @@ -508,14 +504,14 @@ WindowStore country: string, phone: string, email: string, - guid: Object.> // map of (partition, id) used to access the autofill entry in database + guid: string // id used to access the autofill entry in database }, autofillCreditCardDetail: { name: string, card: string, month: string, year: string, - guid: Object.> // map of (partition, id) used to access the autofill entry in database + guid: string // id used to access the autofill entry in database }, importBrowserDataDetail: [ { diff --git a/js/components/frame.js b/js/components/frame.js index 474027dad73..d36b44de196 100644 --- a/js/components/frame.js +++ b/js/components/frame.js @@ -115,13 +115,12 @@ class Frame extends ImmutableComponent { } else if (location === 'about:flash') { this.webview.send(messages.BRAVERY_DEFAULTS_UPDATED, this.braveryDefaults) } else if (location === 'about:autofill') { - const partition = FrameStateUtil.getPartition(this.frame) if (this.props.autofillAddresses) { - const addresses = this.props.autofillAddresses.get('guid') + const guids = this.props.autofillAddresses.get('guid') let list = [] - addresses.forEach((entry) => { - const guid = entry.get(partition) - const address = currentWindow.webContents.session.autofill.getProfile(guid) + guids.forEach((entry) => { + console.log(entry) + const address = currentWindow.webContents.session.autofill.getProfile(entry) const valid = Object.getOwnPropertyNames(address).length > 0 let addressDetail = { name: address.full_name, @@ -133,7 +132,7 @@ class Frame extends ImmutableComponent { country: address.country_code, phone: address.phone, email: address.email, - guid: entry.toJS() + guid: entry } if (valid) { list.push(addressDetail) @@ -144,18 +143,17 @@ class Frame extends ImmutableComponent { this.webview.send(messages.AUTOFILL_ADDRESSES_UPDATED, list) } if (this.props.autofillCreditCards) { - const creditCards = this.props.autofillCreditCards.get('guid') + const guids = this.props.autofillCreditCards.get('guid') let list = [] - creditCards.forEach((entry) => { - const guid = entry.get(partition) - const creditCard = currentWindow.webContents.session.autofill.getCreditCard(guid) + guids.forEach((entry) => { + const creditCard = currentWindow.webContents.session.autofill.getCreditCard(entry) const valid = Object.getOwnPropertyNames(creditCard).length > 0 let creditCardDetail = { name: creditCard.name, card: creditCard.card_number, month: creditCard.expiration_month, year: creditCard.expiration_year, - guid: entry.toJS() + guid: entry } if (valid) { list.push(creditCardDetail) diff --git a/js/stores/appStore.js b/js/stores/appStore.js index 42607ee85ef..f1a1b715f5c 100644 --- a/js/stores/appStore.js +++ b/js/stores/appStore.js @@ -613,17 +613,14 @@ const handleAppAction = (action) => { { const Filtering = require('../../app/filtering') appState = appState.setIn(['autofill', 'addresses', 'guid'], - appState.getIn(['autofill', 'addresses', 'guid']).filterNot((address) => { - return Immutable.is(address, action.originalDetail.get('guid')) + appState.getIn(['autofill', 'addresses', 'guid']).filterNot((guid) => { + return guid === action.originalDetail.get('guid') })) - let addresses = appState.getIn(['autofill', 'addresses', 'guid']) - const guid = Filtering.addAutofillAddress(action.detail.toJS()) - if (action.originalDetail.get('guid') !== undefined && - !Immutable.is(Immutable.fromJS(guid), action.originalDetail.get('guid'))) { - Filtering.removeAutofillAddress(action.originalDetail.get('guid').toJS()) - } - appState = appState.setIn(['autofill', 'addresses', 'guid'], addresses.push(Immutable.fromJS(guid))) + let guids = appState.getIn(['autofill', 'addresses', 'guid']) + const guid = Filtering.addAutofillAddress(action.detail.toJS(), + action.originalDetail.get('guid') === undefined ? '-1' : action.originalDetail.get('guid')) + appState = appState.setIn(['autofill', 'addresses', 'guid'], guids.push(guid)) appState = appState.setIn(['autofill', 'addresses', 'timestamp'], new Date().getTime()) break } @@ -631,10 +628,10 @@ const handleAppAction = (action) => { { const Filtering = require('../../app/filtering') appState = appState.setIn(['autofill', 'addresses', 'guid'], - appState.getIn(['autofill', 'addresses', 'guid']).filterNot((address) => { - return Immutable.is(address, action.detail.get('guid')) + appState.getIn(['autofill', 'addresses', 'guid']).filterNot((guid) => { + return guid === action.detail.get('guid') })) - Filtering.removeAutofillAddress(action.detail.get('guid').toJS()) + Filtering.removeAutofillAddress(action.detail.get('guid')) appState = appState.setIn(['autofill', 'addresses', 'timestamp'], new Date().getTime()) break } @@ -642,17 +639,14 @@ const handleAppAction = (action) => { { const Filtering = require('../../app/filtering') appState = appState.setIn(['autofill', 'creditCards', 'guid'], - appState.getIn(['autofill', 'creditCards', 'guid']).filterNot((card) => { - return Immutable.is(card, action.originalDetail.get('guid')) + appState.getIn(['autofill', 'creditCards', 'guid']).filterNot((guid) => { + return guid === action.originalDetail.get('guid') })) - let creditCards = appState.getIn(['autofill', 'creditCards', 'guid']) - const guid = Filtering.addAutofillCreditCard(action.detail.toJS()) - if (action.originalDetail.get('guid') !== undefined && - !Immutable.is(Immutable.fromJS(guid), action.originalDetail.get('guid'))) { - Filtering.removeAutofillCreditCard(action.originalDetail.get('guid').toJS()) - } - appState = appState.setIn(['autofill', 'creditCards', 'guid'], creditCards.push(Immutable.fromJS(guid))) + let guids = appState.getIn(['autofill', 'creditCards', 'guid']) + const guid = Filtering.addAutofillCreditCard(action.detail.toJS(), + action.originalDetail.get('guid') === undefined ? '-1' : action.originalDetail.get('guid')) + appState = appState.setIn(['autofill', 'creditCards', 'guid'], guids.push(guid)) appState = appState.setIn(['autofill', 'creditCards', 'timestamp'], new Date().getTime()) break } @@ -660,10 +654,10 @@ const handleAppAction = (action) => { { const Filtering = require('../../app/filtering') appState = appState.setIn(['autofill', 'creditCards', 'guid'], - appState.getIn(['autofill', 'creditCards', 'guid']).filterNot((card) => { - return Immutable.is(card, action.detail.get('guid')) + appState.getIn(['autofill', 'creditCards', 'guid']).filterNot((guid) => { + return guid === action.detail.get('guid') })) - Filtering.removeAutofillCreditCard(action.detail.get('guid').toJS()) + Filtering.removeAutofillCreditCard(action.detail.get('guid')) appState = appState.setIn(['autofill', 'creditCards', 'timestamp'], new Date().getTime()) break }