| 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 require('event_bindings'); | 11 require('event_bindings'); |
| 12 var json = require('json'); |
| 12 var lastError = require('lastError'); | 13 var lastError = require('lastError'); |
| 13 var miscNatives = requireNative('miscellaneous_bindings'); | 14 var miscNatives = requireNative('miscellaneous_bindings'); |
| 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(); |
| 21 | 22 |
| (...skipping 23 matching lines...) Expand all Loading... |
| 45 function PortImpl(portId, opt_name) { | 46 function PortImpl(portId, opt_name) { |
| 46 this.portId_ = portId; | 47 this.portId_ = portId; |
| 47 this.name = opt_name; | 48 this.name = opt_name; |
| 48 this.onDisconnect = new chrome.Event(); | 49 this.onDisconnect = new chrome.Event(); |
| 49 this.onMessage = new chrome.Event(); | 50 this.onMessage = new chrome.Event(); |
| 50 } | 51 } |
| 51 | 52 |
| 52 // Sends a message asynchronously to the context on the other end of this | 53 // Sends a message asynchronously to the context on the other end of this |
| 53 // port. | 54 // port. |
| 54 PortImpl.prototype.postMessage = function(msg) { | 55 PortImpl.prototype.postMessage = function(msg) { |
| 55 // JSON.stringify doesn't support a root object which is undefined. | 56 // json.stringify doesn't support a root object which is undefined. |
| 56 if (msg === undefined) | 57 if (msg === undefined) |
| 57 msg = null; | 58 msg = null; |
| 58 PostMessage(this.portId_, chromeHidden.JSON.stringify(msg)); | 59 PostMessage(this.portId_, json.stringify(msg)); |
| 59 }; | 60 }; |
| 60 | 61 |
| 61 // Disconnects the port from the other end. | 62 // Disconnects the port from the other end. |
| 62 PortImpl.prototype.disconnect = function() { | 63 PortImpl.prototype.disconnect = function() { |
| 63 CloseChannel(this.portId_, true); | 64 CloseChannel(this.portId_, true); |
| 64 this.destroy_(); | 65 this.destroy_(); |
| 65 }; | 66 }; |
| 66 | 67 |
| 67 PortImpl.prototype.destroy_ = function() { | 68 PortImpl.prototype.destroy_ = function() { |
| 68 var portId = this.portId_; | 69 var portId = this.portId_; |
| (...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 190 if (targetExtensionId != extensionId) | 191 if (targetExtensionId != extensionId) |
| 191 return false; // not for us | 192 return false; // not for us |
| 192 if (ports[getOppositePortId(portId)]) | 193 if (ports[getOppositePortId(portId)]) |
| 193 return false; // this channel was opened by us, so ignore it | 194 return false; // this channel was opened by us, so ignore it |
| 194 | 195 |
| 195 // Determine whether this is coming from another extension, so we can use | 196 // Determine whether this is coming from another extension, so we can use |
| 196 // the right event. | 197 // the right event. |
| 197 var isExternal = sourceExtensionId != extensionId; | 198 var isExternal = sourceExtensionId != extensionId; |
| 198 | 199 |
| 199 if (tab) | 200 if (tab) |
| 200 tab = chromeHidden.JSON.parse(tab); | 201 tab = json.parse(tab); |
| 201 var sender = {tab: tab, id: sourceExtensionId}; | 202 var sender = {tab: tab, id: sourceExtensionId}; |
| 202 | 203 |
| 203 // Special case for sendRequest/onRequest and sendMessage/onMessage. | 204 // Special case for sendRequest/onRequest and sendMessage/onMessage. |
| 204 if (channelName == chromeHidden.kRequestChannel || | 205 if (channelName == chromeHidden.kRequestChannel || |
| 205 channelName == chromeHidden.kMessageChannel) { | 206 channelName == chromeHidden.kMessageChannel) { |
| 206 return dispatchOnRequest(portId, channelName, sender, | 207 return dispatchOnRequest(portId, channelName, sender, |
| 207 sourceExtensionId, targetExtensionId, | 208 sourceExtensionId, targetExtensionId, |
| 208 isExternal); | 209 isExternal); |
| 209 } | 210 } |
| 210 | 211 |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 242 lastError.clear(); | 243 lastError.clear(); |
| 243 } | 244 } |
| 244 } | 245 } |
| 245 }; | 246 }; |
| 246 | 247 |
| 247 // Called by native code when a message has been sent to the given port. | 248 // Called by native code when a message has been sent to the given port. |
| 248 chromeHidden.Port.dispatchOnMessage = function(msg, portId) { | 249 chromeHidden.Port.dispatchOnMessage = function(msg, portId) { |
| 249 var port = ports[portId]; | 250 var port = ports[portId]; |
| 250 if (port) { | 251 if (port) { |
| 251 if (msg) { | 252 if (msg) { |
| 252 msg = chromeHidden.JSON.parse(msg); | 253 msg = json.parse(msg); |
| 253 } | 254 } |
| 254 port.onMessage.dispatch(msg, port); | 255 port.onMessage.dispatch(msg, port); |
| 255 } | 256 } |
| 256 }; | 257 }; |
| 257 | 258 |
| 258 // Shared implementation used by tabs.sendMessage and runtime.sendMessage. | 259 // Shared implementation used by tabs.sendMessage and runtime.sendMessage. |
| 259 chromeHidden.Port.sendMessageImpl = function(port, request, | 260 chromeHidden.Port.sendMessageImpl = function(port, request, |
| 260 responseCallback) { | 261 responseCallback) { |
| 261 if (port.name != chromeHidden.kNativeMessageChannel) | 262 if (port.name != chromeHidden.kNativeMessageChannel) |
| 262 port.postMessage(request); | 263 port.postMessage(request); |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 313 var targetId = null; | 314 var targetId = null; |
| 314 if (lastArg >= 0) | 315 if (lastArg >= 0) |
| 315 targetId = args[lastArg--]; | 316 targetId = args[lastArg--]; |
| 316 | 317 |
| 317 if (lastArg != -1) | 318 if (lastArg != -1) |
| 318 throw new Error('Invalid arguments to ' + functionName + '.'); | 319 throw new Error('Invalid arguments to ' + functionName + '.'); |
| 319 return [targetId, request, responseCallback]; | 320 return [targetId, request, responseCallback]; |
| 320 } | 321 } |
| 321 | 322 |
| 322 exports.sendMessageUpdateArguments = sendMessageUpdateArguments; | 323 exports.sendMessageUpdateArguments = sendMessageUpdateArguments; |
| OLD | NEW |