proper implementation for requesting permissions

This commit is contained in:
Dima Gerasimov 2019-12-10 00:27:04 +00:00 committed by Dmitrii Gerasimov
parent 4130b4eca0
commit f24ca71ba4

View file

@ -1,4 +1,6 @@
/* @flow */ /* @flow */
import {getOptions} from './options';
export const COMMAND_CAPTURE_SIMPLE = 'capture-simple'; export const COMMAND_CAPTURE_SIMPLE = 'capture-simple';
export const METHOD_CAPTURE_WITH_EXTRAS = 'captureWithExtras'; export const METHOD_CAPTURE_WITH_EXTRAS = 'captureWithExtras';
@ -37,35 +39,38 @@ export function chromePermissionsContains(params: any) {
return awrap(chrome.permissions.contains, params); return awrap(chrome.permissions.contains, params);
} }
// TODO ugh. use promises already...
// TODO fuck. doesn't seem to work for Firefox at all... // TODO fuck. doesn't seem to work for Firefox at all...
// just getting Error: "An unexpected error occurred" background.bundle.js // just getting Error: "An unexpected error occurred" background.bundle.js
// seems like this bug https://bugzilla.mozilla.org/show_bug.cgi?id=1382953 // seems like this bug https://bugzilla.mozilla.org/show_bug.cgi?id=1382953
export function ensurePermissions(cb: () => void) { export function ensurePermissions(cb: () => void) {
const needs_check = false; // TODO FIXME
if (!needs_check) {
cb();
return;
}
const back = chrome.extension.getBackgroundPage().console; const back = chrome.extension.getBackgroundPage().console;
function onResponse(response) {
const err = chrome.runtime.lastError; getOptions().then(opts => {
console.error("ERROR!", err); const endpoint = opts.endpoint;
if (response) { // that's capable of handling default (localhost), so shouldn't prompt
const perms = {
origins: [
endpoint,
],
};
return chromePermissionsContains(perms).then(has => {
if (has) {
// no need to do anything
return true;
} else {
return chromePermissionsRequest(perms);
}
});
}).then(granted => {
if (granted) {
back.info("Permission was granted"); back.info("Permission was granted");
cb(); cb();
} else { } else {
showNotification(`ERROR: Permission was refused`); const err = chrome.runtime.lastError;
const msg = err ? String(err) : 'Permission was refused';
showNotification(`ERROR: ${msg}`);
} }
} });
const perms = {
// TODO FIXME only if it's not localhost?
origins: ["https://*/capture"],
};
// TODO improve message somehow??? // TODO improve message somehow???
chrome.permissions.request(perms, onResponse);
// browser.permissions.request(perms).then(onResponse, error => {
// back.error("ERROR!!!!", error);
// });
} }