Skip to content
This repository has been archived by the owner on Dec 11, 2019. It is now read-only.

Commit

Permalink
Merge pull request #11538 from brave/ETHWaterloo
Browse files Browse the repository at this point in the history
detecting web3 element is not provided
  • Loading branch information
bbondy authored Oct 15, 2017
2 parents 5b4382f + 37838ae commit 8a62081
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 40 deletions.
65 changes: 40 additions & 25 deletions app/browser/reducers/dappReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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)
}
}

Expand All @@ -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
6 changes: 3 additions & 3 deletions app/extensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
]
},
{
Expand Down Expand Up @@ -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: [
'<all_urls>'
Expand Down
46 changes: 36 additions & 10 deletions app/extensions/brave/content/scripts/dappListener.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
2 changes: 1 addition & 1 deletion app/extensions/brave/locales/en-US/app.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions js/state/contentSettings.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: '*'
Expand Down
16 changes: 15 additions & 1 deletion test/bravery-components/notificationBarTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
13 changes: 13 additions & 0 deletions test/fixtures/DappsNoDefine.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<html>
<head>
<meta charset="utf-8">
<title>Dapps</title>
</head>
<body>
This page uses a web3 object without defining it.
<div id="result"></div>
<script type="text/javascript">
window.web3.currentProvider
</script>
</body>
</html>

0 comments on commit 8a62081

Please sign in to comment.