diff --git a/app/browser/reducers/dappReducer.js b/app/browser/reducers/dappReducer.js index daf88f1d937..ec381715036 100644 --- a/app/browser/reducers/dappReducer.js +++ b/app/browser/reducers/dappReducer.js @@ -15,13 +15,16 @@ const getSetting = require('../../../js/settings').getSetting const tabActions = require('../../common/actions/tabActions') const tabState = require('../../common/state/tabState') const config = require('../../../js/constants/config') +const {getOrigin} = require('../../../js/state/siteUtil') + +let notificationCallbacks = [] const dappReducer = (state, action, immutableAction) => { action = immutableAction || makeImmutable(action) switch (action.get('actionType')) { case appConstants.APP_DAPP_AVAILABLE: - if (!getSetting(settings.METAMASK_PROMPT_DISMISSED) && !getSetting(settings.METAMASK_ENABLED)) { - showDappNotification() + if (!getSetting(settings.METAMASK_ENABLED)) { + showDappNotification(action.get('location')) } break } @@ -31,20 +34,8 @@ const dappReducer = (state, action, immutableAction) => { const notifications = { text: { greeting: locale.translation('updateHello'), - message: locale.translation('dappDetected'), enable: locale.translation('dappEnableExtension'), dismiss: locale.translation('dappDismiss') - }, - onResponse: (message, buttonIndex) => { - // Index 0 is for dismiss - if (buttonIndex === 0) { - appActions.changeSetting(settings.METAMASK_PROMPT_DISMISSED, true) - } - // Index 1 is for enable - if (buttonIndex === 1) { - appActions.changeSetting(settings.METAMASK_ENABLED, true) - } - appActions.hideNotification(message) } } @@ -54,28 +45,52 @@ process.on('extension-ready', (installInfo) => { } }) -const showDappNotification = () => { +const showDappNotification = (location) => { + const origin = getOrigin(location) + if (!origin || getSetting(settings.METAMASK_PROMPT_DISMISSED)) { + return + } + + const message = locale.translation('dappDetected').replace(/{{\s*origin\s*}}/, origin) + appActions.showNotification({ greeting: notifications.text.greeting, - message: notifications.text.message, + message: message, buttons: [ {text: notifications.text.dismiss}, {text: notifications.text.enable} ], + frameOrigin: origin, options: { - persist: false + persist: true } }) -} -if (ipcMain) { - ipcMain.on(messages.NOTIFICATION_RESPONSE, (e, message, buttonIndex) => { - switch (message) { - case notifications.text.message: - notifications.onResponse(message, buttonIndex) - break + if (!notificationCallbacks[message]) { + notificationCallbacks[message] = (e, msg, buttonIndex, persist) => { + if (msg === message) { + appActions.hideNotification(message) + // Index 0 is for dismiss + if (buttonIndex === 0) { + if (persist) { + appActions.changeSetting(settings.METAMASK_PROMPT_DISMISSED, true) + } + } + + // Index 1 is for enable + if (buttonIndex === 1) { + appActions.changeSetting(settings.METAMASK_ENABLED, true) + } + + if (notificationCallbacks[message]) { + ipcMain.removeListener(messages.NOTIFICATION_RESPONSE, notificationCallbacks[message]) + delete notificationCallbacks[message] + } + } } - }) + } + + ipcMain.on(messages.NOTIFICATION_RESPONSE, notificationCallbacks[message]) } module.exports = dappReducer diff --git a/app/extensions.js b/app/extensions.js index 01423e42046..7bf9aafd83c 100644 --- a/app/extensions.js +++ b/app/extensions.js @@ -95,8 +95,7 @@ let generateBraveManifest = () => { js: [ 'content/scripts/adInsertion.js', 'content/scripts/pageInformation.js', - 'content/scripts/flashListener.js', - 'content/scripts/dappListener.js' + 'content/scripts/flashListener.js' ] }, { @@ -140,7 +139,8 @@ let generateBraveManifest = () => { run_at: 'document_start', all_frames: true, js: [ - 'content/scripts/dndHandler.js' + 'content/scripts/dndHandler.js', + 'content/scripts/dappListener.js' ], matches: [ '' diff --git a/app/extensions/brave/content/scripts/dappListener.js b/app/extensions/brave/content/scripts/dappListener.js index dc394d2c382..afe96a96939 100644 --- a/app/extensions/brave/content/scripts/dappListener.js +++ b/app/extensions/brave/content/scripts/dappListener.js @@ -3,20 +3,46 @@ * You can obtain one at http://mozilla.org/MPL/2.0/. */ const script = - `if (window.web3) { - if (!window.web3.currentProvider || !window.web3.currentProvider.isMetaMask) { + ` + function __insertWeb3Installed() { + if (!window.alreadyInserted) { const meta = document.createElement('meta') meta.name = 'web3-installed' document.head.appendChild(meta) + window.alreadyInserted = true + } + } + if (window.web3) { + if (!window.web3.currentProvider || !window.web3.currentProvider.isMetaMask) { + __insertWeb3Installed() } + } else { + var oldWeb3 = window.web3 + Object.defineProperty(window, 'web3', { + configurable: true, + set: function (val) { + __insertWeb3Installed() + oldWeb3 = val + }, + get: function () { + __insertWeb3Installed() + return oldWeb3 + } + }) }` -executeScript(script) -const isDapp = document.querySelector('meta[name="web3-installed"]') - -if (isDapp) { - chrome.ipcRenderer.send('dispatch-action', JSON.stringify([{ - actionType: 'app-dapp-available', - location: window.location.href - }])) +if (chrome.contentSettings.dappDetection == 'allow') { + executeScript(script) } + +setTimeout(function () { + console.log('checking now for toolbar') + const isDapp = document.querySelector('meta[name="web3-installed"]') + + if (isDapp) { + chrome.ipcRenderer.send('dispatch-action', JSON.stringify([{ + actionType: 'app-dapp-available', + location: window.location.href + }])) + } +}, 3000) diff --git a/app/extensions/brave/locales/en-US/app.properties b/app/extensions/brave/locales/en-US/app.properties index 9699bdc39dd..3c784d9f3dc 100644 --- a/app/extensions/brave/locales/en-US/app.properties +++ b/app/extensions/brave/locales/en-US/app.properties @@ -239,7 +239,7 @@ walletConvertedBackup=Back up your new wallet walletConvertedDismiss=Later walletConvertedLearnMore=Learn More walletConvertedToBat=Your BTC will be converted to BAT and will appear in your Brave wallet within approximately 30 minutes. -dappDetected=This page contains a Dapp, would you like to enable the MetaMask extension? +dappDetected={{origin}} contains a Dapp, would you like to enable the MetaMask extension? dappDismiss=No thanks dappEnableExtension=Install MetaMask windowCaptionButtonMinimize=Minimize diff --git a/js/state/contentSettings.js b/js/state/contentSettings.js index 27e67f7c06e..56018d62124 100644 --- a/js/state/contentSettings.js +++ b/js/state/contentSettings.js @@ -114,6 +114,10 @@ const getDefaultUserPrefContentSettings = (braveryDefaults, appSettings, appConf setting: 'block', primaryPattern: '*' }], + dappDetection: [{ + setting: getSetting(settings.METAMASK_PROMPT_DISMISSED) ? 'block' : 'allow', + primaryPattern: '*' + }], popups: [{ setting: 'block', primaryPattern: '*' diff --git a/test/bravery-components/notificationBarTest.js b/test/bravery-components/notificationBarTest.js index 1b11796e8b4..bec2f63cd20 100644 --- a/test/bravery-components/notificationBarTest.js +++ b/test/bravery-components/notificationBarTest.js @@ -38,7 +38,7 @@ describe('notificationBar permissions', function () { }) describe('Dapps', function () { - it('shows notification bar for Dapps', function * () { + it('shows notification bar for Dapps that define web3', function * () { let notificationUrl = Brave.server.url('Dapps.html') yield this.app.client .tabByIndex(0) @@ -52,6 +52,20 @@ describe('notificationBar permissions', function () { }).click('button=No thanks') }) + it('shows notification bar for Dapps that use web3 without defining', function * () { + let notificationUrl = Brave.server.url('DappsNoDefine.html') + yield this.app.client + .tabByIndex(0) + .loadUrl(notificationUrl) + .windowByUrl(Brave.browserWindowUrl) + .waitForExist(notificationBar) + .waitUntil(function () { + return this.getText(notificationBar).then((val) => { + return val.includes('Dapp') + }) + }).click('button=No thanks') + }) + it('does not show when prompt is dismissed', function * () { let notificationUrl = Brave.server.url('Dapps.html') yield this.app.client diff --git a/test/fixtures/DappsNoDefine.html b/test/fixtures/DappsNoDefine.html new file mode 100644 index 00000000000..b02424607ae --- /dev/null +++ b/test/fixtures/DappsNoDefine.html @@ -0,0 +1,13 @@ + + + + Dapps + + + This page uses a web3 object without defining it. +
+ + + \ No newline at end of file