146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)// Copyright 2014 The Chromium Authors. All rights reserved.
2868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
3868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// found in the LICENSE file.
4868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
5868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Special unload event. We don't use the DOM unload because that slows down
6868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// tab shutdown. On the other hand, onUnload might not always fire, since
7868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Chrome will terminate renderers on shutdown (SuddenTermination).
8868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
9868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Implement a primitive subset of the Event interface as needed, since if this
10868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// was to use the real event object there would be a circular dependency.
11868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)var listeners = [];
12868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
13868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)exports.addListener = function(listener) {
14eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  $Array.push(listeners, listener);
15868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)};
16868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
17868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)exports.removeListener = function(listener) {
18868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  for (var i = 0; i < listeners.length; ++i) {
19868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    if (listeners[i] == listener) {
20eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      $Array.splice(listeners, i, 1);
21868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      return;
22868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    }
23868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  }
24868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)};
25868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
26868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)exports.wasDispatched = false;
27868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
28868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// dispatch() is called from C++.
29868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)exports.dispatch = function() {
30868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  exports.wasDispatched = true;
31868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  for (var i = 0; i < listeners.length; ++i)
32868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    listeners[i]();
33868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)};
34