|
1 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
2 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 |
|
5 // apiPort is our port to WorkerAPI |
|
6 let apiPort; |
|
7 // testerPort is whatever port a test calls us on |
|
8 let testerPort; |
|
9 |
|
10 onconnect = function(e) { |
|
11 // assume this is a test connecting, but if we get |
|
12 // social.initialize, we know it is our WorkerAPI |
|
13 // instance connecting and we'll set apiPort |
|
14 let port = e.ports[0]; |
|
15 port.onmessage = function onMessage(event) { |
|
16 let {topic, data} = event.data; |
|
17 switch (topic) { |
|
18 case "social.initialize": |
|
19 apiPort = port; |
|
20 break; |
|
21 case "test-initialization": |
|
22 testerPort = port; |
|
23 port.postMessage({topic: "test-initialization-complete"}); |
|
24 break; |
|
25 case "test-profile": |
|
26 apiPort.postMessage({topic: "social.user-profile", data: data}); |
|
27 break; |
|
28 case "test-ambient": |
|
29 apiPort.postMessage({topic: "social.ambient-notification", data: data}); |
|
30 break; |
|
31 case "test.cookies-get": |
|
32 apiPort.postMessage({topic: "social.cookies-get"}); |
|
33 break; |
|
34 case "social.cookies-get-response": |
|
35 testerPort.postMessage({topic: "test.cookies-get-response", data: data}); |
|
36 break; |
|
37 case "test-reload-init": |
|
38 apiPort.postMessage({topic: 'social.reload-worker'}); |
|
39 break; |
|
40 case "test-notification-create": |
|
41 apiPort.postMessage({topic: 'social.notification-create', |
|
42 data: data}); |
|
43 testerPort.postMessage({topic: 'did-notification-create'}); |
|
44 break; |
|
45 case "test-indexeddb-create": |
|
46 var request = indexedDB.open("workerdb", 1); |
|
47 request.onerror = function(event) { |
|
48 port.postMessage({topic: 'social.indexeddb-result', data: { result: "error" }}); |
|
49 }; |
|
50 request.onsuccess = function(event) { |
|
51 // Do something with request.result! |
|
52 var db = request.result; |
|
53 db.close(); |
|
54 port.postMessage({topic: 'social.indexeddb-result', data: { result: "ok" }}); |
|
55 }; |
|
56 break; |
|
57 } |
|
58 } |
|
59 } |