Index: chrome/renderer/resources/extensions/extension_custom_bindings.js |
diff --git a/chrome/renderer/resources/extensions/extension_custom_bindings.js b/chrome/renderer/resources/extensions/extension_custom_bindings.js |
index be19e9c11b956ae421265991ab28c1f58ab4bf7e..1c5967ecdb682f535c58631e6326e310de974907 100644 |
--- a/chrome/renderer/resources/extensions/extension_custom_bindings.js |
+++ b/chrome/renderer/resources/extensions/extension_custom_bindings.js |
@@ -7,6 +7,7 @@ |
var extensionNatives = requireNative('extension'); |
var GetExtensionViews = extensionNatives.GetExtensionViews; |
var OpenChannelToExtension = extensionNatives.OpenChannelToExtension; |
+var OpenChannelToNativeApp = extensionNatives.OpenChannelToNativeApp; |
var chromeHidden = requireNative('chrome_hidden').GetChromeHidden(); |
@@ -93,6 +94,8 @@ chromeHidden.registerCustomHook('extension', |
sendMessageUpdateArguments.bind(null, 'sendRequest')); |
apiFunctions.setUpdateArgumentsPreValidate('sendMessage', |
sendMessageUpdateArguments.bind(null, 'sendMessage')); |
+ apiFunctions.setUpdateArgumentsPreValidate('sendNativeMessage', |
+ sendMessageUpdateArguments.bind(null, 'sendNativeMessage')); |
apiFunctions.setHandleRequest('sendRequest', |
function(targetId, request, responseCallback) { |
@@ -108,12 +111,28 @@ chromeHidden.registerCustomHook('extension', |
chromeHidden.Port.sendMessageImpl(port, message, responseCallback); |
}); |
- apiFunctions.setUpdateArgumentsPreValidate('connect', function() { |
+ apiFunctions.setHandleRequest('sendNativeMessage', |
+ function(targetId, message, responseCallback) { |
+ var port = chrome.extension.connectNative(targetId, |
+ {name: chromeHidden.kNativeMessageChannel, |
+ message: message}); |
+ // 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
|
+ port.onMessage.addListener(function(response) { |
+ try { |
+ responseCallback(response); |
+ } finally { |
+ port.disconnect(); |
+ port = null; |
+ } |
+ }); |
+ }); |
+ |
+ function connectUpdateArguments(functionName) { |
// Align missing (optional) function arguments with the arguments that |
// schema validation is expecting, e.g. |
// extension.connect() -> extension.connect(null, null) |
// extension.connect({}) -> extension.connect(null, {}) |
- var nextArg = 0; |
+ var nextArg = 1; // skip functionName |
// targetId (first argument) is optional. |
var targetId = null; |
@@ -126,9 +145,19 @@ chromeHidden.registerCustomHook('extension', |
connectInfo = arguments[nextArg++]; |
if (nextArg != arguments.length) |
- throw new Error('Invalid arguments to connect'); |
+ throw new Error('Invalid arguments to ' + functionName + '.'); |
+ |
+ if (functionName == 'connectNative') { |
+ connectInfo = {name: connectInfo.name || functionName, |
+ message: connectInfo.message || connectInfo}; |
+ } |
return [targetId, connectInfo]; |
- }); |
+ } |
+ |
+ apiFunctions.setUpdateArgumentsPreValidate('connect', |
+ connectUpdateArguments.bind(null, 'connect')); |
+ apiFunctions.setUpdateArgumentsPreValidate('connectNative', |
+ connectUpdateArguments.bind(null, 'connectNative')); |
apiFunctions.setHandleRequest('connect', function(targetId, connectInfo) { |
if (!targetId) |
@@ -142,4 +171,26 @@ chromeHidden.registerCustomHook('extension', |
return chromeHidden.Port.createPort(portId, name); |
throw new Error('Error connecting to extension ' + targetId); |
}); |
+ |
+ apiFunctions.setHandleRequest('connectNative', |
+ function(nativeAppName, connectInfo) { |
+ if (!nativeAppName) |
+ throw new Error('Need native app name'); |
+ |
+ if (!connectInfo || !connectInfo.name) |
+ throw new Error('Need a name for native connect'); |
+ |
+ if (!connectInfo.message) |
+ 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.
|
+ |
+ // Turn the object into a string here, because it eventually will be. |
+ var portId = OpenChannelToNativeApp(extensionId, |
+ nativeAppName, |
+ connectInfo.name, |
+ JSON.stringify(connectInfo.message)); |
+ if (portId >= 0) { |
+ return chromeHidden.Port.createPort(portId, connectInfo.name); |
+ } |
+ throw new Error('Error connecting to native app: ' + nativeAppName); |
+ }); |
}); |