onlinehandler.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. // Copyright 2008 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /**
  15. * @fileoverview This event handler will dispatch events when
  16. * {@code navigator.onLine} changes. HTML5 defines two events, online and
  17. * offline that is fired on the window. As of today 3 browsers support these
  18. * events: Firefox 3 (Gecko 1.9), Opera 9.5, and IE8. If we have any of these
  19. * we listen to the 'online' and 'offline' events on the current window
  20. * object. Otherwise we poll the navigator.onLine property to detect changes.
  21. *
  22. * Note that this class only reflects what the browser tells us and this usually
  23. * only reflects changes to the File -> Work Offline menu item.
  24. *
  25. * @author arv@google.com (Erik Arvidsson)
  26. * @see ../demos/onlinehandler.html
  27. */
  28. // TODO(arv): We should probably implement some kind of polling service and/or
  29. // a poll for changes event handler that can be used to fire events when a state
  30. // changes.
  31. goog.provide('goog.events.OnlineHandler');
  32. goog.provide('goog.events.OnlineHandler.EventType');
  33. goog.require('goog.Timer');
  34. goog.require('goog.events.BrowserFeature');
  35. goog.require('goog.events.EventHandler');
  36. goog.require('goog.events.EventTarget');
  37. goog.require('goog.events.EventType');
  38. goog.require('goog.net.NetworkStatusMonitor');
  39. /**
  40. * Basic object for detecting whether the online state changes.
  41. * @constructor
  42. * @extends {goog.events.EventTarget}
  43. * @implements {goog.net.NetworkStatusMonitor}
  44. */
  45. goog.events.OnlineHandler = function() {
  46. goog.events.OnlineHandler.base(this, 'constructor');
  47. /**
  48. * @private {goog.events.EventHandler<!goog.events.OnlineHandler>}
  49. */
  50. this.eventHandler_ = new goog.events.EventHandler(this);
  51. // Some browsers do not support navigator.onLine and therefore we don't
  52. // bother setting up events or timers.
  53. if (!goog.events.BrowserFeature.HAS_NAVIGATOR_ONLINE_PROPERTY) {
  54. return;
  55. }
  56. if (goog.events.BrowserFeature.HAS_HTML5_NETWORK_EVENT_SUPPORT) {
  57. var target = goog.events.BrowserFeature.HTML5_NETWORK_EVENTS_FIRE_ON_BODY ?
  58. document.body :
  59. window;
  60. this.eventHandler_.listen(
  61. target, [goog.events.EventType.ONLINE, goog.events.EventType.OFFLINE],
  62. this.handleChange_);
  63. } else {
  64. this.online_ = this.isOnline();
  65. this.timer_ = new goog.Timer(goog.events.OnlineHandler.POLL_INTERVAL_);
  66. this.eventHandler_.listen(this.timer_, goog.Timer.TICK, this.handleTick_);
  67. this.timer_.start();
  68. }
  69. };
  70. goog.inherits(goog.events.OnlineHandler, goog.events.EventTarget);
  71. /**
  72. * Enum for the events dispatched by the OnlineHandler.
  73. * @enum {string}
  74. * @deprecated Use goog.net.NetworkStatusMonitor.EventType instead.
  75. */
  76. goog.events.OnlineHandler.EventType = goog.net.NetworkStatusMonitor.EventType;
  77. /**
  78. * The time to wait before checking the {@code navigator.onLine} again.
  79. * @type {number}
  80. * @private
  81. */
  82. goog.events.OnlineHandler.POLL_INTERVAL_ = 250;
  83. /**
  84. * Stores the last value of the online state so we can detect if this has
  85. * changed.
  86. * @type {boolean}
  87. * @private
  88. */
  89. goog.events.OnlineHandler.prototype.online_;
  90. /**
  91. * The timer object used to poll the online state.
  92. * @type {goog.Timer}
  93. * @private
  94. */
  95. goog.events.OnlineHandler.prototype.timer_;
  96. /** @override */
  97. goog.events.OnlineHandler.prototype.isOnline = function() {
  98. return goog.events.BrowserFeature.HAS_NAVIGATOR_ONLINE_PROPERTY ?
  99. navigator.onLine :
  100. true;
  101. };
  102. /**
  103. * Called every time the timer ticks to see if the state has changed and when
  104. * the online state changes the method handleChange_ is called.
  105. * @private
  106. */
  107. goog.events.OnlineHandler.prototype.handleTick_ = function() {
  108. var online = this.isOnline();
  109. if (online != this.online_) {
  110. this.online_ = online;
  111. this.handleChange_();
  112. }
  113. };
  114. /**
  115. * Called when the online state changes. This dispatches the
  116. * {@code ONLINE} and {@code OFFLINE} events respectively.
  117. * @private
  118. */
  119. goog.events.OnlineHandler.prototype.handleChange_ = function() {
  120. var type = this.isOnline() ? goog.net.NetworkStatusMonitor.EventType.ONLINE :
  121. goog.net.NetworkStatusMonitor.EventType.OFFLINE;
  122. this.dispatchEvent(type);
  123. };
  124. /** @override */
  125. goog.events.OnlineHandler.prototype.disposeInternal = function() {
  126. goog.events.OnlineHandler.base(this, 'disposeInternal');
  127. this.eventHandler_.dispose();
  128. this.eventHandler_ = null;
  129. if (this.timer_) {
  130. this.timer_.dispose();
  131. this.timer_ = null;
  132. }
  133. };