diff --git a/app/ledger.js b/app/ledger.js index 0a118027551..39844bf5c47 100644 --- a/app/ledger.js +++ b/app/ledger.js @@ -253,14 +253,6 @@ var quit = () => { visit('NOOP', underscore.now(), null) clearInterval(doneTimer) doneWriter() - if (v2RulesetDB) { - v2RulesetDB.close() - v2RulesetDB = null - } - if (v2PublishersDB) { - v2PublishersDB.close() - v2PublishersDB = null - } } var boot = () => { @@ -578,7 +570,11 @@ eventStore.addChangeListener(() => { var entry, faviconURL, pattern, publisher var location = page.url - if ((location.match(/^about/)) || ((locations[location]) && (locations[location].publisher))) return + if (location.match(/^about/)) return + + location = urlFormat(underscore.pick(urlParse(location), [ 'protocol', 'host', 'hostname', 'port', 'pathname' ])) + publisher = locations[location] && locations[location].publisher + if (publisher) return updateLocation(location, publisher) if (!page.publisher) { try { @@ -589,7 +585,7 @@ eventStore.addChangeListener(() => { console.log('getPublisher error for ' + location + ': ' + ex.toString()) } } - locations[location] = underscore.omit(page, [ 'url' ]) + locations[location] = underscore.omit(page, [ 'url', 'protocol', 'faviconURL' ]) if (!page.publisher) return publisher = page.publisher @@ -602,6 +598,7 @@ eventStore.addChangeListener(() => { updatePublisherInfo() }) } + updateLocation(location, publisher) entry = synopsis.publishers[publisher] if ((page.protocol) && (!entry.protocol)) entry.protocol = page.protocol @@ -866,6 +863,7 @@ var enable = (paymentsEnabled) => { entries.forEach((entry) => { locations[entry.location] = entry publishers[publisher][entry.location] = { timestamp: entry.when, tabIds: [] } + updateLocation(entry.location, publisher) }) }) } catch (ex) { @@ -875,6 +873,47 @@ var enable = (paymentsEnabled) => { }) } +/* + * update location information + */ + +var updateLocationInfo = (location) => { + appActions.updateLocationInfo(locations) +} + +var updateLocation = (location, publisher) => { + var updateP + + if (typeof locations[location].stickyP === 'undefined') locations[location].stickyP = stickyP(publisher) + if (typeof locations[location].verified !== 'undefined') return + + if (synopsis && synopsis.publishers[publisher] && (typeof synopsis.publishers[publisher].options.verified !== 'undefined')) { + locations[location].verified = synopsis.publishers[publisher].options.verified || false + updateP = true + } else { + verifiedP(publisher, (err, result) => { + if ((err) && (!err.notFound)) return + + locations[location].verified = (result && result.verified) || false + updateLocationInfo(location) + }) + } + + if (synopsis && synopsis.publishers[publisher] && (typeof synopsis.publishers[publisher].options.exclude !== 'undefined')) { + locations[location].exclude = synopsis.publishers[publisher].options.exclude || false + updateP = true + } else { + excludeP(publisher, (err, result) => { + if ((err) && (!err.notFound)) return + + locations[location].exclude = (result && result.exclude) || false + updateLocationInfo(location) + }) + } + + if (updateP) updateLocationInfo(location) +} + /* * update publisher information */ diff --git a/app/renderer/components/publisherToggle.js b/app/renderer/components/publisherToggle.js index ea598b48456..29886e61765 100644 --- a/app/renderer/components/publisherToggle.js +++ b/app/renderer/components/publisherToggle.js @@ -3,7 +3,6 @@ * You can obtain one at http://mozilla.org/MPL/2.0/. */ const React = require('react') -const tldjs = require('tldjs') const ImmutableComponent = require('../../../js/components/immutableComponent') const appActions = require('../../../js/actions/appActions') const settings = require('../../../js/constants/settings') @@ -11,6 +10,8 @@ const getSetting = require('../../../js/settings').getSetting const {StyleSheet, css} = require('aphrodite') const globalStyles = require('./styles/global') const commonStyles = require('./styles/commonStyles') +const {getHostPattern, isHttpOrHttps} = require('../../../js/lib/urlutil') +const {getBaseUrl} = require('../../../js/lib/appUrlUtil') const noFundVerifiedPublisherImage = require('../../extensions/brave/img/urlbar/browser_URL_fund_no_verified.svg') const fundVerifiedPublisherImage = require('../../extensions/brave/img/urlbar/browser_URL_fund_yes_verified.svg') @@ -23,97 +24,89 @@ class PublisherToggle extends ImmutableComponent { this.onAuthorizePublisher = this.onAuthorizePublisher.bind(this) } - get publisherId () { -// @cezaraugusto: publisherIds aren't limited to a domain name -// when this runs, do you think that app/extensions/brave/content/scripts/pageInformation.js has already run? - - return tldjs.getDomain(this.props.url) + get locationId () { + return getBaseUrl(this.props.location) } - get hostPattern () { - return `https?://${this.publisherId}` + get publisherId () { + return this.props.locationInfo.getIn([this.locationId, 'publisher']) } get hostSettings () { - // hostPattern defines it's own identifier for authorized publishers - // sites that do not match criteria would populate siteSettings - // with their default protocol, not hostPattern - return this.props.hostSettings.get(this.hostPattern) + const hostPattern = getHostPattern(this.publisherId) + return this.props.siteSettings.get(hostPattern) } get validPublisherSynopsis () { - // If session is clear then siteSettings is undefined and icon will never be shown, - // but synopsis may not be empty. In such cases let's check if synopsis matches current publisherId - return this.props.synopsis.map(entry => entry.get('site')).includes(this.publisherId) + // If session is clear then siteSettings is undefined and icon + // will never be shown, but synopsis may not be empty. + // In such cases let's check if synopsis matches current publisherId + return !!this.props.synopsis.map(entry => entry.get('site')).includes(this.publisherId) } - get authorizedPublisher () { - // If we can't get ledgerPayments, then it's likely that we are - // on a clean session. Let's then check for publisher's synopsis + get enabledForPaymentsPublisher () { + // All publishers will be enabled by default if AUTO_SUGGEST is ON, + // excluding publishers defined on ledger's exclusion list + const excluded = this.props.locationInfo.getIn([this.locationId, 'exclude']) + const autoSuggestSites = getSetting(settings.AUTO_SUGGEST_SITES) + + // hostSettings is undefined until user hit addFunds button. + // For such cases check autoSuggestSites for eligibility. return this.hostSettings ? this.hostSettings.get('ledgerPayments') !== false - : this.validPublisherSynopsis + : this.validPublisherSynopsis || (autoSuggestSites && !excluded) } get verifiedPublisher () { -// @cezaraugusto: should call `verifiedP` in ledger.js and return the 2nd parameter of the callback - - let verifiedPublisher - this.props.synopsis.map(publisher => { - if (publisher.get('site') === this.publisherId && publisher.get('verified') === true) { - verifiedPublisher = !!publisher - return false - } - return true - }) - return verifiedPublisher + return this.props.locationInfo.getIn([this.locationId, 'verified']) } get visiblePublisher () { - // ledgerPaymentsShown is undefined by default until user decide to permanently hide the publisher + // ledgerPaymentsShown is undefined by default until + // user decide to permanently hide the publisher, // so for icon to be shown it can be everything but false const ledgerPaymentsShown = this.hostSettings && this.hostSettings.get('ledgerPaymentsShown') - return ledgerPaymentsShown !== false + return typeof ledgerPaymentsShown === 'boolean' + ? ledgerPaymentsShown + : true } get shouldShowAddPublisherButton () { - if ((!!this.hostSettings || !!this.validPublisherSynopsis) && this.visiblePublisher) { - // Only show publisher icon if ledger is enabled - return getSetting(settings.PAYMENTS_ENABLED) - } - return false + return getSetting(settings.PAYMENTS_ENABLED) && + isHttpOrHttps(this.props.location) && + this.visiblePublisher } get l10nString () { - if (this.verifiedPublisher && !this.authorizedPublisher) { - return 'verifiedPublisher' - } else if (this.authorizedPublisher) { - return 'enabledPublisher' + let l10nData = 'disabledPublisher' + if (this.verifiedPublisher && !this.enabledForPaymentsPublisher) { + l10nData = 'verifiedPublisher' + } else if (this.enabledForPaymentsPublisher) { + l10nData = 'enabledPublisher' } - return 'disabledPublisher' + return l10nData } onAuthorizePublisher () { - this.authorizedPublisher - ? appActions.changeSiteSetting(this.hostPattern, 'ledgerPayments', false) - : appActions.changeSiteSetting(this.hostPattern, 'ledgerPayments', true) + const hostPattern = getHostPattern(this.publisherId) + appActions.changeSiteSetting(hostPattern, 'ledgerPayments', !this.enabledForPaymentsPublisher) } render () { return this.shouldShowAddPublisherButton ?