| 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 script contains unprivileged javascript APIs related to chrome | 5 // This script contains unprivileged javascript APIs related to chrome |
| 6 // extensions. It is loaded by any extension-related context, such as content | 6 // extensions. It is loaded by any extension-related context, such as content |
| 7 // scripts or background pages. | 7 // scripts or background pages. |
| 8 // See user_script_slave.cc for script that is loaded by content scripts only. | 8 // See user_script_slave.cc for script that is loaded by content scripts only. |
| 9 | 9 |
| 10 require('json_schema'); | 10 require('json_schema'); |
| 11 require('event_bindings'); | 11 require('event_bindings'); |
| 12 var lastError = require('lastError'); | 12 var lastError = require('lastError'); |
| 13 var miscNatives = requireNative('miscellaneous_bindings'); | 13 var miscNatives = requireNative('miscellaneous_bindings'); |
| 14 var CloseChannel = miscNatives.CloseChannel; | 14 var CloseChannel = miscNatives.CloseChannel; |
| 15 var PortAddRef = miscNatives.PortAddRef; | 15 var PortAddRef = miscNatives.PortAddRef; |
| 16 var PortRelease = miscNatives.PortRelease; | 16 var PortRelease = miscNatives.PortRelease; |
| 17 var PostMessage = miscNatives.PostMessage; | 17 var PostMessage = miscNatives.PostMessage; |
| 18 var BindToGC = miscNatives.BindToGC; | 18 var BindToGC = miscNatives.BindToGC; |
| 19 | 19 |
| 20 var chromeHidden = requireNative('chrome_hidden').GetChromeHidden(); | 20 var chromeHidden = requireNative('chrome_hidden').GetChromeHidden(); |
| 21 | 21 |
| 22 var processNatives = requireNative('process'); | 22 var processNatives = requireNative('process'); |
| 23 var manifestVersion = processNatives.GetManifestVersion(); | 23 var manifestVersion = processNatives.GetManifestVersion(); |
| 24 var extensionId = processNatives.GetExtensionId(); | 24 var extensionId = processNatives.GetExtensionId(); |
| 25 | 25 |
| 26 // The reserved channel name for the sendRequest/sendMessage APIs. | 26 // The reserved channel name for the sendRequest/sendMessage APIs. |
| 27 // Note: sendRequest is deprecated. | 27 // Note: sendRequest is deprecated. |
| 28 chromeHidden.kRequestChannel = "chrome.extension.sendRequest"; | 28 chromeHidden.kRequestChannel = "chrome.extension.sendRequest"; |
| 29 chromeHidden.kMessageChannel = "chrome.extension.sendMessage"; | 29 chromeHidden.kMessageChannel = "chrome.extension.sendMessage"; |
| 30 chromeHidden.kNativeMessageChannel = "chrome.extension.sendNativeMessage"; | |
| 31 | 30 |
| 32 // Map of port IDs to port object. | 31 // Map of port IDs to port object. |
| 33 var ports = {}; | 32 var ports = {}; |
| 34 | 33 |
| 35 // Map of port IDs to chromeHidden.onUnload listeners. Keep track of these | 34 // Map of port IDs to chromeHidden.onUnload listeners. Keep track of these |
| 36 // to free the onUnload listeners when ports are closed. | 35 // to free the onUnload listeners when ports are closed. |
| 37 var portReleasers = {}; | 36 var portReleasers = {}; |
| 38 | 37 |
| 39 // Change even to odd and vice versa, to get the other side of a given | 38 // Change even to odd and vice versa, to get the other side of a given |
| 40 // channel. | 39 // channel. |
| (...skipping 210 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 251 if (msg) { | 250 if (msg) { |
| 252 msg = chromeHidden.JSON.parse(msg); | 251 msg = chromeHidden.JSON.parse(msg); |
| 253 } | 252 } |
| 254 port.onMessage.dispatch(msg, port); | 253 port.onMessage.dispatch(msg, port); |
| 255 } | 254 } |
| 256 }; | 255 }; |
| 257 | 256 |
| 258 // Shared implementation used by tabs.sendMessage and extension.sendMessage. | 257 // Shared implementation used by tabs.sendMessage and extension.sendMessage. |
| 259 chromeHidden.Port.sendMessageImpl = function(port, request, | 258 chromeHidden.Port.sendMessageImpl = function(port, request, |
| 260 responseCallback) { | 259 responseCallback) { |
| 261 if (port.name != chromeHidden.kNativeMessageChannel) | 260 port.postMessage(request); |
| 262 port.postMessage(request); | |
| 263 | 261 |
| 264 if (port.name != chromeHidden.kRequestChannel && !responseCallback) { | 262 if (port.name == chromeHidden.kMessageChannel && !responseCallback) { |
| 265 // TODO(mpcomplete): Do this for the old sendRequest API too, after | 263 // TODO(mpcomplete): Do this for the old sendRequest API too, after |
| 266 // verifying it doesn't break anything. | 264 // verifying it doesn't break anything. |
| 267 // Go ahead and disconnect immediately if the sender is not expecting | 265 // Go ahead and disconnect immediately if the sender is not expecting |
| 268 // a response. | 266 // a response. |
| 269 port.disconnect(); | 267 port.disconnect(); |
| 270 return; | 268 return; |
| 271 } | 269 } |
| 272 | 270 |
| 273 // Ensure the callback exists for the older sendRequest API. | 271 // Ensure the callback exists for the older sendRequest API. |
| 274 if (!responseCallback) | 272 if (!responseCallback) |
| (...skipping 10 matching lines...) Expand all Loading... |
| 285 }); | 283 }); |
| 286 port.onMessage.addListener(function(response) { | 284 port.onMessage.addListener(function(response) { |
| 287 try { | 285 try { |
| 288 responseCallback(response); | 286 responseCallback(response); |
| 289 } finally { | 287 } finally { |
| 290 port.disconnect(); | 288 port.disconnect(); |
| 291 port = null; | 289 port = null; |
| 292 } | 290 } |
| 293 }); | 291 }); |
| 294 } | 292 } |
| OLD | NEW |