Skip to content

Commit

Permalink
firefox support
Browse files Browse the repository at this point in the history
  • Loading branch information
nsjames committed May 27, 2018
1 parent 15d9c0b commit bc0afc2
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 64 deletions.
15 changes: 15 additions & 0 deletions src/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ let seed = '';
let inactivityInterval = 0;
let timeoutLocker = null;

let prompt = null;

// This is the script that runs in the extension's background ( singleton )
export default class Background {

Expand Down Expand Up @@ -72,6 +74,8 @@ export default class Background {
case InternalMessageTypes.REQUEST_GET_VERSION: Background.requestGetVersion(sendResponse); break;
case InternalMessageTypes.REQUEST_VERSION_UPDATE: Background.requestVersionUpdate(sendResponse, message.payload); break;
case InternalMessageTypes.AUTHENTICATE: Background.authenticate(sendResponse, message.payload); break;
case InternalMessageTypes.SET_PROMPT: Background.setPrompt(sendResponse, message.payload); break;
case InternalMessageTypes.GET_PROMPT: Background.getPrompt(sendResponse); break;
}
}

Expand All @@ -83,6 +87,17 @@ export default class Background {
}


static setPrompt(sendResponse, notification){
console.log('setting prompt');
console.log('noti', notification);
prompt = notification;
sendResponse(true);
}

static getPrompt(sendResponse){
console.log('send res', prompt);
sendResponse(prompt);
}

/********************************************/
/* Handlers */
Expand Down
3 changes: 3 additions & 0 deletions src/messages/InternalMessageTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,6 @@ export const REQUEST_ADD_NETWORK = 'requestAddNetwork';
export const REQUEST_GET_VERSION = 'requestGetVersion';
export const REQUEST_VERSION_UPDATE = 'requestVersionUpdate';
export const AUTHENTICATE = 'authenticate';

export const SET_PROMPT = 'setPrompt';
export const GET_PROMPT = 'getPrompt';
15 changes: 7 additions & 8 deletions src/prompt.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@ import SearchComponent from './components/SearchComponent.vue'
import InputComponent from './components/InputComponent.vue'
import SelectComponent from './components/SelectComponent.vue'
import KeyValue from './components/KeyValue.vue'
import InternalMessage from './messages/InternalMessage'
import * as InternalMessageTypes from './messages/InternalMessageTypes'
import {apis} from './util/BrowserApis';

