summaryrefslogtreecommitdiffstats
path: root/image/test/unit/image_load_helpers.js
blob: 3a97db00ca0e7d75a378ca2ac615e07efa40b948 (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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
 * Helper structures to track callbacks from image and channel loads.
 */

// START_REQUEST and STOP_REQUEST are used by ChannelListener, and
// stored in ChannelListener.requestStatus.
const START_REQUEST = 0x01;
const STOP_REQUEST = 0x02;
const DATA_AVAILABLE = 0x04;

// One bit per callback that imageListener below implements. Stored in
// ImageListener.state.
const SIZE_AVAILABLE = 0x01;
const FRAME_UPDATE = 0x02;
const FRAME_COMPLETE = 0x04;
const LOAD_COMPLETE = 0x08;
const DECODE_COMPLETE = 0x10;

// Safebrowsing requires that the profile dir is set.
do_get_profile();

// An implementation of imgIScriptedNotificationObserver with the ability to
// call specified functions on onStartRequest and onStopRequest.
function ImageListener(start_callback, stop_callback) {
  this.sizeAvailable = function onSizeAvailable(aRequest) {
    Assert.ok(!this.synchronous);

    this.state |= SIZE_AVAILABLE;

    if (this.start_callback) {
      this.start_callback(this, aRequest);
    }
  };
  this.frameComplete = function onFrameComplete() {
    Assert.ok(!this.synchronous);

    this.state |= FRAME_COMPLETE;
  };
  this.decodeComplete = function onDecodeComplete() {
    Assert.ok(!this.synchronous);

    this.state |= DECODE_COMPLETE;
  };
  this.loadComplete = function onLoadcomplete(aRequest) {
    Assert.ok(!this.synchronous);

    this.state |= LOAD_COMPLETE;

    if (this.stop_callback) {
      this.stop_callback(this, aRequest);
    }
  };
  this.frameUpdate = function onFrameUpdate() {};
  this.isAnimated = function onIsAnimated() {};

  // Initialize the synchronous flag to true to start. This must be set to
  // false before exiting to the event loop!
  this.synchronous = true;

  // A function to call when onStartRequest is called.
  this.start_callback = start_callback;

  // A function to call when onStopRequest is called.
  this.stop_callback = stop_callback;

  // The image load/decode state.
  // A bitfield that tracks which callbacks have been called. Takes the bits
  // defined above.
  this.state = 0;
}

function NS_FAILED(val) {
  return !!(val & 0x80000000);
}

function ChannelListener() {
  this.onStartRequest = function onStartRequest(aRequest) {
    if (this.outputListener) {
      this.outputListener.onStartRequest(aRequest);
    }

    this.requestStatus |= START_REQUEST;
  };

  this.onDataAvailable = function onDataAvailable(
    aRequest,
    aInputStream,
    aOffset,
    aCount
  ) {
    if (this.outputListener) {
      this.outputListener.onDataAvailable(
        aRequest,
        aInputStream,
        aOffset,
        aCount
      );
    }

    this.requestStatus |= DATA_AVAILABLE;
  };

  this.onStopRequest = function onStopRequest(aRequest, aStatusCode) {
    if (this.outputListener) {
      this.outputListener.onStopRequest(aRequest, aStatusCode);
    }

    // If we failed (or were canceled - failure is implied if canceled),
    // there's no use tracking our state, since it's meaningless.
    if (NS_FAILED(aStatusCode)) {
      this.requestStatus = 0;
    } else {
      this.requestStatus |= STOP_REQUEST;
    }
  };

  // A listener to pass the notifications we get to.
  this.outputListener = null;

  // The request's status. A bitfield that holds one or both of START_REQUEST
  // and STOP_REQUEST, according to which callbacks have been called on the
  // associated request.
  this.requestStatus = 0;
}