Progress.jsm 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. "use strict";
  5. const EXPORTED_SYMBOLS = ["S4EProgressService"];
  6. const CI = Components.interfaces;
  7. const CU = Components.utils;
  8. CU.import("resource://gre/modules/XPCOMUtils.jsm");
  9. function S4EProgressService(gBrowser, service, getters, statusService) {
  10. this._gBrowser = gBrowser;
  11. this._service = service;
  12. this._getters = getters;
  13. this._statusService = statusService;
  14. this._gBrowser.addProgressListener(this);
  15. }
  16. S4EProgressService.prototype =
  17. {
  18. _gBrowser: null,
  19. _service: null,
  20. _getters: null,
  21. _statusService: null,
  22. _busyUI: false,
  23. set value(val)
  24. {
  25. let toolbar_progress = this._getters.toolbarProgress;
  26. if(toolbar_progress)
  27. {
  28. toolbar_progress.value = val;
  29. }
  30. let throbber_progress = this._getters.throbberProgress;
  31. if(throbber_progress)
  32. {
  33. if(val)
  34. {
  35. throbber_progress.setAttribute("progress", val);
  36. }
  37. else
  38. {
  39. throbber_progress.removeAttribute("progress");
  40. }
  41. }
  42. },
  43. set collapsed(val)
  44. {
  45. let toolbar_progress = this._getters.toolbarProgress;
  46. if(toolbar_progress)
  47. {
  48. toolbar_progress.collapsed = val;
  49. }
  50. let throbber_progress = this._getters.throbberProgress;
  51. if(throbber_progress)
  52. {
  53. if(val)
  54. {
  55. throbber_progress.removeAttribute("busy");
  56. }
  57. else
  58. {
  59. throbber_progress.setAttribute("busy", true);
  60. }
  61. }
  62. },
  63. destroy: function()
  64. {
  65. this._gBrowser.removeProgressListener(this);
  66. ["_gBrowser", "_service", "_getters", "_statusService"].forEach(function(prop)
  67. {
  68. delete this[prop];
  69. }, this);
  70. },
  71. onStatusChange: function(aWebProgress, aRequest, aStatus, aMessage)
  72. {
  73. this._statusService.setNetworkStatus(aMessage, this._busyUI);
  74. },
  75. onStateChange: function(aWebProgress, aRequest, aStateFlags, aStatus)
  76. {
  77. let nsIWPL = CI.nsIWebProgressListener;
  78. if(!this._busyUI
  79. && aStateFlags & nsIWPL.STATE_START
  80. && aStateFlags & nsIWPL.STATE_IS_NETWORK
  81. && !(aStateFlags & nsIWPL.STATE_RESTORING))
  82. {
  83. this._busyUI = true;
  84. this.value = 0;
  85. this.collapsed = false;
  86. }
  87. else if(aStateFlags & nsIWPL.STATE_STOP)
  88. {
  89. if(aRequest)
  90. {
  91. let msg = "";
  92. let location;
  93. if(aRequest instanceof CI.nsIChannel || "URI" in aRequest)
  94. {
  95. location = aRequest.URI;
  96. if(location.spec != "about:blank")
  97. {
  98. switch (aStatus)
  99. {
  100. case Components.results.NS_BINDING_ABORTED:
  101. msg = this._getters.strings.getString("nv_stopped");
  102. break;
  103. case Components.results.NS_ERROR_NET_TIMEOUT:
  104. msg = this._getters.strings.getString("nv_timeout");
  105. break;
  106. }
  107. }
  108. }
  109. if(!msg && (!location || location.spec != "about:blank"))
  110. {
  111. msg = this._getters.strings.getString("nv_done");
  112. }
  113. this._statusService.setDefaultStatus(msg);
  114. this._statusService.setNetworkStatus("", this._busyUI);
  115. }
  116. if(this._busyUI)
  117. {
  118. this._busyUI = false;
  119. this.collapsed = true;
  120. this.value = 0;
  121. }
  122. }
  123. },
  124. onUpdateCurrentBrowser: function(aStateFlags, aStatus, aMessage, aTotalProgress)
  125. {
  126. let nsIWPL = CI.nsIWebProgressListener;
  127. let loadingDone = aStateFlags & nsIWPL.STATE_STOP;
  128. this.onStateChange(
  129. this._gBrowser.webProgress,
  130. { URI: this._gBrowser.currentURI },
  131. ((loadingDone ? nsIWPL.STATE_STOP : nsIWPL.STATE_START) | (aStateFlags & nsIWPL.STATE_IS_NETWORK)),
  132. aStatus
  133. );
  134. if(!loadingDone)
  135. {
  136. this.onProgressChange(this._gBrowser.webProgress, null, 0, 0, aTotalProgress, 1);
  137. this.onStatusChange(this._gBrowser.webProgress, null, 0, aMessage);
  138. }
  139. },
  140. onProgressChange: function(aWebProgress, aRequest, aCurSelfProgress, aMaxSelfProgress, aCurTotalProgress, aMaxTotalProgress)
  141. {
  142. if (aMaxTotalProgress > 0 && this._busyUI)
  143. {
  144. // This is highly optimized. Don't touch this code unless
  145. // you are intimately familiar with the cost of setting
  146. // attrs on XUL elements. -- hyatt
  147. let percentage = (aCurTotalProgress * 100) / aMaxTotalProgress;
  148. this.value = percentage;
  149. }
  150. },
  151. onProgressChange64: function(aWebProgress, aRequest, aCurSelfProgress, aMaxSelfProgress, aCurTotalProgress, aMaxTotalProgress)
  152. {
  153. return this.onProgressChange(aWebProgress, aRequest, aCurSelfProgress, aMaxSelfProgress, aCurTotalProgress, aMaxTotalProgress);
  154. },
  155. QueryInterface: XPCOMUtils.generateQI([ CI.nsIWebProgressListener, CI.nsIWebProgressListener2 ])
  156. };