2016-01-15 03:26:54 +01:00
|
|
|
const injectCss = require('inject-css')
|
2017-11-15 18:39:54 +01:00
|
|
|
const OldMetaMaskUiCss = require('../../old-ui/css')
|
|
|
|
const NewMetaMaskUiCss = require('../../ui/css')
|
2016-09-13 08:28:07 +02:00
|
|
|
const startPopup = require('./popup-core')
|
2018-08-21 00:39:03 +02:00
|
|
|
const PortStream = require('extension-port-stream')
|
2018-04-17 08:03:47 +02:00
|
|
|
const { getEnvironmentType } = require('./lib/util')
|
|
|
|
const { ENVIRONMENT_TYPE_NOTIFICATION } = require('./lib/enums')
|
2017-03-31 03:33:19 +02:00
|
|
|
const extension = require('extensionizer')
|
2017-04-01 03:04:13 +02:00
|
|
|
const ExtensionPlatform = require('./platforms/extension')
|
2017-03-31 03:33:19 +02:00
|
|
|
const NotificationManager = require('./lib/notification-manager')
|
|
|
|
const notificationManager = new NotificationManager()
|
2018-10-20 08:22:50 +02:00
|
|
|
const setupSentry = require('./lib/setupSentry')
|
2018-04-12 23:06:59 +02:00
|
|
|
const log = require('loglevel')
|
2016-01-15 03:26:54 +01:00
|
|
|
|
2018-03-15 00:31:45 +01:00
|
|
|
start().catch(log.error)
|
|
|
|
|
2018-07-03 00:49:33 +02:00
|
|
|
async function start () {
|
2018-03-15 00:31:45 +01:00
|
|
|
|
|
|
|
// create platform global
|
|
|
|
global.platform = new ExtensionPlatform()
|
|
|
|
|
|
|
|
// setup sentry error reporting
|
|
|
|
const release = global.platform.getVersion()
|
2018-10-20 09:14:59 +02:00
|
|
|
setupSentry({ release, getState })
|
|
|
|
// provide app state to append to error logs
|
|
|
|
function getState() {
|
|
|
|
// get app state
|
|
|
|
const state = window.getCleanAppState()
|
|
|
|
// remove unnecessary data
|
|
|
|
delete state.localeMessages
|
|
|
|
delete state.metamask.recentBlocks
|
|
|
|
// return state to be added to request
|
|
|
|
return state
|
|
|
|
}
|
2018-03-15 00:31:45 +01:00
|
|
|
|
|
|
|
// inject css
|
|
|
|
// const css = MetaMaskUiCss()
|
|
|
|
// injectCss(css)
|
|
|
|
|
|
|
|
// identify window type (popup, notification)
|
2018-04-17 08:03:47 +02:00
|
|
|
const windowType = getEnvironmentType(window.location.href)
|
2018-03-15 00:31:45 +01:00
|
|
|
global.METAMASK_UI_TYPE = windowType
|
|
|
|
closePopupIfOpen(windowType)
|
|
|
|
|
|
|
|
// setup stream to background
|
|
|
|
const extensionPort = extension.runtime.connect({ name: windowType })
|
|
|
|
const connectionStream = new PortStream(extensionPort)
|
|
|
|
|
|
|
|
// start ui
|
|
|
|
const container = document.getElementById('app-content')
|
2018-03-19 17:06:16 +01:00
|
|
|
startPopup({ container, connectionStream }, (err, store) => {
|
2018-03-15 00:31:45 +01:00
|
|
|
if (err) return displayCriticalError(err)
|
|
|
|
|
|
|
|
// Code commented out until we begin auto adding users to NewUI
|
|
|
|
// const { isMascara, identities = {}, featureFlags = {} } = store.getState().metamask
|
|
|
|
// const firstTime = Object.keys(identities).length === 0
|
|
|
|
const { isMascara, featureFlags = {} } = store.getState().metamask
|
|
|
|
let betaUIState = featureFlags.betaUI
|
|
|
|
|
|
|
|
// Code commented out until we begin auto adding users to NewUI
|
|
|
|
// const useBetaCss = isMascara || firstTime || betaUIState
|
|
|
|
const useBetaCss = isMascara || betaUIState
|
|
|
|
|
|
|
|
let css = useBetaCss ? NewMetaMaskUiCss() : OldMetaMaskUiCss()
|
|
|
|
let deleteInjectedCss = injectCss(css)
|
|
|
|
let newBetaUIState
|
|
|
|
|
|
|
|
store.subscribe(() => {
|
|
|
|
const state = store.getState()
|
|
|
|
newBetaUIState = state.metamask.featureFlags.betaUI
|
|
|
|
if (newBetaUIState !== betaUIState) {
|
|
|
|
deleteInjectedCss()
|
|
|
|
betaUIState = newBetaUIState
|
|
|
|
css = betaUIState ? NewMetaMaskUiCss() : OldMetaMaskUiCss()
|
|
|
|
deleteInjectedCss = injectCss(css)
|
|
|
|
}
|
|
|
|
})
|
2017-03-31 22:32:47 +02:00
|
|
|
})
|
2016-01-15 11:03:42 +01:00
|
|
|
|
2016-08-17 01:46:44 +02:00
|
|
|
|
2018-03-15 00:31:45 +01:00
|
|
|
function closePopupIfOpen (windowType) {
|
2018-07-24 16:15:36 +02:00
|
|
|
if (windowType !== ENVIRONMENT_TYPE_NOTIFICATION) {
|
2018-03-22 01:58:58 +01:00
|
|
|
// should close only chrome popup
|
2018-07-24 16:15:36 +02:00
|
|
|
notificationManager.closePopup()
|
|
|
|
}
|
2018-03-15 00:31:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2016-08-24 00:44:50 +02:00
|
|
|
}
|
2017-03-31 22:20:16 +02:00
|
|
|
|
|
|
|
}
|