| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // ----------------------------------------------------------------------------- | 5 // ----------------------------------------------------------------------------- |
| 6 // NOTE: If you change this file you need to touch renderer_resources.grd to | 6 // NOTE: If you change this file you need to touch renderer_resources.grd to |
| 7 // have your change take effect. | 7 // have your change take effect. |
| 8 // ----------------------------------------------------------------------------- | 8 // ----------------------------------------------------------------------------- |
| 9 | 9 |
| 10 var chrome = chrome || {}; | 10 var chrome = chrome || {}; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 // chrome.Event.dispatch_("tab-changed", "hi"); | 22 // chrome.Event.dispatch_("tab-changed", "hi"); |
| 23 // will result in an alert dialog that says 'hi'. | 23 // will result in an alert dialog that says 'hi'. |
| 24 chrome.Event = function(opt_eventName) { | 24 chrome.Event = function(opt_eventName) { |
| 25 this.eventName_ = opt_eventName; | 25 this.eventName_ = opt_eventName; |
| 26 this.listeners_ = []; | 26 this.listeners_ = []; |
| 27 }; | 27 }; |
| 28 | 28 |
| 29 // A map of event names to the event object that is registered to that name. | 29 // A map of event names to the event object that is registered to that name. |
| 30 chrome.Event.attached_ = {}; | 30 chrome.Event.attached_ = {}; |
| 31 | 31 |
| 32 // An array of all attached event objects, used for detaching on unload. |
| 33 chrome.Event.allAttached_ = []; |
| 34 |
| 32 // Dispatches a named event with the given JSON array, which is deserialized | 35 // Dispatches a named event with the given JSON array, which is deserialized |
| 33 // before dispatch. The JSON array is the list of arguments that will be | 36 // before dispatch. The JSON array is the list of arguments that will be |
| 34 // sent with the event callback. | 37 // sent with the event callback. |
| 35 chrome.Event.dispatchJSON_ = function(name, args) { | 38 chrome.Event.dispatchJSON_ = function(name, args) { |
| 36 if (chrome.Event.attached_[name]) { | 39 if (chrome.Event.attached_[name]) { |
| 37 if (args) { | 40 if (args) { |
| 38 args = JSON.parse(args); | 41 args = JSON.parse(args); |
| 39 } | 42 } |
| 40 chrome.Event.attached_[name].dispatch.apply( | 43 chrome.Event.attached_[name].dispatch.apply( |
| 41 chrome.Event.attached_[name], args); | 44 chrome.Event.attached_[name], args); |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 98 } catch (e) { | 101 } catch (e) { |
| 99 console.error(e); | 102 console.error(e); |
| 100 } | 103 } |
| 101 } | 104 } |
| 102 }; | 105 }; |
| 103 | 106 |
| 104 // Attaches this event object to its name. Only one object can have a given | 107 // Attaches this event object to its name. Only one object can have a given |
| 105 // name. | 108 // name. |
| 106 chrome.Event.prototype.attach_ = function() { | 109 chrome.Event.prototype.attach_ = function() { |
| 107 AttachEvent(this.eventName_); | 110 AttachEvent(this.eventName_); |
| 108 this.unloadHandler_ = this.detach_.bind(this); | 111 chrome.Event.allAttached_[chrome.Event.allAttached_.length] = this; |
| 109 window.addEventListener('unload', this.unloadHandler_, false); | |
| 110 if (!this.eventName_) | 112 if (!this.eventName_) |
| 111 return; | 113 return; |
| 112 | 114 |
| 113 if (chrome.Event.attached_[this.eventName_]) { | 115 if (chrome.Event.attached_[this.eventName_]) { |
| 114 throw new Error("chrome.Event '" + this.eventName_ + | 116 throw new Error("chrome.Event '" + this.eventName_ + |
| 115 "' is already attached."); | 117 "' is already attached."); |
| 116 } | 118 } |
| 117 | 119 |
| 118 chrome.Event.attached_[this.eventName_] = this; | 120 chrome.Event.attached_[this.eventName_] = this; |
| 119 }; | 121 }; |
| 120 | 122 |
| 121 // Detaches this event object from its name. | 123 // Detaches this event object from its name. |
| 122 chrome.Event.prototype.detach_ = function() { | 124 chrome.Event.prototype.detach_ = function() { |
| 123 window.removeEventListener('unload', this.unloadHandler_, false); | 125 var i = chrome.Event.allAttached_.indexOf(this); |
| 126 if (i >= 0) |
| 127 delete chrome.Event.allAttached_[i]; |
| 124 DetachEvent(this.eventName_); | 128 DetachEvent(this.eventName_); |
| 125 if (!this.eventName_) | 129 if (!this.eventName_) |
| 126 return; | 130 return; |
| 127 | 131 |
| 128 if (!chrome.Event.attached_[this.eventName_]) { | 132 if (!chrome.Event.attached_[this.eventName_]) { |
| 129 throw new Error("chrome.Event '" + this.eventName_ + | 133 throw new Error("chrome.Event '" + this.eventName_ + |
| 130 "' is not attached."); | 134 "' is not attached."); |
| 131 } | 135 } |
| 132 | 136 |
| 133 delete chrome.Event.attached_[this.eventName_]; | 137 delete chrome.Event.attached_[this.eventName_]; |
| 134 }; | 138 }; |
| 139 |
| 140 // Load events. Note that onUnload_ might not always fire, since Chrome will |
| 141 // terminate renderers on shutdown. |
| 142 chrome.onLoad_ = new chrome.Event(); |
| 143 chrome.onUnload_ = new chrome.Event(); |
| 144 |
| 145 // This is called by native code when the DOM is ready. |
| 146 chrome.dispatchOnLoad_ = function() { |
| 147 chrome.onLoad_.dispatch(); |
| 148 delete chrome.dispatchOnLoad_; |
| 149 } |
| 150 |
| 151 chrome.dispatchOnUnload_ = function() { |
| 152 chrome.onUnload_.dispatch(); |
| 153 for (var i in chrome.Event.allAttached_) |
| 154 chrome.Event.allAttached_[i].detach_(); |
| 155 } |
| 135 })(); | 156 })(); |
| OLD | NEW |