| 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 runtime API. | 5 // Custom bindings for the runtime API. |
| 6 | 6 |
| 7 var Bindings = require('schema_binding_generator').Bindings; |
| 8 var bindings = new Bindings('runtime'); |
| 9 |
| 7 var runtimeNatives = requireNative('runtime'); | 10 var runtimeNatives = requireNative('runtime'); |
| 8 var extensionNatives = requireNative('extension'); | 11 var extensionNatives = requireNative('extension'); |
| 9 var GetExtensionViews = extensionNatives.GetExtensionViews; | 12 var GetExtensionViews = extensionNatives.GetExtensionViews; |
| 10 var OpenChannelToExtension = runtimeNatives.OpenChannelToExtension; | 13 var OpenChannelToExtension = runtimeNatives.OpenChannelToExtension; |
| 11 var OpenChannelToNativeApp = runtimeNatives.OpenChannelToNativeApp; | 14 var OpenChannelToNativeApp = runtimeNatives.OpenChannelToNativeApp; |
| 12 var chromeHidden = requireNative('chrome_hidden').GetChromeHidden(); | 15 var chromeHidden = requireNative('chrome_hidden').GetChromeHidden(); |
| 13 var sendMessageUpdateArguments = | 16 var sendMessageUpdateArguments = |
| 14 require('miscellaneous_bindings').sendMessageUpdateArguments; | 17 require('miscellaneous_bindings').sendMessageUpdateArguments; |
| 15 | 18 |
| 16 chromeHidden.registerCustomHook('runtime', function(bindings, id, contextType) { | 19 bindings.registerCustomHook(function(bindings, id, contextType) { |
| 17 var apiFunctions = bindings.apiFunctions; | 20 var apiFunctions = bindings.apiFunctions; |
| 21 var runtime = bindings.compiledApi; |
| 18 | 22 |
| 19 // | 23 // |
| 20 // Unprivileged APIs. | 24 // Unprivileged APIs. |
| 21 // | 25 // |
| 22 | 26 |
| 23 chrome.runtime.id = id; | 27 runtime.id = id; |
| 24 | 28 |
| 25 apiFunctions.setHandleRequest('getManifest', function() { | 29 apiFunctions.setHandleRequest('getManifest', function() { |
| 26 return runtimeNatives.GetManifest(); | 30 return runtimeNatives.GetManifest(); |
| 27 }); | 31 }); |
| 28 | 32 |
| 29 apiFunctions.setHandleRequest('getURL', function(path) { | 33 apiFunctions.setHandleRequest('getURL', function(path) { |
| 30 path = String(path); | 34 path = String(path); |
| 31 if (!path.length || path[0] != '/') | 35 if (!path.length || path[0] != '/') |
| 32 path = '/' + path; | 36 path = '/' + path; |
| 33 return 'chrome-extension://' + id + path; | 37 return 'chrome-extension://' + id + path; |
| 34 }); | 38 }); |
| 35 | 39 |
| 36 apiFunctions.setUpdateArgumentsPreValidate('sendMessage', | 40 apiFunctions.setUpdateArgumentsPreValidate('sendMessage', |
| 37 sendMessageUpdateArguments.bind(null, 'sendMessage')); | 41 sendMessageUpdateArguments.bind(null, 'sendMessage')); |
| 38 apiFunctions.setUpdateArgumentsPreValidate('sendNativeMessage', | 42 apiFunctions.setUpdateArgumentsPreValidate('sendNativeMessage', |
| 39 sendMessageUpdateArguments.bind(null, 'sendNativeMessage')); | 43 sendMessageUpdateArguments.bind(null, 'sendNativeMessage')); |
| 40 | 44 |
| 41 apiFunctions.setHandleRequest('sendMessage', | 45 apiFunctions.setHandleRequest('sendMessage', |
| 42 function(targetId, message, responseCallback) { | 46 function(targetId, message, responseCallback) { |
| 43 var port = chrome.runtime.connect(targetId || chrome.runtime.id, | 47 var port = runtime.connect(targetId || runtime.id, |
| 44 {name: chromeHidden.kMessageChannel}); | 48 {name: chromeHidden.kMessageChannel}); |
| 45 chromeHidden.Port.sendMessageImpl(port, message, responseCallback); | 49 chromeHidden.Port.sendMessageImpl(port, message, responseCallback); |
| 46 }); | 50 }); |
| 47 | 51 |
| 48 apiFunctions.setHandleRequest('sendNativeMessage', | 52 apiFunctions.setHandleRequest('sendNativeMessage', |
| 49 function(targetId, message, responseCallback) { | 53 function(targetId, message, responseCallback) { |
| 50 var port = chrome.runtime.connectNative( | 54 var port = runtime.connectNative( |
| 51 targetId, message, chromeHidden.kNativeMessageChannel); | 55 targetId, message, chromeHidden.kNativeMessageChannel); |
| 52 chromeHidden.Port.sendMessageImpl(port, '', responseCallback); | 56 chromeHidden.Port.sendMessageImpl(port, '', responseCallback); |
| 53 }); | 57 }); |
| 54 | 58 |
| 55 apiFunctions.setUpdateArgumentsPreValidate('connect', function() { | 59 apiFunctions.setUpdateArgumentsPreValidate('connect', function() { |
| 56 // Align missing (optional) function arguments with the arguments that | 60 // Align missing (optional) function arguments with the arguments that |
| 57 // schema validation is expecting, e.g. | 61 // schema validation is expecting, e.g. |
| 58 // runtime.connect() -> runtime.connect(null, null) | 62 // runtime.connect() -> runtime.connect(null, null) |
| 59 // runtime.connect({}) -> runtime.connect(null, {}) | 63 // runtime.connect({}) -> runtime.connect(null, {}) |
| 60 var nextArg = 0; | 64 var nextArg = 0; |
| (...skipping 27 matching lines...) Expand all Loading... |
| 88 if (typeof(arguments[nextArg]) == 'string') | 92 if (typeof(arguments[nextArg]) == 'string') |
| 89 channelName = arguments[nextArg++]; | 93 channelName = arguments[nextArg++]; |
| 90 | 94 |
| 91 if (nextArg != arguments.length) | 95 if (nextArg != arguments.length) |
| 92 throw new Error('Invalid arguments to connectNative.'); | 96 throw new Error('Invalid arguments to connectNative.'); |
| 93 return [appName, {name: channelName, message: connectMessage}]; | 97 return [appName, {name: channelName, message: connectMessage}]; |
| 94 }); | 98 }); |
| 95 | 99 |
| 96 apiFunctions.setHandleRequest('connect', function(targetId, connectInfo) { | 100 apiFunctions.setHandleRequest('connect', function(targetId, connectInfo) { |
| 97 if (!targetId) | 101 if (!targetId) |
| 98 targetId = chrome.runtime.id; | 102 targetId = runtime.id; |
| 99 var name = ''; | 103 var name = ''; |
| 100 if (connectInfo && connectInfo.name) | 104 if (connectInfo && connectInfo.name) |
| 101 name = connectInfo.name; | 105 name = connectInfo.name; |
| 102 | 106 |
| 103 var portId = OpenChannelToExtension(chrome.runtime.id, targetId, name); | 107 var portId = OpenChannelToExtension(runtime.id, targetId, name); |
| 104 if (portId >= 0) | 108 if (portId >= 0) |
| 105 return chromeHidden.Port.createPort(portId, name); | 109 return chromeHidden.Port.createPort(portId, name); |
| 106 throw new Error('Error connecting to extension ' + targetId); | 110 throw new Error('Error connecting to extension ' + targetId); |
| 107 }); | 111 }); |
| 108 | 112 |
| 109 // | 113 // |
| 110 // Privileged APIs. | 114 // Privileged APIs. |
| 111 // | 115 // |
| 112 if (contextType != 'BLESSED_EXTENSION') | 116 if (contextType != 'BLESSED_EXTENSION') |
| 113 return; | 117 return; |
| 114 | 118 |
| 115 apiFunctions.setHandleRequest('connectNative', | 119 apiFunctions.setHandleRequest('connectNative', |
| 116 function(nativeAppName, connectInfo) { | 120 function(nativeAppName, connectInfo) { |
| 117 // Turn the object into a string here, because it eventually will be. | 121 // Turn the object into a string here, because it eventually will be. |
| 118 var portId = OpenChannelToNativeApp(chrome.runtime.id, | 122 var portId = OpenChannelToNativeApp(runtime.id, |
| 119 nativeAppName, | 123 nativeAppName, |
| 120 connectInfo.name, | 124 connectInfo.name, |
| 121 JSON.stringify(connectInfo.message)); | 125 JSON.stringify(connectInfo.message)); |
| 122 if (portId >= 0) { | 126 if (portId >= 0) { |
| 123 return chromeHidden.Port.createPort(portId, connectInfo.name); | 127 return chromeHidden.Port.createPort(portId, connectInfo.name); |
| 124 } | 128 } |
| 125 throw new Error('Error connecting to native app: ' + nativeAppName); | 129 throw new Error('Error connecting to native app: ' + nativeAppName); |
| 126 }); | 130 }); |
| 127 | 131 |
| 128 apiFunctions.setCustomCallback('getBackgroundPage', | 132 apiFunctions.setCustomCallback('getBackgroundPage', |
| 129 function(name, request, response) { | 133 function(name, request, response) { |
| 130 if (request.callback) { | 134 if (request.callback) { |
| 131 var bg = GetExtensionViews(-1, 'BACKGROUND')[0] || null; | 135 var bg = GetExtensionViews(-1, 'BACKGROUND')[0] || null; |
| 132 request.callback(bg); | 136 request.callback(bg); |
| 133 } | 137 } |
| 134 request.callback = null; | 138 request.callback = null; |
| 135 }); | 139 }); |
| 136 | 140 |
| 137 }); | 141 }); |
| 142 |
| 143 exports.bindings = bindings.generate(); |
| OLD | NEW |