blob: a7a59772a2494f9a5dba456773f7eb0c1ae870f5 [file] [log] [blame]
[email protected]8f857ef82014-06-04 23:46:161// Copyright 2014 The Chromium Authors. All rights reserved.
[email protected]a485cfa2012-01-17 00:38:362// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]4f1633f2013-03-09 14:26:245// Custom binding for the extension API.
6
7var binding = require('binding').Binding.create('extension');
[email protected]a485cfa2012-01-17 00:38:368
[email protected]d710efe2013-07-30 22:25:489var messaging = require('messaging');
[email protected]481a87e62012-12-19 23:00:2710var runtimeNatives = requireNative('runtime');
[email protected]c1e5fb12013-11-13 03:40:3111var GetExtensionViews = runtimeNatives.GetExtensionViews;
[email protected]4f1633f2013-03-09 14:26:2412var chrome = requireNative('chrome').GetChrome();
[email protected]a485cfa2012-01-17 00:38:3613
[email protected]e60e6be82012-07-31 07:21:1514var inIncognitoContext = requireNative('process').InIncognitoContext();
[email protected]820b2452012-10-19 00:45:3415var sendRequestIsDisabled = requireNative('process').IsSendRequestDisabled();
16var contextType = requireNative('process').GetContextType();
[email protected]e60e6be82012-07-31 07:21:1517var manifestVersion = requireNative('process').GetManifestVersion();
[email protected]e60e6be82012-07-31 07:21:1518
[email protected]b3dd6cf2012-01-21 00:53:2519// 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.
24var WINDOW_ID_NONE = -1;
catmullings15fd52b2016-07-14 23:46:5925var TAB_ID_NONE = -1;
[email protected]b3dd6cf2012-01-21 00:53:2526
[email protected]4f1633f2013-03-09 14:26:2427binding.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]a485cfa2012-01-17 00:38:3635 var apiFunctions = bindingsAPI.apiFunctions;
36
[email protected]a72542052012-02-28 05:52:0437 apiFunctions.setHandleRequest('getViews', function(properties) {
[email protected]b3dd6cf2012-01-21 00:53:2538 var windowId = WINDOW_ID_NONE;
catmullings15fd52b2016-07-14 23:46:5939 var tabId = TAB_ID_NONE;
[email protected]a72542052012-02-28 05:52:0440 var type = 'ALL';
[email protected]f231e932012-03-07 06:15:1341 if (properties) {
42 if (properties.type != null) {
[email protected]a485cfa2012-01-17 00:38:3643 type = properties.type;
44 }
[email protected]f231e932012-03-07 06:15:1345 if (properties.windowId != null) {
[email protected]a485cfa2012-01-17 00:38:3646 windowId = properties.windowId;
47 }
catmullings15fd52b2016-07-14 23:46:5948 if (properties.tabId != null) {
49 tabId = properties.tabId;
50 }
[email protected]a485cfa2012-01-17 00:38:3651 }
catmullings15fd52b2016-07-14 23:46:5952 return GetExtensionViews(windowId, tabId, type);
[email protected]a485cfa2012-01-17 00:38:3653 });
54
[email protected]a72542052012-02-28 05:52:0455 apiFunctions.setHandleRequest('getBackgroundPage', function() {
catmullings15fd52b2016-07-14 23:46:5956 return GetExtensionViews(-1, -1, 'BACKGROUND')[0] || null;
[email protected]a485cfa2012-01-17 00:38:3657 });
58
[email protected]a72542052012-02-28 05:52:0459 apiFunctions.setHandleRequest('getExtensionTabs', function(windowId) {
[email protected]f231e932012-03-07 06:15:1360 if (windowId == null)
[email protected]b3dd6cf2012-01-21 00:53:2561 windowId = WINDOW_ID_NONE;
catmullings15fd52b2016-07-14 23:46:5962 return GetExtensionViews(windowId, -1, 'TAB');
[email protected]a485cfa2012-01-17 00:38:3663 });
[email protected]729c1e92012-02-22 01:41:4464
[email protected]a72542052012-02-28 05:52:0465 apiFunctions.setHandleRequest('getURL', function(path) {
[email protected]729c1e92012-02-22 01:41:4466 path = String(path);
[email protected]a72542052012-02-28 05:52:0467 if (!path.length || path[0] != '/')
68 path = '/' + path;
69 return 'chrome-extension://' + extensionId + path;
[email protected]729c1e92012-02-22 01:41:4470 });
71
[email protected]481a87e62012-12-19 23:00:2772 // Alias several messaging deprecated APIs to their runtime counterparts.
[email protected]754ea8b72013-01-08 15:10:3173 var mayNeedAlias = [
74 // Types
75 'Port',
76 // Functions
77 'connect', 'sendMessage', 'connectNative', 'sendNativeMessage',
78 // Events
79 'onConnect', 'onConnectExternal', 'onMessage', 'onMessageExternal'
80 ];
[email protected]295890bd2013-06-15 10:52:4581 $Array.forEach(mayNeedAlias, function(alias) {
[email protected]afc421022013-02-12 06:34:0482 // 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]556231dc2013-06-07 06:08:2784 if (chrome.runtime &&
[email protected]31bbfd72013-06-22 02:35:5485 $Object.hasOwnProperty(chrome.runtime, alias) &&
[email protected]afc421022013-02-12 06:34:0486 chrome.runtime.__lookupGetter__(alias) === undefined) {
[email protected]4f1633f2013-03-09 14:26:2487 extension[alias] = chrome.runtime[alias];
[email protected]afc421022013-02-12 06:34:0488 }
[email protected]754ea8b72013-01-08 15:10:3189 });
[email protected]481a87e62012-12-19 23:00:2790
[email protected]00536212012-04-02 21:35:5191 apiFunctions.setUpdateArgumentsPreValidate('sendRequest',
[email protected]d710efe2013-07-30 22:25:4892 $Function.bind(messaging.sendMessageUpdateArguments,
[email protected]8ad95b72013-10-16 02:54:1193 null, 'sendRequest', false /* hasOptionsArgument */));
[email protected]729c1e92012-02-22 01:41:4494
[email protected]a72542052012-02-28 05:52:0495 apiFunctions.setHandleRequest('sendRequest',
96 function(targetId, request, responseCallback) {
[email protected]820b2452012-10-19 00:45:3497 if (sendRequestIsDisabled)
98 throw new Error(sendRequestIsDisabled);
[email protected]481a87e62012-12-19 23:00:2799 var port = chrome.runtime.connect(targetId || extensionId,
[email protected]d710efe2013-07-30 22:25:48100 {name: messaging.kRequestChannel});
101 messaging.sendMessageImpl(port, request, responseCallback);
[email protected]00536212012-04-02 21:35:51102 });
[email protected]729c1e92012-02-22 01:41:44103
[email protected]820b2452012-10-19 00:45:34104 if (sendRequestIsDisabled) {
[email protected]4f1633f2013-03-09 14:26:24105 extension.onRequest.addListener = function() {
[email protected]820b2452012-10-19 00:45:34106 throw new Error(sendRequestIsDisabled);
[email protected]481a87e62012-12-19 23:00:27107 };
[email protected]820b2452012-10-19 00:45:34108 if (contextType == 'BLESSED_EXTENSION') {
[email protected]4f1633f2013-03-09 14:26:24109 extension.onRequestExternal.addListener = function() {
[email protected]820b2452012-10-19 00:45:34110 throw new Error(sendRequestIsDisabled);
[email protected]481a87e62012-12-19 23:00:27111 };
[email protected]820b2452012-10-19 00:45:34112 }
113 }
[email protected]630b9deb2013-03-07 18:38:34114});
[email protected]4f1633f2013-03-09 14:26:24115
rdevlin.cronin83a4b3a2015-10-28 21:43:58116exports.$set('binding', binding.generate());