class PromptWindow {

constructor(){
let prompt = window.data || null;
let prompt = window.data || apis.extension.getBackgroundPage().notification || null;

// TODO: Pair prompt with a checksum from the state store so that
// even if an attacker manages to open a clickjack/malicious prompt
Expand All @@ -36,16 +39,12 @@ class PromptWindow {
{tag:'alert', vue:Alert},
{tag:'key-value', vue:KeyValue},
];
const routes = Routing.routes(true);

// TODO: Request unlock for prompt
const middleware = (to, next, store) => {
next();
// store.dispatch(Actions.IS_UNLOCKED)
// .then(unlocked => (unlocked) ? next() : next({name:RouteNames.PROMPT_REQUEST_UNLOCK}));
};
const routes = Routing.routes(true);
const middleware = (to, next, store) => next();

new VueInitializer(routes, components, middleware, (router, store) => {
console.log('store', store);
store.dispatch(Actions.PUSH_PROMPT, prompt);
router.push({name:prompt.routeName()});
});
Expand Down
96 changes: 54 additions & 42 deletions src/services/NotificationService.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import Error from '../models/errors/Error'
import {apis} from '../util/BrowserApis';
import InternalMessage from '../messages/InternalMessage'
import * as InternalMessageTypes from '../messages/InternalMessageTypes'

let openWindow = null;

Expand All @@ -10,56 +12,61 @@ export default class NotificationService {
* @param notification
*/
static async open(notification){
if(openWindow){
// For now we're just going to close the window to get rid of the error
// that is caused by already open windows swallowing all further requests
openWindow.close();
// if(openWindow){
// // For now we're just going to close the window to get rid of the error
// // that is caused by already open windows swallowing all further requests
// openWindow.close();
// openWindow = null;
//
// // Alternatively we could focus the old window, but this would cause
// // urgent 1-time messages to be lost, such as after dying in a game and
// // uploading a high-score. That message will be lost.
// // openWindow.focus();
// // return false;
//
// // A third option would be to add a queue, but this could cause
// // virus-like behavior as apps overflow the queue causing the user
// // to have to quit the browser to regain control.
// }

// Alternatively we could focus the old window, but this would cause
// urgent 1-time messages to be lost, such as after dying in a game and
// uploading a high-score. That message will be lost.
// openWindow.focus();
// return false;

// A third option would be to add a queue, but this could cause
// virus-like behavior as apps overflow the queue causing the user
// to have to quit the browser to regain control.
}

const height = 600;
const width = 700;
let middleX = window.screen.availWidth/2 - (width/2);
let middleY = window.screen.availHeight/2 - (height/2);

const getPopup = async () => {
const url = apis.runtime.getURL('/prompt.html');
return window.open(url, 'ScatterPrompt', `width=${width},height=${height},resizable=0,top=${middleY},left=${middleX},titlebar=0`);
// if(typeof browser !== 'undefined') {
// const created = await browser.windows.create({
// url,
// height,
// width,
// type:'panel'
// });
//
// console.log(await browser.windows.getCurrent());
// return created;
// // return await browser.windows.get(created.id);
//
// }
// else return window.open(url, 'ScatterPrompt', `width=${width},height=${height},resizable=0,top=${middleY},left=${middleX},titlebar=0`);
try {
const url = apis.runtime.getURL('/prompt.html');

// Notifications get bound differently depending on browser
// as Firefox does not support opening windows from background.
if(typeof browser !== 'undefined') {
const created = await apis.windows.create({
url,
height,
width,
type:'popup'
});

window.notification = notification;
return created;
}
else {
const win = window.open(url, 'ScatterPrompt', `width=${width},height=${height},resizable=0,top=${middleY},left=${middleX},titlebar=0`);
win.data = notification;
openWindow = win;
return win;
}
} catch (e) {
console.log('notification error', e);
return null;
}
}

let popup = await getPopup();

// Binding the notification to the popup
popup.data = notification;
await InternalMessage.payload(InternalMessageTypes.SET_PROMPT, JSON.stringify(notification)).send();

// console.log('popup', popup, window)

// let popup = window.open(url, 'ScatterPrompt', `width=${width},height=${height},resizable=0,top=${middleY},left=${middleX},titlebar=0`);

openWindow = popup;
let popup = await getPopup();

// Handles the user closing the popup without taking any action
popup.onbeforeunload = () => {
Expand All @@ -76,9 +83,14 @@ export default class NotificationService {
* Always use this method for closing notification popups.
* Otherwise you will double send responses and one will always be null.
*/
static close(){
window.onbeforeunload = () => {};
window.close();
static async close(){
if(typeof browser !== 'undefined') {
const {id: windowId,} = (await apis.windows.getCurrent());
apis.windows.remove(windowId);
} else {
window.onbeforeunload = () => {};
window.close();
}
}

}
3 changes: 2 additions & 1 deletion src/util/BrowserApis.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ class ApiGenerator {
'app',
'storage',
'extension',
'runtime'
'runtime',
'windows'
]
.map(api => {
if(typeof chrome !== 'undefined') swallow(() => {if(chrome[api]) this[api] = chrome[api]});
Expand Down
26 changes: 13 additions & 13 deletions src/views/IdentityView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,19 @@
<figure class="sub-header" style="margin-bottom:0;">{{locale(langKeys.IDENTITY_NameDescription)}}</figure>
<cin v-if="identity.ridl > 0 || !registeringIdentity" :text="identity.name" v-on:changed="changed => bind(changed, 'identity.name')" :disabled="true"></cin>
<cin v-else :placeholder="locale(langKeys.PLACEHOLDER_Name)" :text="newName" v-on:changed="changed => bind(changed, 'newName')"></cin>
<section v-if="identity.ridl <= 0">
<btn v-if="!isNew && !registeringIdentity"
:text="registeringIdentity ? locale(langKeys.BUTTON_RegisterIdentity) : locale(langKeys.BUTTON_ChangeName)"
v-on:clicked="registerIdentity" :is-blue="registeringIdentity" margined="true"></btn>

<btn v-if="!isNew && registeringIdentity"
:text="locale(langKeys.BUTTON_ClaimIdentity)"
v-on:clicked="claimIdentity" is-blue="true" margined="true"></btn>

<btn v-if="!isNew && registeringIdentity"
:text="locale(langKeys.BUTTON_Cancel)"
v-on:clicked="registeringIdentity = false" margined="true" :is-red="true"></btn>
</section>
<!--<section v-if="identity.ridl <= 0">-->
<!--<btn v-if="!isNew && !registeringIdentity"-->
<!--:text="registeringIdentity ? locale(langKeys.BUTTON_RegisterIdentity) : locale(langKeys.BUTTON_ChangeName)"-->
<!--v-on:clicked="registerIdentity" :is-blue="registeringIdentity" margined="true"></btn>-->

<!--<btn v-if="!isNew && registeringIdentity"-->
<!--:text="locale(langKeys.BUTTON_ClaimIdentity)"-->
<!--v-on:clicked="claimIdentity" is-blue="true" margined="true"></btn>-->

<!--<btn v-if="!isNew && registeringIdentity"-->
<!--:text="locale(langKeys.BUTTON_Cancel)"-->
<!--v-on:clicked="registeringIdentity = false" margined="true" :is-red="true"></btn>-->
<!--</section>-->
</section>

<!-- Account -->
Expand Down

0 comments on commit bc0afc2

Please sign in to comment.