-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #66 from luniehq/fabo/56-bind-requests-to-tabs
Fabo/56 bind requests to tabs
- Loading branch information
Showing
9 changed files
with
186 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
[Changed] Remove pending sign requests if a tab closes or changes the URL @faboweb |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,5 +31,8 @@ | |
"contentScript.js" | ||
] | ||
} | ||
], | ||
"permissions": [ | ||
"tabs" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
export default class SignRequestQueue { | ||
constructor() { | ||
this.queue = [] | ||
this.unqueueSignRequest('') // to reset the icon in the beginning | ||
} | ||
|
||
queueSignRequest({ signMessage, senderAddress, tabID }) { | ||
this.queue.push({ signMessage, senderAddress, id: Date.now(), tabID }) | ||
chrome.browserAction.setIcon({ path: 'icons/128x128-alert.png' }) | ||
} | ||
|
||
unqueueSignRequest(id) { | ||
const signRequest = this.queue.find(({ id: storedId }) => storedId === id) | ||
this.queue = this.queue.filter(({ id: storedId }) => storedId !== id) | ||
if (this.queue.length === 0) { | ||
chrome.browserAction.setIcon({ path: 'icons/128x128.png' }) | ||
} | ||
return signRequest | ||
} | ||
|
||
unqueueSignRequestForTab(tabID) { | ||
this.queue | ||
.filter(({ tabID: storedtabID }) => storedtabID === tabID) | ||
.map(({ id }) => { | ||
this.unqueueSignRequest(id) | ||
}) | ||
} | ||
|
||
getSignRequest() { | ||
return this.queue.length > 0 ? this.queue[0] : undefined | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// requests always reference a tab so that a response finds the right listener | ||
// if a tab is killed or it's url changes the request is not useful anymore | ||
export function bindRequestsToTabs(signRequestQueue, whitelisted) { | ||
// check if tab got removed | ||
chrome.tabs.onRemoved.addListener(function(tabID) { | ||
signRequestQueue.unqueueSignRequestForTab(tabID) | ||
}) | ||
// check if url changed | ||
chrome.tabs.onUpdated.addListener(function(tabID, changeInfo) { | ||
// if the url doesn't change, ignore the update | ||
if (!changeInfo.url) { | ||
return | ||
} | ||
if ( | ||
!whitelisted.find(whitelistedUrl => | ||
changeInfo.url.startsWith(whitelistedUrl) | ||
) | ||
) { | ||
signRequestQueue.unqueueSignRequestForTab(tabID) | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import SignRequestQueue from '../../src/requests' | ||
|
||
const mockSignRequest = { | ||
signMessage: 'HALLOOOOO', | ||
senderAddress: 'cosmos1234', | ||
tabID: 42, | ||
id: expect.any(Number) | ||
} | ||
|
||
describe('Sign request queue', () => { | ||
let instance | ||
|
||
beforeEach(() => { | ||
global.chrome = { | ||
browserAction: { | ||
setIcon: jest.fn() | ||
} | ||
} | ||
instance = new SignRequestQueue() | ||
global.chrome.browserAction.setIcon.mockClear() | ||
}) | ||
|
||
it('should queue a sign request', () => { | ||
instance.queueSignRequest(mockSignRequest) | ||
expect(instance.getSignRequest()).toEqual(mockSignRequest) | ||
}) | ||
|
||
it('should unqueue a sign request', () => { | ||
instance.queueSignRequest(mockSignRequest) | ||
const { id } = instance.queue[0] | ||
instance.unqueueSignRequest(id) | ||
|
||
expect(instance.getSignRequest()).toEqual(undefined) | ||
}) | ||
|
||
it('should unqueueSignRequestForTab', () => { | ||
instance.queueSignRequest(mockSignRequest) | ||
instance.unqueueSignRequestForTab(42) | ||
|
||
expect(instance.getSignRequest()).toEqual(undefined) | ||
}) | ||
|
||
describe('icons', () => { | ||
it('shows a pending sign request icon', () => { | ||
instance.queueSignRequest(mockSignRequest) | ||
expect(global.chrome.browserAction.setIcon).toHaveBeenCalled() | ||
}) | ||
|
||
it('removed the pending sign request icon', () => { | ||
instance.queueSignRequest(mockSignRequest) | ||
expect(global.chrome.browserAction.setIcon).toHaveBeenCalled() | ||
|
||
global.chrome.browserAction.setIcon.mockClear() | ||
|
||
instance.unqueueSignRequestForTab(42) | ||
expect(global.chrome.browserAction.setIcon).toHaveBeenCalled() | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { bindRequestsToTabs } from '../../src/tabsHandler' | ||
|
||
describe('Sign request queue', () => { | ||
let signRequestQueue | ||
|
||
beforeEach(() => { | ||
signRequestQueue = { | ||
unqueueSignRequestForTab: jest.fn() | ||
} | ||
}) | ||
|
||
it('kills on tab removal', () => { | ||
global.chrome = { | ||
tabs: { | ||
onRemoved: { addListener: callback => callback(42) }, | ||
onUpdated: { addListener: () => {} } | ||
} | ||
} | ||
bindRequestsToTabs(signRequestQueue, []) | ||
|
||
expect(signRequestQueue.unqueueSignRequestForTab).toHaveBeenCalledWith(42) | ||
expect(signRequestQueue.unqueueSignRequestForTab).toHaveBeenCalledTimes(1) | ||
}) | ||
|
||
it('kills on tab url not accepted', () => { | ||
global.chrome = { | ||
tabs: { | ||
onRemoved: { addListener: () => {} }, | ||
onUpdated: { addListener: callback => callback(42, {}) } | ||
} | ||
} | ||
bindRequestsToTabs(signRequestQueue, ['https://lunie.io']) | ||
expect(signRequestQueue.unqueueSignRequestForTab).not.toHaveBeenCalled() | ||
|
||
global.chrome = { | ||
tabs: { | ||
onRemoved: { addListener: () => {} }, | ||
onUpdated: { | ||
addListener: callback => callback(42, { url: 'https://funkytown.io' }) | ||
} | ||
} | ||
} | ||
bindRequestsToTabs(signRequestQueue, ['https://lunie.io']) | ||
|
||
expect(signRequestQueue.unqueueSignRequestForTab).toHaveBeenCalledWith(42) | ||
expect(signRequestQueue.unqueueSignRequestForTab).toHaveBeenCalledTimes(1) | ||
}) | ||
}) |
This file was deleted.
Oops, something went wrong.