Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(758)

Side by Side Diff: chrome/renderer/resources/renderer_extension_bindings.js

Issue 262016: Implement chrome.extension.connectExternal and fix various API inconsistencies. (Closed)
Patch Set: addressed comments Created 11 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 // -----------------------------------------------------------------------------
6 // NOTE: If you change this file you need to touch renderer_resources.grd to
7 // have your change take effect.
8 // -----------------------------------------------------------------------------
9
10 // This script contains unprivileged javascript APIs related to chrome 5 // This script contains unprivileged javascript APIs related to chrome
11 // 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
12 // scripts or toolstrips. 7 // scripts or toolstrips.
13 // 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.
14 // TODO(mpcomplete): we also load this in regular web pages, but don't need 9 // TODO(mpcomplete): we also load this in regular web pages, but don't need
15 // to. 10 // to.
16 11
17 var chrome = chrome || {}; 12 var chrome = chrome || {};
18 (function () { 13 (function () {
19 native function OpenChannelToExtension(id, name); 14 native function OpenChannelToExtension(sourceId, targetId, name);
20 native function CloseChannel(portId); 15 native function CloseChannel(portId);
21 native function PortAddRef(portId); 16 native function PortAddRef(portId);
22 native function PortRelease(portId); 17 native function PortRelease(portId);
23 native function PostMessage(portId, msg); 18 native function PostMessage(portId, msg);
24 native function GetChromeHidden(); 19 native function GetChromeHidden();
25 20
26 var chromeHidden = GetChromeHidden(); 21 var chromeHidden = GetChromeHidden();
27 22
28 // Map of port IDs to port object. 23 // Map of port IDs to port object.
29 var ports = {}; 24 var ports = {};
(...skipping 19 matching lines...) Expand all
49 ports[portId] = port; 44 ports[portId] = port;
50 chromeHidden.onUnload.addListener(function() { 45 chromeHidden.onUnload.addListener(function() {
51 PortRelease(portId); 46 PortRelease(portId);
52 }); 47 });
53 PortAddRef(portId); 48 PortAddRef(portId);
54 return port; 49 return port;
55 } 50 }
56 51
57 // Called by native code when a channel has been opened to this context. 52 // Called by native code when a channel has been opened to this context.
58 chromeHidden.Port.dispatchOnConnect = function(portId, channelName, tab, 53 chromeHidden.Port.dispatchOnConnect = function(portId, channelName, tab,
59 extensionId) { 54 sourceExtensionId,
55 targetExtensionId) {
60 // Only create a new Port if someone is actually listening for a connection. 56 // Only create a new Port if someone is actually listening for a connection.
61 // In addition to being an optimization, this also fixes a bug where if 2 57 // In addition to being an optimization, this also fixes a bug where if 2
62 // channels were opened to and from the same process, closing one would 58 // channels were opened to and from the same process, closing one would
63 // close both. 59 // close both.
64 var connectEvent = "channel-connect:" + extensionId; 60 if (targetExtensionId != chromeHidden.extensionId)
65 if (chromeHidden.Event.hasListener(connectEvent)) { 61 return; // not for us
62
63 // Determine whether this is coming from another extension, and use the
64 // right event.
65 var connectEvent = (sourceExtensionId == chromeHidden.extensionId ?
66 chrome.extension.onConnect : chrome.extension.onConnectExternal);
67 if (connectEvent.hasListeners()) {
66 var port = chromeHidden.Port.createPort(portId, channelName); 68 var port = chromeHidden.Port.createPort(portId, channelName);
67 if (tab) { 69 if (tab) {
68 tab = JSON.parse(tab); 70 tab = JSON.parse(tab);
69 } 71 }
70 port.tab = tab; 72 port.sender = {tab: tab, id: sourceExtensionId};
71 chromeHidden.Event.dispatch(connectEvent, [port]); 73 // TODO(EXTENSIONS_DEPRECATED): port.tab is obsolete.
74 port.tab = port.sender.tab;
75 connectEvent.dispatch(port);
72 } 76 }
73 }; 77 };
74 78
75 // Called by native code when a channel has been closed. 79 // Called by native code when a channel has been closed.
76 chromeHidden.Port.dispatchOnDisconnect = function(portId) { 80 chromeHidden.Port.dispatchOnDisconnect = function(portId) {
77 var port = ports[portId]; 81 var port = ports[portId];
78 if (port) { 82 if (port) {
79 port.onDisconnect.dispatch(port); 83 port.onDisconnect.dispatch(port);
80 delete ports[portId]; 84 delete ports[portId];
81 } 85 }
(...skipping 18 matching lines...) Expand all
100 msg = null; 104 msg = null;
101 PostMessage(this.portId_, JSON.stringify(msg)); 105 PostMessage(this.portId_, JSON.stringify(msg));
102 }; 106 };
103 107
104 // Disconnects the port from the other end. 108 // Disconnects the port from the other end.
105 chrome.Port.prototype.disconnect = function() { 109 chrome.Port.prototype.disconnect = function() {
106 delete ports[this.portId_]; 110 delete ports[this.portId_];
107 CloseChannel(this.portId_); 111 CloseChannel(this.portId_);
108 } 112 }
109 113
110 // Extension object. 114 // This function is called on context initialization for both content scripts
111 chrome.Extension = function(id) { 115 // and extension contexts.
112 this.id_ = id; 116 chrome.initExtension = function(extensionId) {
113 this.onConnect = new chrome.Event('channel-connect:' + id); 117 delete chrome.initExtension;
114 }; 118 chromeHidden.extensionId = extensionId;
115 119
116 // Opens a message channel to the extension. Returns a Port for 120 chrome.extension = chrome.extension || {};
117 // message passing.
118 chrome.Extension.prototype.connect = function(connectInfo) {
119 var name = "";
120 if (connectInfo) {
121 name = connectInfo.name || name;
122 }
123 var portId = OpenChannelToExtension(this.id_, name);
124 if (portId == -1)
125 throw new Error("No such extension: '" + this.id_ + "'");
126 return chromeHidden.Port.createPort(portId, name);
127 };
128 121
129 // Returns a resource URL that can be used to fetch a resource from this 122 // TODO(EXTENSIONS_DEPRECATED): chrome.self is obsolete.
130 // extension. 123 // http://code.google.com/p/chromium/issues/detail?id=16356
131 chrome.Extension.prototype.getURL = function(path) { 124 chrome.self = chrome.extension;
132 return "chrome-extension://" + this.id_ + "/" + path;
133 };
134 125
135 chrome.self = chrome.self || {}; 126 // Events for when a message channel is opened to our extension.
127 chrome.extension.onConnect = new chrome.Event();
128 chrome.extension.onConnectExternal = new chrome.Event();
129
130 // Opens a message channel to the given target extension, or the current one
131 // if unspecified. Returns a Port for message passing.
132 chrome.extension.connect = function(targetId_opt, connectInfo_opt) {
133 var name = "";
134 var targetId = extensionId;
135 var nextArg = 0;
136 if (typeof(arguments[nextArg]) == "string")
137 targetId = arguments[nextArg++];
138 if (typeof(arguments[nextArg]) == "object")
139 name = arguments[nextArg++].name || name;
140
141 var portId = OpenChannelToExtension(extensionId, targetId, name);
142 if (portId >= 0)
143 return chromeHidden.Port.createPort(portId, name);
144 throw new Error("Error connecting to extension '" + targetId + "'");
145 };
146
147 // Returns a resource URL that can be used to fetch a resource from this
148 // extension.
149 chrome.extension.getURL = function(path) {
150 return "chrome-extension://" + extensionId + "/" + path;
151 };
152 }
136 })(); 153 })();
OLDNEW
« no previous file with comments | « chrome/renderer/resources/extension_process_bindings.js ('k') | chrome/renderer/user_script_slave.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698