Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 <!DOCTYPE html> | |
| 2 <title> | |
| 3 Tests accepting and rejecting connections calling navigator.connect from | |
| 4 a cross origin iframe. | |
| 5 </title> | |
| 6 <script src="../../resources/testharness.js"></script> | |
| 7 <script src="../../resources/testharnessreport.js"></script> | |
| 8 <script src="../../resources/testharness-helpers.js"></script> | |
| 9 <script src="../serviceworker/resources/test-helpers.js"></script> | |
| 10 <script src="resources/connect-tests.js"></script> | |
| 11 <body> | |
| 12 <script> | |
| 13 var cross_origin = get_host_info().UNAUTHENTICATED_ORIGIN; | |
| 14 var sw_scope = 'resources/service-worker-scope' + window.location.pathname; | |
| 15 | |
| 16 // Method that behaves similarly to navigator.connect, but the actual connect | 1 // Method that behaves similarly to navigator.connect, but the actual connect |
| 17 // call is made from a cross origin iframe. | 2 // call is made from a cross origin iframe. |
| 18 function cross_origin_connect(t, service) { | 3 function cross_origin_connect(t, service) { |
| 19 // |service| is a relative URL, but for this to work from the iframe it needs | 4 // |service| is a relative URL, but for this to work from the iframe it needs |
| 20 // an absolute URL. | 5 // an absolute URL. |
| 21 var target_url = location.origin + base_path() + service; | 6 var target_url = location.origin + base_path() + service; |
| 22 return with_iframe( | 7 return with_iframe( |
| 23 cross_origin + base_path() + 'resources/connect-helper.html') | 8 cross_origin + base_path() + 'resources/connect-helper.html') |
| 24 .then(function(iframe) { | 9 .then(function(iframe) { |
| 25 var channel = new MessageChannel(); | 10 var channel = new MessageChannel(); |
| 26 iframe.contentWindow.postMessage( | 11 iframe.contentWindow.postMessage( |
| 27 {connect: target_url, port: channel.port2}, '*', [channel.port2]); | 12 {connect: target_url, port: channel.port2}, '*', [channel.port2]); |
| 28 return new Promise(function(resolve, reject) { | 13 return new Promise(function(resolve, reject) { |
| 29 var got_reply = false; | 14 var got_reply = false; |
| 30 channel.port1.onmessage = t.step_func(function(event) { | 15 channel.port1.onmessage = t.step_func(function(event) { |
| 31 assert_false(got_reply); | 16 assert_false(got_reply); |
| 32 assert_true('success' in event.data); | 17 assert_true('success' in event.data); |
| 33 assert_true('result' in event.data); | 18 assert_true('result' in event.data); |
| 34 got_reply = true; | 19 got_reply = true; |
| 35 if (event.data.success) | 20 if (event.data.success) |
| 36 resolve(event.data.result); | 21 resolve(event.data.result); |
| 37 else | 22 else |
| 38 reject(event.data.result); | 23 reject(event.data.result); |
| 39 }); | 24 }); |
| 40 }); | 25 }); |
| 41 }); | 26 }); |
| 42 } | 27 } |
| 43 | 28 |
| 44 run_connect_tests(cross_origin_connect); | 29 // Method that behaves similarly to navigator.connect, but the actual connect |
| 45 </script> | 30 // call is made from a worker. |
| 46 </body> | 31 function connect_from_worker(t, service) { |
| 32 // |service| is a relative URL, but for this to work from the worker it needs | |
| 33 // an absolute URL. | |
| 34 var target_url = location.origin + base_path() + service; | |
| 35 var worker = new Worker('resources/connect-helper.js'); | |
| 36 var channel = new MessageChannel(); | |
| 37 worker.postMessage | |
| 38 ({connect: target_url, port: channel.port2}, [channel.port2]); | |
| 39 return new Promise(function(resolve, reject) { | |
|
Jeffrey Yasskin
2014/12/20 00:31:51
Can you share this Promise creation with cross_ori
| |
| 40 var got_reply = false; | |
| 41 channel.port1.onmessage = t.step_func(function(event) { | |
| 42 assert_false(got_reply); | |
| 43 assert_true('success' in event.data); | |
| 44 assert_true('result' in event.data); | |
| 45 got_reply = true; | |
| 46 if (event.data.success) | |
| 47 resolve(event.data.result); | |
| 48 else | |
| 49 reject(event.data.result); | |
| 50 }); | |
| 51 }); | |
| 52 } | |
| OLD | NEW |