mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-12-22 09:23:21 +01:00
introduce platform api and rename notifications to notification-manager
This commit is contained in:
parent
be88c87b25
commit
5036263f88
@ -4,12 +4,13 @@ const asyncQ = require('async-q')
|
|||||||
const pipe = require('pump')
|
const pipe = require('pump')
|
||||||
const LocalStorageStore = require('obs-store/lib/localStorage')
|
const LocalStorageStore = require('obs-store/lib/localStorage')
|
||||||
const storeTransform = require('obs-store/lib/transform')
|
const storeTransform = require('obs-store/lib/transform')
|
||||||
|
const ExtensionPlatform = require('./platforms/extension')
|
||||||
const Migrator = require('./lib/migrator/')
|
const Migrator = require('./lib/migrator/')
|
||||||
const migrations = require('./migrations/')
|
const migrations = require('./migrations/')
|
||||||
const PortStream = require('./lib/port-stream.js')
|
const PortStream = require('./lib/port-stream.js')
|
||||||
const notification = require('./lib/notifications.js')
|
const NotificationManager = require('./lib/notification-manager.js')
|
||||||
const MetamaskController = require('./metamask-controller')
|
const MetamaskController = require('./metamask-controller')
|
||||||
const extension = require('./lib/extension')
|
const extension = require('extensionizer')
|
||||||
const firstTimeState = require('./first-time-state')
|
const firstTimeState = require('./first-time-state')
|
||||||
|
|
||||||
const STORAGE_KEY = 'metamask-config'
|
const STORAGE_KEY = 'metamask-config'
|
||||||
@ -19,6 +20,10 @@ const log = require('loglevel')
|
|||||||
window.log = log
|
window.log = log
|
||||||
log.setDefaultLevel(METAMASK_DEBUG ? 'debug' : 'warn')
|
log.setDefaultLevel(METAMASK_DEBUG ? 'debug' : 'warn')
|
||||||
|
|
||||||
|
const platform = new ExtensionPlatform()
|
||||||
|
const notificationManager = new NotificationManager()
|
||||||
|
global.METAMASK_NOTIFIER = notificationManager
|
||||||
|
|
||||||
let popupIsOpen = false
|
let popupIsOpen = false
|
||||||
|
|
||||||
// state persistence
|
// state persistence
|
||||||
@ -68,6 +73,8 @@ function setupController (initState) {
|
|||||||
showUnapprovedTx: triggerUi,
|
showUnapprovedTx: triggerUi,
|
||||||
// initial state
|
// initial state
|
||||||
initState,
|
initState,
|
||||||
|
// platform specific api
|
||||||
|
platform,
|
||||||
})
|
})
|
||||||
global.metamaskController = controller
|
global.metamaskController = controller
|
||||||
|
|
||||||
@ -140,7 +147,7 @@ function setupController (initState) {
|
|||||||
|
|
||||||
// popup trigger
|
// popup trigger
|
||||||
function triggerUi () {
|
function triggerUi () {
|
||||||
if (!popupIsOpen) notification.show()
|
if (!popupIsOpen) notificationManager.show()
|
||||||
}
|
}
|
||||||
|
|
||||||
// On first install, open a window to MetaMask website to how-it-works.
|
// On first install, open a window to MetaMask website to how-it-works.
|
||||||
|
74
app/scripts/lib/notification-manager.js
Normal file
74
app/scripts/lib/notification-manager.js
Normal file
@ -0,0 +1,74 @@
|
|||||||
|
const extension = require('extensionizer')
|
||||||
|
const height = 520
|
||||||
|
const width = 360
|
||||||
|
|
||||||
|
|
||||||
|
class NotificationManager {
|
||||||
|
|
||||||
|
//
|
||||||
|
// Public
|
||||||
|
//
|
||||||
|
|
||||||
|
show () {
|
||||||
|
this.getPopup((err, popup) => {
|
||||||
|
if (err) throw err
|
||||||
|
|
||||||
|
if (popup) {
|
||||||
|
// bring focus to existing popup
|
||||||
|
extension.windows.update(popup.id, { focused: true })
|
||||||
|
} else {
|
||||||
|
// create new popup
|
||||||
|
extension.windows.create({
|
||||||
|
url: 'notification.html',
|
||||||
|
type: 'popup',
|
||||||
|
width,
|
||||||
|
height,
|
||||||
|
})
|
||||||
|
.catch((reason) => {
|
||||||
|
log.error('failed to create poupup', reason)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
getPopup (cb) {
|
||||||
|
this._getWindows((err, windows) => {
|
||||||
|
if (err) throw err
|
||||||
|
cb(null, this._getPopupIn(windows))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
closePopup () {
|
||||||
|
this.getPopup((err, popup) => {
|
||||||
|
if (err) throw err
|
||||||
|
if (!popup) return
|
||||||
|
extension.windows.remove(popup.id, console.error)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Private
|
||||||
|
//
|
||||||
|
|
||||||
|
_getWindows (cb) {
|
||||||
|
// Ignore in test environment
|
||||||
|
if (!extension.windows) {
|
||||||
|
return cb()
|
||||||
|
}
|
||||||
|
|
||||||
|
extension.windows.getAll({}, (windows) => {
|
||||||
|
cb(null, windows)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
_getPopupIn (windows) {
|
||||||
|
return windows ? windows.find((win) => {
|
||||||
|
return (win && win.type === 'popup' &&
|
||||||
|
win.height === height &&
|
||||||
|
win.width === width)
|
||||||
|
}) : null
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = NotificationManager
|
@ -1,67 +0,0 @@
|
|||||||
const extension = require('./extension')
|
|
||||||
const height = 520
|
|
||||||
const width = 360
|
|
||||||
|
|
||||||
const notifications = {
|
|
||||||
show,
|
|
||||||
getPopup,
|
|
||||||
closePopup,
|
|
||||||
}
|
|
||||||
module.exports = notifications
|
|
||||||
window.METAMASK_NOTIFIER = notifications
|
|
||||||
|
|
||||||
function show () {
|
|
||||||
getPopup((err, popup) => {
|
|
||||||
if (err) throw err
|
|
||||||
|
|
||||||
if (popup) {
|
|
||||||
// bring focus to existing popup
|
|
||||||
extension.windows.update(popup.id, { focused: true })
|
|
||||||
} else {
|
|
||||||
// create new popup
|
|
||||||
extension.windows.create({
|
|
||||||
url: 'notification.html',
|
|
||||||
type: 'popup',
|
|
||||||
width,
|
|
||||||
height,
|
|
||||||
})
|
|
||||||
.catch((reason) => {
|
|
||||||
log.error('failed to create poupup', reason)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
function getWindows (cb) {
|
|
||||||
// Ignore in test environment
|
|
||||||
if (!extension.windows) {
|
|
||||||
return cb()
|
|
||||||
}
|
|
||||||
|
|
||||||
extension.windows.getAll({}, (windows) => {
|
|
||||||
cb(null, windows)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
function getPopup (cb) {
|
|
||||||
getWindows((err, windows) => {
|
|
||||||
if (err) throw err
|
|
||||||
cb(null, getPopupIn(windows))
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
function getPopupIn (windows) {
|
|
||||||
return windows ? windows.find((win) => {
|
|
||||||
return (win && win.type === 'popup' &&
|
|
||||||
win.height === height &&
|
|
||||||
win.width === width)
|
|
||||||
}) : null
|
|
||||||
}
|
|
||||||
|
|
||||||
function closePopup () {
|
|
||||||
getPopup((err, popup) => {
|
|
||||||
if (err) throw err
|
|
||||||
if (!popup) return
|
|
||||||
extension.windows.remove(popup.id, console.error)
|
|
||||||
})
|
|
||||||
}
|
|
@ -20,7 +20,6 @@ const MessageManager = require('./lib/message-manager')
|
|||||||
const PersonalMessageManager = require('./lib/personal-message-manager')
|
const PersonalMessageManager = require('./lib/personal-message-manager')
|
||||||
const TxManager = require('./transaction-manager')
|
const TxManager = require('./transaction-manager')
|
||||||
const ConfigManager = require('./lib/config-manager')
|
const ConfigManager = require('./lib/config-manager')
|
||||||
const extension = require('./lib/extension')
|
|
||||||
const autoFaucet = require('./lib/auto-faucet')
|
const autoFaucet = require('./lib/auto-faucet')
|
||||||
const nodeify = require('./lib/nodeify')
|
const nodeify = require('./lib/nodeify')
|
||||||
const accountImporter = require('./account-import-strategies')
|
const accountImporter = require('./account-import-strategies')
|
||||||
@ -34,6 +33,9 @@ module.exports = class MetamaskController extends EventEmitter {
|
|||||||
this.opts = opts
|
this.opts = opts
|
||||||
let initState = opts.initState || {}
|
let initState = opts.initState || {}
|
||||||
|
|
||||||
|
// platform-specific api
|
||||||
|
this.platform = opts.platform
|
||||||
|
|
||||||
// observable state store
|
// observable state store
|
||||||
this.store = new ObservableStore(initState)
|
this.store = new ObservableStore(initState)
|
||||||
|
|
||||||
@ -629,7 +631,7 @@ module.exports = class MetamaskController extends EventEmitter {
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
if (url) extension.tabs.create({ url })
|
if (url) this.platform.openWindow({ url })
|
||||||
}
|
}
|
||||||
|
|
||||||
createShapeShiftTx (depositAddress, depositType) {
|
createShapeShiftTx (depositAddress, depositType) {
|
||||||
@ -647,7 +649,7 @@ module.exports = class MetamaskController extends EventEmitter {
|
|||||||
|
|
||||||
setDefaultRpc () {
|
setDefaultRpc () {
|
||||||
this.configManager.setRpcTarget('http://localhost:8545')
|
this.configManager.setRpcTarget('http://localhost:8545')
|
||||||
extension.runtime.reload()
|
this.platform.reload()
|
||||||
this.lookupNetwork()
|
this.lookupNetwork()
|
||||||
return Promise.resolve('http://localhost:8545')
|
return Promise.resolve('http://localhost:8545')
|
||||||
}
|
}
|
||||||
@ -656,7 +658,7 @@ module.exports = class MetamaskController extends EventEmitter {
|
|||||||
this.configManager.setRpcTarget(rpcTarget)
|
this.configManager.setRpcTarget(rpcTarget)
|
||||||
return this.preferencesController.updateFrequentRpcList(rpcTarget)
|
return this.preferencesController.updateFrequentRpcList(rpcTarget)
|
||||||
.then(() => {
|
.then(() => {
|
||||||
extension.runtime.reload()
|
this.platform.reload()
|
||||||
this.lookupNetwork()
|
this.lookupNetwork()
|
||||||
return Promise.resolve(rpcTarget)
|
return Promise.resolve(rpcTarget)
|
||||||
})
|
})
|
||||||
@ -664,13 +666,13 @@ module.exports = class MetamaskController extends EventEmitter {
|
|||||||
|
|
||||||
setProviderType (type) {
|
setProviderType (type) {
|
||||||
this.configManager.setProviderType(type)
|
this.configManager.setProviderType(type)
|
||||||
extension.runtime.reload()
|
this.platform.reload()
|
||||||
this.lookupNetwork()
|
this.lookupNetwork()
|
||||||
}
|
}
|
||||||
|
|
||||||
useEtherscanProvider () {
|
useEtherscanProvider () {
|
||||||
this.configManager.useEtherscanProvider()
|
this.configManager.useEtherscanProvider()
|
||||||
extension.runtime.reload()
|
this.platform.reload()
|
||||||
}
|
}
|
||||||
|
|
||||||
getNetworkState () {
|
getNetworkState () {
|
||||||
|
19
app/scripts/platforms/extension.js
Normal file
19
app/scripts/platforms/extension.js
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
const extension = require('extensionizer')
|
||||||
|
|
||||||
|
class ExtensionPlatform {
|
||||||
|
|
||||||
|
//
|
||||||
|
// Public
|
||||||
|
//
|
||||||
|
|
||||||
|
reload () {
|
||||||
|
extension.runtime.reload()
|
||||||
|
}
|
||||||
|
|
||||||
|
openWindow ({ url }) {
|
||||||
|
extension.tabs.create({ url })
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = ExtensionPlatform
|
@ -3,8 +3,10 @@ const MetaMaskUiCss = require('../../ui/css')
|
|||||||
const startPopup = require('./popup-core')
|
const startPopup = require('./popup-core')
|
||||||
const PortStream = require('./lib/port-stream.js')
|
const PortStream = require('./lib/port-stream.js')
|
||||||
const isPopupOrNotification = require('./lib/is-popup-or-notification')
|
const isPopupOrNotification = require('./lib/is-popup-or-notification')
|
||||||
const extension = require('./lib/extension')
|
const extension = require('extensionizer')
|
||||||
const notification = require('./lib/notifications')
|
const NotificationManager = require('./lib/notification-manager')
|
||||||
|
|
||||||
|
const notificationManager = new NotificationManager()
|
||||||
|
|
||||||
var css = MetaMaskUiCss()
|
var css = MetaMaskUiCss()
|
||||||
injectCss(css)
|
injectCss(css)
|
||||||
@ -20,6 +22,6 @@ startPopup(portStream)
|
|||||||
|
|
||||||
function closePopupIfOpen (name) {
|
function closePopupIfOpen (name) {
|
||||||
if (name !== 'notification') {
|
if (name !== 'notification') {
|
||||||
notification.closePopup()
|
notificationManager.closePopup()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
const extend = require('xtend')
|
const extend = require('xtend')
|
||||||
const actions = require('../actions')
|
const actions = require('../actions')
|
||||||
const txHelper = require('../../lib/tx-helper')
|
const txHelper = require('../../lib/tx-helper')
|
||||||
const notification = require('../../../app/scripts/lib/notifications')
|
const notification = require('../../../app/scripts/lib/notification-manager')
|
||||||
|
|
||||||
module.exports = reduceApp
|
module.exports = reduceApp
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user