Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open notification UI when eth_requestAccounts waits for unlock #8508

Merged
merged 3 commits into from
May 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion app/scripts/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,8 @@ function setupController (initState, initLangCode) {
showUnconfirmedMessage: triggerUi,
showUnapprovedTx: triggerUi,
showPermissionRequest: triggerUi,
openPopup: openPopup,
showUnlockRequest: triggerUi,
openPopup,
// initial state
initState,
// initial locale code
Expand Down
27 changes: 24 additions & 3 deletions app/scripts/controllers/app-state.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class AppStateController extends EventEmitter {
isUnlocked,
initState,
onInactiveTimeout,
showUnlockRequest,
preferencesStore,
} = opts
const { preferences } = preferencesStore.getState()
Expand All @@ -29,6 +30,8 @@ class AppStateController extends EventEmitter {
this.waitingForUnlock = []
addUnlockListener(this.handleUnlock.bind(this))

this._showUnlockRequest = showUnlockRequest

preferencesStore.subscribe((state) => {
this._setInactiveTimeout(state.preferences.autoLockTimeLimit)
})
Expand All @@ -40,20 +43,38 @@ class AppStateController extends EventEmitter {
* Get a Promise that resolves when the extension is unlocked.
* This Promise will never reject.
*
* @param {boolean} shouldShowUnlockRequest - Whether the extension notification
* popup should be opened.
* @returns {Promise<void>} A promise that resolves when the extension is
* unlocked, or immediately if the extension is already unlocked.
*/
getUnlockPromise () {
getUnlockPromise (shouldShowUnlockRequest) {
return new Promise((resolve) => {
if (this.isUnlocked()) {
resolve()
} else {
this.waitingForUnlock.push({ resolve })
this.emit('updateBadge')
this.waitForUnlock(resolve, shouldShowUnlockRequest)
}
})
}

/**
* Adds a Promise's resolve function to the waitingForUnlock queue.
* Also opens the extension popup if specified.
*
* @param {Promise.resolve} resolve - A Promise's resolve function that will
* be called when the extension is unlocked.
* @param {boolean} shouldShowUnlockRequest - Whether the extension notification
* popup should be opened.
*/
waitForUnlock (resolve, shouldShowUnlockRequest) {
this.waitingForUnlock.push({ resolve })
this.emit('updateBadge')
if (shouldShowUnlockRequest) {
this._showUnlockRequest()
}
}

/**
* Drains the waitingForUnlock queue, resolving all the related Promises.
*/
Expand Down
6 changes: 3 additions & 3 deletions app/scripts/controllers/permissions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export class PermissionsController {
})

this.getKeyringAccounts = getKeyringAccounts
this.getUnlockPromise = getUnlockPromise
this._getUnlockPromise = getUnlockPromise
this._notifyDomain = notifyDomain
this.notifyAllDomains = notifyAllDomains
this._showPermissionRequest = showPermissionRequest
Expand Down Expand Up @@ -92,7 +92,7 @@ export class PermissionsController {
store: this.store,
storeKey: METADATA_STORE_KEY,
getAccounts: this.getAccounts.bind(this, origin),
getUnlockPromise: this.getUnlockPromise,
getUnlockPromise: () => this._getUnlockPromise(true),
hasPermission: this.hasPermission.bind(this, origin),
requestAccountsPermission: this._requestPermissions.bind(
this, origin, { eth_accounts: {} }
Expand Down Expand Up @@ -602,7 +602,7 @@ export class PermissionsController {
this.pendingApprovals.has(id)
) {
throw new Error(
`Pending approval with id ${id} or origin ${origin} already exists.`
`Pending approval with id '${id}' or origin '${origin}' already exists.`
)
}

Expand Down
1 change: 1 addition & 0 deletions app/scripts/metamask-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ export default class MetamaskController extends EventEmitter {
isUnlocked: this.isUnlocked.bind(this),
initState: initState.AppStateController,
onInactiveTimeout: () => this.setLocked(),
showUnlockRequest: opts.showUnlockRequest,
preferencesStore: this.preferencesController.store,
})

Expand Down
2 changes: 1 addition & 1 deletion test/unit/app/controllers/permissions/mocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ export const getters = deepFreeze({
pendingApprovals: {
duplicateOriginOrId: (id, origin) => {
return {
message: `Pending approval with id ${id} or origin ${origin} already exists.`,
message: `Pending approval with id '${id}' or origin '${origin}' already exists.`,
}
},
requestAlreadyPending: () => {
Expand Down