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"; |
30 | 31 |
31 // Map of port IDs to port object. | 32 // Map of port IDs to port object. |
32 var ports = {}; | 33 var ports = {}; |
33 | 34 |
34 // Map of port IDs to chromeHidden.onUnload listeners. Keep track of these | 35 // Map of port IDs to chromeHidden.onUnload listeners. Keep track of these |
35 // to free the onUnload listeners when ports are closed. | 36 // to free the onUnload listeners when ports are closed. |
36 var portReleasers = {}; | 37 var portReleasers = {}; |
37 | 38 |
38 // Change even to odd and vice versa, to get the other side of a given | 39 // Change even to odd and vice versa, to get the other side of a given |
39 // channel. | 40 // channel. |
(...skipping 210 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
250 if (msg) { | 251 if (msg) { |
251 msg = chromeHidden.JSON.parse(msg); | 252 msg = chromeHidden.JSON.parse(msg); |
252 } | 253 } |
253 port.onMessage.dispatch(msg, port); | 254 port.onMessage.dispatch(msg, port); |
254 } | 255 } |
255 }; | 256 }; |
256 | 257 |
257 // Shared implementation used by tabs.sendMessage and extension.sendMessage. | 258 // Shared implementation used by tabs.sendMessage and extension.sendMessage. |
258 chromeHidden.Port.sendMessageImpl = function(port, request, | 259 chromeHidden.Port.sendMessageImpl = function(port, request, |
259 responseCallback) { | 260 responseCallback) { |
260 port.postMessage(request); | 261 if (port.name != chromeHidden.kNativeMessageChannel) |
| 262 port.postMessage(request); |
261 | 263 |
262 if (port.name == chromeHidden.kMessageChannel && !responseCallback) { | 264 if (port.name != chromeHidden.kRequestChannel && !responseCallback) { |
263 // TODO(mpcomplete): Do this for the old sendRequest API too, after | 265 // TODO(mpcomplete): Do this for the old sendRequest API too, after |
264 // verifying it doesn't break anything. | 266 // verifying it doesn't break anything. |
265 // Go ahead and disconnect immediately if the sender is not expecting | 267 // Go ahead and disconnect immediately if the sender is not expecting |
266 // a response. | 268 // a response. |
267 port.disconnect(); | 269 port.disconnect(); |
268 return; | 270 return; |
269 } | 271 } |
270 | 272 |
271 // Ensure the callback exists for the older sendRequest API. | 273 // Ensure the callback exists for the older sendRequest API. |
272 if (!responseCallback) | 274 if (!responseCallback) |
(...skipping 10 matching lines...) Expand all Loading... |
283 }); | 285 }); |
284 port.onMessage.addListener(function(response) { | 286 port.onMessage.addListener(function(response) { |
285 try { | 287 try { |
286 responseCallback(response); | 288 responseCallback(response); |
287 } finally { | 289 } finally { |
288 port.disconnect(); | 290 port.disconnect(); |
289 port = null; | 291 port = null; |
290 } | 292 } |
291 }); | 293 }); |
292 } | 294 } |
OLD | NEW |