| OLD | NEW |
| (Empty) | |
| 1 // Helper method that waits for a {success: <boolean>, result: any} reply on |
| 2 // a port and returns a promise that resolves (if success is true) or rejects |
| 3 // the promise with the value of the result attribute. |
| 4 function reply_as_promise(t, port) { |
| 5 return new Promise(function(resolve, reject) { |
| 6 var got_reply = false; |
| 7 port.onmessage = t.step_func(function(event) { |
| 8 assert_false(got_reply); |
| 9 assert_true('success' in event.data); |
| 10 assert_true('result' in event.data); |
| 11 got_reply = true; |
| 12 if (event.data.success) |
| 13 resolve(event.data.result); |
| 14 else |
| 15 reject(event.data.result); |
| 16 }); |
| 17 }); |
| 18 } |
| 19 |
| 20 // Method that behaves similarly to navigator.connect, but the actual connect |
| 21 // call is made from a cross origin iframe. |
| 22 function cross_origin_connect(t, service) { |
| 23 // |service| is a relative URL, but for this to work from the iframe it needs |
| 24 // an absolute URL. |
| 25 var target_url = location.origin + base_path() + service; |
| 26 return with_iframe( |
| 27 cross_origin + base_path() + 'resources/connect-helper.html') |
| 28 .then(function(iframe) { |
| 29 var channel = new MessageChannel(); |
| 30 iframe.contentWindow.postMessage( |
| 31 {connect: target_url, port: channel.port2}, '*', [channel.port2]); |
| 32 return reply_as_promise(t, channel.port1); |
| 33 }); |
| 34 } |
| 35 |
| 36 // Method that behaves similarly to navigator.connect, but the actual connect |
| 37 // call is made from a worker. |
| 38 function connect_from_worker(t, service) { |
| 39 // |service| is a relative URL, but for this to work from the worker it needs |
| 40 // an absolute URL. |
| 41 var target_url = location.origin + base_path() + service; |
| 42 var worker = new Worker('resources/connect-helper.js'); |
| 43 var channel = new MessageChannel(); |
| 44 worker.postMessage |
| 45 ({connect: target_url, port: channel.port2}, [channel.port2]); |
| 46 return reply_as_promise(t, channel.port1); |
| 47 } |
| OLD | NEW |