| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 // ----------------------------------------------------------------------------- | 5 // ----------------------------------------------------------------------------- |
| 6 // NOTE: If you change this file you need to touch renderer_resources.grd to | 6 // NOTE: If you change this file you need to touch renderer_resources.grd to |
| 7 // have your change take effect. | 7 // have your change take effect. |
| 8 // ----------------------------------------------------------------------------- | 8 // ----------------------------------------------------------------------------- |
| 9 | 9 |
| 10 var chrome = chrome || {}; | 10 var chrome = chrome || {}; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 // Port object. Represents a connection to another script context through | 22 // Port object. Represents a connection to another script context through |
| 23 // which messages can be passed. | 23 // which messages can be passed. |
| 24 chrome.Port = function(portId) { | 24 chrome.Port = function(portId) { |
| 25 if (ports[portId]) { | 25 if (ports[portId]) { |
| 26 throw new Error("Port '" + portId + "' already exists."); | 26 throw new Error("Port '" + portId + "' already exists."); |
| 27 } | 27 } |
| 28 this.portId_ = portId; // TODO(mpcomplete): readonly | 28 this.portId_ = portId; // TODO(mpcomplete): readonly |
| 29 this.onDisconnect = new chrome.Event(); | 29 this.onDisconnect = new chrome.Event(); |
| 30 this.onMessage = new chrome.Event(); | 30 this.onMessage = new chrome.Event(); |
| 31 ports[portId] = this; | 31 ports[portId] = this; |
| 32 | 32 |
| 33 var port = this; |
| 33 chromeHidden.onUnload.addListener(function() { | 34 chromeHidden.onUnload.addListener(function() { |
| 34 this.disconnect(); | 35 port.disconnect(); |
| 35 }); | 36 }); |
| 36 }; | 37 }; |
| 37 | 38 |
| 38 chromeHidden.Port = {}; | 39 chromeHidden.Port = {}; |
| 39 | 40 |
| 40 // Called by native code when a channel has been opened to this context. | 41 // Called by native code when a channel has been opened to this context. |
| 41 chromeHidden.Port.dispatchOnConnect = function(portId, tab) { | 42 chromeHidden.Port.dispatchOnConnect = function(portId, tab, extensionId) { |
| 42 var port = new chrome.Port(portId); | 43 // Only create a new Port if someone is actually listening for a connection. |
| 43 if (tab) { | 44 // In addition to being an optimization, this also fixes a bug where if 2 |
| 44 tab = JSON.parse(tab); | 45 // channels were opened to and from the same process, closing one would |
| 46 // close both. |
| 47 var connectEvent = "channel-connect:" + (extensionId || ""); |
| 48 if (chromeHidden.Event.hasListener(connectEvent)) { |
| 49 var port = new chrome.Port(portId); |
| 50 if (tab) { |
| 51 tab = JSON.parse(tab); |
| 52 } |
| 53 port.tab = tab; |
| 54 chromeHidden.Event.dispatch(connectEvent, [port]); |
| 45 } | 55 } |
| 46 port.tab = tab; | |
| 47 chromeHidden.Event.dispatch("channel-connect", [port]); | |
| 48 }; | 56 }; |
| 49 | 57 |
| 50 // Called by native code when a channel has been closed. | 58 // Called by native code when a channel has been closed. |
| 51 chromeHidden.Port.dispatchOnDisconnect = function(portId) { | 59 chromeHidden.Port.dispatchOnDisconnect = function(portId) { |
| 52 var port = ports[portId]; | 60 var port = ports[portId]; |
| 53 if (port) { | 61 if (port) { |
| 54 port.onDisconnect.dispatch(port); | 62 port.onDisconnect.dispatch(port); |
| 55 delete ports[portId]; | 63 delete ports[portId]; |
| 56 } | 64 } |
| 57 }; | 65 }; |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 95 throw new Error("No such extension: '" + this.id_ + "'"); | 103 throw new Error("No such extension: '" + this.id_ + "'"); |
| 96 return new chrome.Port(portId); | 104 return new chrome.Port(portId); |
| 97 }; | 105 }; |
| 98 | 106 |
| 99 // Returns a resource URL that can be used to fetch a resource from this | 107 // Returns a resource URL that can be used to fetch a resource from this |
| 100 // extension. | 108 // extension. |
| 101 chrome.Extension.prototype.getURL = function(path) { | 109 chrome.Extension.prototype.getURL = function(path) { |
| 102 return "chrome-extension://" + this.id_ + "/" + path; | 110 return "chrome-extension://" + this.id_ + "/" + path; |
| 103 }; | 111 }; |
| 104 })(); | 112 })(); |
| OLD | NEW |