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 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
175 }); | 176 }); |
176 return true; | 177 return true; |
177 } | 178 } |
178 return false; | 179 return false; |
179 } | 180 } |
180 | 181 |
181 // Called by native code when a channel has been opened to this context. | 182 // Called by native code when a channel has been opened to this context. |
182 chromeHidden.Port.dispatchOnConnect = function(portId, channelName, tab, | 183 chromeHidden.Port.dispatchOnConnect = function(portId, channelName, tab, |
183 sourceExtensionId, | 184 sourceExtensionId, |
184 targetExtensionId) { | 185 targetExtensionId) { |
186 // Native connections only require one end in JS. | |
187 if (targetExtensionId == "native") | |
Matt Perry
2012/08/09 02:13:00
This shouldn't happen if you remove the DispatchOn
eaugusti
2012/08/13 23:22:34
Done.
| |
188 return true; | |
189 | |
185 // Only create a new Port if someone is actually listening for a connection. | 190 // Only create a new Port if someone is actually listening for a connection. |
186 // In addition to being an optimization, this also fixes a bug where if 2 | 191 // In addition to being an optimization, this also fixes a bug where if 2 |
187 // channels were opened to and from the same process, closing one would | 192 // channels were opened to and from the same process, closing one would |
188 // close both. | 193 // close both. |
189 if (targetExtensionId != extensionId) | 194 if (targetExtensionId != extensionId) |
190 return false; // not for us | 195 return false; // not for us |
191 if (ports[getOppositePortId(portId)]) | 196 if (ports[getOppositePortId(portId)]) |
192 return false; // this channel was opened by us, so ignore it | 197 return false; // this channel was opened by us, so ignore it |
193 | 198 |
194 // Determine whether this is coming from another extension, so we can use | 199 // Determine whether this is coming from another extension, so we can use |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
283 }); | 288 }); |
284 port.onMessage.addListener(function(response) { | 289 port.onMessage.addListener(function(response) { |
285 try { | 290 try { |
286 responseCallback(response); | 291 responseCallback(response); |
287 } finally { | 292 } finally { |
288 port.disconnect(); | 293 port.disconnect(); |
289 port = null; | 294 port = null; |
290 } | 295 } |
291 }); | 296 }); |
292 } | 297 } |
OLD | NEW |