diff --git a/app/ledger.js b/app/ledger.js index 6b62df4e8a2..c874794e175 100644 --- a/app/ledger.js +++ b/app/ledger.js @@ -9,11 +9,12 @@ module entry points: init() - called by app/index.js to start module quit() - .. .. .. .. prior to browser quitting - boot() - .. .. .. .. to create wallet + boot() - .. .. .. .. to create wallet + reset() - .. .. .. .. to remove state IPC entry point: - LEDGER_PUBLISHER - called synchronously by app/extensions/brave/content/scripts/pageInformation.js - CHANGE_SETTING - called asynchronously to record a settings change + LEDGER_PUBLISHER - called synchronously by app/extensions/brave/content/scripts/pageInformation.js + CHANGE_SETTING - called asynchronously to record a settings change eventStore entry point: addChangeListener - called when tabs render or gain focus @@ -105,6 +106,7 @@ const clientOptions = { } var doneTimer +var quitP var v2RulesetDB const v2RulesetPath = 'ledger-rulesV2.leveldb' @@ -165,6 +167,10 @@ const doAction = (action) => { quit() break + case appConstants.APP_CLEAR_HISTORY: + if (!getSetting(settings.PAYMENTS_ENABLED)) reset(true) + break + case appConstants.APP_IDLE_STATE_CHANGED: visit('NOOP', underscore.now(), null) break @@ -263,19 +269,23 @@ const doAction = (action) => { /* * module entry points */ + var init = () => { try { appDispatcher.register(doAction) initialize(getSetting(settings.PAYMENTS_ENABLED)) - doneTimer = setInterval(doneWriter, 1 * msecs.hour) + doneTimer = setInterval(doneWriter, 1 * msecs.minute) } catch (ex) { console.log('ledger.js initialization failed: ' + ex.toString() + '\n' + ex.stack) } } var quit = () => { + quitP = true visit('NOOP', underscore.now(), null) clearInterval(doneTimer) doneWriter() + + if ((!getSetting(settings.PAYMENTS_ENABLED)) && (getSetting(settings.SHUTDOWN_CLEAR_HISTORY))) reset(true) } var boot = () => { @@ -305,6 +315,17 @@ var boot = () => { }) } +var reset = (doneP) => { + var files = [ logPath, publisherPath, scoresPath, synopsisPath ] + + if (!doneP) files.push(statePath) + files.forEach((file) => { + fs.unlink(pathName(file), (err) => { + if ((err) && (err.code !== 'ENOENT')) console.log(err) + }) + }) +} + /* * Print or Save Recovery Keys */ @@ -811,29 +832,8 @@ var enable = (paymentsEnabled) => { // change undefined include publishers to include publishers appActions.enableUndefinedPublishers(synopsis.publishers) - fs.readFile(pathName(publisherPath), (err, data) => { - if (err) { - if (err.code !== 'ENOENT') console.log('publisherPath read error: ' + err.toString()) - return - } - - if (publisherInfo._internal.verboseP) console.log('\nfound ' + pathName(publisherPath)) - try { - data = JSON.parse(data) - underscore.keys(data).sort().forEach((publisher) => { - var entries = data[publisher] - - publishers[publisher] = {} - entries.forEach((entry) => { - locations[entry.location] = entry - publishers[publisher][entry.location] = { timestamp: entry.when, tabIds: [] } - updateLocation(entry.location, publisher) - }) - }) - } catch (ex) { - console.log('publishersPath parse error: ' + ex.toString()) - } - }) + fs.unlink(pathName(publisherPath), (err) => { if ((err) && (err.code !== 'ENOENT')) console.log(err) }) + fs.unlink(pathName(scoresPath), (err) => { if ((err) && (err.code !== 'ENOENT')) console.log(err) }) }) } @@ -978,22 +978,7 @@ const fetchFavIcon = (entry, url, redirects) => { } var updatePublisherInfo = (changedPublisher) => { - var data = {} - var then = underscore.now() - msecs.week - - underscore.keys(publishers).sort().forEach((publisher) => { - var entries = [] - - underscore.keys(publishers[publisher]).forEach((location) => { - var when = publishers[publisher][location].timestamp - - if (when > then) entries.push({ location: location, when: when }) - }) - - if (entries.length > 0) data[publisher] = entries - }) - atomicWriter(pathName(publisherPath), data, () => {}) - atomicWriter(pathName(scoresPath), synopsis.allN(), () => {}) + var data atomicWriter(pathName(synopsisPath), synopsis, () => {}) if (!publisherInfo._internal.enabled) return @@ -2144,6 +2129,15 @@ var atomicWriter = (path, obj, options, cb) => { return cb(err) } + if ((quitP) && (!getSetting(settings.PAYMENTS_ENABLED)) && (getSetting(settings.SHUTDOWN_CLEAR_HISTORY))) { + if (ledgerInfo._internal.debugP) console.log('\ndeleting ' + path + suffix) + fs.unlink(path + suffix, (err) => { + if (err) console.log('unlink error: ' + err.toString()) + cb(err) + }) + return + } + if (ledgerInfo._internal.debugP) console.log('\nrenaming ' + path + suffix) fs.rename(path + suffix, path, (err) => { if (err) console.log('rename error: ' + err.toString()) @@ -2171,7 +2165,7 @@ var pathName = (name) => { return path.join(app.getPath('userData'), parts.name + parts.ext) } -/** +/* * UI controller functionality */ @@ -2314,5 +2308,6 @@ module.exports = { recoverKeys: recoverKeys, backupKeys: backupKeys, quit: quit, - boot: boot + boot: boot, + reset: reset } diff --git a/js/about/preferences.js b/js/about/preferences.js index 1bf638d21e1..5b2606fa102 100644 --- a/js/about/preferences.js +++ b/js/about/preferences.js @@ -586,7 +586,7 @@ class SecurityTab extends ImmutableComponent { return
- + diff --git a/test/about/ledgerPanelTest.js b/test/about/ledgerPanelTest.js index 0d2af3593bc..9788d68db38 100644 --- a/test/about/ledgerPanelTest.js +++ b/test/about/ledgerPanelTest.js @@ -1,12 +1,29 @@ /* global describe, it, beforeEach, before */ const Brave = require('../lib/brave') -const {urlInput, advancedSettingsButton, addFundsButton, paymentsWelcomePage, paymentsTab, walletSwitch, siteSettingItem, ledgerTable} = require('../lib/selectors') +const { + urlInput, + advancedSettingsButton, + addFundsButton, + paymentsWelcomePage, + paymentsTab, + walletSwitch, + siteSettingItem, + ledgerTable, + securityTab +} = require('../lib/selectors') const assert = require('assert') const settings = require('../../js/constants/settings') const prefsUrl = 'about:preferences' const ledgerAPIWaitTimeout = 20000 +const site1 = 'http://example.com/' +const site2 = 'https://www.eff.org/' +const site3 = 'http://web.mit.edu/zyan/Public/wait.html' + +function * setupBrave () { + Brave.addCommands() +} function * setup (client) { yield client @@ -115,6 +132,149 @@ describe('Regular payment panel tests', function () { }) }) + describe('ledger history', function () { + Brave.beforeAllServerSetup(this) + + beforeEach(function * () { + yield Brave.startApp() + yield setupBrave(Brave.app.client) + }) + + it('is NOT cleared if payment is disabled before the close and clear history is false', function * () { + yield setup(Brave.app.client) + yield Brave.app.client + .tabByIndex(0) + .loadUrl(prefsUrl) + .waitForVisible(paymentsTab) + .click(paymentsTab) + .waitForVisible(paymentsWelcomePage) + .waitForVisible(walletSwitch) + .click(walletSwitch) + .waitForEnabled(addFundsButton, ledgerAPIWaitTimeout) + .tabByIndex(0) + .loadUrl(site1) + .windowByUrl(Brave.browserWindowUrl) + .waitForSiteEntry(site1) + .tabByUrl(site1) + .loadUrl(site2) + .windowByUrl(Brave.browserWindowUrl) + .waitForSiteEntry(site2) + .tabByUrl(site2) + .loadUrl(prefsUrl) + .waitForVisible(paymentsTab) + .click(paymentsTab) + .waitForElementCount('[data-tbody-index="1"] tr', 2) + .click(walletSwitch) + .waitForElementCount(addFundsButton, 0) + yield Brave.stopApp(false) + + yield Brave.startApp() + yield setupBrave(Brave.app.client) + yield Brave.app.client + .waitForBrowserWindow() + .waitForVisible(urlInput) + .tabByIndex(0) + .waitForVisible(paymentsWelcomePage) + .waitForVisible(walletSwitch) + .click(walletSwitch) + .waitForEnabled(addFundsButton, ledgerAPIWaitTimeout) + .waitForElementCount('[data-tbody-index="1"] tr', 2) + + yield Brave.stopApp() + }) + + it('is NOT cleared if payment is enabled before the close and clear history is true', function * () { + yield setup(Brave.app.client) + yield Brave.app.client + .tabByIndex(0) + .loadUrl(prefsUrl) + .waitForVisible(paymentsTab) + .click(paymentsTab) + .waitForVisible(paymentsWelcomePage) + .waitForVisible(walletSwitch) + .click(walletSwitch) + .waitForEnabled(addFundsButton, ledgerAPIWaitTimeout) + .tabByIndex(0) + .loadUrl(site1) + .windowByUrl(Brave.browserWindowUrl) + .waitForSiteEntry(site1) + .tabByUrl(site1) + .loadUrl(site2) + .windowByUrl(Brave.browserWindowUrl) + .waitForSiteEntry(site2) + .tabByUrl(site2) + .loadUrl(prefsUrl) + .waitForVisible(paymentsTab) + .click(paymentsTab) + .waitForElementCount('[data-tbody-index="1"] tr', 2) + .click(securityTab) + .waitForVisible('[data-test-id="clearBrowsingHistory"]') + .click('[data-test-id="clearBrowsingHistory"] .switchBackground') + yield Brave.stopApp(false) + + yield Brave.startApp() + yield setupBrave(Brave.app.client) + yield Brave.app.client + .waitForBrowserWindow() + .waitForVisible(urlInput) + .tabByIndex(0) + .waitForVisible('[data-test-id="clearBrowsingHistory"]') + .click(paymentsTab) + .waitForEnabled(addFundsButton) + .waitForElementCount('[data-tbody-index="1"] tr', 2) + + yield Brave.stopApp() + }) + + it('is CLEARED if payment is disabled before the close and clear history is true', function * () { + yield setup(Brave.app.client) + yield Brave.app.client + .tabByIndex(0) + .loadUrl(prefsUrl) + .waitForVisible(paymentsTab) + .click(paymentsTab) + .waitForVisible(paymentsWelcomePage) + .waitForVisible(walletSwitch) + .click(walletSwitch, ledgerAPIWaitTimeout) + .waitForEnabled(addFundsButton) + .tabByIndex(0) + .loadUrl(site1) + .windowByUrl(Brave.browserWindowUrl) + .waitForSiteEntry(site1) + .tabByUrl(site1) + .loadUrl(site2) + .windowByUrl(Brave.browserWindowUrl) + .waitForSiteEntry(site2) + .tabByUrl(site2) + .loadUrl(prefsUrl) + .waitForVisible(paymentsTab) + .click(paymentsTab) + .waitForElementCount('[data-tbody-index="1"] tr', 2) + .click(walletSwitch) + .waitForElementCount(addFundsButton, 0) + .click(securityTab) + .waitForVisible('[data-test-id="clearBrowsingHistory"]') + .click('[data-test-id="clearBrowsingHistory"] .switchBackground') + yield Brave.stopApp(false) + + yield Brave.startApp() + yield setupBrave(Brave.app.client) + yield Brave.app.client + .waitForBrowserWindow() + .waitForVisible(urlInput) + .tabByIndex(0) + .waitForVisible('[data-test-id="clearBrowsingHistory"]') + .click(paymentsTab) + .waitForVisible(paymentsWelcomePage) + .waitForVisible(walletSwitch) + .click(walletSwitch, ledgerAPIWaitTimeout) + .waitForEnabled(addFundsButton) + .waitForElementCount('[data-tbody-index="1"] tr', 0) + + yield Brave.stopApp() + }) + }) + describe('auto include', function () { Brave.beforeEach(this) @@ -133,8 +293,6 @@ describe('Regular payment panel tests', function () { }) it('site is added automatically', function * () { - const site1 = 'http://example.com/' - const site2 = 'https://www.eff.org/' yield this.app.client .loadUrl(site1) .windowByUrl(Brave.browserWindowUrl) @@ -162,8 +320,6 @@ describe('Regular payment panel tests', function () { }) it('site is not added automatically', function * () { - const site1 = 'http://example.com/' - const site2 = 'https://www.eff.org/' yield this.app.client .windowByUrl(Brave.browserWindowUrl) .changeSetting(settings.AUTO_SUGGEST_SITES, false) @@ -194,8 +350,6 @@ describe('Regular payment panel tests', function () { }) it('first site included, second site excluded', function * () { - const site1 = 'http://example.com/' - const site2 = 'https://www.eff.org/' yield this.app.client .tabByIndex(0) .loadUrl(site1) @@ -250,12 +404,11 @@ describe('synopsis', function () { }) it('creates synopsis table after visiting a site', function * () { - const site1 = 'http://web.mit.edu/zyan/Public/wait.html' yield this.app.client - .url(site1) + .url(site3) .waitForTextValue('div', 'done') .windowByUrl(Brave.browserWindowUrl) - .tabByUrl(site1) + .tabByUrl(site3) .loadUrl(prefsUrl) .waitForVisible(paymentsTab) .click(paymentsTab) @@ -263,22 +416,19 @@ describe('synopsis', function () { }) it('can sort synopsis table', function * () { - const site1 = 'http://web.mit.edu/zyan/Public/wait.html' - const site2 = 'http://example.com/' - const site3 = 'https://www.eff.org/' yield this.app.client + .loadUrl(site3) + .windowByUrl(Brave.browserWindowUrl) + .waitForSiteEntry(site3, false) + .tabByUrl(site3) .loadUrl(site1) .windowByUrl(Brave.browserWindowUrl) - .waitForSiteEntry(site1, false) + .waitForSiteEntry(site1) .tabByUrl(site1) .loadUrl(site2) .windowByUrl(Brave.browserWindowUrl) .waitForSiteEntry(site2) .tabByUrl(site2) - .loadUrl(site3) - .windowByUrl(Brave.browserWindowUrl) - .waitForSiteEntry(site3) - .tabByUrl(site3) .loadUrl(prefsUrl) .waitForVisible(paymentsTab) .click(paymentsTab) @@ -298,9 +448,8 @@ describe('synopsis', function () { }) it('can disable site', function * () { - const site1 = 'https://www.eff.org/' yield this.app.client - .loadUrl(site1) + .loadUrl(site2) .loadUrl(prefsUrl) .waitForVisible(paymentsTab) .click(paymentsTab)