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