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