1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 // Custom binding for the extension API.
7 var binding
= require('binding').Binding
.create('extension');
9 var messaging
= require('messaging');
10 var runtimeNatives
= requireNative('runtime');
11 var GetExtensionViews
= runtimeNatives
.GetExtensionViews
;
12 var OpenChannelToExtension
= runtimeNatives
.OpenChannelToExtension
;
13 var OpenChannelToNativeApp
= runtimeNatives
.OpenChannelToNativeApp
;
14 var chrome
= requireNative('chrome').GetChrome();
16 var inIncognitoContext
= requireNative('process').InIncognitoContext();
17 var sendRequestIsDisabled
= requireNative('process').IsSendRequestDisabled();
18 var contextType
= requireNative('process').GetContextType();
19 var manifestVersion
= requireNative('process').GetManifestVersion();
21 // This should match chrome.windows.WINDOW_ID_NONE.
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;
28 binding
.registerCustomHook(function(bindingsAPI
, extensionId
) {
29 var extension
= bindingsAPI
.compiledApi
;
30 if (manifestVersion
< 2) {
31 chrome
.self
= extension
;
32 extension
.inIncognitoTab
= inIncognitoContext
;
34 extension
.inIncognitoContext
= inIncognitoContext
;
36 var apiFunctions
= bindingsAPI
.apiFunctions
;
38 apiFunctions
.setHandleRequest('getViews', function(properties
) {
39 var windowId
= WINDOW_ID_NONE
;
42 if (properties
.type
!= null) {
43 type
= properties
.type
;
45 if (properties
.windowId
!= null) {
46 windowId
= properties
.windowId
;
49 return GetExtensionViews(windowId
, type
);
52 apiFunctions
.setHandleRequest('getBackgroundPage', function() {
53 return GetExtensionViews(-1, 'BACKGROUND')[0] || null;
56 apiFunctions
.setHandleRequest('getExtensionTabs', function(windowId
) {
58 windowId
= WINDOW_ID_NONE
;
59 return GetExtensionViews(windowId
, 'TAB');
62 apiFunctions
.setHandleRequest('getURL', function(path
) {
64 if (!path
.length
|| path
[0] != '/')
66 return 'chrome-extension://' + extensionId
+ path
;
69 // Alias several messaging deprecated APIs to their runtime counterparts.
74 'connect', 'sendMessage', 'connectNative', 'sendNativeMessage',
76 'onConnect', 'onConnectExternal', 'onMessage', 'onMessageExternal'
78 $Array
.forEach(mayNeedAlias
, function(alias
) {
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.
82 $Object
.hasOwnProperty(chrome
.runtime
, alias
) &&
83 chrome
.runtime
.__lookupGetter__(alias
) === undefined) {
84 extension
[alias
] = chrome
.runtime
[alias
];
88 apiFunctions
.setUpdateArgumentsPreValidate('sendRequest',
89 $Function
.bind(messaging
.sendMessageUpdateArguments
,
90 null, 'sendRequest', false /* hasOptionsArgument */));
92 apiFunctions
.setHandleRequest('sendRequest',
93 function(targetId
, request
, responseCallback
) {
94 if (sendRequestIsDisabled
)
95 throw new Error(sendRequestIsDisabled
);
96 var port
= chrome
.runtime
.connect(targetId
|| extensionId
,
97 {name
: messaging
.kRequestChannel
});
98 messaging
.sendMessageImpl(port
, request
, responseCallback
);
101 if (sendRequestIsDisabled
) {
102 extension
.onRequest
.addListener = function() {
103 throw new Error(sendRequestIsDisabled
);
105 if (contextType
== 'BLESSED_EXTENSION') {
106 extension
.onRequestExternal
.addListener = function() {
107 throw new Error(sendRequestIsDisabled
);
113 exports
.binding
= binding
.generate();