|
|
@ -21,6 +21,11 @@ const setupMetamaskMeshMetrics = require('./lib/setupMetamaskMeshMetrics') |
|
|
|
const EdgeEncryptor = require('./edge-encryptor') |
|
|
|
const EdgeEncryptor = require('./edge-encryptor') |
|
|
|
const getFirstPreferredLangCode = require('./lib/get-first-preferred-lang-code') |
|
|
|
const getFirstPreferredLangCode = require('./lib/get-first-preferred-lang-code') |
|
|
|
const getObjStructure = require('./lib/getObjStructure') |
|
|
|
const getObjStructure = require('./lib/getObjStructure') |
|
|
|
|
|
|
|
const { |
|
|
|
|
|
|
|
ENVIRONMENT_TYPE_POPUP, |
|
|
|
|
|
|
|
ENVIRONMENT_TYPE_NOTIFICATION, |
|
|
|
|
|
|
|
ENVIRONMENT_TYPE_FULLSCREEN, |
|
|
|
|
|
|
|
} = require('./lib/enums') |
|
|
|
|
|
|
|
|
|
|
|
const STORAGE_KEY = 'metamask-config' |
|
|
|
const STORAGE_KEY = 'metamask-config' |
|
|
|
const METAMASK_DEBUG = process.env.METAMASK_DEBUG |
|
|
|
const METAMASK_DEBUG = process.env.METAMASK_DEBUG |
|
|
@ -43,7 +48,7 @@ const isEdge = !isIE && !!window.StyleMedia |
|
|
|
|
|
|
|
|
|
|
|
let popupIsOpen = false |
|
|
|
let popupIsOpen = false |
|
|
|
let notificationIsOpen = false |
|
|
|
let notificationIsOpen = false |
|
|
|
let openMetamaskTabsIDs = {} |
|
|
|
const openMetamaskTabsIDs = {} |
|
|
|
|
|
|
|
|
|
|
|
// state persistence
|
|
|
|
// state persistence
|
|
|
|
const diskStore = new LocalStorageStore({ storageKey: STORAGE_KEY }) |
|
|
|
const diskStore = new LocalStorageStore({ storageKey: STORAGE_KEY }) |
|
|
@ -191,33 +196,53 @@ function setupController (initState, initLangCode) { |
|
|
|
//
|
|
|
|
//
|
|
|
|
// connect to other contexts
|
|
|
|
// connect to other contexts
|
|
|
|
//
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
|
|
extension.runtime.onConnect.addListener(connectRemote) |
|
|
|
extension.runtime.onConnect.addListener(connectRemote) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const metamaskInternalProcessHash = { |
|
|
|
|
|
|
|
[ENVIRONMENT_TYPE_POPUP]: true, |
|
|
|
|
|
|
|
[ENVIRONMENT_TYPE_NOTIFICATION]: true, |
|
|
|
|
|
|
|
[ENVIRONMENT_TYPE_FULLSCREEN]: true, |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const isClientOpenStatus = () => { |
|
|
|
|
|
|
|
return popupIsOpen || Boolean(Object.keys(openMetamaskTabsIDs).length) || notificationIsOpen |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
function connectRemote (remotePort) { |
|
|
|
function connectRemote (remotePort) { |
|
|
|
const isMetaMaskInternalProcess = remotePort.name === 'popup' || remotePort.name === 'notification' |
|
|
|
const processName = remotePort.name |
|
|
|
|
|
|
|
const isMetaMaskInternalProcess = metamaskInternalProcessHash[processName] |
|
|
|
const portStream = new PortStream(remotePort) |
|
|
|
const portStream = new PortStream(remotePort) |
|
|
|
|
|
|
|
|
|
|
|
if (isMetaMaskInternalProcess) { |
|
|
|
if (isMetaMaskInternalProcess) { |
|
|
|
// communication with popup
|
|
|
|
// communication with popup
|
|
|
|
popupIsOpen = popupIsOpen || (remotePort.name === 'popup') |
|
|
|
|
|
|
|
controller.isClientOpen = true |
|
|
|
controller.isClientOpen = true |
|
|
|
controller.setupTrustedCommunication(portStream, 'MetaMask') |
|
|
|
controller.setupTrustedCommunication(portStream, 'MetaMask') |
|
|
|
// record popup as closed
|
|
|
|
|
|
|
|
if (remotePort.sender.url.match(/home.html$/)) { |
|
|
|
if (processName === ENVIRONMENT_TYPE_POPUP) { |
|
|
|
openMetamaskTabsIDs[remotePort.sender.tab.id] = true |
|
|
|
popupIsOpen = true |
|
|
|
} |
|
|
|
|
|
|
|
if (remotePort.name === 'popup') { |
|
|
|
|
|
|
|
endOfStream(portStream, () => { |
|
|
|
endOfStream(portStream, () => { |
|
|
|
popupIsOpen = false |
|
|
|
popupIsOpen = false |
|
|
|
if (remotePort.sender.url.match(/home.html$/)) { |
|
|
|
controller.isClientOpen = isClientOpenStatus() |
|
|
|
openMetamaskTabsIDs[remotePort.sender.tab.id] = false |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
controller.isClientOpen = popupIsOpen || |
|
|
|
|
|
|
|
Object.keys(openMetamaskTabsIDs).some(key => openMetamaskTabsIDs[key]) |
|
|
|
|
|
|
|
}) |
|
|
|
}) |
|
|
|
} |
|
|
|
} |
|
|
|
if (remotePort.name === 'notification') { |
|
|
|
|
|
|
|
|
|
|
|
if (processName === ENVIRONMENT_TYPE_NOTIFICATION) { |
|
|
|
|
|
|
|
notificationIsOpen = true |
|
|
|
|
|
|
|
|
|
|
|
endOfStream(portStream, () => { |
|
|
|
endOfStream(portStream, () => { |
|
|
|
notificationIsOpen = false |
|
|
|
notificationIsOpen = false |
|
|
|
|
|
|
|
controller.isClientOpen = isClientOpenStatus() |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (processName === ENVIRONMENT_TYPE_FULLSCREEN) { |
|
|
|
|
|
|
|
const tabId = remotePort.sender.tab.id |
|
|
|
|
|
|
|
openMetamaskTabsIDs[tabId] = true |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
endOfStream(portStream, () => { |
|
|
|
|
|
|
|
delete openMetamaskTabsIDs[tabId] |
|
|
|
|
|
|
|
controller.isClientOpen = isClientOpenStatus() |
|
|
|
}) |
|
|
|
}) |
|
|
|
} |
|
|
|
} |
|
|
|
} else { |
|
|
|
} else { |
|
|
@ -260,10 +285,11 @@ function setupController (initState, initLangCode) { |
|
|
|
|
|
|
|
|
|
|
|
// popup trigger
|
|
|
|
// popup trigger
|
|
|
|
function triggerUi () { |
|
|
|
function triggerUi () { |
|
|
|
extension.tabs.query({ active: true }, (tabs) => { |
|
|
|
extension.tabs.query({ active: true }, tabs => { |
|
|
|
const currentlyActiveMetamaskTab = tabs.find(tab => openMetamaskTabsIDs[tab.id]) |
|
|
|
const currentlyActiveMetamaskTab = Boolean(tabs.find(tab => openMetamaskTabsIDs[tab.id])) |
|
|
|
if (!popupIsOpen && !currentlyActiveMetamaskTab && !notificationIsOpen) notificationManager.showPopup() |
|
|
|
if (!popupIsOpen && !currentlyActiveMetamaskTab && !notificationIsOpen) { |
|
|
|
notificationIsOpen = true |
|
|
|
notificationManager.showPopup() |
|
|
|
|
|
|
|
} |
|
|
|
}) |
|
|
|
}) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|