aboutsummaryrefslogtreecommitdiff
/**
 * This file is part of Haketilo.
 *
 * Function: Storage through messages (client side).
 *
 * Copyright (C) 2021 Wojtek Kosior
 * Redistribution terms are gathered in the `copyright' file.
 */

/*
 * IMPORTS_START
 * IMPORT CONNECTION_TYPE
 * IMPORT list_prefixes
 * IMPORT make_once
 * IMPORT browser
 * IMPORTS_END
 */

var call_id = 0;
var port;
var calls_waiting = new Map();

function set_call_callback(resolve, reject, func, args)
{
    port.postMessage([call_id, func, args]);
    calls_waiting.set(call_id++, [resolve, reject]);
}

async function remote_call(func, args)
{
    return new Promise((resolve, reject) =>
		       set_call_callback(resolve, reject, func, args));
}

function handle_message(message)
{
    let callbacks = calls_waiting.get(message.call_id);
    if (callbacks === undefined) {
	handle_change(message);
	return;
    }

    let [resolve, reject] = callbacks;
    calls_waiting.delete(message.call_id);
    if (message.error !== undefined)
	setTimeout(reject, 0, message.error);
    else
	setTimeout(resolve, 0, message.result);
}

const list_by_prefix = {};

for (const prefix of list_prefixes)
    list_by_prefix[prefix] = {prefix, listeners : new Set()};

var resolve_init;

function handle_first_message(message)
{
    for (let prefix of Object.keys(message))
	list_by_prefix[prefix].map = new Map(message[prefix]);

    port.onMessage.removeListener(handle_first_message);
    port.onMessage.addListener(handle_message);

    resolve_init();
}

function handle_change(change)
{
    let list = list_by_prefix[change.prefix];

    if (change.new_val === undefined)
	list.map.delete(change.item);
    else
	list.map.set(change.item, change.new_val);

    for (let listener_callback of list.listeners)
	listener_callback(change);
}

var exports = {};

function start_connection(resolve)
{
    resolve_init = resolve;
    port = browser.runtime.connect({name : CONNECTION_TYPE.REMOTE_STORAGE});
    port.onMessage.addListener(handle_first_message);
}

async function init() {
    await new Promise((resolve, reject) => start_connection(resolve));
    return exports;
}

for (let call_name of ["set", "remove", "replace", "clear"])
    exports [call_name] = (...args) => remote_call(call_name, args);

// TODO: Much of the code below is copy-pasted from /background/storage.mjs.
//       This should later be refactored into a separate module
//       to avoid duplication.

/*
 * Facilitate listening to changes
 */

exports.add_change_listener = function (cb, prefixes=list_prefixes)
{
    if (typeof(prefixes) === "string")
	prefixes = [prefixes];

    for (let prefix of prefixes)
	list_by_prefix[prefix].listeners.add(cb);
}

exports.remove_change_listener = function (cb, prefixes=list_prefixes)
{
    if (typeof(prefixes) === "string")
	prefixes = [prefixes];

    for (let prefix of prefixes)
	list_by_prefix[prefix].listeners.delete(cb);
}

/* Prepare some hepler functions to get elements of a list */

function list_items_it(list, with_values=false)
{
    return with_values ? list.map.entries() : list.map.keys();
}

function list_entries_it(list)
{
    return list_items_it(list, true);
}

function list_items(list, with_values=false)
{
    let array = [];

    for (let item of list_items_it(list, with_values))
	array.push(item);

    return array;
}

function list_entries(list)
{
    return list_items(list, true);
}

exports.get = function (prefix, item)
{
    return list_by_prefix[prefix].map.get(item);
}

exports.get_all_names = function (prefix)
{
    return list_items(list_by_prefix[prefix]);
}

exports.get_all_names_it = function (prefix)
{
    return list_items_it(list_by_prefix[prefix]);
}

exports.get_all = function (prefix)
{
    return list_entries(list_by_prefix[prefix]);
}

exports.get_all_it = function (prefix)
{
    return list_entries_it(list_by_prefix[prefix]);
}

const get_remote_storage = make_once(init);

/*
 * EXPORTS_START
 * EXPORT get_remote_storage
 * EXPORTS_END
 */
wise. 2017-08-23gnu: bootloader: Deprecate "device" field in favor of "target".Andy Wingo * gnu/bootloader.scm (<bootloader-configuration>): Deprecate "device" field in favor of "target" field. This is mostly a renaming but also a generalization to support UEFI targets being paths to a mounted partition instead of a device name. * gnu/system/examples/bare-bones.tmpl: * gnu/system/examples/desktop.tmpl: * gnu/system/examples/lightweight-desktop.tmpl: * gnu/system/examples/vm-image.tmpl: * gnu/system/install.scm: * gnu/tests.scm: * gnu/tests/install.scm: * gnu/tests/nfs.scm: * tests/system.scm: Adapt all invocations of bootloader-configuration. * guix/scripts/system.scm (perform-action): Rename device argument to bootloader-target. (process-action): Adapt caller. * doc/guix.texi (Proceeding with the Installation): * doc/guix.texi (Bootloader Configuration): Update documentation. 2017-07-20tests: Use 'virtual-machine' records instead of monadic procedures.Ludovic Courtès * gnu/tests/base.scm (%test-basic-os): Use 'let*' instead of 'mlet*' and 'virtual-machine' instead of 'system-qemu-image/shared-store-script'. (run-mcron-test): Likewise. (run-nss-mdns-test): Likewise. * gnu/tests/dict.scm (run-dicod-test): Likewise. * gnu/tests/mail.scm (run-opensmtpd-test): Likewise. (run-exim-test): Likewise. * gnu/tests/messaging.scm (run-xmpp-test): Likewise. * gnu/tests/networking.scm (run-inetd-test): Likewise. * gnu/tests/nfs.scm (run-nfs-test): Likewise. * gnu/tests/ssh.scm (run-ssh-test): Likewise. * gnu/tests/web.scm (run-nginx-test): Likewise. 2017-05-16bootloader: Add extlinux support.Mathieu Othacehe * gnu/bootloader.scm: New file. * gnu/bootloader/extlinux.scm: New file. * gnu/bootloader/grub.scm: New file. * gnu/local.mk: Build new files. * gnu/system.scm: Adapt to new bootloader api. * gnu/scripts/system.scm: Adapt to new bootloader api. * gnu.scm: Remove (gnu system grub) and replace by (gnu bootloader) and (gnu bootloader grub) modules. * gnu/system/grub.scm: Moved content to gnu/bootloader/grub.scm. * gnu/system/vm: Replace (gnu system grub) module by (gnu bootloader). * gnu/tests.scm: Ditto. * gnu/tests/nfs.scm: Ditto. 2016-12-15gnu: Add system test for the rpcbind-daemon service.John Darrington * gnu/tests/nfs.scm: New file. * gnu/local.mk: Add it.