| 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 || {}; |
| 11 (function () { | 11 (function () { |
| 12 native function GetChromeHidden(); | 12 native function GetChromeHidden(); |
| 13 native function AttachEvent(eventName); | 13 native function AttachEvent(eventName); |
| 14 native function DetachEvent(eventName); | 14 native function DetachEvent(eventName); |
| 15 | 15 |
| 16 var chromeHidden = GetChromeHidden(); | 16 var chromeHidden = GetChromeHidden(); |
| 17 | 17 |
| 18 // Event object. If opt_eventName is provided, this object represents | 18 // Event object. If opt_eventName is provided, this object represents |
| 19 // the unique instance of that named event, and dispatching an event | 19 // the unique instance of that named event, and dispatching an event |
| 20 // with that name will route through this object's listeners. | 20 // with that name will route through this object's listeners. |
| 21 // | 21 // |
| 22 // Example: | 22 // Example: |
| 23 // chrome.tabs.onChanged = new chrome.Event("tab-changed"); | 23 // chrome.tabs.onChanged = new chrome.Event("tab-changed"); |
| 24 // chrome.tabs.onChanged.addListener(function(data) { alert(data); }); | 24 // chrome.tabs.onChanged.addListener(function(data) { alert(data); }); |
| 25 // chromeHidden.Event.dispatch("tab-changed", "hi"); | 25 // chromeHidden.Event.dispatch("tab-changed", "hi"); |
| 26 // will result in an alert dialog that says 'hi'. | 26 // will result in an alert dialog that says 'hi'. |
| 27 chrome.Event = function(opt_eventName) { | 27 chrome.Event = function(opt_eventName, opt_argSchemas) { |
| 28 this.eventName_ = opt_eventName; | 28 this.eventName_ = opt_eventName; |
| 29 this.listeners_ = []; | 29 this.listeners_ = []; |
| 30 |
| 31 // Validate event parameters if we are in debug. |
| 32 if (opt_argSchemas && |
| 33 chromeHidden.validateCallbacks && |
| 34 chromeHidden.validate) { |
| 35 |
| 36 this.validate_ = function(args) { |
| 37 try { |
| 38 chromeHidden.validate(args, opt_argSchemas); |
| 39 } catch (exception) { |
| 40 return "Event validation error during " + opt_eventName + " -- " + |
| 41 exception; |
| 42 } |
| 43 } |
| 44 } |
| 30 }; | 45 }; |
| 31 | 46 |
| 32 // A map of event names to the event object that is registered to that name. | 47 // A map of event names to the event object that is registered to that name. |
| 33 var attachedNamedEvents = {}; | 48 var attachedNamedEvents = {}; |
| 34 | 49 |
| 35 // An array of all attached event objects, used for detaching on unload. | 50 // An array of all attached event objects, used for detaching on unload. |
| 36 var allAttachedEvents = []; | 51 var allAttachedEvents = []; |
| 37 | 52 |
| 38 chromeHidden.Event = {}; | 53 chromeHidden.Event = {}; |
| 39 | 54 |
| 40 // Dispatches a named event with the given JSON array, which is deserialized | 55 // Dispatches a named event with the given JSON array, which is deserialized |
| 41 // before dispatch. The JSON array is the list of arguments that will be | 56 // before dispatch. The JSON array is the list of arguments that will be |
| 42 // sent with the event callback. | 57 // sent with the event callback. |
| 43 chromeHidden.Event.dispatchJSON = function(name, args) { | 58 chromeHidden.Event.dispatchJSON = function(name, args) { |
| 44 if (attachedNamedEvents[name]) { | 59 if (attachedNamedEvents[name]) { |
| 45 if (args) { | 60 if (args) { |
| 46 args = JSON.parse(args); | 61 args = JSON.parse(args); |
| 47 } | 62 } |
| 48 attachedNamedEvents[name].dispatch.apply( | 63 return attachedNamedEvents[name].dispatch.apply( |
| 49 attachedNamedEvents[name], args); | 64 attachedNamedEvents[name], args); |
| 50 } | 65 } |
| 51 }; | 66 }; |
| 52 | 67 |
| 53 // Dispatches a named event with the given arguments, supplied as an array. | 68 // Dispatches a named event with the given arguments, supplied as an array. |
| 54 chromeHidden.Event.dispatch = function(name, args) { | 69 chromeHidden.Event.dispatch = function(name, args) { |
| 55 if (attachedNamedEvents[name]) { | 70 if (attachedNamedEvents[name]) { |
| 56 attachedNamedEvents[name].dispatch.apply( | 71 attachedNamedEvents[name].dispatch.apply( |
| 57 attachedNamedEvents[name], args); | 72 attachedNamedEvents[name], args); |
| 58 } | 73 } |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 99 } | 114 } |
| 100 } | 115 } |
| 101 | 116 |
| 102 return -1; | 117 return -1; |
| 103 }; | 118 }; |
| 104 | 119 |
| 105 // Dispatches this event object to all listeners, passing all supplied | 120 // Dispatches this event object to all listeners, passing all supplied |
| 106 // arguments to this function each listener. | 121 // arguments to this function each listener. |
| 107 chrome.Event.prototype.dispatch = function(varargs) { | 122 chrome.Event.prototype.dispatch = function(varargs) { |
| 108 var args = Array.prototype.slice.call(arguments); | 123 var args = Array.prototype.slice.call(arguments); |
| 124 if (this.validate_) { |
| 125 var validationErrors = this.validate_(args); |
| 126 if (validationErrors) { |
| 127 return validationErrors; |
| 128 } |
| 129 } |
| 109 for (var i = 0; i < this.listeners_.length; i++) { | 130 for (var i = 0; i < this.listeners_.length; i++) { |
| 110 try { | 131 try { |
| 111 this.listeners_[i].apply(null, args); | 132 this.listeners_[i].apply(null, args); |
| 112 } catch (e) { | 133 } catch (e) { |
| 113 console.error(e); | 134 console.error(e); |
| 114 } | 135 } |
| 115 } | 136 } |
| 116 }; | 137 }; |
| 117 | 138 |
| 118 // Attaches this event object to its name. Only one object can have a given | 139 // Attaches this event object to its name. Only one object can have a given |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 161 chromeHidden.dispatchOnUnload = function() { | 182 chromeHidden.dispatchOnUnload = function() { |
| 162 chromeHidden.onUnload.dispatch(); | 183 chromeHidden.onUnload.dispatch(); |
| 163 for (var i in allAttachedEvents) | 184 for (var i in allAttachedEvents) |
| 164 allAttachedEvents[i].detach_(); | 185 allAttachedEvents[i].detach_(); |
| 165 } | 186 } |
| 166 | 187 |
| 167 chromeHidden.dispatchError = function(msg) { | 188 chromeHidden.dispatchError = function(msg) { |
| 168 console.error(msg); | 189 console.error(msg); |
| 169 } | 190 } |
| 170 })(); | 191 })(); |
| OLD | NEW |