child_channel_id.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. /**
  2. * Send HTTP requests and notify the parent about their channelId
  3. */
  4. Cu.import("resource://gre/modules/NetUtil.jsm");
  5. let shouldQuit = false;
  6. function run_test() {
  7. // keep the event loop busy and the test alive until a "finish" command
  8. // is issued by parent
  9. do_timeout(100, function keepAlive() {
  10. if (!shouldQuit) {
  11. do_timeout(100, keepAlive);
  12. }
  13. });
  14. }
  15. function makeRequest(uri) {
  16. let requestChannel = NetUtil.newChannel({uri, loadUsingSystemPrincipal: true});
  17. requestChannel.asyncOpen2(new ChannelListener(checkResponse, requestChannel));
  18. requestChannel.QueryInterface(Ci.nsIHttpChannel);
  19. dump(`Child opened request: ${uri}, channelId=${requestChannel.channelId}\n`);
  20. }
  21. function checkResponse(request, buffer, requestChannel) {
  22. // notify the parent process about the original request channel
  23. requestChannel.QueryInterface(Ci.nsIHttpChannel);
  24. do_send_remote_message(`request:${requestChannel.channelId}`);
  25. // the response channel can be different (if it was redirected)
  26. let responseChannel = request.QueryInterface(Ci.nsIHttpChannel);
  27. let uri = responseChannel.URI.spec;
  28. let origUri = responseChannel.originalURI.spec;
  29. let id = responseChannel.channelId;
  30. dump(`Child got response to: ${uri} (orig=${origUri}), channelId=${id}\n`);
  31. // notify the parent process about this channel's ID
  32. do_send_remote_message(`response:${id}`);
  33. }
  34. function finish() {
  35. shouldQuit = true;
  36. }