| 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 extension API. | 5 // Custom bindings for the extension API. |
| 6 | |
| 7 var binding = require('binding').Binding.create('extension'); | |
| 8 | 6 |
| 9 var extensionNatives = requireNative('extension'); | 7 var extensionNatives = requireNative('extension'); |
| 10 var GetExtensionViews = extensionNatives.GetExtensionViews; | 8 var GetExtensionViews = extensionNatives.GetExtensionViews; |
| 11 var runtimeNatives = requireNative('runtime'); | 9 var runtimeNatives = requireNative('runtime'); |
| 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 chrome = requireNative('chrome').GetChrome(); | |
| 16 var sendMessageUpdateArguments = | 13 var sendMessageUpdateArguments = |
| 17 require('miscellaneous_bindings').sendMessageUpdateArguments; | 14 require('miscellaneous_bindings').sendMessageUpdateArguments; |
| 18 | 15 |
| 19 var inIncognitoContext = requireNative('process').InIncognitoContext(); | 16 var inIncognitoContext = requireNative('process').InIncognitoContext(); |
| 20 var sendRequestIsDisabled = requireNative('process').IsSendRequestDisabled(); | 17 var sendRequestIsDisabled = requireNative('process').IsSendRequestDisabled(); |
| 21 var contextType = requireNative('process').GetContextType(); | 18 var contextType = requireNative('process').GetContextType(); |
| 19 |
| 20 chrome.extension = chrome.extension || {}; |
| 21 |
| 22 var manifestVersion = requireNative('process').GetManifestVersion(); | 22 var manifestVersion = requireNative('process').GetManifestVersion(); |
| 23 if (manifestVersion < 2) { |
| 24 chrome.self = chrome.extension; |
| 25 chrome.extension.inIncognitoTab = inIncognitoContext; |
| 26 } |
| 27 |
| 28 chrome.extension.inIncognitoContext = inIncognitoContext; |
| 23 | 29 |
| 24 // This should match chrome.windows.WINDOW_ID_NONE. | 30 // This should match chrome.windows.WINDOW_ID_NONE. |
| 25 // | 31 // |
| 26 // We can't use chrome.windows.WINDOW_ID_NONE directly because the | 32 // We can't use chrome.windows.WINDOW_ID_NONE directly because the |
| 27 // chrome.windows API won't exist unless this extension has permission for it; | 33 // chrome.windows API won't exist unless this extension has permission for it; |
| 28 // which may not be the case. | 34 // which may not be the case. |
| 29 var WINDOW_ID_NONE = -1; | 35 var WINDOW_ID_NONE = -1; |
| 30 | 36 |
| 31 binding.registerCustomHook(function(bindingsAPI, extensionId) { | 37 chromeHidden.registerCustomHook('extension', |
| 32 var extension = bindingsAPI.compiledApi; | 38 function(bindingsAPI, extensionId) { |
| 33 if (manifestVersion < 2) { | |
| 34 chrome.self = extension; | |
| 35 extension.inIncognitoTab = inIncognitoContext; | |
| 36 } | |
| 37 extension.inIncognitoContext = inIncognitoContext; | |
| 38 | |
| 39 var apiFunctions = bindingsAPI.apiFunctions; | 39 var apiFunctions = bindingsAPI.apiFunctions; |
| 40 | 40 |
| 41 apiFunctions.setHandleRequest('getViews', function(properties) { | 41 apiFunctions.setHandleRequest('getViews', function(properties) { |
| 42 var windowId = WINDOW_ID_NONE; | 42 var windowId = WINDOW_ID_NONE; |
| 43 var type = 'ALL'; | 43 var type = 'ALL'; |
| 44 if (properties) { | 44 if (properties) { |
| 45 if (properties.type != null) { | 45 if (properties.type != null) { |
| 46 type = properties.type; | 46 type = properties.type; |
| 47 } | 47 } |
| 48 if (properties.windowId != null) { | 48 if (properties.windowId != null) { |
| (...skipping 27 matching lines...) Expand all Loading... |
| 76 // Functions | 76 // Functions |
| 77 'connect', 'sendMessage', 'connectNative', 'sendNativeMessage', | 77 'connect', 'sendMessage', 'connectNative', 'sendNativeMessage', |
| 78 // Events | 78 // Events |
| 79 'onConnect', 'onConnectExternal', 'onMessage', 'onMessageExternal' | 79 'onConnect', 'onConnectExternal', 'onMessage', 'onMessageExternal' |
| 80 ]; | 80 ]; |
| 81 mayNeedAlias.forEach(function(alias) { | 81 mayNeedAlias.forEach(function(alias) { |
| 82 // Checking existence isn't enough since some functions are disabled via | 82 // Checking existence isn't enough since some functions are disabled via |
| 83 // getters that throw exceptions. Assume that any getter is such a function. | 83 // getters that throw exceptions. Assume that any getter is such a function. |
| 84 if (chrome.runtime.hasOwnProperty(alias) && | 84 if (chrome.runtime.hasOwnProperty(alias) && |
| 85 chrome.runtime.__lookupGetter__(alias) === undefined) { | 85 chrome.runtime.__lookupGetter__(alias) === undefined) { |
| 86 extension[alias] = chrome.runtime[alias]; | 86 chrome.extension[alias] = chrome.runtime[alias]; |
| 87 } | 87 } |
| 88 }); | 88 }); |
| 89 | 89 |
| 90 apiFunctions.setUpdateArgumentsPreValidate('sendRequest', | 90 apiFunctions.setUpdateArgumentsPreValidate('sendRequest', |
| 91 sendMessageUpdateArguments.bind(null, 'sendRequest')); | 91 sendMessageUpdateArguments.bind(null, 'sendRequest')); |
| 92 | 92 |
| 93 apiFunctions.setHandleRequest('sendRequest', | 93 apiFunctions.setHandleRequest('sendRequest', |
| 94 function(targetId, request, responseCallback) { | 94 function(targetId, request, responseCallback) { |
| 95 if (sendRequestIsDisabled) | 95 if (sendRequestIsDisabled) |
| 96 throw new Error(sendRequestIsDisabled); | 96 throw new Error(sendRequestIsDisabled); |
| 97 var port = chrome.runtime.connect(targetId || extensionId, | 97 var port = chrome.runtime.connect(targetId || extensionId, |
| 98 {name: chromeHidden.kRequestChannel}); | 98 {name: chromeHidden.kRequestChannel}); |
| 99 chromeHidden.Port.sendMessageImpl(port, request, responseCallback); | 99 chromeHidden.Port.sendMessageImpl(port, request, responseCallback); |
| 100 }); | 100 }); |
| 101 | 101 |
| 102 if (sendRequestIsDisabled) { | 102 if (sendRequestIsDisabled) { |
| 103 extension.onRequest.addListener = function() { | 103 chrome.extension.onRequest.addListener = function() { |
| 104 throw new Error(sendRequestIsDisabled); | 104 throw new Error(sendRequestIsDisabled); |
| 105 }; | 105 }; |
| 106 if (contextType == 'BLESSED_EXTENSION') { | 106 if (contextType == 'BLESSED_EXTENSION') { |
| 107 extension.onRequestExternal.addListener = function() { | 107 chrome.extension.onRequestExternal.addListener = function() { |
| 108 throw new Error(sendRequestIsDisabled); | 108 throw new Error(sendRequestIsDisabled); |
| 109 }; | 109 }; |
| 110 } | 110 } |
| 111 } | 111 } |
| 112 |
| 112 }); | 113 }); |
| 113 | |
| 114 exports.binding = binding.generate(); | |
| OLD | NEW |