michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: // apiPort is our port to WorkerAPI michael@0: let apiPort; michael@0: // testerPort is whatever port a test calls us on michael@0: let testerPort; michael@0: michael@0: onconnect = function(e) { michael@0: // assume this is a test connecting, but if we get michael@0: // social.initialize, we know it is our WorkerAPI michael@0: // instance connecting and we'll set apiPort michael@0: let port = e.ports[0]; michael@0: port.onmessage = function onMessage(event) { michael@0: let {topic, data} = event.data; michael@0: switch (topic) { michael@0: case "social.initialize": michael@0: apiPort = port; michael@0: break; michael@0: case "test-initialization": michael@0: testerPort = port; michael@0: port.postMessage({topic: "test-initialization-complete"}); michael@0: break; michael@0: case "test-profile": michael@0: apiPort.postMessage({topic: "social.user-profile", data: data}); michael@0: break; michael@0: case "test-ambient": michael@0: apiPort.postMessage({topic: "social.ambient-notification", data: data}); michael@0: break; michael@0: case "test.cookies-get": michael@0: apiPort.postMessage({topic: "social.cookies-get"}); michael@0: break; michael@0: case "social.cookies-get-response": michael@0: testerPort.postMessage({topic: "test.cookies-get-response", data: data}); michael@0: break; michael@0: case "test-reload-init": michael@0: apiPort.postMessage({topic: 'social.reload-worker'}); michael@0: break; michael@0: case "test-notification-create": michael@0: apiPort.postMessage({topic: 'social.notification-create', michael@0: data: data}); michael@0: testerPort.postMessage({topic: 'did-notification-create'}); michael@0: break; michael@0: case "test-indexeddb-create": michael@0: var request = indexedDB.open("workerdb", 1); michael@0: request.onerror = function(event) { michael@0: port.postMessage({topic: 'social.indexeddb-result', data: { result: "error" }}); michael@0: }; michael@0: request.onsuccess = function(event) { michael@0: // Do something with request.result! michael@0: var db = request.result; michael@0: db.close(); michael@0: port.postMessage({topic: 'social.indexeddb-result', data: { result: "ok" }}); michael@0: }; michael@0: break; michael@0: } michael@0: } michael@0: }