| 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 binding for the runtime API. | 5 // Custom bindings for the runtime API. |
| 6 | |
| 7 var binding = require('binding').Binding.create('runtime'); | |
| 8 | 6 |
| 9 var runtimeNatives = requireNative('runtime'); | 7 var runtimeNatives = requireNative('runtime'); |
| 10 var extensionNatives = requireNative('extension'); | 8 var extensionNatives = requireNative('extension'); |
| 11 var GetExtensionViews = extensionNatives.GetExtensionViews; | 9 var GetExtensionViews = extensionNatives.GetExtensionViews; |
| 12 var OpenChannelToExtension = runtimeNatives.OpenChannelToExtension; | 10 var OpenChannelToExtension = runtimeNatives.OpenChannelToExtension; |
| 13 var OpenChannelToNativeApp = runtimeNatives.OpenChannelToNativeApp; | 11 var OpenChannelToNativeApp = runtimeNatives.OpenChannelToNativeApp; |
| 14 var chromeHidden = requireNative('chrome_hidden').GetChromeHidden(); | 12 var chromeHidden = requireNative('chrome_hidden').GetChromeHidden(); |
| 15 var sendMessageUpdateArguments = | 13 var sendMessageUpdateArguments = |
| 16 require('miscellaneous_bindings').sendMessageUpdateArguments; | 14 require('miscellaneous_bindings').sendMessageUpdateArguments; |
| 17 | 15 |
| 18 binding.registerCustomHook(function(binding, id, contextType) { | 16 chromeHidden.registerCustomHook('runtime', function(bindings, id, contextType) { |
| 19 var apiFunctions = binding.apiFunctions; | 17 var apiFunctions = bindings.apiFunctions; |
| 20 var runtime = binding.compiledApi; | |
| 21 | 18 |
| 22 // | 19 // |
| 23 // Unprivileged APIs. | 20 // Unprivileged APIs. |
| 24 // | 21 // |
| 25 | 22 |
| 26 runtime.id = id; | 23 chrome.runtime.id = id; |
| 27 | 24 |
| 28 apiFunctions.setHandleRequest('getManifest', function() { | 25 apiFunctions.setHandleRequest('getManifest', function() { |
| 29 return runtimeNatives.GetManifest(); | 26 return runtimeNatives.GetManifest(); |
| 30 }); | 27 }); |
| 31 | 28 |
| 32 apiFunctions.setHandleRequest('getURL', function(path) { | 29 apiFunctions.setHandleRequest('getURL', function(path) { |
| 33 path = String(path); | 30 path = String(path); |
| 34 if (!path.length || path[0] != '/') | 31 if (!path.length || path[0] != '/') |
| 35 path = '/' + path; | 32 path = '/' + path; |
| 36 return 'chrome-extension://' + id + path; | 33 return 'chrome-extension://' + id + path; |
| 37 }); | 34 }); |
| 38 | 35 |
| 39 apiFunctions.setUpdateArgumentsPreValidate('sendMessage', | 36 apiFunctions.setUpdateArgumentsPreValidate('sendMessage', |
| 40 sendMessageUpdateArguments.bind(null, 'sendMessage')); | 37 sendMessageUpdateArguments.bind(null, 'sendMessage')); |
| 41 apiFunctions.setUpdateArgumentsPreValidate('sendNativeMessage', | 38 apiFunctions.setUpdateArgumentsPreValidate('sendNativeMessage', |
| 42 sendMessageUpdateArguments.bind(null, 'sendNativeMessage')); | 39 sendMessageUpdateArguments.bind(null, 'sendNativeMessage')); |
| 43 | 40 |
| 44 apiFunctions.setHandleRequest('sendMessage', | 41 apiFunctions.setHandleRequest('sendMessage', |
| 45 function(targetId, message, responseCallback) { | 42 function(targetId, message, responseCallback) { |
| 46 var port = runtime.connect(targetId || runtime.id, | 43 var port = chrome.runtime.connect(targetId || chrome.runtime.id, |
| 47 {name: chromeHidden.kMessageChannel}); | 44 {name: chromeHidden.kMessageChannel}); |
| 48 chromeHidden.Port.sendMessageImpl(port, message, responseCallback); | 45 chromeHidden.Port.sendMessageImpl(port, message, responseCallback); |
| 49 }); | 46 }); |
| 50 | 47 |
| 51 apiFunctions.setHandleRequest('sendNativeMessage', | 48 apiFunctions.setHandleRequest('sendNativeMessage', |
| 52 function(targetId, message, responseCallback) { | 49 function(targetId, message, responseCallback) { |
| 53 var port = runtime.connectNative(targetId); | 50 var port = chrome.runtime.connectNative(targetId); |
| 54 chromeHidden.Port.sendMessageImpl(port, message, responseCallback); | 51 chromeHidden.Port.sendMessageImpl(port, message, responseCallback); |
| 55 }); | 52 }); |
| 56 | 53 |
| 57 apiFunctions.setUpdateArgumentsPreValidate('connect', function() { | 54 apiFunctions.setUpdateArgumentsPreValidate('connect', function() { |
| 58 // Align missing (optional) function arguments with the arguments that | 55 // Align missing (optional) function arguments with the arguments that |
| 59 // schema validation is expecting, e.g. | 56 // schema validation is expecting, e.g. |
| 60 // runtime.connect() -> runtime.connect(null, null) | 57 // runtime.connect() -> runtime.connect(null, null) |
| 61 // runtime.connect({}) -> runtime.connect(null, {}) | 58 // runtime.connect({}) -> runtime.connect(null, {}) |
| 62 var nextArg = 0; | 59 var nextArg = 0; |
| 63 | 60 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 79 apiFunctions.setUpdateArgumentsPreValidate('connectNative', | 76 apiFunctions.setUpdateArgumentsPreValidate('connectNative', |
| 80 function(appName) { | 77 function(appName) { |
| 81 if (typeof(appName) !== 'string') { | 78 if (typeof(appName) !== 'string') { |
| 82 throw new Error('Invalid arguments to connectNative.'); | 79 throw new Error('Invalid arguments to connectNative.'); |
| 83 } | 80 } |
| 84 return [appName]; | 81 return [appName]; |
| 85 }); | 82 }); |
| 86 | 83 |
| 87 apiFunctions.setHandleRequest('connect', function(targetId, connectInfo) { | 84 apiFunctions.setHandleRequest('connect', function(targetId, connectInfo) { |
| 88 if (!targetId) | 85 if (!targetId) |
| 89 targetId = runtime.id; | 86 targetId = chrome.runtime.id; |
| 90 var name = ''; | 87 var name = ''; |
| 91 if (connectInfo && connectInfo.name) | 88 if (connectInfo && connectInfo.name) |
| 92 name = connectInfo.name; | 89 name = connectInfo.name; |
| 93 | 90 |
| 94 // Don't let orphaned content scripts communicate with their extension. | 91 // Don't let orphaned content scripts communicate with their extension. |
| 95 // http://crbug.com/168263 | 92 // http://crbug.com/168263 |
| 96 if (!chromeHidden.wasUnloaded) { | 93 if (!chromeHidden.wasUnloaded) { |
| 97 var portId = OpenChannelToExtension(runtime.id, targetId, name); | 94 var portId = OpenChannelToExtension(chrome.runtime.id, targetId, name); |
| 98 if (portId >= 0) | 95 if (portId >= 0) |
| 99 return chromeHidden.Port.createPort(portId, name); | 96 return chromeHidden.Port.createPort(portId, name); |
| 100 } | 97 } |
| 101 throw new Error('Error connecting to extension ' + targetId); | 98 throw new Error('Error connecting to extension ' + targetId); |
| 102 }); | 99 }); |
| 103 | 100 |
| 104 // | 101 // |
| 105 // Privileged APIs. | 102 // Privileged APIs. |
| 106 // | 103 // |
| 107 if (contextType != 'BLESSED_EXTENSION') | 104 if (contextType != 'BLESSED_EXTENSION') |
| 108 return; | 105 return; |
| 109 | 106 |
| 110 apiFunctions.setHandleRequest('connectNative', | 107 apiFunctions.setHandleRequest('connectNative', |
| 111 function(nativeAppName) { | 108 function(nativeAppName) { |
| 112 if (!chromeHidden.wasUnloaded) { | 109 if (!chromeHidden.wasUnloaded) { |
| 113 var portId = OpenChannelToNativeApp(runtime.id, nativeAppName); | 110 var portId = OpenChannelToNativeApp(chrome.runtime.id, nativeAppName); |
| 114 if (portId >= 0) | 111 if (portId >= 0) |
| 115 return chromeHidden.Port.createPort(portId, ''); | 112 return chromeHidden.Port.createPort(portId, ''); |
| 116 } | 113 } |
| 117 throw new Error('Error connecting to native app: ' + nativeAppName); | 114 throw new Error('Error connecting to native app: ' + nativeAppName); |
| 118 }); | 115 }); |
| 119 | 116 |
| 120 apiFunctions.setCustomCallback('getBackgroundPage', | 117 apiFunctions.setCustomCallback('getBackgroundPage', |
| 121 function(name, request, response) { | 118 function(name, request, response) { |
| 122 if (request.callback) { | 119 if (request.callback) { |
| 123 var bg = GetExtensionViews(-1, 'BACKGROUND')[0] || null; | 120 var bg = GetExtensionViews(-1, 'BACKGROUND')[0] || null; |
| 124 request.callback(bg); | 121 request.callback(bg); |
| 125 } | 122 } |
| 126 request.callback = null; | 123 request.callback = null; |
| 127 }); | 124 }); |
| 128 | 125 |
| 129 }); | 126 }); |
| 130 | |
| 131 exports.binding = binding.generate(); | |
| OLD | NEW |