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