OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 // chrome.runtime.messaging API implementation. | 5 // chrome.runtime.messaging API implementation. |
6 // TODO(robwu): Fix this indentation. | 6 // TODO(robwu): Fix this indentation. |
7 | 7 |
8 // TODO(kalman): factor requiring chrome out of here. | 8 // TODO(kalman): factor requiring chrome out of here. |
9 var chrome = requireNative('chrome').GetChrome(); | 9 var chrome = requireNative('chrome').GetChrome(); |
10 var Event = require('event_bindings').Event; | 10 var Event = require('event_bindings').Event; |
11 var lastError = require('lastError'); | 11 var lastError = require('lastError'); |
12 var logActivity = requireNative('activityLogger'); | 12 var logActivity = requireNative('activityLogger'); |
13 var logging = requireNative('logging'); | 13 var logging = requireNative('logging'); |
14 var messagingNatives = requireNative('messaging_natives'); | 14 var messagingNatives = requireNative('messaging_natives'); |
15 var processNatives = requireNative('process'); | 15 var processNatives = requireNative('process'); |
16 var utils = require('utils'); | 16 var utils = require('utils'); |
17 var messagingUtils = require('messaging_utils'); | 17 var messagingUtils = require('messaging_utils'); |
18 | 18 |
19 // The reserved channel name for the sendRequest/send(Native)Message APIs. | 19 // The reserved channel name for the sendRequest/send(Native)Message APIs. |
20 // Note: sendRequest is deprecated. | 20 // Note: sendRequest is deprecated. |
21 var kRequestChannel = "chrome.extension.sendRequest"; | 21 var kRequestChannel = "chrome.extension.sendRequest"; |
22 var kMessageChannel = "chrome.runtime.sendMessage"; | 22 var kMessageChannel = "chrome.runtime.sendMessage"; |
23 var kNativeMessageChannel = "chrome.runtime.sendNativeMessage"; | 23 var kNativeMessageChannel = "chrome.runtime.sendNativeMessage"; |
24 var kPortClosedError = 'Attempting to use a disconnected port object'; | 24 var kPortClosedError = 'Attempting to use a disconnected port object'; |
25 | 25 |
26 // Map of port IDs to port object. | 26 // Map of port IDs to port object. |
27 var ports = {__proto__: null}; | 27 var ports = {__proto__: null}; |
28 | 28 |
29 // Change even to odd and vice versa, to get the other side of a given | |
30 // channel. | |
31 function getOppositePortId(portId) { return portId ^ 1; } | |
32 | |
33 // Port object. Represents a connection to another script context through | 29 // Port object. Represents a connection to another script context through |
34 // which messages can be passed. | 30 // which messages can be passed. |
35 function PortImpl(portId, opt_name) { | 31 function PortImpl(portId, opt_name) { |
36 this.portId_ = portId; | 32 this.portId_ = portId; |
37 this.name = opt_name; | 33 this.name = opt_name; |
38 | 34 |
39 // Note: Keep these schemas in sync with the documentation in runtime.json | 35 // Note: Keep these schemas in sync with the documentation in runtime.json |
40 var portSchema = { | 36 var portSchema = { |
41 __proto__: null, | 37 __proto__: null, |
42 name: 'port', | 38 name: 'port', |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
97 messagingNatives.CloseChannel(this.portId_, false); | 93 messagingNatives.CloseChannel(this.portId_, false); |
98 this.destroy_(); | 94 this.destroy_(); |
99 }; | 95 }; |
100 | 96 |
101 PortImpl.prototype.destroy_ = function() { | 97 PortImpl.prototype.destroy_ = function() { |
102 privates(this.onDisconnect).impl.destroy_(); | 98 privates(this.onDisconnect).impl.destroy_(); |
103 privates(this.onMessage).impl.destroy_(); | 99 privates(this.onMessage).impl.destroy_(); |
104 delete ports[this.portId_]; | 100 delete ports[this.portId_]; |
105 }; | 101 }; |
106 | 102 |
107 // Returns true if the specified port id is in this context. This is used by | |
108 // the C++ to avoid creating the javascript message for all the contexts that | |
109 // don't care about a particular message. | |
110 function hasPort(portId) { | |
111 return $Object.hasOwnProperty(ports, portId); | |
112 }; | |
113 | |
114 // Hidden port creation function. We don't want to expose an API that lets | 103 // Hidden port creation function. We don't want to expose an API that lets |
115 // people add arbitrary port IDs to the port list. | 104 // people add arbitrary port IDs to the port list. |
116 function createPort(portId, opt_name) { | 105 function createPort(portId, opt_name) { |
117 if (ports[portId]) | 106 if (ports[portId]) |
118 throw new Error("Port '" + portId + "' already exists."); | 107 throw new Error("Port '" + portId + "' already exists."); |
119 var port = new Port(portId, opt_name); | 108 var port = new Port(portId, opt_name); |
120 ports[portId] = port; | 109 ports[portId] = port; |
121 return port; | 110 return port; |
122 }; | 111 }; |
123 | 112 |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
242 // Only create a new Port if someone is actually listening for a connection. | 231 // Only create a new Port if someone is actually listening for a connection. |
243 // In addition to being an optimization, this also fixes a bug where if 2 | 232 // In addition to being an optimization, this also fixes a bug where if 2 |
244 // channels were opened to and from the same process, closing one would | 233 // channels were opened to and from the same process, closing one would |
245 // close both. | 234 // close both. |
246 var extensionId = processNatives.GetExtensionId(); | 235 var extensionId = processNatives.GetExtensionId(); |
247 | 236 |
248 // messaging_bindings.cc should ensure that this method only gets called for | 237 // messaging_bindings.cc should ensure that this method only gets called for |
249 // the right extension. | 238 // the right extension. |
250 logging.CHECK(targetExtensionId == extensionId); | 239 logging.CHECK(targetExtensionId == extensionId); |
251 | 240 |
252 if (ports[getOppositePortId(portId)]) | |
253 return false; // this channel was opened by us, so ignore it | |
254 | |
255 // Determine whether this is coming from another extension, so we can use | 241 // Determine whether this is coming from another extension, so we can use |
256 // the right event. | 242 // the right event. |
257 var isExternal = sourceExtensionId != extensionId; | 243 var isExternal = sourceExtensionId != extensionId; |
258 | 244 |
259 var sender = {}; | 245 var sender = {}; |
260 if (sourceExtensionId != '') | 246 if (sourceExtensionId != '') |
261 sender.id = sourceExtensionId; | 247 sender.id = sourceExtensionId; |
262 if (sourceUrl) | 248 if (sourceUrl) |
263 sender.url = sourceUrl; | 249 sender.url = sourceUrl; |
264 if (sourceTab) | 250 if (sourceTab) |
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
427 | 413 |
428 exports.$set('kRequestChannel', kRequestChannel); | 414 exports.$set('kRequestChannel', kRequestChannel); |
429 exports.$set('kMessageChannel', kMessageChannel); | 415 exports.$set('kMessageChannel', kMessageChannel); |
430 exports.$set('kNativeMessageChannel', kNativeMessageChannel); | 416 exports.$set('kNativeMessageChannel', kNativeMessageChannel); |
431 exports.$set('Port', Port); | 417 exports.$set('Port', Port); |
432 exports.$set('createPort', createPort); | 418 exports.$set('createPort', createPort); |
433 exports.$set('sendMessageImpl', sendMessageImpl); | 419 exports.$set('sendMessageImpl', sendMessageImpl); |
434 exports.$set('sendMessageUpdateArguments', sendMessageUpdateArguments); | 420 exports.$set('sendMessageUpdateArguments', sendMessageUpdateArguments); |
435 | 421 |
436 // For C++ code to call. | 422 // For C++ code to call. |
437 exports.$set('hasPort', hasPort); | |
438 exports.$set('dispatchOnConnect', dispatchOnConnect); | 423 exports.$set('dispatchOnConnect', dispatchOnConnect); |
439 exports.$set('dispatchOnDisconnect', dispatchOnDisconnect); | 424 exports.$set('dispatchOnDisconnect', dispatchOnDisconnect); |
440 exports.$set('dispatchOnMessage', dispatchOnMessage); | 425 exports.$set('dispatchOnMessage', dispatchOnMessage); |
OLD | NEW |