addEventListener('activate', async (e) => {
    await self.clients.claim();
});
addEventListener('message', async (e) => {
    try {
        if (e.data === 'write') {
            await writeDB();
            await self.caches.open(e.data);
            e.source.postMessage('written');
            return;
        }
        if (e.data === 'read') {
            var keys = await self.caches.keys();
            var db = await readDB();
            if (!db)
                db = null;
            var result = { cache : keys, db : db };
            e.source.postMessage(JSON.stringify(result));
            return;
        }
        if (e.data === 'set-cookie') {
            await fetch("/WebKit/service-workers/resources/set-cookie.py?name=mycookie&path=%2F");
            e.source.postMessage('cookie-is-set');
            return;
        }
        if (e.data === 'ancestorOrigins') {
            e.source.postMessage(e.source.ancestorOrigins);
            return;
        }
        e.source.postMessage('error');
    } catch (e) {
        e.source.postMessage('Got error:' + e);
    }
});

function readDB() {
    return new Promise(function(resolve, reject) {
        var openRequest = indexedDB.open('db');

        openRequest.onerror = reject;
        openRequest.onupgradeneeded = function() {
            var db = openRequest.result;
            db.createObjectStore('store');
        };

        openRequest.onsuccess = function() {
            var db = openRequest.result;
            var tx = db.transaction('store');
            var store = tx.objectStore('store');
            var getRequest = store.get('key');

            getRequest.onerror = function() {
                db.close();
                reject(getRequest.error);
            };
            getRequest.onsuccess = function() {
                db.close();
                resolve(getRequest.result);
            };
        };
    });
}

function writeDB() {
    return new Promise(function(resolve, reject) {
        var openRequest = indexedDB.open('db');

        openRequest.onerror = reject;
        openRequest.onupgradeneeded = function() {
            var db = openRequest.result;
            db.createObjectStore('store');
        };
        openRequest.onsuccess = function() {
            var db = openRequest.result;
            var tx = db.transaction('store', 'readwrite');
            var store = tx.objectStore('store');
            store.put('value', 'key');

            tx.onerror = function() {
                db.close();
                reject(tx.error);
            };
            tx.oncomplete = function() {
                db.close();
                resolve();
            };
        };
    });
}
