Chromium Code Reviews| Index: extensions/renderer/resources/runtime_custom_bindings.js |
| diff --git a/extensions/renderer/resources/runtime_custom_bindings.js b/extensions/renderer/resources/runtime_custom_bindings.js |
| index 1f829d4deb11abfe83933bb9fc0ec94881e267f1..bbadf32f26540a18fed95407be8e71dff4574621 100644 |
| --- a/extensions/renderer/resources/runtime_custom_bindings.js |
| +++ b/extensions/renderer/resources/runtime_custom_bindings.js |
| @@ -9,11 +9,12 @@ var binding = require('binding').Binding.create('runtime'); |
| var messaging = require('messaging'); |
| var runtimeNatives = requireNative('runtime'); |
| var process = requireNative('process'); |
| -var forEach = require('utils').forEach; |
| +var utils = require('utils'); |
| var backgroundPage = window; |
| var backgroundRequire = require; |
| var contextType = process.GetContextType(); |
| + |
| if (contextType == 'BLESSED_EXTENSION' || |
| contextType == 'UNBLESSED_EXTENSION') { |
| var manifest = runtimeNatives.GetManifest(); |
| @@ -71,8 +72,8 @@ if (window == backgroundPage) { |
| // backgroundPageModuleSystem.require('runtime') is insufficient as |
| // requireNative is only allowed while lazily loading an API. |
| backgroundPage.chrome.runtime; |
| - var bindDirectoryEntryCallback = backgroundRequire( |
| - 'runtime').bindDirectoryEntryCallback; |
| + var bindDirectoryEntryCallback = |
| + backgroundRequire('runtime').bindDirectoryEntryCallback; |
| } |
| binding.registerCustomHook(function(binding, id, contextType) { |
| @@ -84,34 +85,37 @@ binding.registerCustomHook(function(binding, id, contextType) { |
| // |
| if (id != '') |
| - runtime.id = id; |
| + utils.defineProperty(runtime, 'id', id); |
| apiFunctions.setHandleRequest('getManifest', function() { |
| return runtimeNatives.GetManifest(); |
| }); |
| apiFunctions.setHandleRequest('getURL', function(path) { |
| - path = String(path); |
| + path = $String.self(path); |
| if (!path.length || path[0] != '/') |
| path = '/' + path; |
| return 'chrome-extension://' + id + path; |
| }); |
| var sendMessageUpdateArguments = messaging.sendMessageUpdateArguments; |
| - apiFunctions.setUpdateArgumentsPreValidate('sendMessage', |
| + apiFunctions.setUpdateArgumentsPreValidate( |
| + 'sendMessage', |
| $Function.bind(sendMessageUpdateArguments, null, 'sendMessage', |
| true /* hasOptionsArgument */)); |
| - apiFunctions.setUpdateArgumentsPreValidate('sendNativeMessage', |
| + apiFunctions.setUpdateArgumentsPreValidate( |
| + 'sendNativeMessage', |
| $Function.bind(sendMessageUpdateArguments, null, 'sendNativeMessage', |
| false /* hasOptionsArgument */)); |
| - apiFunctions.setHandleRequest('sendMessage', |
| + apiFunctions.setHandleRequest( |
| + 'sendMessage', |
| function(targetId, message, options, responseCallback) { |
| - var connectOptions = {name: messaging.kMessageChannel}; |
| - forEach(options, function(k, v) { |
| - connectOptions[k] = v; |
| - }); |
| - var port = runtime.connect(targetId || runtime.id, connectOptions); |
| + var connectOptions = $Object.assign({ |
| + __proto__: null, |
| + name: messaging.kMessageChannel, |
| + }, options); |
| + var port = runtime.connect(targetId, connectOptions); |
| messaging.sendMessageImpl(port, message, responseCallback); |
| }); |
| @@ -121,46 +125,16 @@ binding.registerCustomHook(function(binding, id, contextType) { |
| messaging.sendMessageImpl(port, message, responseCallback); |
| }); |
| - apiFunctions.setUpdateArgumentsPreValidate('connect', function() { |
| - // Align missing (optional) function arguments with the arguments that |
| - // schema validation is expecting, e.g. |
| - // runtime.connect() -> runtime.connect(null, null) |
| - // runtime.connect({}) -> runtime.connect(null, {}) |
| - var nextArg = 0; |
| - |
| - // targetId (first argument) is optional. |
| - var targetId = null; |
| - if (typeof(arguments[nextArg]) == 'string') |
| - targetId = arguments[nextArg++]; |
| - |
| - // connectInfo (second argument) is optional. |
| - var connectInfo = null; |
| - if (typeof(arguments[nextArg]) == 'object') |
| - connectInfo = arguments[nextArg++]; |
| - |
| - if (nextArg != arguments.length) |
| - throw new Error('Invalid arguments to connect.'); |
| - return [targetId, connectInfo]; |
| - }); |
| - |
| - apiFunctions.setUpdateArgumentsPreValidate('connectNative', |
| - function(appName) { |
| - if (typeof(appName) !== 'string') { |
| - throw new Error('Invalid arguments to connectNative.'); |
| - } |
| - return [appName]; |
| - }); |
|
robwu
2016/05/08 18:06:50
Nice. Removing this unnecessary logic improves the
|
| - |
| apiFunctions.setHandleRequest('connect', function(targetId, connectInfo) { |
| if (!targetId) { |
| - // runtime.id is only defined inside extensions. If we're in a webpage, |
| - // the best we can do at this point is to fail. |
| - if (!runtime.id) { |
| + // id is only defined inside extensions. If we're in a webpage, the best |
| + // we can do at this point is to fail. |
| + if (!id) { |
| throw new Error('chrome.runtime.connect() called from a webpage must ' + |
| 'specify an Extension ID (string) for its first ' + |
| 'argument'); |
| } |
| - targetId = runtime.id; |
| + targetId = id; |
| } |
| var name = ''; |
| @@ -184,8 +158,7 @@ binding.registerCustomHook(function(binding, id, contextType) { |
| apiFunctions.setHandleRequest('connectNative', |
| function(nativeAppName) { |
| - var portId = runtimeNatives.OpenChannelToNativeApp(runtime.id, |
| - nativeAppName); |
| + var portId = runtimeNatives.OpenChannelToNativeApp(nativeAppName); |
| if (portId >= 0) |
| return messaging.createPort(portId, ''); |
| throw new Error('Error connecting to native app: ' + nativeAppName); |