OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 // This contains unprivileged javascript APIs for extensions and apps. It | 5 // This contains unprivileged javascript APIs for extensions and apps. It |
6 // can be loaded by any extension-related context, such as content scripts or | 6 // can be loaded by any extension-related context, such as content scripts or |
7 // background pages. See user_script_slave.cc for script that is loaded by | 7 // background pages. See user_script_slave.cc for script that is loaded by |
8 // content scripts only. | 8 // content scripts only. |
9 | 9 |
10 require('json_schema'); | 10 require('json_schema'); |
| 11 var json = require('json'); |
11 var lastError = require('lastError'); | 12 var lastError = require('lastError'); |
12 var miscNatives = requireNative('miscellaneous_bindings'); | 13 var miscNatives = requireNative('miscellaneous_bindings'); |
13 var chrome = requireNative('chrome').GetChrome(); | 14 var chrome = requireNative('chrome').GetChrome(); |
14 var CloseChannel = miscNatives.CloseChannel; | 15 var CloseChannel = miscNatives.CloseChannel; |
15 var PortAddRef = miscNatives.PortAddRef; | 16 var PortAddRef = miscNatives.PortAddRef; |
16 var PortRelease = miscNatives.PortRelease; | 17 var PortRelease = miscNatives.PortRelease; |
17 var PostMessage = miscNatives.PostMessage; | 18 var PostMessage = miscNatives.PostMessage; |
18 var BindToGC = miscNatives.BindToGC; | 19 var BindToGC = miscNatives.BindToGC; |
19 | 20 |
20 var chromeHidden = requireNative('chrome_hidden').GetChromeHidden(); | 21 var chromeHidden = requireNative('chrome_hidden').GetChromeHidden(); |
(...skipping 26 matching lines...) Expand all Loading... |
47 function PortImpl(portId, opt_name) { | 48 function PortImpl(portId, opt_name) { |
48 this.portId_ = portId; | 49 this.portId_ = portId; |
49 this.name = opt_name; | 50 this.name = opt_name; |
50 this.onDisconnect = new chrome.Event(); | 51 this.onDisconnect = new chrome.Event(); |
51 this.onMessage = new chrome.Event(); | 52 this.onMessage = new chrome.Event(); |
52 } | 53 } |
53 | 54 |
54 // Sends a message asynchronously to the context on the other end of this | 55 // Sends a message asynchronously to the context on the other end of this |
55 // port. | 56 // port. |
56 PortImpl.prototype.postMessage = function(msg) { | 57 PortImpl.prototype.postMessage = function(msg) { |
57 PostMessage(this.portId_, msg); | 58 // json.stringify doesn't support a root object which is undefined. |
| 59 if (msg === undefined) |
| 60 msg = null; |
| 61 PostMessage(this.portId_, json.stringify(msg)); |
58 }; | 62 }; |
59 | 63 |
60 // Disconnects the port from the other end. | 64 // Disconnects the port from the other end. |
61 PortImpl.prototype.disconnect = function() { | 65 PortImpl.prototype.disconnect = function() { |
62 CloseChannel(this.portId_, true); | 66 CloseChannel(this.portId_, true); |
63 this.destroy_(); | 67 this.destroy_(); |
64 }; | 68 }; |
65 | 69 |
66 PortImpl.prototype.destroy_ = function() { | 70 PortImpl.prototype.destroy_ = function() { |
67 var portId = this.portId_; | 71 var portId = this.portId_; |
(...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
255 } finally { | 259 } finally { |
256 port.destroy_(); | 260 port.destroy_(); |
257 lastError.clear(chrome); | 261 lastError.clear(chrome); |
258 } | 262 } |
259 } | 263 } |
260 }; | 264 }; |
261 | 265 |
262 // Called by native code when a message has been sent to the given port. | 266 // Called by native code when a message has been sent to the given port. |
263 chromeHidden.Port.dispatchOnMessage = function(msg, portId) { | 267 chromeHidden.Port.dispatchOnMessage = function(msg, portId) { |
264 var port = ports[portId]; | 268 var port = ports[portId]; |
265 if (port) | 269 if (port) { |
| 270 if (msg) { |
| 271 msg = json.parse(msg); |
| 272 } |
266 port.onMessage.dispatch(msg, port); | 273 port.onMessage.dispatch(msg, port); |
| 274 } |
267 }; | 275 }; |
268 | 276 |
269 // Shared implementation used by tabs.sendMessage and runtime.sendMessage. | 277 // Shared implementation used by tabs.sendMessage and runtime.sendMessage. |
270 chromeHidden.Port.sendMessageImpl = function(port, request, | 278 chromeHidden.Port.sendMessageImpl = function(port, request, |
271 responseCallback) { | 279 responseCallback) { |
272 if (port.name != chromeHidden.kNativeMessageChannel) | 280 if (port.name != chromeHidden.kNativeMessageChannel) |
273 port.postMessage(request); | 281 port.postMessage(request); |
274 | 282 |
275 if (port.name == chromeHidden.kMessageChannel && !responseCallback) { | 283 if (port.name == chromeHidden.kMessageChannel && !responseCallback) { |
276 // TODO(mpcomplete): Do this for the old sendRequest API too, after | 284 // TODO(mpcomplete): Do this for the old sendRequest API too, after |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
324 var targetId = null; | 332 var targetId = null; |
325 if (lastArg >= 0) | 333 if (lastArg >= 0) |
326 targetId = args[lastArg--]; | 334 targetId = args[lastArg--]; |
327 | 335 |
328 if (lastArg != -1) | 336 if (lastArg != -1) |
329 throw new Error('Invalid arguments to ' + functionName + '.'); | 337 throw new Error('Invalid arguments to ' + functionName + '.'); |
330 return [targetId, request, responseCallback]; | 338 return [targetId, request, responseCallback]; |
331 } | 339 } |
332 | 340 |
333 exports.sendMessageUpdateArguments = sendMessageUpdateArguments; | 341 exports.sendMessageUpdateArguments = sendMessageUpdateArguments; |
OLD | NEW |