Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(163)

Side by Side Diff: chrome/renderer/resources/event_bindings.js

Issue 7980055: Move bindings javascript into its own directory. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 9 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « chrome/renderer/renderer_resources.grd ('k') | chrome/renderer/resources/extension_apitest.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 var chrome = chrome || {};
6 (function () {
7 native function GetChromeHidden();
8 native function AttachEvent(eventName);
9 native function DetachEvent(eventName);
10 native function GetExternalFileEntry(fileDefinition);
11 native function Print();
12
13 var chromeHidden = GetChromeHidden();
14
15 // Local implementation of JSON.parse & JSON.stringify that protect us
16 // from being clobbered by an extension.
17 chromeHidden.JSON = new (function() {
18 var $Object = Object;
19 var $Array = Array;
20 var $jsonStringify = JSON.stringify;
21 var $jsonParse = JSON.parse;
22
23 this.stringify = function(thing) {
24 var customizedObjectToJSON = $Object.prototype.toJSON;
25 var customizedArrayToJSON = $Array.prototype.toJSON;
26 if (customizedObjectToJSON !== undefined) {
27 $Object.prototype.toJSON = null;
28 }
29 if (customizedArrayToJSON !== undefined) {
30 $Array.prototype.toJSON = null;
31 }
32 try {
33 return $jsonStringify(thing);
34 } finally {
35 if (customizedObjectToJSON !== undefined) {
36 $Object.prototype.toJSON = customizedObjectToJSON;
37 }
38 if (customizedArrayToJSON !== undefined) {
39 $Array.prototype.toJSON = customizedArrayToJSON;
40 }
41 }
42 };
43
44 this.parse = function(thing) {
45 return $jsonParse(thing);
46 };
47 })();
48
49 // Event object. If opt_eventName is provided, this object represents
50 // the unique instance of that named event, and dispatching an event
51 // with that name will route through this object's listeners.
52 //
53 // Example:
54 // chrome.tabs.onChanged = new chrome.Event("tab-changed");
55 // chrome.tabs.onChanged.addListener(function(data) { alert(data); });
56 // chromeHidden.Event.dispatch("tab-changed", "hi");
57 // will result in an alert dialog that says 'hi'.
58 chrome.Event = function(opt_eventName, opt_argSchemas) {
59 this.eventName_ = opt_eventName;
60 this.listeners_ = [];
61
62 // Validate event parameters if we are in debug.
63 if (opt_argSchemas &&
64 chromeHidden.validateCallbacks &&
65 chromeHidden.validate) {
66
67 this.validate_ = function(args) {
68 try {
69 chromeHidden.validate(args, opt_argSchemas);
70 } catch (exception) {
71 return "Event validation error during " + opt_eventName + " -- " +
72 exception;
73 }
74 };
75 }
76 };
77
78 // A map of event names to the event object that is registered to that name.
79 var attachedNamedEvents = {};
80
81 // An array of all attached event objects, used for detaching on unload.
82 var allAttachedEvents = [];
83
84 chromeHidden.Event = {};
85
86 // Dispatches a named event with the given JSON array, which is deserialized
87 // before dispatch. The JSON array is the list of arguments that will be
88 // sent with the event callback.
89 chromeHidden.Event.dispatchJSON = function(name, args) {
90 if (attachedNamedEvents[name]) {
91 if (args) {
92 args = chromeHidden.JSON.parse(args);
93 // TODO(zelidrag|aa): Remove this hack from here once we enable event
94 // JSON payload unpacking on C++ side.
95 if (name == "fileBrowserHandler.onExecute") {
96 if (args.length < 2)
97 return;
98 var fileList = args[1].entries;
99 if (!fileList)
100 return;
101 // The second parameter for this event's payload is file definition
102 // dictionary that we used to reconstruct File API's Entry instance
103 // here.
104 for (var i = 0; i < fileList.length; i++)
105 fileList[i] = GetExternalFileEntry(fileList[i]);
106 }
107 }
108 return attachedNamedEvents[name].dispatch.apply(
109 attachedNamedEvents[name], args);
110 }
111 };
112
113 // Dispatches a named event with the given arguments, supplied as an array.
114 chromeHidden.Event.dispatch = function(name, args) {
115 if (attachedNamedEvents[name]) {
116 attachedNamedEvents[name].dispatch.apply(
117 attachedNamedEvents[name], args);
118 }
119 };
120
121 // Test if a named event has any listeners.
122 chromeHidden.Event.hasListener = function(name) {
123 return (attachedNamedEvents[name] &&
124 attachedNamedEvents[name].listeners_.length > 0);
125 };
126
127 // Registers a callback to be called when this event is dispatched.
128 chrome.Event.prototype.addListener = function(cb) {
129 if (this.listeners_.length == 0) {
130 this.attach_();
131 }
132 this.listeners_.push(cb);
133 };
134
135 // Unregisters a callback.
136 chrome.Event.prototype.removeListener = function(cb) {
137 var idx = this.findListener_(cb);
138 if (idx == -1) {
139 return;
140 }
141
142 this.listeners_.splice(idx, 1);
143 if (this.listeners_.length == 0) {
144 this.detach_();
145 }
146 };
147
148 // Test if the given callback is registered for this event.
149 chrome.Event.prototype.hasListener = function(cb) {
150 return this.findListener_(cb) > -1;
151 };
152
153 // Test if any callbacks are registered for this event.
154 chrome.Event.prototype.hasListeners = function(cb) {
155 return this.listeners_.length > 0;
156 };
157
158 // Returns the index of the given callback if registered, or -1 if not
159 // found.
160 chrome.Event.prototype.findListener_ = function(cb) {
161 for (var i = 0; i < this.listeners_.length; i++) {
162 if (this.listeners_[i] == cb) {
163 return i;
164 }
165 }
166
167 return -1;
168 };
169
170 // Dispatches this event object to all listeners, passing all supplied
171 // arguments to this function each listener.
172 chrome.Event.prototype.dispatch = function(varargs) {
173 var args = Array.prototype.slice.call(arguments);
174 if (this.validate_) {
175 var validationErrors = this.validate_(args);
176 if (validationErrors) {
177 return validationErrors;
178 }
179 }
180 for (var i = 0; i < this.listeners_.length; i++) {
181 try {
182 this.listeners_[i].apply(null, args);
183 } catch (e) {
184 console.error("Error in event handler for '" + this.eventName_ +
185 "': " + e.stack);
186 }
187 }
188 };
189
190 // Attaches this event object to its name. Only one object can have a given
191 // name.
192 chrome.Event.prototype.attach_ = function() {
193 AttachEvent(this.eventName_);
194 allAttachedEvents[allAttachedEvents.length] = this;
195 if (!this.eventName_)
196 return;
197
198 if (attachedNamedEvents[this.eventName_]) {
199 throw new Error("chrome.Event '" + this.eventName_ +
200 "' is already attached.");
201 }
202
203 attachedNamedEvents[this.eventName_] = this;
204 };
205
206 // Detaches this event object from its name.
207 chrome.Event.prototype.detach_ = function() {
208 var i = allAttachedEvents.indexOf(this);
209 if (i >= 0)
210 delete allAttachedEvents[i];
211 DetachEvent(this.eventName_);
212 if (!this.eventName_)
213 return;
214
215 if (!attachedNamedEvents[this.eventName_]) {
216 throw new Error("chrome.Event '" + this.eventName_ +
217 "' is not attached.");
218 }
219
220 delete attachedNamedEvents[this.eventName_];
221 };
222
223 chrome.Event.prototype.destroy_ = function() {
224 this.listeners_ = [];
225 this.validate_ = [];
226 this.detach_();
227 };
228
229 // Special load events: we don't use the DOM unload because that slows
230 // down tab shutdown. On the other hand, onUnload might not always fire,
231 // since Chrome will terminate renderers on shutdown (SuddenTermination).
232 chromeHidden.onLoad = new chrome.Event();
233 chromeHidden.onUnload = new chrome.Event();
234
235 chromeHidden.dispatchOnLoad = function(extensionId, isExtensionProcess,
236 isIncognitoContext) {
237 chromeHidden.onLoad.dispatch(extensionId, isExtensionProcess,
238 isIncognitoContext);
239 };
240
241 chromeHidden.dispatchOnUnload = function() {
242 chromeHidden.onUnload.dispatch();
243 for (var i = 0; i < allAttachedEvents.length; ++i) {
244 var event = allAttachedEvents[i];
245 if (event)
246 event.detach_();
247 }
248 };
249
250 chromeHidden.dispatchError = function(msg) {
251 console.error(msg);
252 };
253 })();
OLDNEW
« no previous file with comments | « chrome/renderer/renderer_resources.grd ('k') | chrome/renderer/resources/extension_apitest.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698