OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 // Custom bindings for the extension API. | 5 // Custom bindings for the extension API. |
6 | 6 |
7 var extensionNatives = requireNative('extension'); | 7 var extensionNatives = requireNative('extension'); |
8 var GetExtensionViews = extensionNatives.GetExtensionViews; | 8 var GetExtensionViews = extensionNatives.GetExtensionViews; |
9 var OpenChannelToExtension = extensionNatives.OpenChannelToExtension; | 9 var OpenChannelToExtension = extensionNatives.OpenChannelToExtension; |
10 var OpenChannelToNativeApp = extensionNatives.OpenChannelToNativeApp; | |
10 | 11 |
11 var chromeHidden = requireNative('chrome_hidden').GetChromeHidden(); | 12 var chromeHidden = requireNative('chrome_hidden').GetChromeHidden(); |
12 | 13 |
13 var inIncognitoContext = requireNative('process').InIncognitoContext(); | 14 var inIncognitoContext = requireNative('process').InIncognitoContext(); |
14 | 15 |
15 chrome.extension = chrome.extension || {}; | 16 chrome.extension = chrome.extension || {}; |
16 | 17 |
17 var manifestVersion = requireNative('process').GetManifestVersion(); | 18 var manifestVersion = requireNative('process').GetManifestVersion(); |
18 if (manifestVersion < 2) { | 19 if (manifestVersion < 2) { |
19 chrome.self = chrome.extension; | 20 chrome.self = chrome.extension; |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
86 targetId = args[lastArg--]; | 87 targetId = args[lastArg--]; |
87 | 88 |
88 if (lastArg != -1) | 89 if (lastArg != -1) |
89 throw new Error('Invalid arguments to ' + functionName + '.'); | 90 throw new Error('Invalid arguments to ' + functionName + '.'); |
90 return [targetId, request, responseCallback]; | 91 return [targetId, request, responseCallback]; |
91 } | 92 } |
92 apiFunctions.setUpdateArgumentsPreValidate('sendRequest', | 93 apiFunctions.setUpdateArgumentsPreValidate('sendRequest', |
93 sendMessageUpdateArguments.bind(null, 'sendRequest')); | 94 sendMessageUpdateArguments.bind(null, 'sendRequest')); |
94 apiFunctions.setUpdateArgumentsPreValidate('sendMessage', | 95 apiFunctions.setUpdateArgumentsPreValidate('sendMessage', |
95 sendMessageUpdateArguments.bind(null, 'sendMessage')); | 96 sendMessageUpdateArguments.bind(null, 'sendMessage')); |
97 apiFunctions.setUpdateArgumentsPreValidate('sendNativeMessage', | |
98 sendMessageUpdateArguments.bind(null, 'sendNativeMessage')); | |
96 | 99 |
97 apiFunctions.setHandleRequest('sendRequest', | 100 apiFunctions.setHandleRequest('sendRequest', |
98 function(targetId, request, responseCallback) { | 101 function(targetId, request, responseCallback) { |
99 var port = chrome.extension.connect(targetId || extensionId, | 102 var port = chrome.extension.connect(targetId || extensionId, |
100 {name: chromeHidden.kRequestChannel}); | 103 {name: chromeHidden.kRequestChannel}); |
101 chromeHidden.Port.sendMessageImpl(port, request, responseCallback); | 104 chromeHidden.Port.sendMessageImpl(port, request, responseCallback); |
102 }); | 105 }); |
103 | 106 |
104 apiFunctions.setHandleRequest('sendMessage', | 107 apiFunctions.setHandleRequest('sendMessage', |
105 function(targetId, message, responseCallback) { | 108 function(targetId, message, responseCallback) { |
106 var port = chrome.extension.connect(targetId || extensionId, | 109 var port = chrome.extension.connect(targetId || extensionId, |
107 {name: chromeHidden.kMessageChannel}); | 110 {name: chromeHidden.kMessageChannel}); |
108 chromeHidden.Port.sendMessageImpl(port, message, responseCallback); | 111 chromeHidden.Port.sendMessageImpl(port, message, responseCallback); |
109 }); | 112 }); |
110 | 113 |
111 apiFunctions.setUpdateArgumentsPreValidate('connect', function() { | 114 apiFunctions.setHandleRequest('sendNativeMessage', |
115 function(targetId, message, responseCallback) { | |
116 var port = chrome.extension.connectNative(targetId, | |
117 {name: chromeHidden.kNativeMessageChannel, | |
118 message: message}); | |
119 // TODO(eriq): Properly handle the callback like in sendMessageImpl() | |
Matt Perry
2012/08/09 02:13:00
can you just use sendMessageImpl directly?
eaugusti
2012/08/13 23:22:34
Yes, but it it counter-intuitive since we don't wa
| |
120 port.onMessage.addListener(function(response) { | |
121 try { | |
122 responseCallback(response); | |
123 } finally { | |
124 port.disconnect(); | |
125 port = null; | |
126 } | |
127 }); | |
128 }); | |
129 | |
130 function connectUpdateArguments(functionName) { | |
112 // Align missing (optional) function arguments with the arguments that | 131 // Align missing (optional) function arguments with the arguments that |
113 // schema validation is expecting, e.g. | 132 // schema validation is expecting, e.g. |
114 // extension.connect() -> extension.connect(null, null) | 133 // extension.connect() -> extension.connect(null, null) |
115 // extension.connect({}) -> extension.connect(null, {}) | 134 // extension.connect({}) -> extension.connect(null, {}) |
116 var nextArg = 0; | 135 var nextArg = 1; // skip functionName |
117 | 136 |
118 // targetId (first argument) is optional. | 137 // targetId (first argument) is optional. |
119 var targetId = null; | 138 var targetId = null; |
120 if (typeof(arguments[nextArg]) == 'string') | 139 if (typeof(arguments[nextArg]) == 'string') |
121 targetId = arguments[nextArg++]; | 140 targetId = arguments[nextArg++]; |
122 | 141 |
123 // connectInfo (second argument) is optional. | 142 // connectInfo (second argument) is optional. |
124 var connectInfo = null; | 143 var connectInfo = null; |
125 if (typeof(arguments[nextArg]) == 'object') | 144 if (typeof(arguments[nextArg]) == 'object') |
126 connectInfo = arguments[nextArg++]; | 145 connectInfo = arguments[nextArg++]; |
127 | 146 |
128 if (nextArg != arguments.length) | 147 if (nextArg != arguments.length) |
129 throw new Error('Invalid arguments to connect'); | 148 throw new Error('Invalid arguments to ' + functionName + '.'); |
149 | |
150 if (functionName == 'connectNative') { | |
151 connectInfo = {name: connectInfo.name || functionName, | |
152 message: connectInfo.message || connectInfo}; | |
153 } | |
130 return [targetId, connectInfo]; | 154 return [targetId, connectInfo]; |
131 }); | 155 } |
156 | |
157 apiFunctions.setUpdateArgumentsPreValidate('connect', | |
158 connectUpdateArguments.bind(null, 'connect')); | |
159 apiFunctions.setUpdateArgumentsPreValidate('connectNative', | |
160 connectUpdateArguments.bind(null, 'connectNative')); | |
132 | 161 |
133 apiFunctions.setHandleRequest('connect', function(targetId, connectInfo) { | 162 apiFunctions.setHandleRequest('connect', function(targetId, connectInfo) { |
134 if (!targetId) | 163 if (!targetId) |
135 targetId = extensionId; | 164 targetId = extensionId; |
136 var name = ''; | 165 var name = ''; |
137 if (connectInfo && connectInfo.name) | 166 if (connectInfo && connectInfo.name) |
138 name = connectInfo.name; | 167 name = connectInfo.name; |
139 | 168 |
140 var portId = OpenChannelToExtension(extensionId, targetId, name); | 169 var portId = OpenChannelToExtension(extensionId, targetId, name); |
141 if (portId >= 0) | 170 if (portId >= 0) |
142 return chromeHidden.Port.createPort(portId, name); | 171 return chromeHidden.Port.createPort(portId, name); |
143 throw new Error('Error connecting to extension ' + targetId); | 172 throw new Error('Error connecting to extension ' + targetId); |
144 }); | 173 }); |
174 | |
175 apiFunctions.setHandleRequest('connectNative', | |
176 function(nativeAppName, connectInfo) { | |
177 if (!nativeAppName) | |
178 throw new Error('Need native app name'); | |
179 | |
180 if (!connectInfo || !connectInfo.name) | |
181 throw new Error('Need a name for native connect'); | |
182 | |
183 if (!connectInfo.message) | |
184 throw new Error('Need a message for native connect'); | |
Matt Perry
2012/08/09 02:13:00
I think you can use the schema validation for thes
eaugusti
2012/08/13 23:22:34
Done.
| |
185 | |
186 // Turn the object into a string here, because it eventually will be. | |
187 var portId = OpenChannelToNativeApp(extensionId, | |
188 nativeAppName, | |
189 connectInfo.name, | |
190 JSON.stringify(connectInfo.message)); | |
191 if (portId >= 0) { | |
192 return chromeHidden.Port.createPort(portId, connectInfo.name); | |
193 } | |
194 throw new Error('Error connecting to native app: ' + nativeAppName); | |
195 }); | |
145 }); | 196 }); |
OLD | NEW |