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 var chrome = chrome || {}; | 5 var chrome = chrome || {}; |
6 (function () { | 6 (function () { |
7 native function GetChromeHidden(); | 7 native function GetChromeHidden(); |
8 native function AttachEvent(eventName); | 8 native function AttachEvent(eventName); |
9 native function DetachEvent(eventName); | 9 native function DetachEvent(eventName); |
10 | 10 |
11 var chromeHidden = GetChromeHidden(); | 11 var chromeHidden = GetChromeHidden(); |
| 12 |
| 13 // Local implementation of JSON.parse & JSON.stringify that protect us |
| 14 // from being clobbered by an extension. |
| 15 chromeHidden.JSON = new (function() { |
| 16 const $Object = Object; |
| 17 const $Array = Array; |
| 18 const $jsonStringify = JSON.stringify; |
| 19 const $jsonParse = JSON.parse; |
| 20 |
| 21 this.stringify = function(thing) { |
| 22 var customizedObjectToJSON = $Object.prototype.toJSON; |
| 23 var customizedArrayToJSON = $Array.prototype.toJSON; |
| 24 if (customizedObjectToJSON !== undefined) { |
| 25 $Object.prototype.toJSON = null; |
| 26 } |
| 27 if (customizedArrayToJSON !== undefined) { |
| 28 $Array.prototype.toJSON = null; |
| 29 } |
| 30 try { |
| 31 return $jsonStringify(thing); |
| 32 } finally { |
| 33 if (customizedObjectToJSON !== undefined) { |
| 34 $Object.prototype.toJSON = customizedObjectToJSON; |
| 35 } |
| 36 if (customizedArrayToJSON !== undefined) { |
| 37 $Array.prototype.toJSON = customizedArrayToJSON; |
| 38 } |
| 39 } |
| 40 }; |
| 41 |
| 42 this.parse = function(thing) { |
| 43 return $jsonParse(thing); |
| 44 }; |
| 45 })(); |
12 | 46 |
13 // Event object. If opt_eventName is provided, this object represents | 47 // Event object. If opt_eventName is provided, this object represents |
14 // the unique instance of that named event, and dispatching an event | 48 // the unique instance of that named event, and dispatching an event |
15 // with that name will route through this object's listeners. | 49 // with that name will route through this object's listeners. |
16 // | 50 // |
17 // Example: | 51 // Example: |
18 // chrome.tabs.onChanged = new chrome.Event("tab-changed"); | 52 // chrome.tabs.onChanged = new chrome.Event("tab-changed"); |
19 // chrome.tabs.onChanged.addListener(function(data) { alert(data); }); | 53 // chrome.tabs.onChanged.addListener(function(data) { alert(data); }); |
20 // chromeHidden.Event.dispatch("tab-changed", "hi"); | 54 // chromeHidden.Event.dispatch("tab-changed", "hi"); |
21 // will result in an alert dialog that says 'hi'. | 55 // will result in an alert dialog that says 'hi'. |
(...skipping 24 matching lines...) Expand all Loading... |
46 var allAttachedEvents = []; | 80 var allAttachedEvents = []; |
47 | 81 |
48 chromeHidden.Event = {}; | 82 chromeHidden.Event = {}; |
49 | 83 |
50 // Dispatches a named event with the given JSON array, which is deserialized | 84 // Dispatches a named event with the given JSON array, which is deserialized |
51 // before dispatch. The JSON array is the list of arguments that will be | 85 // before dispatch. The JSON array is the list of arguments that will be |
52 // sent with the event callback. | 86 // sent with the event callback. |
53 chromeHidden.Event.dispatchJSON = function(name, args) { | 87 chromeHidden.Event.dispatchJSON = function(name, args) { |
54 if (attachedNamedEvents[name]) { | 88 if (attachedNamedEvents[name]) { |
55 if (args) { | 89 if (args) { |
56 args = JSON.parse(args); | 90 args = chromeHidden.JSON.parse(args); |
57 } | 91 } |
58 return attachedNamedEvents[name].dispatch.apply( | 92 return attachedNamedEvents[name].dispatch.apply( |
59 attachedNamedEvents[name], args); | 93 attachedNamedEvents[name], args); |
60 } | 94 } |
61 }; | 95 }; |
62 | 96 |
63 // Dispatches a named event with the given arguments, supplied as an array. | 97 // Dispatches a named event with the given arguments, supplied as an array. |
64 chromeHidden.Event.dispatch = function(name, args) { | 98 chromeHidden.Event.dispatch = function(name, args) { |
65 if (attachedNamedEvents[name]) { | 99 if (attachedNamedEvents[name]) { |
66 attachedNamedEvents[name].dispatch.apply( | 100 attachedNamedEvents[name].dispatch.apply( |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
185 var event = allAttachedEvents[i]; | 219 var event = allAttachedEvents[i]; |
186 if (event) | 220 if (event) |
187 event.detach_(); | 221 event.detach_(); |
188 } | 222 } |
189 } | 223 } |
190 | 224 |
191 chromeHidden.dispatchError = function(msg) { | 225 chromeHidden.dispatchError = function(msg) { |
192 console.error(msg); | 226 console.error(msg); |
193 } | 227 } |
194 })(); | 228 })(); |
OLD | NEW |