summaryrefslogtreecommitdiffstats
path: root/netwerk/test/unit_ipc/test_alt-data_cross_process_wrap.js
blob: 336b9e3129d74e91217df9cb96ed05a8ba131d42 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// needs to be rooted
var cacheFlushObserver = {
  observe() {
    cacheFlushObserver = null;
    do_send_remote_message("flushed");
  },
};

// We get this from the child a bit later
var url = null;

// needs to be rooted
var cacheFlushObserver2 = {
  observe() {
    cacheFlushObserver2 = null;
    openAltChannel();
  },
};

function run_test() {
  do_get_profile();
  do_await_remote_message("flush").then(() => {
    Services.cache2
      .QueryInterface(Ci.nsICacheTesting)
      .flush(cacheFlushObserver);
  });

  do_await_remote_message("done").then(() => {
    sendCommand("URL;", load_channel);
  });

  run_test_in_child("../unit/test_alt-data_cross_process.js");
}

function load_channel(channelUrl) {
  ok(channelUrl);
  url = channelUrl; // save this to open the alt data channel later
  var chan = make_channel(channelUrl);
  var cc = chan.QueryInterface(Ci.nsICacheInfoChannel);
  cc.preferAlternativeDataType("text/binary", "", Ci.nsICacheInfoChannel.ASYNC);
  chan.asyncOpen(new ChannelListener(readTextData, null));
}

function make_channel(channelUrl, callback, ctx) {
  return NetUtil.newChannel({
    uri: channelUrl,
    loadUsingSystemPrincipal: true,
  });
}

function readTextData(request, buffer) {
  var cc = request.QueryInterface(Ci.nsICacheInfoChannel);
  // Since we are in a different process from what that generated the alt-data,
  // we should receive the original data, not processed content.
  Assert.equal(cc.alternativeDataType, "");
  Assert.equal(buffer, "response body");

  // Now let's generate some alt-data in the parent, and make sure we can get it
  var altContent = "altContentParentGenerated";
  executeSoon(() => {
    var os = cc.openAlternativeOutputStream(
      "text/parent-binary",
      altContent.length
    );
    os.write(altContent, altContent.length);
    os.close();

    executeSoon(() => {
      Services.cache2
        .QueryInterface(Ci.nsICacheTesting)
        .flush(cacheFlushObserver2);
    });
  });
}

function openAltChannel() {
  var chan = make_channel(url);
  var cc = chan.QueryInterface(Ci.nsICacheInfoChannel);
  cc.preferAlternativeDataType(
    "text/parent-binary",
    "",
    Ci.nsICacheInfoChannel.ASYNC
  );
  chan.asyncOpen(new ChannelListener(readAltData, null));
}

function readAltData(request, buffer) {
  var cc = request.QueryInterface(Ci.nsICacheInfoChannel);

  // This was generated in the parent, so it's OK to get it.
  Assert.equal(buffer, "altContentParentGenerated");
  Assert.equal(cc.alternativeDataType, "text/parent-binary");

  // FINISH
  do_send_remote_message("finish");
}