Bug 1568860 - Part 2: Make getAllFonts fission compatible. r=ochameau
[gecko.git] / image / test / unit / image_load_helpers.js
blob6d1e605bf52895dd39774797bd3c0f7631712763
1 /*
2  * Helper structures to track callbacks from image and channel loads.
3  */
5 // START_REQUEST and STOP_REQUEST are used by ChannelListener, and
6 // stored in ChannelListener.requestStatus.
7 const START_REQUEST = 0x01;
8 const STOP_REQUEST = 0x02;
9 const DATA_AVAILABLE = 0x04;
11 // One bit per callback that imageListener below implements. Stored in
12 // ImageListener.state.
13 const SIZE_AVAILABLE = 0x01;
14 const FRAME_UPDATE = 0x02;
15 const FRAME_COMPLETE = 0x04;
16 const LOAD_COMPLETE = 0x08;
17 const DECODE_COMPLETE = 0x10;
19 // Safebrowsing requires that the profile dir is set.
20 do_get_profile();
22 // An implementation of imgIScriptedNotificationObserver with the ability to
23 // call specified functions on onStartRequest and onStopRequest.
24 function ImageListener(start_callback, stop_callback) {
25   this.sizeAvailable = function onSizeAvailable(aRequest) {
26     Assert.ok(!this.synchronous);
28     this.state |= SIZE_AVAILABLE;
30     if (this.start_callback) {
31       this.start_callback(this, aRequest);
32     }
33   };
34   this.frameComplete = function onFrameComplete(aRequest) {
35     Assert.ok(!this.synchronous);
37     this.state |= FRAME_COMPLETE;
38   };
39   this.decodeComplete = function onDecodeComplete(aRequest) {
40     Assert.ok(!this.synchronous);
42     this.state |= DECODE_COMPLETE;
43   };
44   this.loadComplete = function onLoadcomplete(aRequest) {
45     Assert.ok(!this.synchronous);
47     this.state |= LOAD_COMPLETE;
49     if (this.stop_callback) {
50       this.stop_callback(this, aRequest);
51     }
52   };
53   this.frameUpdate = function onFrameUpdate(aRequest) {};
54   this.isAnimated = function onIsAnimated() {};
56   // Initialize the synchronous flag to true to start. This must be set to
57   // false before exiting to the event loop!
58   this.synchronous = true;
60   // A function to call when onStartRequest is called.
61   this.start_callback = start_callback;
63   // A function to call when onStopRequest is called.
64   this.stop_callback = stop_callback;
66   // The image load/decode state.
67   // A bitfield that tracks which callbacks have been called. Takes the bits
68   // defined above.
69   this.state = 0;
72 function NS_FAILED(val) {
73   return !!(val & 0x80000000);
76 function ChannelListener() {
77   this.onStartRequest = function onStartRequest(aRequest) {
78     if (this.outputListener) {
79       this.outputListener.onStartRequest(aRequest);
80     }
82     this.requestStatus |= START_REQUEST;
83   };
85   this.onDataAvailable = function onDataAvailable(
86     aRequest,
87     aInputStream,
88     aOffset,
89     aCount
90   ) {
91     if (this.outputListener) {
92       this.outputListener.onDataAvailable(
93         aRequest,
94         aInputStream,
95         aOffset,
96         aCount
97       );
98     }
100     this.requestStatus |= DATA_AVAILABLE;
101   };
103   this.onStopRequest = function onStopRequest(aRequest, aStatusCode) {
104     if (this.outputListener) {
105       this.outputListener.onStopRequest(aRequest, aStatusCode);
106     }
108     // If we failed (or were canceled - failure is implied if canceled),
109     // there's no use tracking our state, since it's meaningless.
110     if (NS_FAILED(aStatusCode)) {
111       this.requestStatus = 0;
112     } else {
113       this.requestStatus |= STOP_REQUEST;
114     }
115   };
117   // A listener to pass the notifications we get to.
118   this.outputListener = null;
120   // The request's status. A bitfield that holds one or both of START_REQUEST
121   // and STOP_REQUEST, according to which callbacks have been called on the
122   // associated request.
123   this.requestStatus = 0;