diff --git a/app/autofill.js b/app/autofill.js new file mode 100644 index 00000000000..8890721b324 --- /dev/null +++ b/app/autofill.js @@ -0,0 +1,56 @@ +/* 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/. */ + +const electron = require('electron') +const session = electron.session +const appActions = require('../js/actions/appActions') + +module.exports.init = () => { + process.on('personal-data-changed', (profileGuids, creditCardGuids) => { + setImmediate(() => { + appActions.autofillDataChanged(profileGuids, creditCardGuids) + }) + }) +} + +module.exports.addAutofillAddress = (detail, 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: guid + }) +} + +module.exports.removeAutofillAddress = (guid) => { + session.defaultSession.autofill.removeProfile(guid) +} + +module.exports.addAutofillCreditCard = (detail, guid) => { + session.defaultSession.autofill.addCreditCard({ + name: detail.name, + card_number: detail.card, + expiration_month: detail.month, + expiration_year: detail.year, + guid: guid + }) +} + +module.exports.removeAutofillCreditCard = (guid) => { + session.defaultSession.autofill.removeCreditCard(guid) +} + +module.exports.clearAutocompleteData = () => { + session.defaultSession.autofill.clearAutocompleteData() +} + +module.exports.clearAutofillData = () => { + session.defaultSession.autofill.clearAutofillData() +} diff --git a/app/filtering.js b/app/filtering.js index 048ce0e1d8f..c1556397147 100644 --- a/app/filtering.js +++ b/app/filtering.js @@ -645,55 +645,6 @@ module.exports.setDefaultZoomLevel = (zoom) => { } } -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) => { - session.defaultSession.autofill.removeProfile(guid) -} - -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) => { - session.defaultSession.autofill.removeCreditCard(guid) -} - -module.exports.clearAutocompleteData = () => { - for (let partition in registeredSessions) { - let ses = registeredSessions[partition] - ses.autofill.clearAutocompleteData() - } -} - -module.exports.clearAutofillData = () => { - for (let partition in registeredSessions) { - let ses = registeredSessions[partition] - ses.autofill.clearAutofillData() - } -} - module.exports.getMainFrameUrl = (details) => { if (details.resourceType === 'mainFrame') { return details.url diff --git a/app/index.js b/app/index.js index a0599a083d3..a610a80c59b 100644 --- a/app/index.js +++ b/app/index.js @@ -58,6 +58,7 @@ const downloadActions = require('../js/actions/downloadActions') const SessionStore = require('./sessionStore') const AppStore = require('../js/stores/appStore') const PackageLoader = require('./package-loader') +const Autofill = require('./autofill') const Extensions = require('./extensions') const Filtering = require('./filtering') const TrackingProtection = require('./trackingProtection') @@ -420,6 +421,7 @@ app.on('ready', () => { basicAuth.init() contentSettings.init() privacy.init() + Autofill.init() Extensions.init() Filtering.init() SiteHacks.init() diff --git a/app/sessionStore.js b/app/sessionStore.js index 49cb50dbb05..8dee3049518 100644 --- a/app/sessionStore.js +++ b/app/sessionStore.js @@ -24,6 +24,7 @@ const {tabFromFrame} = require('../js/state/frameStateUtil') const siteUtil = require('../js/state/siteUtil') const sessionStorageVersion = 1 const filtering = require('./filtering') +const autofill = require('./autofill') // const tabState = require('./common/state/tabState') const getSetting = require('../js/settings').getSetting @@ -246,11 +247,11 @@ module.exports.cleanAppData = (data, isShutdown) => { } const clearAutocompleteData = isShutdown && getSetting(settings.SHUTDOWN_CLEAR_AUTOCOMPLETE_DATA) === true if (clearAutocompleteData) { - filtering.clearAutocompleteData() + autofill.clearAutocompleteData() } const clearAutofillData = isShutdown && getSetting(settings.SHUTDOWN_CLEAR_AUTOFILL_DATA) === true if (clearAutofillData) { - filtering.clearAutofillData() + autofill.clearAutofillData() } const clearSiteSettings = isShutdown && getSetting(settings.SHUTDOWN_CLEAR_SITE_SETTINGS) === true if (clearSiteSettings) { diff --git a/docs/appActions.md b/docs/appActions.md index e69bc0a7ddc..a8647eac992 100644 --- a/docs/appActions.md +++ b/docs/appActions.md @@ -415,6 +415,18 @@ Remove credit card data +### autofillDataChanged(addressGuids, creditCardGuids) + +Autofill data changed + +**Parameters** + +**addressGuids**: `Array`, the guid array to access address entries in autofill DB + +**creditCardGuids**: `Array`, the guid array to access credit card entries in autofill DB + + + ### windowBlurred(appWindowId) Dispatches a message when appWindowId loses focus diff --git a/js/actions/appActions.js b/js/actions/appActions.js index 5f7ac70b57d..30c5748ca4c 100644 --- a/js/actions/appActions.js +++ b/js/actions/appActions.js @@ -483,6 +483,19 @@ const appActions = { }) }, + /** + * Autofill data changed + * @param {Array} addressGuids - the guid array to access address entries in autofill DB + * @param {Array} creditCardGuids - the guid array to access credit card entries in autofill DB + */ + autofillDataChanged: function (addressGuids, creditCardGuids) { + AppDispatcher.dispatch({ + actionType: AppConstants.APP_AUTOFILL_DATA_CHANGED, + addressGuids, + creditCardGuids + }) + }, + /** * Dispatches a message when appWindowId loses focus * diff --git a/js/components/frame.js b/js/components/frame.js index 8b09e9c733a..8f771a72852 100644 --- a/js/components/frame.js +++ b/js/components/frame.js @@ -117,12 +117,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 defaultSession = global.require('electron').remote.session.defaultSession if (this.props.autofillAddresses) { const guids = this.props.autofillAddresses.get('guid') let list = [] guids.forEach((entry) => { - const address = currentWindow.webContents.session.autofill.getProfile(entry) - const valid = Object.getOwnPropertyNames(address).length > 0 + const address = defaultSession.autofill.getProfile(entry) let addressDetail = { name: address.full_name, organization: address.company_name, @@ -135,11 +135,7 @@ class Frame extends ImmutableComponent { email: address.email, guid: entry } - if (valid) { - list.push(addressDetail) - } else { - appActions.removeAutofillAddress(addressDetail) - } + list.push(addressDetail) }) this.webview.send(messages.AUTOFILL_ADDRESSES_UPDATED, list) } @@ -147,8 +143,7 @@ class Frame extends ImmutableComponent { const guids = this.props.autofillCreditCards.get('guid') let list = [] guids.forEach((entry) => { - const creditCard = currentWindow.webContents.session.autofill.getCreditCard(entry) - const valid = Object.getOwnPropertyNames(creditCard).length > 0 + const creditCard = defaultSession.autofill.getCreditCard(entry) let creditCardDetail = { name: creditCard.name, card: creditCard.card_number, @@ -156,11 +151,7 @@ class Frame extends ImmutableComponent { year: creditCard.expiration_year, guid: entry } - if (valid) { - list.push(creditCardDetail) - } else { - appActions.removeAutofillCreditCard(creditCardDetail) - } + list.push(creditCardDetail) }) this.webview.send(messages.AUTOFILL_CREDIT_CARDS_UPDATED, list) } diff --git a/js/constants/appConstants.js b/js/constants/appConstants.js index dfb9a19896a..041e9f9c2a5 100644 --- a/js/constants/appConstants.js +++ b/js/constants/appConstants.js @@ -47,6 +47,7 @@ const AppConstants = { APP_REMOVE_AUTOFILL_ADDRESS: _, APP_ADD_AUTOFILL_CREDIT_CARD: _, APP_REMOVE_AUTOFILL_CREDIT_CARD: _, + APP_AUTOFILL_DATA_CHANGED: _, APP_SET_LOGIN_REQUIRED_DETAIL: _, APP_SET_LOGIN_RESPONSE_DETAIL: _, APP_WINDOW_BLURRED: _, diff --git a/js/stores/appStore.js b/js/stores/appStore.js index d7935aa4bf9..86253317be3 100644 --- a/js/stores/appStore.js +++ b/js/stores/appStore.js @@ -32,6 +32,7 @@ const locale = require('../../app/locale') const path = require('path') const {channel} = require('../../app/channel') const os = require('os') +const autofill = require('../../app/autofill') // state helpers const basicAuthState = require('../../app/common/state/basicAuthState') @@ -623,12 +624,10 @@ const handleAppAction = (action) => { Filtering.clearStorageData() } if (action.clearDataDetail.get('autocompleteData')) { - const Filtering = require('../../app/filtering') - Filtering.clearAutocompleteData() + autofill.clearAutocompleteData() } if (action.clearDataDetail.get('autofillData')) { - const Filtering = require('../../app/filtering') - Filtering.clearAutofillData() + autofill.clearAutofillData() } if (action.clearDataDetail.get('savedSiteSettings')) { appState = appState.set('siteSettings', Immutable.Map()) @@ -648,57 +647,26 @@ const handleAppAction = (action) => { break } case AppConstants.APP_ADD_AUTOFILL_ADDRESS: - { - const Filtering = require('../../app/filtering') - appState = appState.setIn(['autofill', 'addresses', 'guid'], - appState.getIn(['autofill', 'addresses', 'guid']).filterNot((guid) => { - return guid === action.originalDetail.get('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 - } + autofill.addAutofillAddress(action.detail.toJS(), + action.originalDetail.get('guid') === undefined ? '-1' : action.originalDetail.get('guid')) + break case AppConstants.APP_REMOVE_AUTOFILL_ADDRESS: - { - const Filtering = require('../../app/filtering') - appState = appState.setIn(['autofill', 'addresses', 'guid'], - appState.getIn(['autofill', 'addresses', 'guid']).filterNot((guid) => { - return guid === action.detail.get('guid') - })) - Filtering.removeAutofillAddress(action.detail.get('guid')) - appState = appState.setIn(['autofill', 'addresses', 'timestamp'], new Date().getTime()) - break - } + autofill.removeAutofillAddress(action.detail.get('guid')) + break case AppConstants.APP_ADD_AUTOFILL_CREDIT_CARD: - { - const Filtering = require('../../app/filtering') - appState = appState.setIn(['autofill', 'creditCards', 'guid'], - appState.getIn(['autofill', 'creditCards', 'guid']).filterNot((guid) => { - return guid === action.originalDetail.get('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 - } + autofill.addAutofillCreditCard(action.detail.toJS(), + action.originalDetail.get('guid') === undefined ? '-1' : action.originalDetail.get('guid')) + break case AppConstants.APP_REMOVE_AUTOFILL_CREDIT_CARD: - { - const Filtering = require('../../app/filtering') - appState = appState.setIn(['autofill', 'creditCards', 'guid'], - appState.getIn(['autofill', 'creditCards', 'guid']).filterNot((guid) => { - return guid === action.detail.get('guid') - })) - Filtering.removeAutofillCreditCard(action.detail.get('guid')) - appState = appState.setIn(['autofill', 'creditCards', 'timestamp'], new Date().getTime()) - break - } + autofill.removeAutofillCreditCard(action.detail.get('guid')) + break + case AppConstants.APP_AUTOFILL_DATA_CHANGED: + const date = new Date().getTime() + appState = appState.setIn(['autofill', 'addresses', 'guid'], action.addressGuids) + appState = appState.setIn(['autofill', 'addresses', 'timestamp'], date) + appState = appState.setIn(['autofill', 'creditCards', 'guid'], action.creditCardGuids) + appState = appState.setIn(['autofill', 'creditCards', 'timestamp'], date) + break case AppConstants.APP_SET_LOGIN_REQUIRED_DETAIL: appState = basicAuthState.setLoginRequiredDetail(appState, action.tabId, action.detail) break