1 // Copyright (c) 2012 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 extensionNatives = requireNative('extension');
10 var GetExtensionViews = extensionNatives.GetExtensionViews;
11 var messaging = require('messaging');
12 var runtimeNatives = requireNative('runtime');
13 var OpenChannelToExtension = runtimeNatives.OpenChannelToExtension;
14 var OpenChannelToNativeApp = runtimeNatives.OpenChannelToNativeApp;
15 var chrome = requireNative('chrome').GetChrome();
17 var inIncognitoContext = requireNative('process').InIncognitoContext();
18 var sendRequestIsDisabled = requireNative('process').IsSendRequestDisabled();
19 var contextType = requireNative('process').GetContextType();
20 var manifestVersion = requireNative('process').GetManifestVersion();
22 // This should match chrome.windows.WINDOW_ID_NONE.
24 // We can't use chrome.windows.WINDOW_ID_NONE directly because the
25 // chrome.windows API won't exist unless this extension has permission for it;
26 // which may not be the case.
27 var WINDOW_ID_NONE = -1;
29 binding.registerCustomHook(function(bindingsAPI, extensionId) {
30 var extension = bindingsAPI.compiledApi;
31 if (manifestVersion < 2) {
32 chrome.self = extension;
33 extension.inIncognitoTab = inIncognitoContext;
35 extension.inIncognitoContext = inIncognitoContext;
37 var apiFunctions = bindingsAPI.apiFunctions;
39 apiFunctions.setHandleRequest('getViews', function(properties) {
40 var windowId = WINDOW_ID_NONE;
43 if (properties.type != null) {
44 type = properties.type;
46 if (properties.windowId != null) {
47 windowId = properties.windowId;
50 return GetExtensionViews(windowId, type);
53 apiFunctions.setHandleRequest('getBackgroundPage', function() {
54 return GetExtensionViews(-1, 'BACKGROUND')[0] || null;
57 apiFunctions.setHandleRequest('getExtensionTabs', function(windowId) {
59 windowId = WINDOW_ID_NONE;
60 return GetExtensionViews(windowId, 'TAB');
63 apiFunctions.setHandleRequest('getURL', function(path) {
65 if (!path.length || path[0] != '/')
67 return 'chrome-extension://' + extensionId + path;
70 // Alias several messaging deprecated APIs to their runtime counterparts.
75 'connect', 'sendMessage', 'connectNative', 'sendNativeMessage',
77 'onConnect', 'onConnectExternal', 'onMessage', 'onMessageExternal'
79 $Array.forEach(mayNeedAlias, function(alias) {
80 // Checking existence isn't enough since some functions are disabled via
81 // getters that throw exceptions. Assume that any getter is such a function.
83 $Object.hasOwnProperty(chrome.runtime, alias) &&
84 chrome.runtime.__lookupGetter__(alias) === undefined) {
85 extension[alias] = chrome.runtime[alias];
89 apiFunctions.setUpdateArgumentsPreValidate('sendRequest',
90 $Function.bind(messaging.sendMessageUpdateArguments,
91 null, 'sendRequest', false /* hasOptionsArgument */));
93 apiFunctions.setHandleRequest('sendRequest',
94 function(targetId, request, responseCallback) {
95 if (sendRequestIsDisabled)
96 throw new Error(sendRequestIsDisabled);
97 var port = chrome.runtime.connect(targetId || extensionId,
98 {name: messaging.kRequestChannel});
99 messaging.sendMessageImpl(port, request, responseCallback);
102 if (sendRequestIsDisabled) {
103 extension.onRequest.addListener = function() {
104 throw new Error(sendRequestIsDisabled);
106 if (contextType == 'BLESSED_EXTENSION') {
107 extension.onRequestExternal.addListener = function() {
108 throw new Error(sendRequestIsDisabled);
114 exports.binding = binding.generate();