blob: d114f527f56e92418cb4c8b54a4623b7d3a7b145 [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]481a87e62012-12-19 23:00:2712var OpenChannelToExtension = runtimeNatives.OpenChannelToExtension;
13var OpenChannelToNativeApp = runtimeNatives.OpenChannelToNativeApp;
[email protected]4f1633f2013-03-09 14:26:2414var chrome = requireNative('chrome').GetChrome();
[email protected]a485cfa2012-01-17 00:38:3615
[email protected]e60e6be82012-07-31 07:21:1516var inIncognitoContext = requireNative('process').InIncognitoContext();
[email protected]820b2452012-10-19 00:45:3417var sendRequestIsDisabled = requireNative('process').IsSendRequestDisabled();
18var contextType = requireNative('process').GetContextType();
[email protected]e60e6be82012-07-31 07:21:1519var manifestVersion = requireNative('process').GetManifestVersion();
[email protected]e60e6be82012-07-31 07:21:1520
[email protected]b3dd6cf2012-01-21 00:53:2521// 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.
26var WINDOW_ID_NONE = -1;
27
[email protected]4f1633f2013-03-09 14:26:2428binding.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]a485cfa2012-01-17 00:38:3636 var apiFunctions = bindingsAPI.apiFunctions;
37
[email protected]a72542052012-02-28 05:52:0438 apiFunctions.setHandleRequest('getViews', function(properties) {
[email protected]b3dd6cf2012-01-21 00:53:2539 var windowId = WINDOW_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 }
48 }
[email protected]f8d87d32013-06-06 02:51:2949 return GetExtensionViews(windowId, type);
[email protected]a485cfa2012-01-17 00:38:3650 });
51
[email protected]a72542052012-02-28 05:52:0452 apiFunctions.setHandleRequest('getBackgroundPage', function() {
53 return GetExtensionViews(-1, 'BACKGROUND')[0] || null;
[email protected]a485cfa2012-01-17 00:38:3654 });
55
[email protected]a72542052012-02-28 05:52:0456 apiFunctions.setHandleRequest('getExtensionTabs', function(windowId) {
[email protected]f231e932012-03-07 06:15:1357 if (windowId == null)
[email protected]b3dd6cf2012-01-21 00:53:2558 windowId = WINDOW_ID_NONE;
[email protected]a72542052012-02-28 05:52:0459 return GetExtensionViews(windowId, 'TAB');
[email protected]a485cfa2012-01-17 00:38:3660 });
[email protected]729c1e92012-02-22 01:41:4461
[email protected]a72542052012-02-28 05:52:0462 apiFunctions.setHandleRequest('getURL', function(path) {
[email protected]729c1e92012-02-22 01:41:4463 path = String(path);
[email protected]a72542052012-02-28 05:52:0464 if (!path.length || path[0] != '/')
65 path = '/' + path;
66 return 'chrome-extension://' + extensionId + path;
[email protected]729c1e92012-02-22 01:41:4467 });
68
[email protected]481a87e62012-12-19 23:00:2769 // Alias several messaging deprecated APIs to their runtime counterparts.
[email protected]754ea8b72013-01-08 15:10:3170 var mayNeedAlias = [
71 // Types
72 'Port',
73 // Functions
74 'connect', 'sendMessage', 'connectNative', 'sendNativeMessage',
75 // Events
76 'onConnect', 'onConnectExternal', 'onMessage', 'onMessageExternal'
77 ];
[email protected]295890bd2013-06-15 10:52:4578 $Array.forEach(mayNeedAlias, function(alias) {
[email protected]afc421022013-02-12 06:34:0479 // 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]556231dc2013-06-07 06:08:2781 if (chrome.runtime &&
[email protected]31bbfd72013-06-22 02:35:5482 $Object.hasOwnProperty(chrome.runtime, alias) &&
[email protected]afc421022013-02-12 06:34:0483 chrome.runtime.__lookupGetter__(alias) === undefined) {
[email protected]4f1633f2013-03-09 14:26:2484 extension[alias] = chrome.runtime[alias];
[email protected]afc421022013-02-12 06:34:0485 }
[email protected]754ea8b72013-01-08 15:10:3186 });
[email protected]481a87e62012-12-19 23:00:2787
[email protected]00536212012-04-02 21:35:5188 apiFunctions.setUpdateArgumentsPreValidate('sendRequest',
[email protected]d710efe2013-07-30 22:25:4889 $Function.bind(messaging.sendMessageUpdateArguments,
[email protected]8ad95b72013-10-16 02:54:1190 null, 'sendRequest', false /* hasOptionsArgument */));
[email protected]729c1e92012-02-22 01:41:4491
[email protected]a72542052012-02-28 05:52:0492 apiFunctions.setHandleRequest('sendRequest',
93 function(targetId, request, responseCallback) {
[email protected]820b2452012-10-19 00:45:3494 if (sendRequestIsDisabled)
95 throw new Error(sendRequestIsDisabled);
[email protected]481a87e62012-12-19 23:00:2796 var port = chrome.runtime.connect(targetId || extensionId,
[email protected]d710efe2013-07-30 22:25:4897 {name: messaging.kRequestChannel});
98 messaging.sendMessageImpl(port, request, responseCallback);
[email protected]00536212012-04-02 21:35:5199 });
[email protected]729c1e92012-02-22 01:41:44100
[email protected]820b2452012-10-19 00:45:34101 if (sendRequestIsDisabled) {
[email protected]4f1633f2013-03-09 14:26:24102 extension.onRequest.addListener = function() {
[email protected]820b2452012-10-19 00:45:34103 throw new Error(sendRequestIsDisabled);
[email protected]481a87e62012-12-19 23:00:27104 };
[email protected]820b2452012-10-19 00:45:34105 if (contextType == 'BLESSED_EXTENSION') {
[email protected]4f1633f2013-03-09 14:26:24106 extension.onRequestExternal.addListener = function() {
[email protected]820b2452012-10-19 00:45:34107 throw new Error(sendRequestIsDisabled);
[email protected]481a87e62012-12-19 23:00:27108 };
[email protected]820b2452012-10-19 00:45:34109 }
110 }
[email protected]630b9deb2013-03-07 18:38:34111});
[email protected]4f1633f2013-03-09 14:26:24112
113exports.binding = binding.generate();