forked from retoor/devplacepy
136 lines
3.6 KiB
JavaScript
136 lines
3.6 KiB
JavaScript
import { Config, ConfigError } from "./Config.js";
|
|
import { Fetcher } from "./Fetcher.js";
|
|
import { Limits } from "./Limits.js";
|
|
import { Policy } from "./Policy.js";
|
|
import { State, STATES } from "./State.js";
|
|
import { Usage } from "./Usage.js";
|
|
import { WsClient } from "./WsClient.js";
|
|
|
|
const ALARM = "devplace-node-backstop";
|
|
const ALARM_PERIOD_MINUTES = 1;
|
|
const DISCLOSURE_PAGE = "ui/disclosure.html";
|
|
|
|
const config = new Config(chrome.storage.local);
|
|
const usage = new Usage(chrome.storage.local);
|
|
const state = new State(chrome.storage.session);
|
|
const limits = new Limits();
|
|
const fetcher = new Fetcher({ policy: new Policy(), limits, usage });
|
|
const client = new WsClient({
|
|
config,
|
|
limits,
|
|
fetcher,
|
|
state,
|
|
clientVersion: chrome.runtime.getManifest().version,
|
|
platform: navigator.userAgent.includes("Firefox") ? "firefox" : "chrome",
|
|
});
|
|
|
|
let ready = null;
|
|
|
|
function boot() {
|
|
if (!ready) {
|
|
ready = (async () => {
|
|
await Promise.all([config.load(), usage.load(), state.load()]);
|
|
limits.setLocal(config.data);
|
|
})();
|
|
}
|
|
return ready;
|
|
}
|
|
|
|
async function reconcile() {
|
|
await boot();
|
|
limits.setLocal(config.data);
|
|
if (config.data.enabled && config.data.disclosed && config.hasSecret) {
|
|
await client.start();
|
|
} else if (client.wanted || state.data.state !== STATES.OFF) {
|
|
await client.stop();
|
|
}
|
|
}
|
|
|
|
async function status() {
|
|
await boot();
|
|
return {
|
|
config: config.view(),
|
|
state: state.snapshot(),
|
|
usage: usage.snapshot(),
|
|
live: { concurrency: limits.concurrency, bandwidthBytes: limits.bandwidthBytes },
|
|
};
|
|
}
|
|
|
|
const COMMANDS = {
|
|
status: async () => status(),
|
|
connect: async (message) => {
|
|
await config.connect(message);
|
|
await client.stop("credentials changed");
|
|
await reconcile();
|
|
return status();
|
|
},
|
|
accept: async () => {
|
|
await config.acceptDisclosure();
|
|
return status();
|
|
},
|
|
enable: async (message) => {
|
|
await config.setEnabled(Boolean(message.enabled));
|
|
await reconcile();
|
|
return status();
|
|
},
|
|
settings: async (message) => {
|
|
await config.saveSettings(message.values || {});
|
|
limits.setLocal(config.data);
|
|
return status();
|
|
},
|
|
forget: async () => {
|
|
await config.forget();
|
|
await client.stop("device forgotten");
|
|
return status();
|
|
},
|
|
resetUsage: async () => {
|
|
await usage.reset();
|
|
return status();
|
|
},
|
|
};
|
|
|
|
async function dispatch(message) {
|
|
await boot();
|
|
const command = COMMANDS[message && message.cmd];
|
|
if (!command) {
|
|
return { error: `unknown command ${String(message && message.cmd)}` };
|
|
}
|
|
try {
|
|
return await command(message);
|
|
} catch (error) {
|
|
if (error instanceof ConfigError) {
|
|
return { error: error.message };
|
|
}
|
|
await state.set({ lastError: error.message });
|
|
return { error: error.message };
|
|
}
|
|
}
|
|
|
|
chrome.runtime.onMessage.addListener((message, sender, respond) => {
|
|
if (sender.id !== chrome.runtime.id) {
|
|
return false;
|
|
}
|
|
dispatch(message).then(respond);
|
|
return true;
|
|
});
|
|
|
|
chrome.alarms.onAlarm.addListener((alarm) => {
|
|
if (alarm.name === ALARM) {
|
|
reconcile().catch((error) => state.set({ lastError: error.message }));
|
|
}
|
|
});
|
|
|
|
chrome.runtime.onInstalled.addListener((details) => {
|
|
chrome.alarms.create(ALARM, { periodInMinutes: ALARM_PERIOD_MINUTES });
|
|
if (details.reason === "install") {
|
|
chrome.tabs.create({ url: chrome.runtime.getURL(DISCLOSURE_PAGE) });
|
|
}
|
|
});
|
|
|
|
chrome.runtime.onStartup.addListener(() => {
|
|
chrome.alarms.create(ALARM, { periodInMinutes: ALARM_PERIOD_MINUTES });
|
|
reconcile().catch((error) => state.set({ lastError: error.message }));
|
|
});
|
|
|
|
reconcile().catch((error) => state.set({ lastError: error.message }));
|