| 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 | 6 |
| 7 // TODO(kalman): factor requiring chrome out of here. | 7 // TODO(kalman): factor requiring chrome out of here. |
| 8 var chrome = requireNative('chrome').GetChrome(); | 8 var chrome = requireNative('chrome').GetChrome(); |
| 9 var Event = require('event_bindings').Event; | 9 var Event = require('event_bindings').Event; |
| 10 var lastError = require('lastError'); | 10 var lastError = require('lastError'); |
| 11 var logActivity = requireNative('activityLogger'); | 11 var logActivity = requireNative('activityLogger'); |
| 12 var logging = requireNative('logging'); | 12 var logging = requireNative('logging'); |
| 13 var messagingNatives = requireNative('messaging_natives'); | 13 var messagingNatives = requireNative('messaging_natives'); |
| 14 var processNatives = requireNative('process'); | 14 var processNatives = requireNative('process'); |
| 15 var unloadEvent = require('unload_event'); | |
| 16 var utils = require('utils'); | 15 var utils = require('utils'); |
| 17 var messagingUtils = require('messaging_utils'); | 16 var messagingUtils = require('messaging_utils'); |
| 18 | 17 |
| 19 // The reserved channel name for the sendRequest/send(Native)Message APIs. | 18 // The reserved channel name for the sendRequest/send(Native)Message APIs. |
| 20 // Note: sendRequest is deprecated. | 19 // Note: sendRequest is deprecated. |
| 21 var kRequestChannel = "chrome.extension.sendRequest"; | 20 var kRequestChannel = "chrome.extension.sendRequest"; |
| 22 var kMessageChannel = "chrome.runtime.sendMessage"; | 21 var kMessageChannel = "chrome.runtime.sendMessage"; |
| 23 var kNativeMessageChannel = "chrome.runtime.sendNativeMessage"; | 22 var kNativeMessageChannel = "chrome.runtime.sendNativeMessage"; |
| 24 | 23 |
| 25 // Map of port IDs to port object. | 24 // Map of port IDs to port object. |
| 26 var ports = {}; | 25 var ports = {}; |
| 27 | 26 |
| 28 // Map of port IDs to unloadEvent listeners. Keep track of these to free the | |
| 29 // unloadEvent listeners when ports are closed. | |
| 30 var portReleasers = {}; | |
| 31 | |
| 32 // Change even to odd and vice versa, to get the other side of a given | 27 // Change even to odd and vice versa, to get the other side of a given |
| 33 // channel. | 28 // channel. |
| 34 function getOppositePortId(portId) { return portId ^ 1; } | 29 function getOppositePortId(portId) { return portId ^ 1; } |
| 35 | 30 |
| 36 // Port object. Represents a connection to another script context through | 31 // Port object. Represents a connection to another script context through |
| 37 // which messages can be passed. | 32 // which messages can be passed. |
| 38 function PortImpl(portId, opt_name) { | 33 function PortImpl(portId, opt_name) { |
| 39 this.portId_ = portId; | 34 this.portId_ = portId; |
| 40 this.name = opt_name; | 35 this.name = opt_name; |
| 41 | 36 |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 78 | 73 |
| 79 PortImpl.prototype.destroy_ = function() { | 74 PortImpl.prototype.destroy_ = function() { |
| 80 var portId = this.portId_; | 75 var portId = this.portId_; |
| 81 | 76 |
| 82 if (this.onDestroy_) | 77 if (this.onDestroy_) |
| 83 this.onDestroy_(); | 78 this.onDestroy_(); |
| 84 privates(this.onDisconnect).impl.destroy_(); | 79 privates(this.onDisconnect).impl.destroy_(); |
| 85 privates(this.onMessage).impl.destroy_(); | 80 privates(this.onMessage).impl.destroy_(); |
| 86 | 81 |
| 87 messagingNatives.PortRelease(portId); | 82 messagingNatives.PortRelease(portId); |
| 88 unloadEvent.removeListener(portReleasers[portId]); | |
| 89 | |
| 90 delete ports[portId]; | 83 delete ports[portId]; |
| 91 delete portReleasers[portId]; | |
| 92 }; | 84 }; |
| 93 | 85 |
| 94 // Returns true if the specified port id is in this context. This is used by | 86 // Returns true if the specified port id is in this context. This is used by |
| 95 // the C++ to avoid creating the javascript message for all the contexts that | 87 // the C++ to avoid creating the javascript message for all the contexts that |
| 96 // don't care about a particular message. | 88 // don't care about a particular message. |
| 97 function hasPort(portId) { | 89 function hasPort(portId) { |
| 98 return portId in ports; | 90 return portId in ports; |
| 99 }; | 91 }; |
| 100 | 92 |
| 101 // Hidden port creation function. We don't want to expose an API that lets | 93 // Hidden port creation function. We don't want to expose an API that lets |
| 102 // people add arbitrary port IDs to the port list. | 94 // people add arbitrary port IDs to the port list. |
| 103 function createPort(portId, opt_name) { | 95 function createPort(portId, opt_name) { |
| 104 if (ports[portId]) | 96 if (ports[portId]) |
| 105 throw new Error("Port '" + portId + "' already exists."); | 97 throw new Error("Port '" + portId + "' already exists."); |
| 106 var port = new Port(portId, opt_name); | 98 var port = new Port(portId, opt_name); |
| 107 ports[portId] = port; | 99 ports[portId] = port; |
| 108 portReleasers[portId] = $Function.bind(messagingNatives.PortRelease, | |
| 109 this, | |
| 110 portId); | |
| 111 unloadEvent.addListener(portReleasers[portId]); | |
| 112 messagingNatives.PortAddRef(portId); | 100 messagingNatives.PortAddRef(portId); |
| 113 return port; | 101 return port; |
| 114 }; | 102 }; |
| 115 | 103 |
| 116 // Helper function for dispatchOnRequest. | 104 // Helper function for dispatchOnRequest. |
| 117 function handleSendRequestError(isSendMessage, | 105 function handleSendRequestError(isSendMessage, |
| 118 responseCallbackPreserved, | 106 responseCallbackPreserved, |
| 119 sourceExtensionId, | 107 sourceExtensionId, |
| 120 targetExtensionId, | 108 targetExtensionId, |
| 121 sourceUrl) { | 109 sourceUrl) { |
| (...skipping 273 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 395 exports.Port = Port; | 383 exports.Port = Port; |
| 396 exports.createPort = createPort; | 384 exports.createPort = createPort; |
| 397 exports.sendMessageImpl = sendMessageImpl; | 385 exports.sendMessageImpl = sendMessageImpl; |
| 398 exports.sendMessageUpdateArguments = sendMessageUpdateArguments; | 386 exports.sendMessageUpdateArguments = sendMessageUpdateArguments; |
| 399 | 387 |
| 400 // For C++ code to call. | 388 // For C++ code to call. |
| 401 exports.hasPort = hasPort; | 389 exports.hasPort = hasPort; |
| 402 exports.dispatchOnConnect = dispatchOnConnect; | 390 exports.dispatchOnConnect = dispatchOnConnect; |
| 403 exports.dispatchOnDisconnect = dispatchOnDisconnect; | 391 exports.dispatchOnDisconnect = dispatchOnDisconnect; |
| 404 exports.dispatchOnMessage = dispatchOnMessage; | 392 exports.dispatchOnMessage = dispatchOnMessage; |
| OLD | NEW |