diff options
author | frankiebee <frankie.diamond@gmail.com> | 2017-04-03 14:04:55 +0800 |
---|---|---|
committer | frankiebee <frankie.diamond@gmail.com> | 2017-04-03 14:04:55 +0800 |
commit | f81fdbc34ff037ab64a9f4c98100304e7b1132e4 (patch) | |
tree | 20f2a75068af1b4b13ecb9a9b6276e07af94de93 /app/scripts | |
parent | 89c924101e3ff52849c5a82ced7e058e1fa39dfd (diff) | |
parent | 235a70b9392d8b87eae4fc141e7f0be1d7788ee2 (diff) | |
download | tangerine-wallet-browser-f81fdbc34ff037ab64a9f4c98100304e7b1132e4.tar tangerine-wallet-browser-f81fdbc34ff037ab64a9f4c98100304e7b1132e4.tar.gz tangerine-wallet-browser-f81fdbc34ff037ab64a9f4c98100304e7b1132e4.tar.bz2 tangerine-wallet-browser-f81fdbc34ff037ab64a9f4c98100304e7b1132e4.tar.lz tangerine-wallet-browser-f81fdbc34ff037ab64a9f4c98100304e7b1132e4.tar.xz tangerine-wallet-browser-f81fdbc34ff037ab64a9f4c98100304e7b1132e4.tar.zst tangerine-wallet-browser-f81fdbc34ff037ab64a9f4c98100304e7b1132e4.zip |
Merge branch 'master' into mascara
Diffstat (limited to 'app/scripts')
-rw-r--r-- | app/scripts/background.js | 13 | ||||
-rw-r--r-- | app/scripts/contentscript.js | 2 | ||||
-rw-r--r-- | app/scripts/lib/extension-instance.js | 68 | ||||
-rw-r--r-- | app/scripts/lib/extension.js | 17 | ||||
-rw-r--r-- | app/scripts/lib/notification-manager.js | 74 | ||||
-rw-r--r-- | app/scripts/lib/notifications.js | 67 | ||||
-rw-r--r-- | app/scripts/metamask-controller.js | 14 | ||||
-rw-r--r-- | app/scripts/platforms/extension.js | 23 | ||||
-rw-r--r-- | app/scripts/popup-core.js | 27 | ||||
-rw-r--r-- | app/scripts/popup.js | 48 |
10 files changed, 160 insertions, 193 deletions
diff --git a/app/scripts/background.js b/app/scripts/background.js index 254737dec..7211f1e0c 100644 --- a/app/scripts/background.js +++ b/app/scripts/background.js @@ -4,12 +4,13 @@ const asyncQ = require('async-q') const pipe = require('pump') const LocalStorageStore = require('obs-store/lib/localStorage') const storeTransform = require('obs-store/lib/transform') +const ExtensionPlatform = require('./platforms/extension') const Migrator = require('./lib/migrator/') const migrations = require('./migrations/') 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 extension = require('./lib/extension') +const extension = require('extensionizer') const firstTimeState = require('./first-time-state') const STORAGE_KEY = 'metamask-config' @@ -19,6 +20,10 @@ const log = require('loglevel') window.log = log log.setDefaultLevel(METAMASK_DEBUG ? 'debug' : 'warn') +const platform = new ExtensionPlatform() +const notificationManager = new NotificationManager() +global.METAMASK_NOTIFIER = notificationManager + let popupIsOpen = false // state persistence @@ -68,6 +73,8 @@ function setupController (initState) { showUnapprovedTx: triggerUi, // initial state initState, + // platform specific api + platform, }) global.metamaskController = controller @@ -140,7 +147,7 @@ function setupController (initState) { // popup trigger function triggerUi () { - if (!popupIsOpen) notification.show() + if (!popupIsOpen) notificationManager.showPopup() } // On first install, open a window to MetaMask website to how-it-works. diff --git a/app/scripts/contentscript.js b/app/scripts/contentscript.js index 9a390e580..4d7e682d3 100644 --- a/app/scripts/contentscript.js +++ b/app/scripts/contentscript.js @@ -2,7 +2,7 @@ const LocalMessageDuplexStream = require('post-message-stream') const PongStream = require('ping-pong-stream/pong') const PortStream = require('./lib/port-stream.js') const ObjectMultiplex = require('./lib/obj-multiplex') -const extension = require('./lib/extension') +const extension = require('extensionizer') const fs = require('fs') const path = require('path') diff --git a/app/scripts/lib/extension-instance.js b/app/scripts/lib/extension-instance.js deleted file mode 100644 index 628b62e3f..000000000 --- a/app/scripts/lib/extension-instance.js +++ /dev/null @@ -1,68 +0,0 @@ -const apis = [ - 'alarms', - 'bookmarks', - 'browserAction', - 'commands', - 'contextMenus', - 'cookies', - 'downloads', - 'events', - 'extension', - 'extensionTypes', - 'history', - 'i18n', - 'idle', - 'notifications', - 'pageAction', - 'runtime', - 'storage', - 'tabs', - 'webNavigation', - 'webRequest', - 'windows', -] - -function Extension () { - const _this = this - - apis.forEach(function (api) { - - _this[api] = null - - try { - if (chrome[api]) { - _this[api] = chrome[api] - } - } catch (e) {} - - try { - if (window[api]) { - _this[api] = window[api] - } - } catch (e) {} - - try { - if (browser[api]) { - _this[api] = browser[api] - } - } catch (e) {} - try { - _this.api = browser.extension[api] - } catch (e) {} - }) - - try { - if (browser && browser.runtime) { - this.runtime = browser.runtime - } - } catch (e) {} - - try { - if (browser && browser.browserAction) { - this.browserAction = browser.browserAction - } - } catch (e) {} - -} - -module.exports = Extension diff --git a/app/scripts/lib/extension.js b/app/scripts/lib/extension.js deleted file mode 100644 index 6f8b5d800..000000000 --- a/app/scripts/lib/extension.js +++ /dev/null @@ -1,17 +0,0 @@ -/* Extension.js - * - * A module for unifying browser differences in the WebExtension API. - * - * Initially implemented because Chrome hides all of their WebExtension API - * behind a global `chrome` variable, but we'd like to start grooming - * the code-base for cross-browser extension support. - * - * You can read more about the WebExtension API here: - * https://developer.mozilla.org/en-US/Add-ons/WebExtensions - */ - -const Extension = require('./extension-instance') -const instance = new Extension() -window.METAMASK_EXTENSION = instance -module.exports = instance - diff --git a/app/scripts/lib/notification-manager.js b/app/scripts/lib/notification-manager.js new file mode 100644 index 000000000..55e5b8dd2 --- /dev/null +++ b/app/scripts/lib/notification-manager.js @@ -0,0 +1,74 @@ +const extension = require('extensionizer') +const height = 520 +const width = 360 + + +class NotificationManager { + + // + // Public + // + + showPopup () { + 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) + }) + } + }) + } + + closePopup () { + this._getPopup((err, popup) => { + if (err) throw err + if (!popup) return + extension.windows.remove(popup.id, console.error) + }) + } + + // + // Private + // + + _getPopup (cb) { + this._getWindows((err, windows) => { + if (err) throw err + cb(null, this._getPopupIn(windows)) + }) + } + + _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
\ No newline at end of file diff --git a/app/scripts/lib/notifications.js b/app/scripts/lib/notifications.js deleted file mode 100644 index 0ec01f3a7..000000000 --- a/app/scripts/lib/notifications.js +++ /dev/null @@ -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) - }) -} diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index db80d5f1b..d360e7d95 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -20,7 +20,6 @@ const MessageManager = require('./lib/message-manager') const PersonalMessageManager = require('./lib/personal-message-manager') const TxManager = require('./transaction-manager') const ConfigManager = require('./lib/config-manager') -const extension = require('./lib/extension') const autoFaucet = require('./lib/auto-faucet') const nodeify = require('./lib/nodeify') const accountImporter = require('./account-import-strategies') @@ -34,6 +33,9 @@ module.exports = class MetamaskController extends EventEmitter { this.opts = opts let initState = opts.initState || {} + // platform-specific api + this.platform = opts.platform + // observable state store this.store = new ObservableStore(initState) @@ -629,7 +631,7 @@ module.exports = class MetamaskController extends EventEmitter { break } - if (url) extension.tabs.create({ url }) + if (url) this.platform.openWindow({ url }) } createShapeShiftTx (depositAddress, depositType) { @@ -647,7 +649,7 @@ module.exports = class MetamaskController extends EventEmitter { setDefaultRpc () { this.configManager.setRpcTarget('http://localhost:8545') - extension.runtime.reload() + this.platform.reload() this.lookupNetwork() return Promise.resolve('http://localhost:8545') } @@ -656,7 +658,7 @@ module.exports = class MetamaskController extends EventEmitter { this.configManager.setRpcTarget(rpcTarget) return this.preferencesController.updateFrequentRpcList(rpcTarget) .then(() => { - extension.runtime.reload() + this.platform.reload() this.lookupNetwork() return Promise.resolve(rpcTarget) }) @@ -664,13 +666,13 @@ module.exports = class MetamaskController extends EventEmitter { setProviderType (type) { this.configManager.setProviderType(type) - extension.runtime.reload() + this.platform.reload() this.lookupNetwork() } useEtherscanProvider () { this.configManager.useEtherscanProvider() - extension.runtime.reload() + this.platform.reload() } getNetworkState () { diff --git a/app/scripts/platforms/extension.js b/app/scripts/platforms/extension.js new file mode 100644 index 000000000..00c2aa275 --- /dev/null +++ b/app/scripts/platforms/extension.js @@ -0,0 +1,23 @@ +const extension = require('extensionizer') + +class ExtensionPlatform { + + // + // Public + // + + reload () { + extension.runtime.reload() + } + + openWindow ({ url }) { + extension.tabs.create({ url }) + } + + getVersion () { + return extension.runtime.getManifest().version + } + +} + +module.exports = ExtensionPlatform diff --git a/app/scripts/popup-core.js b/app/scripts/popup-core.js index b1e521a7a..1e5d70e8b 100644 --- a/app/scripts/popup-core.js +++ b/app/scripts/popup-core.js @@ -1,7 +1,8 @@ const EventEmitter = require('events').EventEmitter +const async = require('async') const Dnode = require('dnode') const Web3 = require('web3') -const MetaMaskUi = require('../../ui') +const launchMetamaskUi = require('../../ui') const StreamProvider = require('web3-stream-provider') const setupMultiplex = require('./lib/stream-utils.js').setupMultiplex @@ -9,9 +10,13 @@ const setupMultiplex = require('./lib/stream-utils.js').setupMultiplex module.exports = initializePopup -function initializePopup (connectionStream) { +function initializePopup ({ container, connectionStream }, cb) { // setup app - connectToAccountManager(connectionStream, setupApp) + async.waterfall([ + (cb) => connectToAccountManager(connectionStream, cb), + (accountManager, cb) => launchMetamaskUi({ container, accountManager }, cb), + ], cb) + } function connectToAccountManager (connectionStream, cb) { @@ -47,19 +52,3 @@ function setupControllerConnection (connectionStream, cb) { cb(null, accountManager) }) } - -function setupApp (err, accountManager) { - var container = document.getElementById('app-content') - if (err) { - container.innerHTML = '<div class="critical-error">The MetaMask app failed to load: please open and close MetaMask again to restart.</div>' - container.style.height = '80px' - log.error(err.stack) - throw err - } - - - MetaMaskUi({ - container: container, - accountManager: accountManager, - }) -} diff --git a/app/scripts/popup.js b/app/scripts/popup.js index 62db68c10..0fbde54b3 100644 --- a/app/scripts/popup.js +++ b/app/scripts/popup.js @@ -3,23 +3,47 @@ const MetaMaskUiCss = require('../../ui/css') const startPopup = require('./popup-core') const PortStream = require('./lib/port-stream.js') const isPopupOrNotification = require('./lib/is-popup-or-notification') -const extension = require('./lib/extension') -const notification = require('./lib/notifications') +const extension = require('extensionizer') +const ExtensionPlatform = require('./platforms/extension') +const NotificationManager = require('./lib/notification-manager') +const notificationManager = new NotificationManager() -var css = MetaMaskUiCss() +// create platform global +global.platform = new ExtensionPlatform() + +// inject css +const css = MetaMaskUiCss() injectCss(css) -var name = isPopupOrNotification() -closePopupIfOpen(name) -window.METAMASK_UI_TYPE = name +// identify window type (popup, notification) +const windowType = isPopupOrNotification() +global.METAMASK_UI_TYPE = windowType +closePopupIfOpen(windowType) + +// setup stream to background +const extensionPort = extension.runtime.connect({ name: windowType }) +const connectionStream = new PortStream(extensionPort) -var pluginPort = extension.runtime.connect({ name }) -var portStream = new PortStream(pluginPort) +// start ui +const container = document.getElementById('app-content') +startPopup({ container, connectionStream }, (err, store) => { + if (err) return displayCriticalError(err) + store.subscribe(() => { + const state = store.getState() + if (state.appState.shouldClose) notificationManager.closePopup() + }) +}) -startPopup(portStream) -function closePopupIfOpen (name) { - if (name !== 'notification') { - notification.closePopup() +function closePopupIfOpen (windowType) { + if (windowType !== 'notification') { + notificationManager.closePopup() } } + +function displayCriticalError(err) { + container.innerHTML = '<div class="critical-error">The MetaMask app failed to load: please open and close MetaMask again to restart.</div>' + container.style.height = '80px' + log.error(err.stack) + throw err +} |