| 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 |
/** |
| 16 |
* @fileoverview This event handler will dispatch events when |
| 17 |
* {@code navigator.onLine} changes. HTML5 defines two events, online and |
| 18 |
* offline that is fired on the window. As of today 3 browsers support these |
| 19 |
* events: Firefox 3 (Gecko 1.9), Opera 9.5, and IE8. If we have any of these |
| 20 |
* we listen to the 'online' and 'offline' events on the current window |
| 21 |
* object. Otherwise we poll the navigator.onLine property to detect changes. |
| 22 |
* |
| 23 |
* Note that this class only reflects what the browser tells us and this usually |
| 24 |
* only reflects changes to the File -> Work Offline menu item. |
| 25 |
* |
| 26 |
* @author [email protected] (Erik Arvidsson) |
| 27 |
* @see ../demos/onlinehandler.html |
| 28 |
*/ |
| 29 |
|
| 30 |
// TODO(arv): We should probably implement some kind of polling service and/or |
| 31 |
// a poll for changes event handler that can be used to fire events when a state |
| 32 |
// changes. |
| 33 |
|
| 34 |
goog.provide('goog.events.OnlineHandler'); |
| 35 |
goog.provide('goog.events.OnlineHandler.EventType'); |
| 36 |
|
| 37 |
goog.require('goog.Timer'); |
| 38 |
goog.require('goog.events.BrowserFeature'); |
| 39 |
goog.require('goog.events.EventHandler'); |
| 40 |
goog.require('goog.events.EventTarget'); |
| 41 |
goog.require('goog.events.EventType'); |
| 42 |
goog.require('goog.net.NetworkStatusMonitor'); |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
/** |
| 47 |
* Basic object for detecting whether the online state changes. |
| 48 |
* @constructor |
| 49 |
* @extends {goog.events.EventTarget} |
| 50 |
* @implements {goog.net.NetworkStatusMonitor} |
| 51 |
*/ |
| 52 |
goog.events.OnlineHandler = function() { |
| 53 |
goog.events.OnlineHandler.base(this, 'constructor'); |
| 54 |
|
| 55 |
/** |
| 56 |
* @private {goog.events.EventHandler<!goog.events.OnlineHandler>} |
| 57 |
*/ |
| 58 |
this.eventHandler_ = new goog.events.EventHandler(this); |
| 59 |
|
| 60 |
// Some browsers do not support navigator.onLine and therefore we don't |
| 61 |
// bother setting up events or timers. |
| 62 |
if (!goog.events.BrowserFeature.HAS_NAVIGATOR_ONLINE_PROPERTY) { |
| 63 |
return; |
| 64 |
} |
| 65 |
|
| 66 |
if (goog.events.BrowserFeature.HAS_HTML5_NETWORK_EVENT_SUPPORT) { |
| 67 |
var target = goog.events.BrowserFeature.HTML5_NETWORK_EVENTS_FIRE_ON_BODY ? |
| 68 |
document.body : |
| 69 |
window; |
| 70 |
this.eventHandler_.listen( |
| 71 |
target, [goog.events.EventType.ONLINE, goog.events.EventType.OFFLINE], |
| 72 |
this.handleChange_); |
| 73 |
} else { |
| 74 |
this.online_ = this.isOnline(); |
| 75 |
this.timer_ = new goog.Timer(goog.events.OnlineHandler.POLL_INTERVAL_); |
| 76 |
this.eventHandler_.listen(this.timer_, goog.Timer.TICK, this.handleTick_); |
| 77 |
this.timer_.start(); |
| 78 |
} |
| 79 |
}; |
| 80 |
goog.inherits(goog.events.OnlineHandler, goog.events.EventTarget); |
| 81 |
|
| 82 |
|
| 83 |
/** |
| 84 |
* Enum for the events dispatched by the OnlineHandler. |
| 85 |
* @enum {string} |
| 86 |
* @deprecated Use goog.net.NetworkStatusMonitor.EventType instead. |
| 87 |
*/ |
| 88 |
goog.events.OnlineHandler.EventType = goog.net.NetworkStatusMonitor.EventType; |
| 89 |
|
| 90 |
|
| 91 |
/** |
| 92 |
* The time to wait before checking the {@code navigator.onLine} again. |
| 93 |
* @type {number} |
| 94 |
* @private |
| 95 |
*/ |
| 96 |
goog.events.OnlineHandler.POLL_INTERVAL_ = 250; |
| 97 |
|
| 98 |
|
| 99 |
/** |
| 100 |
* Stores the last value of the online state so we can detect if this has |
| 101 |
* changed. |
| 102 |
* @type {boolean} |
| 103 |
* @private |
| 104 |
*/ |
| 105 |
goog.events.OnlineHandler.prototype.online_; |
| 106 |
|
| 107 |
|
| 108 |
/** |
| 109 |
* The timer object used to poll the online state. |
| 110 |
* @type {goog.Timer} |
| 111 |
* @private |
| 112 |
*/ |
| 113 |
goog.events.OnlineHandler.prototype.timer_; |
| 114 |
|
| 115 |
|
| 116 |
/** @override */ |
| 117 |
goog.events.OnlineHandler.prototype.isOnline = function() { |
| 118 |
return goog.events.BrowserFeature.HAS_NAVIGATOR_ONLINE_PROPERTY ? |
| 119 |
navigator.onLine : |
| 120 |
true; |
| 121 |
}; |
| 122 |
|
| 123 |
|
| 124 |
/** |
| 125 |
* Called every time the timer ticks to see if the state has changed and when |
| 126 |
* the online state changes the method handleChange_ is called. |
| 127 |
* @private |
| 128 |
*/ |
| 129 |
goog.events.OnlineHandler.prototype.handleTick_ = function() { |
| 130 |
var online = this.isOnline(); |
| 131 |
if (online != this.online_) { |
| 132 |
this.online_ = online; |
| 133 |
this.handleChange_(); |
| 134 |
} |
| 135 |
}; |
| 136 |
|
| 137 |
|
| 138 |
/** |
| 139 |
* Called when the online state changes. This dispatches the |
| 140 |
* {@code ONLINE} and {@code OFFLINE} events respectively. |
| 141 |
* @private |
| 142 |
*/ |
| 143 |
goog.events.OnlineHandler.prototype.handleChange_ = function() { |
| 144 |
var type = this.isOnline() ? goog.net.NetworkStatusMonitor.EventType.ONLINE : |
| 145 |
goog.net.NetworkStatusMonitor.EventType.OFFLINE; |
| 146 |
this.dispatchEvent(type); |
| 147 |
}; |
| 148 |
|
| 149 |
|
| 150 |
/** @override */ |
| 151 |
goog.events.OnlineHandler.prototype.disposeInternal = function() { |
| 152 |
goog.events.OnlineHandler.base(this, 'disposeInternal'); |
| 153 |
this.eventHandler_.dispose(); |
| 154 |
this.eventHandler_ = null; |
| 155 |
if (this.timer_) { |
| 156 |
this.timer_.dispose(); |
| 157 |
this.timer_ = null; |
| 158 |
} |
| 159 |
}; |