1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556
|
// Copyright 2014 The Closure Library Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
goog.provide('goog.labs.pubsub.BroadcastPubSub');
goog.require('goog.Disposable');
goog.require('goog.Timer');
goog.require('goog.array');
goog.require('goog.async.run');
goog.require('goog.events.EventHandler');
goog.require('goog.events.EventType');
goog.require('goog.log');
goog.require('goog.math');
goog.require('goog.pubsub.PubSub');
goog.require('goog.storage.Storage');
goog.require('goog.storage.mechanism.HTML5LocalStorage');
goog.require('goog.string');
goog.require('goog.userAgent');
/**
* Topic-based publish/subscribe messaging implementation that provides
* communication between browsing contexts that share the same origin.
*
* Wrapper around PubSub that utilizes localStorage to broadcast publications to
* all browser windows with the same origin as the publishing context. This
* allows for topic-based publish/subscribe implementation of strings shared by
* all browser contexts that share the same origin.
*
* Delivery is guaranteed on all browsers except IE8 where topics expire after a
* timeout. Publishing of a topic within a callback function provides no
* guarantee on ordering in that there is a possibility that separate origin
* contexts may see topics in a different order.
*
* This class is not secure and in certain cases (e.g., a browser crash) data
* that is published can persist in localStorage indefinitely. Do not use this
* class to communicate private or confidential information.
*
* On IE8, localStorage is shared by the http and https origins. An attacker
* could possibly leverage this to publish to the secure origin.
*
* goog.labs.pubsub.BroadcastPubSub wraps an instance of PubSub rather than
* subclassing because the base PubSub class allows publishing of arbitrary
* objects.
*
* Special handling is done for the IE8 browsers. See the IE8_EVENTS_KEY_
* constant and the {@code publish} function for more information.
*
*
* @constructor @struct @extends {goog.Disposable}
*/
goog.labs.pubsub.BroadcastPubSub = function() {
goog.labs.pubsub.BroadcastPubSub.base(this, 'constructor');
goog.labs.pubsub.BroadcastPubSub.instances_.push(this);
/** @private @const */
this.pubSub_ = new goog.pubsub.PubSub();
this.registerDisposable(this.pubSub_);
/** @private @const */
this.handler_ = new goog.events.EventHandler(this);
this.registerDisposable(this.handler_);
/** @private @const */
this.logger_ = goog.log.getLogger('goog.labs.pubsub.BroadcastPubSub');
/** @private @const */
this.mechanism_ = new goog.storage.mechanism.HTML5LocalStorage();
/** @private {goog.storage.Storage} */
this.storage_ = null;
/** @private {Object<string, number>} */
this.ie8LastEventTimes_ = null;
/** @private {number} */
this.ie8StartupTimestamp_ = goog.now() - 1;
if (this.mechanism_.isAvailable()) {
this.storage_ = new goog.storage.Storage(this.mechanism_);
var target = window;
if (goog.labs.pubsub.BroadcastPubSub.IS_IE8_) {
this.ie8LastEventTimes_ = {};
target = document;
}
this.handler_.listen(
target, goog.events.EventType.STORAGE, this.handleStorageEvent_);
}
};
goog.inherits(goog.labs.pubsub.BroadcastPubSub, goog.Disposable);
/** @private @const {!Array<!goog.labs.pubsub.BroadcastPubSub>} */
goog.labs.pubsub.BroadcastPubSub.instances_ = [];
/**
* SitePubSub namespace for localStorage.
* @private @const
*/
goog.labs.pubsub.BroadcastPubSub.STORAGE_KEY_ = '_closure_bps';
/**
* Handle the storage event and possibly dispatch topics.
* @param {!goog.events.BrowserEvent} e Event object.
* @private
*/
goog.labs.pubsub.BroadcastPubSub.prototype.handleStorageEvent_ = function(e) {
if (goog.labs.pubsub.BroadcastPubSub.IS_IE8_) {
// Even though we have the event, IE8 doesn't update our localStorage until
// after we handle the actual event.
goog.async.run(this.handleIe8StorageEvent_, this);
return;
}
var browserEvent = e.getBrowserEvent();
if (browserEvent.key != goog.labs.pubsub.BroadcastPubSub.STORAGE_KEY_) {
return;
}
var data = JSON.parse(browserEvent.newValue);
var args = goog.isObject(data) && data['args'];
if (goog.isArray(args) && goog.array.every(args, goog.isString)) {
this.dispatch_(args);
} else {
goog.log.warning(this.logger_, 'storage event contained invalid arguments');
}
};
/**
* Dispatches args on the internal pubsub queue.
* @param {!Array<string>} args The arguments to publish.
* @private
*/
goog.labs.pubsub.BroadcastPubSub.prototype.dispatch_ = function(args) {
goog.pubsub.PubSub.prototype.publish.apply(this.pubSub_, args);
};
/**
* Publishes a message to a topic. Remote subscriptions in other tabs/windows
* are dispatched via local storage events. Local subscriptions are called
* asynchronously via Timer event in order to simulate remote behavior locally.
* @param {string} topic Topic to publish to.
* @param {...string} var_args String arguments that are applied to each
* subscription function.
*/
goog.labs.pubsub.BroadcastPubSub.prototype.publish = function(topic, var_args) {
var args = goog.array.toArray(arguments);
// Dispatch to localStorage.
if (this.storage_) {
// Update topics to use the optional prefix.
var now = goog.now();
var data = {'args': args, 'timestamp': now};
if (!goog.labs.pubsub.BroadcastPubSub.IS_IE8_) {
// Generated events will contain all the data in modern browsers.
this.storage_.set(goog.labs.pubsub.BroadcastPubSub.STORAGE_KEY_, data);
this.storage_.remove(goog.labs.pubsub.BroadcastPubSub.STORAGE_KEY_);
} else {
// With IE8 we need to manage our own events queue.
var events = null;
try {
events =
this.storage_.get(goog.labs.pubsub.BroadcastPubSub.IE8_EVENTS_KEY_);
} catch (ex) {
goog.log.error(
this.logger_, 'publish encountered invalid event queue at ' +
goog.labs.pubsub.BroadcastPubSub.IE8_EVENTS_KEY_);
}
if (!goog.isArray(events)) {
events = [];
}
// Avoid a race condition where we're publishing in the same
// millisecond that another event that may be getting
// processed. In short, we try go guarantee that whatever event
// we put on the event queue has a timestamp that is older than
// any other timestamp in the queue.
var lastEvent = events[events.length - 1];
var lastTimestamp =
lastEvent && lastEvent['timestamp'] || this.ie8StartupTimestamp_;
if (lastTimestamp >= now) {
now = lastTimestamp +
goog.labs.pubsub.BroadcastPubSub.IE8_TIMESTAMP_UNIQUE_OFFSET_MS_;
data['timestamp'] = now;
}
events.push(data);
this.storage_.set(
goog.labs.pubsub.BroadcastPubSub.IE8_EVENTS_KEY_, events);
// Cleanup this event in IE8_EVENT_LIFETIME_MS_ milliseconds.
goog.Timer.callOnce(
goog.bind(this.cleanupIe8StorageEvents_, this, now),
goog.labs.pubsub.BroadcastPubSub.IE8_EVENT_LIFETIME_MS_);
}
}
// W3C spec is to not dispatch the storage event to the same window that
// modified localStorage. For conforming browsers we have to manually dispatch
// the publish event to subscriptions on instances of BroadcastPubSub in the
// current window.
if (!goog.userAgent.IE) {
// Dispatch the publish event to local instances asynchronously to fix some
// quirks with timings. The result is that all subscriptions are dispatched
// before any future publishes are processed. The effect is that
// subscriptions in the same window are dispatched as if they are the result
// of a publish from another tab.
goog.array.forEach(
goog.labs.pubsub.BroadcastPubSub.instances_, function(instance) {
goog.async.run(goog.bind(instance.dispatch_, instance, args));
});
}
};
/**
* Unsubscribes a function from a topic. Only deletes the first match found.
* Returns a Boolean indicating whether a subscription was removed.
* @param {string} topic Topic to unsubscribe from.
* @param {Function} fn Function to unsubscribe.
* @param {Object=} opt_context Object in whose context the function was to be
* called (the global scope if none).
* @return {boolean} Whether a matching subscription was removed.
*/
goog.labs.pubsub.BroadcastPubSub.prototype.unsubscribe = function(
topic, fn, opt_context) {
return this.pubSub_.unsubscribe(topic, fn, opt_context);
};
/**
* Removes a subscription based on the key returned by {@link #subscribe}. No-op
* if no matching subscription is found. Returns a Boolean indicating whether a
* subscription was removed.
* @param {number} key Subscription key.
* @return {boolean} Whether a matching subscription was removed.
*/
goog.labs.pubsub.BroadcastPubSub.prototype.unsubscribeByKey = function(key) {
return this.pubSub_.unsubscribeByKey(key);
};
/**
* Subscribes a function to a topic. The function is invoked as a method on the
* given {@code opt_context} object, or in the global scope if no context is
* specified. Subscribing the same function to the same topic multiple times
* will result in multiple function invocations while publishing. Returns a
* subscription key that can be used to unsubscribe the function from the topic
* via {@link #unsubscribeByKey}.
* @param {string} topic Topic to subscribe to.
* @param {Function} fn Function to be invoked when a message is published to
* the given topic.
* @param {Object=} opt_context Object in whose context the function is to be
* called (the global scope if none).
* @return {number} Subscription key.
*/
goog.labs.pubsub.BroadcastPubSub.prototype.subscribe = function(
topic, fn, opt_context) {
return this.pubSub_.subscribe(topic, fn, opt_context);
};
/**
* Subscribes a single-use function to a topic. The function is invoked as a
* method on the given {@code opt_context} object, or in the global scope if no
* context is specified, and is then unsubscribed. Returns a subscription key
* that can be used to unsubscribe the function from the topic via {@link
* #unsubscribeByKey}.
* @param {string} topic Topic to subscribe to.
* @param {Function} fn Function to be invoked once and then unsubscribed when
* a message is published to the given topic.
* @param {Object=} opt_context Object in whose context the function is to be
* called (the global scope if none).
* @return {number} Subscription key.
*/
goog.labs.pubsub.BroadcastPubSub.prototype.subscribeOnce = function(
topic, fn, opt_context) {
return this.pubSub_.subscribeOnce(topic, fn, opt_context);
};
/**
* Returns the number of subscriptions to the given topic (or all topics if
* unspecified). This number will not change while publishing any messages.
* @param {string=} opt_topic The topic (all topics if unspecified).
* @return {number} Number of subscriptions to the topic.
*/
goog.labs.pubsub.BroadcastPubSub.prototype.getCount = function(opt_topic) {
return this.pubSub_.getCount(opt_topic);
};
/**
* Clears the subscription list for a topic, or all topics if unspecified.
* @param {string=} opt_topic Topic to clear (all topics if unspecified).
*/
goog.labs.pubsub.BroadcastPubSub.prototype.clear = function(opt_topic) {
this.pubSub_.clear(opt_topic);
};
/** @override */
goog.labs.pubsub.BroadcastPubSub.prototype.disposeInternal = function() {
goog.array.remove(goog.labs.pubsub.BroadcastPubSub.instances_, this);
if (goog.labs.pubsub.BroadcastPubSub.IS_IE8_ &&
goog.isDefAndNotNull(this.storage_) &&
goog.labs.pubsub.BroadcastPubSub.instances_.length == 0) {
this.storage_.remove(goog.labs.pubsub.BroadcastPubSub.IE8_EVENTS_KEY_);
}
goog.labs.pubsub.BroadcastPubSub.base(this, 'disposeInternal');
};
/**
* Prefix for IE8 storage event queue keys.
* @private @const
*/
goog.labs.pubsub.BroadcastPubSub.IE8_EVENTS_KEY_PREFIX_ = '_closure_bps_ie8evt';
/**
* Time (in milliseconds) that IE8 events should live. If they are not
* processed by other windows in this time they will be removed.
* @private @const
*/
goog.labs.pubsub.BroadcastPubSub.IE8_EVENT_LIFETIME_MS_ = 1000 * 10;
/**
* Time (in milliseconds) that the IE8 event queue should live.
* @private @const
*/
goog.labs.pubsub.BroadcastPubSub.IE8_QUEUE_LIFETIME_MS_ = 1000 * 30;
/**
* Time delta that is used to distinguish between timestamps of events that
* happen in the same millisecond.
* @private @const
*/
goog.labs.pubsub.BroadcastPubSub.IE8_TIMESTAMP_UNIQUE_OFFSET_MS_ = .01;
/**
* Name for this window/tab's storage key that stores its IE8 event queue.
*
* The browsers storage events are supposed to track the key which was changed,
* the previous value for that key, and the new value of that key. Our
* implementation is dependent on this information but IE8 doesn't provide it.
* We implement our own event queue using local storage to track this
* information in IE8. Since all instances share the same localStorage context
* in a particular tab, we share the events queue.
*
* This key is a static member shared by all instances of BroadcastPubSub in the
* same Window context. To avoid read-update-write contention, this key is only
* written in a single context in the cleanupIe8StorageEvents_ function. Since
* instances in other contexts will read this key there is code in the {@code
* publish} function to make sure timestamps are unique even within the same
* millisecond.
*
* @private @const {string}
*/
goog.labs.pubsub.BroadcastPubSub.IE8_EVENTS_KEY_ =
goog.labs.pubsub.BroadcastPubSub.IE8_EVENTS_KEY_PREFIX_ +
goog.math.randomInt(1e9);
/**
* All instances of this object should access elements using strings and not
* attributes. Since we are communicating across browser tabs we could be
* dealing with different versions of javascript and thus may have different
* obfuscation in each tab.
* @private @typedef {{'timestamp': number, 'args': !Array<string>}}
*/
goog.labs.pubsub.BroadcastPubSub.Ie8Event_;
/** @private @const */
goog.labs.pubsub.BroadcastPubSub.IS_IE8_ =
goog.userAgent.IE && goog.userAgent.DOCUMENT_MODE == 8;
/**
* Validates an event object.
* @param {!Object} obj The object to validate as an Event.
* @return {?goog.labs.pubsub.BroadcastPubSub.Ie8Event_} A valid
* event object or null if the object is invalid.
* @private
*/
goog.labs.pubsub.BroadcastPubSub.validateIe8Event_ = function(obj) {
if (goog.isObject(obj) && goog.isNumber(obj['timestamp']) &&
goog.array.every(obj['args'], goog.isString)) {
return {'timestamp': obj['timestamp'], 'args': obj['args']};
}
return null;
};
/**
* Returns an array of valid IE8 events.
* @param {!Array<!Object>} events Possible IE8 events.
* @return {!Array<!goog.labs.pubsub.BroadcastPubSub.Ie8Event_>}
* Valid IE8 events.
* @private
*/
goog.labs.pubsub.BroadcastPubSub.filterValidIe8Events_ = function(events) {
return goog.array.filter(
goog.array.map(
events, goog.labs.pubsub.BroadcastPubSub.validateIe8Event_),
goog.isDefAndNotNull);
};
/**
* Returns the IE8 events that have a timestamp later than the provided
* timestamp.
* @param {number} timestamp Expired timestamp.
* @param {!Array<!goog.labs.pubsub.BroadcastPubSub.Ie8Event_>} events
* Possible IE8 events.
* @return {!Array<!goog.labs.pubsub.BroadcastPubSub.Ie8Event_>}
* Unexpired IE8 events.
* @private
*/
goog.labs.pubsub.BroadcastPubSub.filterNewIe8Events_ = function(
timestamp, events) {
return goog.array.filter(
events, function(event) { return event['timestamp'] > timestamp; });
};
/**
* Processes the events array for key if all elements are valid IE8 events.
* @param {string} key The key in localStorage where the event queue is stored.
* @param {!Array<!Object>} events Array of possible events stored at key.
* @return {boolean} Return true if all elements in the array are valid
* events, false otherwise.
* @private
*/
goog.labs.pubsub.BroadcastPubSub.prototype.maybeProcessIe8Events_ = function(
key, events) {
if (!events.length) {
return false;
}
var validEvents =
goog.labs.pubsub.BroadcastPubSub.filterValidIe8Events_(events);
if (validEvents.length == events.length) {
var lastTimestamp = goog.array.peek(validEvents)['timestamp'];
var previousTime =
this.ie8LastEventTimes_[key] || this.ie8StartupTimestamp_;
if (lastTimestamp > previousTime -
goog.labs.pubsub.BroadcastPubSub.IE8_QUEUE_LIFETIME_MS_) {
this.ie8LastEventTimes_[key] = lastTimestamp;
validEvents = goog.labs.pubsub.BroadcastPubSub.filterNewIe8Events_(
previousTime, validEvents);
for (var i = 0, event; event = validEvents[i]; i++) {
this.dispatch_(event['args']);
}
return true;
}
} else {
goog.log.warning(this.logger_, 'invalid events found in queue ' + key);
}
return false;
};
/**
* Handle the storage event and possibly dispatch events. Looks through all keys
* in localStorage for valid keys.
* @private
*/
goog.labs.pubsub.BroadcastPubSub.prototype.handleIe8StorageEvent_ = function() {
var numKeys = this.mechanism_.getCount();
for (var idx = 0; idx < numKeys; idx++) {
var key = this.mechanism_.key(idx);
// Don't process events we generated. The W3C standard says that storage
// events should be queued by the browser for each window whose document's
// storage object is affected by a change in localStorage. Chrome, Firefox,
// and modern IE don't dispatch the event to the window which made the
// change. This code simulates that behavior in IE8.
if (!(goog.isString(key) &&
goog.string.startsWith(
key, goog.labs.pubsub.BroadcastPubSub.IE8_EVENTS_KEY_PREFIX_))) {
continue;
}
var events = null;
try {
events = this.storage_.get(key);
} catch (ex) {
goog.log.warning(this.logger_, 'invalid remote event queue ' + key);
}
if (!(goog.isArray(events) && this.maybeProcessIe8Events_(key, events))) {
// Events is not an array, empty, contains invalid events, or expired.
this.storage_.remove(key);
}
}
};
/**
* Cleanup our IE8 event queue by removing any events that come at or before the
* given timestamp.
* @param {number} timestamp Maximum timestamp to remove from the queue.
* @private
*/
goog.labs.pubsub.BroadcastPubSub.prototype.cleanupIe8StorageEvents_ = function(
timestamp) {
var events = null;
try {
events =
this.storage_.get(goog.labs.pubsub.BroadcastPubSub.IE8_EVENTS_KEY_);
} catch (ex) {
goog.log.error(
this.logger_, 'cleanup encountered invalid event queue key ' +
goog.labs.pubsub.BroadcastPubSub.IE8_EVENTS_KEY_);
}
if (!goog.isArray(events)) {
this.storage_.remove(goog.labs.pubsub.BroadcastPubSub.IE8_EVENTS_KEY_);
return;
}
events = goog.labs.pubsub.BroadcastPubSub.filterNewIe8Events_(
timestamp,
goog.labs.pubsub.BroadcastPubSub.filterValidIe8Events_(events));
if (events.length > 0) {
this.storage_.set(goog.labs.pubsub.BroadcastPubSub.IE8_EVENTS_KEY_, events);
} else {
this.storage_.remove(goog.labs.pubsub.BroadcastPubSub.IE8_EVENTS_KEY_);
}
};
|