| 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 json = require('json'); |
| 12 var lastError = require('lastError'); | 12 var lastError = require('lastError'); |
| 13 var miscNatives = requireNative('miscellaneous_bindings'); | 13 var miscNatives = requireNative('miscellaneous_bindings'); |
| 14 var chrome = requireNative('chrome').GetChrome(); | 14 var chrome = requireNative('chrome').GetChrome(); |
| 15 var CloseChannel = miscNatives.CloseChannel; | 15 var CloseChannel = miscNatives.CloseChannel; |
| 16 var PortAddRef = miscNatives.PortAddRef; | 16 var PortAddRef = miscNatives.PortAddRef; |
| 17 var PortRelease = miscNatives.PortRelease; | 17 var PortRelease = miscNatives.PortRelease; |
| 18 var PostMessage = miscNatives.PostMessage; | 18 var PostMessage = miscNatives.PostMessage; |
| 19 var BindToGC = miscNatives.BindToGC; | 19 var BindToGC = miscNatives.BindToGC; |
| 20 | 20 |
| 21 var chromeHidden = requireNative('chrome_hidden').GetChromeHidden(); | 21 var chromeHidden = requireNative('chrome_hidden').GetChromeHidden(); |
| 22 | 22 |
| 23 var processNatives = requireNative('process'); | 23 var processNatives = requireNative('process'); |
| 24 var manifestVersion = processNatives.GetManifestVersion(); | 24 var manifestVersion = processNatives.GetManifestVersion(); |
| 25 var extensionId = processNatives.GetExtensionId(); | 25 var extensionId = processNatives.GetExtensionId(); |
| 26 | 26 |
| 27 var logActivity = requireNative('activityLogger').LogActivity; |
| 28 |
| 27 // The reserved channel name for the sendRequest/sendMessage APIs. | 29 // The reserved channel name for the sendRequest/sendMessage APIs. |
| 28 // Note: sendRequest is deprecated. | 30 // Note: sendRequest is deprecated. |
| 29 chromeHidden.kRequestChannel = "chrome.extension.sendRequest"; | 31 chromeHidden.kRequestChannel = "chrome.extension.sendRequest"; |
| 30 chromeHidden.kMessageChannel = "chrome.runtime.sendMessage"; | 32 chromeHidden.kMessageChannel = "chrome.runtime.sendMessage"; |
| 31 chromeHidden.kNativeMessageChannel = "chrome.runtime.sendNativeMessage"; | 33 chromeHidden.kNativeMessageChannel = "chrome.runtime.sendNativeMessage"; |
| 32 | 34 |
| 33 // Map of port IDs to port object. | 35 // Map of port IDs to port object. |
| 34 var ports = {}; | 36 var ports = {}; |
| 35 | 37 |
| 36 // Map of port IDs to chromeHidden.onUnload listeners. Keep track of these | 38 // Map of port IDs to chromeHidden.onUnload listeners. Keep track of these |
| (...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 166 responseCallbackPreserved = | 168 responseCallbackPreserved = |
| 167 rv && rv.results && rv.results.indexOf(true) > -1; | 169 rv && rv.results && rv.results.indexOf(true) > -1; |
| 168 if (!responseCallbackPreserved && port) { | 170 if (!responseCallbackPreserved && port) { |
| 169 // If they didn't access the response callback, they're not | 171 // If they didn't access the response callback, they're not |
| 170 // going to send a response, so clean up the port immediately. | 172 // going to send a response, so clean up the port immediately. |
| 171 port.destroy_(); | 173 port.destroy_(); |
| 172 port = null; | 174 port = null; |
| 173 } | 175 } |
| 174 } | 176 } |
| 175 }); | 177 }); |
| 178 var eventName = (isSendMessage ? |
| 179 (isExternal ? |
| 180 "runtime.onMessageExternal" : "runtime.onMessage") : |
| 181 (isExternal ? |
| 182 "extension.onRequestExternal" : "extension.onRequest")); |
| 183 logActivity("EVENT", |
| 184 targetExtensionId, |
| 185 eventName, |
| 186 [sourceExtensionId]); |
| 176 return true; | 187 return true; |
| 177 } | 188 } |
| 178 return false; | 189 return false; |
| 179 } | 190 } |
| 180 | 191 |
| 181 // Called by native code when a channel has been opened to this context. | 192 // Called by native code when a channel has been opened to this context. |
| 182 chromeHidden.Port.dispatchOnConnect = function(portId, channelName, tab, | 193 chromeHidden.Port.dispatchOnConnect = function(portId, channelName, tab, |
| 183 sourceExtensionId, | 194 sourceExtensionId, |
| 184 targetExtensionId) { | 195 targetExtensionId) { |
| 185 // Only create a new Port if someone is actually listening for a connection. | 196 // Only create a new Port if someone is actually listening for a connection. |
| (...skipping 22 matching lines...) Expand all Loading... |
| 208 } | 219 } |
| 209 | 220 |
| 210 var connectEvent = (isExternal ? | 221 var connectEvent = (isExternal ? |
| 211 chrome.runtime.onConnectExternal : chrome.runtime.onConnect); | 222 chrome.runtime.onConnectExternal : chrome.runtime.onConnect); |
| 212 if (connectEvent.hasListeners()) { | 223 if (connectEvent.hasListeners()) { |
| 213 var port = chromeHidden.Port.createPort(portId, channelName); | 224 var port = chromeHidden.Port.createPort(portId, channelName); |
| 214 port.sender = sender; | 225 port.sender = sender; |
| 215 if (manifestVersion < 2) | 226 if (manifestVersion < 2) |
| 216 port.tab = port.sender.tab; | 227 port.tab = port.sender.tab; |
| 217 | 228 |
| 229 var eventName = (isExternal ? |
| 230 "runtime.onConnectExternal" : "runtime.onConnect"); |
| 218 connectEvent.dispatch(port); | 231 connectEvent.dispatch(port); |
| 232 logActivity("EVENT", |
| 233 targetExtensionId, |
| 234 eventName, |
| 235 [sourceExtensionId]); |
| 219 return true; | 236 return true; |
| 220 } | 237 } |
| 221 return false; | 238 return false; |
| 222 }; | 239 }; |
| 223 | 240 |
| 224 // Called by native code when a channel has been closed. | 241 // Called by native code when a channel has been closed. |
| 225 chromeHidden.Port.dispatchOnDisconnect = function( | 242 chromeHidden.Port.dispatchOnDisconnect = function( |
| 226 portId, errorMessage) { | 243 portId, errorMessage) { |
| 227 var port = ports[portId]; | 244 var port = ports[portId]; |
| 228 if (port) { | 245 if (port) { |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 308 var targetId = null; | 325 var targetId = null; |
| 309 if (lastArg >= 0) | 326 if (lastArg >= 0) |
| 310 targetId = args[lastArg--]; | 327 targetId = args[lastArg--]; |
| 311 | 328 |
| 312 if (lastArg != -1) | 329 if (lastArg != -1) |
| 313 throw new Error('Invalid arguments to ' + functionName + '.'); | 330 throw new Error('Invalid arguments to ' + functionName + '.'); |
| 314 return [targetId, request, responseCallback]; | 331 return [targetId, request, responseCallback]; |
| 315 } | 332 } |
| 316 | 333 |
| 317 exports.sendMessageUpdateArguments = sendMessageUpdateArguments; | 334 exports.sendMessageUpdateArguments = sendMessageUpdateArguments; |
| OLD | NEW |