test_alt-data_cross_process_wrap.js (2572B)
1 // needs to be rooted 2 var cacheFlushObserver = { 3 observe() { 4 cacheFlushObserver = null; 5 do_send_remote_message("flushed"); 6 }, 7 }; 8 9 // We get this from the child a bit later 10 var url = null; 11 12 // needs to be rooted 13 var cacheFlushObserver2 = { 14 observe() { 15 cacheFlushObserver2 = null; 16 openAltChannel(); 17 }, 18 }; 19 20 function run_test() { 21 do_get_profile(); 22 do_await_remote_message("flush").then(() => { 23 Services.cache2 24 .QueryInterface(Ci.nsICacheTesting) 25 .flush(cacheFlushObserver); 26 }); 27 28 do_await_remote_message("done").then(() => { 29 sendCommand("URL;", load_channel); 30 }); 31 32 run_test_in_child("../unit/test_alt-data_cross_process.js"); 33 } 34 35 function load_channel(channelUrl) { 36 ok(channelUrl); 37 url = channelUrl; // save this to open the alt data channel later 38 var chan = make_channel(channelUrl); 39 var cc = chan.QueryInterface(Ci.nsICacheInfoChannel); 40 cc.preferAlternativeDataType("text/binary", "", Ci.nsICacheInfoChannel.ASYNC); 41 chan.asyncOpen(new ChannelListener(readTextData, null)); 42 } 43 44 function make_channel(channelUrl) { 45 return NetUtil.newChannel({ 46 uri: channelUrl, 47 loadUsingSystemPrincipal: true, 48 }); 49 } 50 51 function readTextData(request, buffer) { 52 var cc = request.QueryInterface(Ci.nsICacheInfoChannel); 53 // Since we are in a different process from what that generated the alt-data, 54 // we should receive the original data, not processed content. 55 Assert.equal(cc.alternativeDataType, ""); 56 Assert.equal(buffer, "response body"); 57 58 // Now let's generate some alt-data in the parent, and make sure we can get it 59 var altContent = "altContentParentGenerated"; 60 executeSoon(() => { 61 var os = cc.openAlternativeOutputStream( 62 "text/parent-binary", 63 altContent.length 64 ); 65 os.write(altContent, altContent.length); 66 os.close(); 67 68 executeSoon(() => { 69 Services.cache2 70 .QueryInterface(Ci.nsICacheTesting) 71 .flush(cacheFlushObserver2); 72 }); 73 }); 74 } 75 76 function openAltChannel() { 77 var chan = make_channel(url); 78 var cc = chan.QueryInterface(Ci.nsICacheInfoChannel); 79 cc.preferAlternativeDataType( 80 "text/parent-binary", 81 "", 82 Ci.nsICacheInfoChannel.ASYNC 83 ); 84 chan.asyncOpen(new ChannelListener(readAltData, null)); 85 } 86 87 function readAltData(request, buffer) { 88 var cc = request.QueryInterface(Ci.nsICacheInfoChannel); 89 90 // This was generated in the parent, so it's OK to get it. 91 Assert.equal(buffer, "altContentParentGenerated"); 92 Assert.equal(cc.alternativeDataType, "text/parent-binary"); 93 94 // FINISH 95 do_send_remote_message("finish"); 96 }