[email protected] | 8f857ef8 | 2014-06-04 23:46:16 | [diff] [blame] | 1 | // Copyright 2014 The Chromium Authors. All rights reserved. |
[email protected] | a485cfa | 2012-01-17 00:38:36 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 5 | // Custom binding for the extension API. |
| 6 | |
| 7 | var binding = require('binding').Binding.create('extension'); |
[email protected] | a485cfa | 2012-01-17 00:38:36 | [diff] [blame] | 8 | |
[email protected] | d710efe | 2013-07-30 22:25:48 | [diff] [blame] | 9 | var messaging = require('messaging'); |
[email protected] | 481a87e6 | 2012-12-19 23:00:27 | [diff] [blame] | 10 | var runtimeNatives = requireNative('runtime'); |
[email protected] | c1e5fb1 | 2013-11-13 03:40:31 | [diff] [blame] | 11 | var GetExtensionViews = runtimeNatives.GetExtensionViews; |
[email protected] | 481a87e6 | 2012-12-19 23:00:27 | [diff] [blame] | 12 | var OpenChannelToExtension = runtimeNatives.OpenChannelToExtension; |
| 13 | var OpenChannelToNativeApp = runtimeNatives.OpenChannelToNativeApp; |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 14 | var chrome = requireNative('chrome').GetChrome(); |
[email protected] | a485cfa | 2012-01-17 00:38:36 | [diff] [blame] | 15 | |
[email protected] | e60e6be8 | 2012-07-31 07:21:15 | [diff] [blame] | 16 | var inIncognitoContext = requireNative('process').InIncognitoContext(); |
[email protected] | 820b245 | 2012-10-19 00:45:34 | [diff] [blame] | 17 | var sendRequestIsDisabled = requireNative('process').IsSendRequestDisabled(); |
| 18 | var contextType = requireNative('process').GetContextType(); |
[email protected] | e60e6be8 | 2012-07-31 07:21:15 | [diff] [blame] | 19 | var manifestVersion = requireNative('process').GetManifestVersion(); |
[email protected] | e60e6be8 | 2012-07-31 07:21:15 | [diff] [blame] | 20 | |
[email protected] | b3dd6cf | 2012-01-21 00:53:25 | [diff] [blame] | 21 | // This should match chrome.windows.WINDOW_ID_NONE. |
| 22 | // |
| 23 | // We can't use chrome.windows.WINDOW_ID_NONE directly because the |
| 24 | // chrome.windows API won't exist unless this extension has permission for it; |
| 25 | // which may not be the case. |
| 26 | var WINDOW_ID_NONE = -1; |
| 27 | |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 28 | binding.registerCustomHook(function(bindingsAPI, extensionId) { |
| 29 | var extension = bindingsAPI.compiledApi; |
| 30 | if (manifestVersion < 2) { |
| 31 | chrome.self = extension; |
| 32 | extension.inIncognitoTab = inIncognitoContext; |
| 33 | } |
| 34 | extension.inIncognitoContext = inIncognitoContext; |
| 35 | |
[email protected] | a485cfa | 2012-01-17 00:38:36 | [diff] [blame] | 36 | var apiFunctions = bindingsAPI.apiFunctions; |
| 37 | |
[email protected] | a7254205 | 2012-02-28 05:52:04 | [diff] [blame] | 38 | apiFunctions.setHandleRequest('getViews', function(properties) { |
[email protected] | b3dd6cf | 2012-01-21 00:53:25 | [diff] [blame] | 39 | var windowId = WINDOW_ID_NONE; |
[email protected] | a7254205 | 2012-02-28 05:52:04 | [diff] [blame] | 40 | var type = 'ALL'; |
[email protected] | f231e93 | 2012-03-07 06:15:13 | [diff] [blame] | 41 | if (properties) { |
| 42 | if (properties.type != null) { |
[email protected] | a485cfa | 2012-01-17 00:38:36 | [diff] [blame] | 43 | type = properties.type; |
| 44 | } |
[email protected] | f231e93 | 2012-03-07 06:15:13 | [diff] [blame] | 45 | if (properties.windowId != null) { |
[email protected] | a485cfa | 2012-01-17 00:38:36 | [diff] [blame] | 46 | windowId = properties.windowId; |
| 47 | } |
| 48 | } |
[email protected] | f8d87d3 | 2013-06-06 02:51:29 | [diff] [blame] | 49 | return GetExtensionViews(windowId, type); |
[email protected] | a485cfa | 2012-01-17 00:38:36 | [diff] [blame] | 50 | }); |
| 51 | |
[email protected] | a7254205 | 2012-02-28 05:52:04 | [diff] [blame] | 52 | apiFunctions.setHandleRequest('getBackgroundPage', function() { |
| 53 | return GetExtensionViews(-1, 'BACKGROUND')[0] || null; |
[email protected] | a485cfa | 2012-01-17 00:38:36 | [diff] [blame] | 54 | }); |
| 55 | |
[email protected] | a7254205 | 2012-02-28 05:52:04 | [diff] [blame] | 56 | apiFunctions.setHandleRequest('getExtensionTabs', function(windowId) { |
[email protected] | f231e93 | 2012-03-07 06:15:13 | [diff] [blame] | 57 | if (windowId == null) |
[email protected] | b3dd6cf | 2012-01-21 00:53:25 | [diff] [blame] | 58 | windowId = WINDOW_ID_NONE; |
[email protected] | a7254205 | 2012-02-28 05:52:04 | [diff] [blame] | 59 | return GetExtensionViews(windowId, 'TAB'); |
[email protected] | a485cfa | 2012-01-17 00:38:36 | [diff] [blame] | 60 | }); |
[email protected] | 729c1e9 | 2012-02-22 01:41:44 | [diff] [blame] | 61 | |
[email protected] | a7254205 | 2012-02-28 05:52:04 | [diff] [blame] | 62 | apiFunctions.setHandleRequest('getURL', function(path) { |
[email protected] | 729c1e9 | 2012-02-22 01:41:44 | [diff] [blame] | 63 | path = String(path); |
[email protected] | a7254205 | 2012-02-28 05:52:04 | [diff] [blame] | 64 | if (!path.length || path[0] != '/') |
| 65 | path = '/' + path; |
| 66 | return 'chrome-extension://' + extensionId + path; |
[email protected] | 729c1e9 | 2012-02-22 01:41:44 | [diff] [blame] | 67 | }); |
| 68 | |
[email protected] | 481a87e6 | 2012-12-19 23:00:27 | [diff] [blame] | 69 | // Alias several messaging deprecated APIs to their runtime counterparts. |
[email protected] | 754ea8b7 | 2013-01-08 15:10:31 | [diff] [blame] | 70 | var mayNeedAlias = [ |
| 71 | // Types |
| 72 | 'Port', |
| 73 | // Functions |
| 74 | 'connect', 'sendMessage', 'connectNative', 'sendNativeMessage', |
| 75 | // Events |
| 76 | 'onConnect', 'onConnectExternal', 'onMessage', 'onMessageExternal' |
| 77 | ]; |
[email protected] | 295890bd | 2013-06-15 10:52:45 | [diff] [blame] | 78 | $Array.forEach(mayNeedAlias, function(alias) { |
[email protected] | afc42102 | 2013-02-12 06:34:04 | [diff] [blame] | 79 | // Checking existence isn't enough since some functions are disabled via |
| 80 | // getters that throw exceptions. Assume that any getter is such a function. |
[email protected] | 556231dc | 2013-06-07 06:08:27 | [diff] [blame] | 81 | if (chrome.runtime && |
[email protected] | 31bbfd7 | 2013-06-22 02:35:54 | [diff] [blame] | 82 | $Object.hasOwnProperty(chrome.runtime, alias) && |
[email protected] | afc42102 | 2013-02-12 06:34:04 | [diff] [blame] | 83 | chrome.runtime.__lookupGetter__(alias) === undefined) { |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 84 | extension[alias] = chrome.runtime[alias]; |
[email protected] | afc42102 | 2013-02-12 06:34:04 | [diff] [blame] | 85 | } |
[email protected] | 754ea8b7 | 2013-01-08 15:10:31 | [diff] [blame] | 86 | }); |
[email protected] | 481a87e6 | 2012-12-19 23:00:27 | [diff] [blame] | 87 | |
[email protected] | 0053621 | 2012-04-02 21:35:51 | [diff] [blame] | 88 | apiFunctions.setUpdateArgumentsPreValidate('sendRequest', |
[email protected] | d710efe | 2013-07-30 22:25:48 | [diff] [blame] | 89 | $Function.bind(messaging.sendMessageUpdateArguments, |
[email protected] | 8ad95b7 | 2013-10-16 02:54:11 | [diff] [blame] | 90 | null, 'sendRequest', false /* hasOptionsArgument */)); |
[email protected] | 729c1e9 | 2012-02-22 01:41:44 | [diff] [blame] | 91 | |
[email protected] | a7254205 | 2012-02-28 05:52:04 | [diff] [blame] | 92 | apiFunctions.setHandleRequest('sendRequest', |
| 93 | function(targetId, request, responseCallback) { |
[email protected] | 820b245 | 2012-10-19 00:45:34 | [diff] [blame] | 94 | if (sendRequestIsDisabled) |
| 95 | throw new Error(sendRequestIsDisabled); |
[email protected] | 481a87e6 | 2012-12-19 23:00:27 | [diff] [blame] | 96 | var port = chrome.runtime.connect(targetId || extensionId, |
[email protected] | d710efe | 2013-07-30 22:25:48 | [diff] [blame] | 97 | {name: messaging.kRequestChannel}); |
| 98 | messaging.sendMessageImpl(port, request, responseCallback); |
[email protected] | 0053621 | 2012-04-02 21:35:51 | [diff] [blame] | 99 | }); |
[email protected] | 729c1e9 | 2012-02-22 01:41:44 | [diff] [blame] | 100 | |
[email protected] | 820b245 | 2012-10-19 00:45:34 | [diff] [blame] | 101 | if (sendRequestIsDisabled) { |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 102 | extension.onRequest.addListener = function() { |
[email protected] | 820b245 | 2012-10-19 00:45:34 | [diff] [blame] | 103 | throw new Error(sendRequestIsDisabled); |
[email protected] | 481a87e6 | 2012-12-19 23:00:27 | [diff] [blame] | 104 | }; |
[email protected] | 820b245 | 2012-10-19 00:45:34 | [diff] [blame] | 105 | if (contextType == 'BLESSED_EXTENSION') { |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 106 | extension.onRequestExternal.addListener = function() { |
[email protected] | 820b245 | 2012-10-19 00:45:34 | [diff] [blame] | 107 | throw new Error(sendRequestIsDisabled); |
[email protected] | 481a87e6 | 2012-12-19 23:00:27 | [diff] [blame] | 108 | }; |
[email protected] | 820b245 | 2012-10-19 00:45:34 | [diff] [blame] | 109 | } |
| 110 | } |
[email protected] | 630b9deb | 2013-03-07 18:38:34 | [diff] [blame] | 111 | }); |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 112 | |
| 113 | exports.binding = binding.generate(); |