| 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 || {}; |
| 11 (function () { | 11 (function () { |
| 12 native function OpenChannelToExtension(id); | 12 native function OpenChannelToExtension(id); |
| 13 native function CloseChannel(portId); |
| 13 native function PostMessage(portId, msg); | 14 native function PostMessage(portId, msg); |
| 14 | 15 |
| 15 // Port object. Represents a connection to another script context through | 16 // Port object. Represents a connection to another script context through |
| 16 // which messages can be passed. | 17 // which messages can be passed. |
| 17 chrome.Port = function(portId) { | 18 chrome.Port = function(portId) { |
| 18 if (chrome.Port.ports_[portId]) { | 19 if (chrome.Port.ports_[portId]) { |
| 19 throw new Error("Port '" + portId + "' already exists."); | 20 throw new Error("Port '" + portId + "' already exists."); |
| 20 } | 21 } |
| 21 this.portId_ = portId; // TODO(mpcomplete): readonly | 22 this.portId_ = portId; // TODO(mpcomplete): readonly |
| 22 this.onDisconnect = new chrome.Event(); | 23 this.onDisconnect = new chrome.Event(); |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 59 | 60 |
| 60 // Sends a message asynchronously to the context on the other end of this | 61 // Sends a message asynchronously to the context on the other end of this |
| 61 // port. | 62 // port. |
| 62 chrome.Port.prototype.postMessage = function(msg) { | 63 chrome.Port.prototype.postMessage = function(msg) { |
| 63 // JSON.stringify doesn't support a root object which is undefined. | 64 // JSON.stringify doesn't support a root object which is undefined. |
| 64 if (msg === undefined) | 65 if (msg === undefined) |
| 65 msg = null; | 66 msg = null; |
| 66 PostMessage(this.portId_, JSON.stringify(msg)); | 67 PostMessage(this.portId_, JSON.stringify(msg)); |
| 67 }; | 68 }; |
| 68 | 69 |
| 70 // Disconnects the port from the other end. |
| 71 chrome.Port.prototype.disconnect = function() { |
| 72 delete chrome.Port.ports_[this.portId_]; |
| 73 CloseChannel(this.portId_); |
| 74 } |
| 75 |
| 69 // Extension object. | 76 // Extension object. |
| 70 chrome.Extension = function(id) { | 77 chrome.Extension = function(id) { |
| 71 this.id_ = id; | 78 this.id_ = id; |
| 72 }; | 79 }; |
| 73 | 80 |
| 74 // Opens a message channel to the extension. Returns a Port for | 81 // Opens a message channel to the extension. Returns a Port for |
| 75 // message passing. | 82 // message passing. |
| 76 chrome.Extension.prototype.connect = function() { | 83 chrome.Extension.prototype.connect = function() { |
| 77 var portId = OpenChannelToExtension(this.id_); | 84 var portId = OpenChannelToExtension(this.id_); |
| 78 if (portId == -1) | 85 if (portId == -1) |
| 79 throw new Error("No such extension: '" + this.id_ + "'"); | 86 throw new Error("No such extension: '" + this.id_ + "'"); |
| 80 return new chrome.Port(portId); | 87 return new chrome.Port(portId); |
| 81 }; | 88 }; |
| 82 | 89 |
| 83 // Returns a resource URL that can be used to fetch a resource from this | 90 // Returns a resource URL that can be used to fetch a resource from this |
| 84 // extension. | 91 // extension. |
| 85 chrome.Extension.prototype.getURL = function(path) { | 92 chrome.Extension.prototype.getURL = function(path) { |
| 86 return "chrome-extension://" + this.id_ + "/" + path; | 93 return "chrome-extension://" + this.id_ + "/" + path; |
| 87 }; | 94 }; |
| 95 |
| 96 chrome.onUnload_.addListener(function() { |
| 97 for (var i in chrome.Port.ports_) { |
| 98 chrome.Port.ports_[i].disconnect(); |
| 99 } |
| 100 }); |
| 88 })(); | 101 })(); |
| OLD | NEW |