[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] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 12 | var chrome = requireNative('chrome').GetChrome(); |
[email protected] | a485cfa | 2012-01-17 00:38:36 | [diff] [blame] | 13 | |
[email protected] | e60e6be8 | 2012-07-31 07:21:15 | [diff] [blame] | 14 | var inIncognitoContext = requireNative('process').InIncognitoContext(); |
[email protected] | 820b245 | 2012-10-19 00:45:34 | [diff] [blame] | 15 | var sendRequestIsDisabled = requireNative('process').IsSendRequestDisabled(); |
| 16 | var contextType = requireNative('process').GetContextType(); |
[email protected] | e60e6be8 | 2012-07-31 07:21:15 | [diff] [blame] | 17 | var manifestVersion = requireNative('process').GetManifestVersion(); |
[email protected] | e60e6be8 | 2012-07-31 07:21:15 | [diff] [blame] | 18 | |
[email protected] | b3dd6cf | 2012-01-21 00:53:25 | [diff] [blame] | 19 | // This should match chrome.windows.WINDOW_ID_NONE. |
| 20 | // |
| 21 | // We can't use chrome.windows.WINDOW_ID_NONE directly because the |
| 22 | // chrome.windows API won't exist unless this extension has permission for it; |
| 23 | // which may not be the case. |
| 24 | var WINDOW_ID_NONE = -1; |
catmullings | 15fd52b | 2016-07-14 23:46:59 | [diff] [blame] | 25 | var TAB_ID_NONE = -1; |
[email protected] | b3dd6cf | 2012-01-21 00:53:25 | [diff] [blame] | 26 | |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 27 | binding.registerCustomHook(function(bindingsAPI, extensionId) { |
| 28 | var extension = bindingsAPI.compiledApi; |
| 29 | if (manifestVersion < 2) { |
| 30 | chrome.self = extension; |
| 31 | extension.inIncognitoTab = inIncognitoContext; |
| 32 | } |
| 33 | extension.inIncognitoContext = inIncognitoContext; |
| 34 | |
[email protected] | a485cfa | 2012-01-17 00:38:36 | [diff] [blame] | 35 | var apiFunctions = bindingsAPI.apiFunctions; |
| 36 | |
[email protected] | a7254205 | 2012-02-28 05:52:04 | [diff] [blame] | 37 | apiFunctions.setHandleRequest('getViews', function(properties) { |
[email protected] | b3dd6cf | 2012-01-21 00:53:25 | [diff] [blame] | 38 | var windowId = WINDOW_ID_NONE; |
catmullings | 15fd52b | 2016-07-14 23:46:59 | [diff] [blame] | 39 | var tabId = TAB_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 | } |
catmullings | 15fd52b | 2016-07-14 23:46:59 | [diff] [blame] | 48 | if (properties.tabId != null) { |
| 49 | tabId = properties.tabId; |
| 50 | } |
[email protected] | a485cfa | 2012-01-17 00:38:36 | [diff] [blame] | 51 | } |
catmullings | 15fd52b | 2016-07-14 23:46:59 | [diff] [blame] | 52 | return GetExtensionViews(windowId, tabId, type); |
[email protected] | a485cfa | 2012-01-17 00:38:36 | [diff] [blame] | 53 | }); |
| 54 | |
[email protected] | a7254205 | 2012-02-28 05:52:04 | [diff] [blame] | 55 | apiFunctions.setHandleRequest('getBackgroundPage', function() { |
catmullings | 15fd52b | 2016-07-14 23:46:59 | [diff] [blame] | 56 | return GetExtensionViews(-1, -1, 'BACKGROUND')[0] || null; |
[email protected] | a485cfa | 2012-01-17 00:38:36 | [diff] [blame] | 57 | }); |
| 58 | |
[email protected] | a7254205 | 2012-02-28 05:52:04 | [diff] [blame] | 59 | apiFunctions.setHandleRequest('getExtensionTabs', function(windowId) { |
[email protected] | f231e93 | 2012-03-07 06:15:13 | [diff] [blame] | 60 | if (windowId == null) |
[email protected] | b3dd6cf | 2012-01-21 00:53:25 | [diff] [blame] | 61 | windowId = WINDOW_ID_NONE; |
catmullings | 15fd52b | 2016-07-14 23:46:59 | [diff] [blame] | 62 | return GetExtensionViews(windowId, -1, 'TAB'); |
[email protected] | a485cfa | 2012-01-17 00:38:36 | [diff] [blame] | 63 | }); |
[email protected] | 729c1e9 | 2012-02-22 01:41:44 | [diff] [blame] | 64 | |
[email protected] | a7254205 | 2012-02-28 05:52:04 | [diff] [blame] | 65 | apiFunctions.setHandleRequest('getURL', function(path) { |
[email protected] | 729c1e9 | 2012-02-22 01:41:44 | [diff] [blame] | 66 | path = String(path); |
[email protected] | a7254205 | 2012-02-28 05:52:04 | [diff] [blame] | 67 | if (!path.length || path[0] != '/') |
| 68 | path = '/' + path; |
| 69 | return 'chrome-extension://' + extensionId + path; |
[email protected] | 729c1e9 | 2012-02-22 01:41:44 | [diff] [blame] | 70 | }); |
| 71 | |
[email protected] | 481a87e6 | 2012-12-19 23:00:27 | [diff] [blame] | 72 | // Alias several messaging deprecated APIs to their runtime counterparts. |
[email protected] | 754ea8b7 | 2013-01-08 15:10:31 | [diff] [blame] | 73 | var mayNeedAlias = [ |
| 74 | // Types |
| 75 | 'Port', |
| 76 | // Functions |
| 77 | 'connect', 'sendMessage', 'connectNative', 'sendNativeMessage', |
| 78 | // Events |
| 79 | 'onConnect', 'onConnectExternal', 'onMessage', 'onMessageExternal' |
| 80 | ]; |
[email protected] | 295890bd | 2013-06-15 10:52:45 | [diff] [blame] | 81 | $Array.forEach(mayNeedAlias, function(alias) { |
[email protected] | afc42102 | 2013-02-12 06:34:04 | [diff] [blame] | 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. |
[email protected] | 556231dc | 2013-06-07 06:08:27 | [diff] [blame] | 84 | if (chrome.runtime && |
[email protected] | 31bbfd7 | 2013-06-22 02:35:54 | [diff] [blame] | 85 | $Object.hasOwnProperty(chrome.runtime, alias) && |
[email protected] | afc42102 | 2013-02-12 06:34:04 | [diff] [blame] | 86 | chrome.runtime.__lookupGetter__(alias) === undefined) { |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 87 | extension[alias] = chrome.runtime[alias]; |
[email protected] | afc42102 | 2013-02-12 06:34:04 | [diff] [blame] | 88 | } |
[email protected] | 754ea8b7 | 2013-01-08 15:10:31 | [diff] [blame] | 89 | }); |
[email protected] | 481a87e6 | 2012-12-19 23:00:27 | [diff] [blame] | 90 | |
[email protected] | 0053621 | 2012-04-02 21:35:51 | [diff] [blame] | 91 | apiFunctions.setUpdateArgumentsPreValidate('sendRequest', |
[email protected] | d710efe | 2013-07-30 22:25:48 | [diff] [blame] | 92 | $Function.bind(messaging.sendMessageUpdateArguments, |
[email protected] | 8ad95b7 | 2013-10-16 02:54:11 | [diff] [blame] | 93 | null, 'sendRequest', false /* hasOptionsArgument */)); |
[email protected] | 729c1e9 | 2012-02-22 01:41:44 | [diff] [blame] | 94 | |
[email protected] | a7254205 | 2012-02-28 05:52:04 | [diff] [blame] | 95 | apiFunctions.setHandleRequest('sendRequest', |
| 96 | function(targetId, request, responseCallback) { |
[email protected] | 820b245 | 2012-10-19 00:45:34 | [diff] [blame] | 97 | if (sendRequestIsDisabled) |
| 98 | throw new Error(sendRequestIsDisabled); |
[email protected] | 481a87e6 | 2012-12-19 23:00:27 | [diff] [blame] | 99 | var port = chrome.runtime.connect(targetId || extensionId, |
[email protected] | d710efe | 2013-07-30 22:25:48 | [diff] [blame] | 100 | {name: messaging.kRequestChannel}); |
| 101 | messaging.sendMessageImpl(port, request, responseCallback); |
[email protected] | 0053621 | 2012-04-02 21:35:51 | [diff] [blame] | 102 | }); |
[email protected] | 729c1e9 | 2012-02-22 01:41:44 | [diff] [blame] | 103 | |
[email protected] | 820b245 | 2012-10-19 00:45:34 | [diff] [blame] | 104 | if (sendRequestIsDisabled) { |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 105 | extension.onRequest.addListener = function() { |
[email protected] | 820b245 | 2012-10-19 00:45:34 | [diff] [blame] | 106 | throw new Error(sendRequestIsDisabled); |
[email protected] | 481a87e6 | 2012-12-19 23:00:27 | [diff] [blame] | 107 | }; |
[email protected] | 820b245 | 2012-10-19 00:45:34 | [diff] [blame] | 108 | if (contextType == 'BLESSED_EXTENSION') { |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 109 | extension.onRequestExternal.addListener = function() { |
[email protected] | 820b245 | 2012-10-19 00:45:34 | [diff] [blame] | 110 | throw new Error(sendRequestIsDisabled); |
[email protected] | 481a87e6 | 2012-12-19 23:00:27 | [diff] [blame] | 111 | }; |
[email protected] | 820b245 | 2012-10-19 00:45:34 | [diff] [blame] | 112 | } |
| 113 | } |
[email protected] | 630b9deb | 2013-03-07 18:38:34 | [diff] [blame] | 114 | }); |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 115 | |
rdevlin.cronin | 83a4b3a | 2015-10-28 21:43:58 | [diff] [blame] | 116 | exports.$set('binding', binding.generate()); |