diff --git a/manifest.json b/manifest.json index 1a2f78d..4c43b64 100644 --- a/manifest.json +++ b/manifest.json @@ -31,7 +31,8 @@ "sessions", "storage", "tabs", - "clipboardRead" + "clipboardRead", + "notifications" ], "web_accessible_resources": [ "build/console.html", diff --git a/src/background/index.js b/src/background/index.js index 3ef712f..ff27796 100644 --- a/src/background/index.js +++ b/src/background/index.js @@ -3,6 +3,7 @@ import messages from 'shared/messages'; import BackgroundComponent from 'background/components/background'; import reducers from 'background/reducers'; import { createStore } from 'shared/store'; +import * as versions from 'shared/versions'; const store = createStore(reducers, (e, sender) => { console.error('Vim-Vixen:', e); @@ -17,3 +18,12 @@ const store = createStore(reducers, (e, sender) => { const backgroundComponent = new BackgroundComponent(store); store.dispatch(settingActions.load()); + +versions.checkUpdated().then((updated) => { + if (!updated) { + return; + } + return versions.notify(); +}).then(() => { + return versions.commit(); +}); diff --git a/src/shared/versions/index.js b/src/shared/versions/index.js new file mode 100644 index 0000000..ee9f3b5 --- /dev/null +++ b/src/shared/versions/index.js @@ -0,0 +1,39 @@ +import * as storage from './storage'; +import * as releaseNotes from './release-notes'; +import manifest from '../../../manifest.json'; + +const NOTIFICATION_ID = 'vimvixen-update'; + +const notificationClickListener = (id) => { + if (id !== NOTIFICATION_ID) { + return; + } + + browser.tabs.create({ url: releaseNotes.url(manifest.version) }); + browser.notifications.onClicked.removeListener(notificationClickListener); +}; + +const checkUpdated = () => { + return storage.load().then((prev) => { + if (!prev) { + return true; + } + return manifest.version !== prev; + }); +}; + +const notify = () => { + browser.notifications.onClicked.addListener(notificationClickListener); + return browser.notifications.create(NOTIFICATION_ID, { + 'type': 'basic', + 'iconUrl': browser.extension.getURL('resources/icon_48x48.png'), + 'title': 'Vim Vixen ' + manifest.version + ' has been installed', + 'message': 'Click here to see release notes', + }); +}; + +const commit = () => { + storage.save(manifest.version); +}; + +export { checkUpdated, notify, commit }; diff --git a/src/shared/versions/release-notes.js b/src/shared/versions/release-notes.js new file mode 100644 index 0000000..9bd17a2 --- /dev/null +++ b/src/shared/versions/release-notes.js @@ -0,0 +1,5 @@ +const url = (version) => { + return 'https://github.com/ueokande/vim-vixen/releases/tag/' + version; +}; + +export { url }; diff --git a/src/shared/versions/storage.js b/src/shared/versions/storage.js new file mode 100644 index 0000000..37603c8 --- /dev/null +++ b/src/shared/versions/storage.js @@ -0,0 +1,11 @@ +const load = () => { + return browser.storage.local.get('version').then(({ version }) => { + return version; + }); +}; + +const save = (version) => { + return browser.storage.local.set({ version }); +}; + +export { load, save }; diff --git a/test/shared/versions/index.test.js b/test/shared/versions/index.test.js new file mode 100644 index 0000000..3bb307a --- /dev/null +++ b/test/shared/versions/index.test.js @@ -0,0 +1,55 @@ +import * as versions from 'shared/versions'; +import manifest from '../../../manifest.json'; + +describe("shared/versions/storage", () => { + describe('#checkUpdated', () => { + beforeEach(() => { + return browser.storage.local.remove('version'); + }); + + it('return true if no previous versions', () => { + return Promise.resolve().then(() => { + return versions.checkUpdated(); + }).then((updated) => { + expect(updated).to.be.true; + }); + }); + + it('return true if updated', () => { + return Promise.resolve().then(() => { + return browser.storage.local.set({ version: '0.001' }); + }).then(() => { + return versions.checkUpdated(); + }).then((updated) => { + expect(updated).to.be.true; + }); + }); + + it('return false if not updated', () => { + return Promise.resolve().then(() => { + return browser.storage.local.set({ version: manifest.version }); + }).then(() => { + return versions.checkUpdated(); + }).then((updated) => { + expect(updated).to.be.false; + }); + }); + }); + + describe('#commit', () => { + beforeEach(() => { + return browser.storage.local.remove('version'); + }); + + it('saves current version from manifest.json', () => { + return Promise.resolve().then(() => { + return versions.commit(); + }).then(() => { + return browser.storage.local.get('version'); + }).then(({version}) => { + expect(version).to.be.a('string'); + expect(version).to.equal(manifest.version); + }); + }); + }); +}); diff --git a/test/shared/versions/storage.test.js b/test/shared/versions/storage.test.js new file mode 100644 index 0000000..bcfa259 --- /dev/null +++ b/test/shared/versions/storage.test.js @@ -0,0 +1,39 @@ +import * as storage from 'shared/versions/storage'; + +describe("shared/versions/storage", () => { + describe('#load', () => { + beforeEach(() => { + return browser.storage.local.remove('version'); + }); + + it('loads saved version', () => { + return Promise.resolve().then(() => { + return browser.storage.local.set({ version: '1.2.3' }); + }).then(() => { + return storage.load(); + }).then((version) => { + expect(version).to.equal('1.2.3'); + }); + }); + + it('returns undefined if no versions in storage', () => { + return Promise.resolve().then(() => { + return storage.load(); + }).then((version) => { + expect(version).to.be.a('undefined'); + }); + }); + }); + + describe('#save', () => { + it('saves version string', () => { + return Promise.resolve().then(() => { + return storage.save('2.3.4'); + }).then(() => { + return browser.storage.local.get('version'); + }).then(({version}) => { + expect(version).to.equal('2.3.4'); + }); + }); + }); +});