image_load_helpers.js (3395B)
1 /* 2 * Helper structures to track callbacks from image and channel loads. 3 */ 4 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; 10 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; 18 19 // Safebrowsing requires that the profile dir is set. 20 do_get_profile(); 21 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); 27 28 this.state |= SIZE_AVAILABLE; 29 30 if (this.start_callback) { 31 this.start_callback(this, aRequest); 32 } 33 }; 34 this.frameComplete = function onFrameComplete() { 35 Assert.ok(!this.synchronous); 36 37 this.state |= FRAME_COMPLETE; 38 }; 39 this.decodeComplete = function onDecodeComplete() { 40 Assert.ok(!this.synchronous); 41 42 this.state |= DECODE_COMPLETE; 43 }; 44 this.loadComplete = function onLoadcomplete(aRequest) { 45 Assert.ok(!this.synchronous); 46 47 this.state |= LOAD_COMPLETE; 48 49 if (this.stop_callback) { 50 this.stop_callback(this, aRequest); 51 } 52 }; 53 this.frameUpdate = function onFrameUpdate() {}; 54 this.isAnimated = function onIsAnimated() {}; 55 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; 59 60 // A function to call when onStartRequest is called. 61 this.start_callback = start_callback; 62 63 // A function to call when onStopRequest is called. 64 this.stop_callback = stop_callback; 65 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; 70 } 71 72 function NS_FAILED(val) { 73 return !!(val & 0x80000000); 74 } 75 76 function ChannelListener() { 77 this.onStartRequest = function onStartRequest(aRequest) { 78 if (this.outputListener) { 79 this.outputListener.onStartRequest(aRequest); 80 } 81 82 this.requestStatus |= START_REQUEST; 83 }; 84 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 } 99 100 this.requestStatus |= DATA_AVAILABLE; 101 }; 102 103 this.onStopRequest = function onStopRequest(aRequest, aStatusCode) { 104 if (this.outputListener) { 105 this.outputListener.onStopRequest(aRequest, aStatusCode); 106 } 107 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 }; 116 117 // A listener to pass the notifications we get to. 118 this.outputListener = null; 119 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; 124 }