clients-get-cross-origin.https.html (2996B)
1 <!DOCTYPE html> 2 <title>Service Worker: Clients.get across origins</title> 3 <script src="/resources/testharness.js"></script> 4 <script src="/resources/testharnessreport.js"></script> 5 <script src="/common/get-host-info.sub.js"></script> 6 <script src="resources/test-helpers.sub.js"></script> 7 <script> 8 var host_info = get_host_info(); 9 10 var scope = 'resources/clients-get-frame.html'; 11 var other_origin_iframe = host_info['HTTPS_REMOTE_ORIGIN'] + base_path() + 12 'resources/clients-get-cross-origin-frame.html'; 13 // The ID of a client from the same origin as us. 14 var my_origin_client_id; 15 // This test asserts the behavior of the Client API in cases where the client 16 // belongs to a foreign origin. It does this by creating an iframe with a 17 // foreign origin which connects to a server worker in the current origin. 18 promise_test(function(t) { 19 return service_worker_unregister_and_register( 20 t, 'resources/clients-get-worker.js', scope) 21 .then(function(registration) { 22 add_completion_callback(function() { registration.unregister(); }); 23 return wait_for_state(t, registration.installing, 'activated'); 24 }) 25 .then(function() { 26 return with_iframe(scope); 27 }) 28 // Create a same-origin client and use it to populate |my_origin_client_id|. 29 .then(function(frame1) { 30 add_completion_callback(function() { frame1.remove(); }); 31 return new Promise(function(resolve, reject) { 32 function get_client_id(e) { 33 window.removeEventListener('message', get_client_id); 34 resolve(e.data.clientId); 35 } 36 window.addEventListener('message', get_client_id, false); 37 }); 38 }) 39 // Create a cross-origin client. We'll communicate with this client to 40 // test the cross-origin service worker's behavior. 41 .then(function(client_id) { 42 my_origin_client_id = client_id; 43 return with_iframe(other_origin_iframe); 44 }) 45 // Post the 'getClientId' message to the cross-origin client. The client 46 // will then ask its service worker to look up |my_origin_client_id| via 47 // Clients#get. Since this client ID is for a different origin, we expect 48 // the client to not be found. 49 .then(function(frame2) { 50 add_completion_callback(function() { frame2.remove(); }); 51 52 frame2.contentWindow.postMessage( 53 {clientId: my_origin_client_id, type: 'getClientId'}, 54 host_info['HTTPS_REMOTE_ORIGIN'] 55 ); 56 57 return new Promise(function(resolve) { 58 window.addEventListener('message', function(e) { 59 if (e.data && e.data.type === 'clientId') { 60 resolve(e.data.value); 61 } 62 }); 63 }); 64 }) 65 .then(function(client_id) { 66 assert_equals(client_id, undefined, 'iframe client ID'); 67 }); 68 }, 'Test Clients.get() cross origin'); 69 </script>