image_load_helpers.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * Helper structures to track callbacks from image and channel loads.
  3. */
  4. // START_REQUEST and STOP_REQUEST are used by ChannelListener, and
  5. // stored in ChannelListener.requestStatus.
  6. const START_REQUEST = 0x01;
  7. const STOP_REQUEST = 0x02;
  8. const DATA_AVAILABLE = 0x04;
  9. // One bit per callback that imageListener below implements. Stored in
  10. // ImageListener.state.
  11. const SIZE_AVAILABLE = 0x01;
  12. const FRAME_UPDATE = 0x02;
  13. const FRAME_COMPLETE = 0x04;
  14. const LOAD_COMPLETE = 0x08;
  15. const DECODE_COMPLETE = 0x10;
  16. // Safebrowsing requires that the profile dir is set.
  17. do_get_profile();
  18. // An implementation of imgIScriptedNotificationObserver with the ability to
  19. // call specified functions on onStartRequest and onStopRequest.
  20. function ImageListener(start_callback, stop_callback)
  21. {
  22. this.sizeAvailable = function onSizeAvailable(aRequest)
  23. {
  24. do_check_false(this.synchronous);
  25. this.state |= SIZE_AVAILABLE;
  26. if (this.start_callback)
  27. this.start_callback(this, aRequest);
  28. }
  29. this.frameComplete = function onFrameComplete(aRequest)
  30. {
  31. do_check_false(this.synchronous);
  32. this.state |= FRAME_COMPLETE;
  33. }
  34. this.decodeComplete = function onDecodeComplete(aRequest)
  35. {
  36. do_check_false(this.synchronous);
  37. this.state |= DECODE_COMPLETE;
  38. }
  39. this.loadComplete = function onLoadcomplete(aRequest)
  40. {
  41. do_check_false(this.synchronous);
  42. this.state |= LOAD_COMPLETE;
  43. if (this.stop_callback)
  44. this.stop_callback(this, aRequest);
  45. }
  46. this.frameUpdate = function onFrameUpdate(aRequest)
  47. {
  48. }
  49. this.isAnimated = function onIsAnimated()
  50. {
  51. }
  52. // Initialize the synchronous flag to true to start. This must be set to
  53. // false before exiting to the event loop!
  54. this.synchronous = true;
  55. // A function to call when onStartRequest is called.
  56. this.start_callback = start_callback;
  57. // A function to call when onStopRequest is called.
  58. this.stop_callback = stop_callback;
  59. // The image load/decode state.
  60. // A bitfield that tracks which callbacks have been called. Takes the bits
  61. // defined above.
  62. this.state = 0;
  63. }
  64. function NS_FAILED(val)
  65. {
  66. return !!(val & 0x80000000);
  67. }
  68. function ChannelListener()
  69. {
  70. this.onStartRequest = function onStartRequest(aRequest, aContext)
  71. {
  72. if (this.outputListener)
  73. this.outputListener.onStartRequest(aRequest, aContext);
  74. this.requestStatus |= START_REQUEST;
  75. }
  76. this.onDataAvailable = function onDataAvailable(aRequest, aContext, aInputStream, aOffset, aCount)
  77. {
  78. if (this.outputListener)
  79. this.outputListener.onDataAvailable(aRequest, aContext, aInputStream, aOffset, aCount);
  80. this.requestStatus |= DATA_AVAILABLE;
  81. }
  82. this.onStopRequest = function onStopRequest(aRequest, aContext, aStatusCode)
  83. {
  84. if (this.outputListener)
  85. this.outputListener.onStopRequest(aRequest, aContext, aStatusCode);
  86. // If we failed (or were canceled - failure is implied if canceled),
  87. // there's no use tracking our state, since it's meaningless.
  88. if (NS_FAILED(aStatusCode))
  89. this.requestStatus = 0;
  90. else
  91. this.requestStatus |= STOP_REQUEST;
  92. }
  93. // A listener to pass the notifications we get to.
  94. this.outputListener = null;
  95. // The request's status. A bitfield that holds one or both of START_REQUEST
  96. // and STOP_REQUEST, according to which callbacks have been called on the
  97. // associated request.
  98. this.requestStatus = 0;
  99. }