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 chrome = requireNative('chrome').GetChrome();
14 var inIncognitoContext = requireNative('process').InIncognitoContext();
15 var sendRequestIsDisabled = requireNative('process').IsSendRequestDisabled();
16 var contextType = requireNative('process').GetContextType();
17 var manifestVersion = requireNative('process').GetManifestVersion();
19 // This should match chrome.windows.WINDOW_ID_NONE.
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;
26 binding.registerCustomHook(function(bindingsAPI, extensionId) {
27 var extension = bindingsAPI.compiledApi;
28 if (manifestVersion < 2) {
29 chrome.self = extension;
30 extension.inIncognitoTab = inIncognitoContext;
32 extension.inIncognitoContext = inIncognitoContext;
34 var apiFunctions = bindingsAPI.apiFunctions;
36 apiFunctions.setHandleRequest('getViews', function(properties) {
37 var windowId = WINDOW_ID_NONE;
40 if (properties.type != null) {
41 type = properties.type;
43 if (properties.windowId != null) {
44 windowId = properties.windowId;
47 return GetExtensionViews(windowId, type);
50 apiFunctions.setHandleRequest('getBackgroundPage', function() {
51 return GetExtensionViews(-1, 'BACKGROUND')[0] || null;
54 apiFunctions.setHandleRequest('getExtensionTabs', function(windowId) {
56 windowId = WINDOW_ID_NONE;
57 return GetExtensionViews(windowId, 'TAB');
60 apiFunctions.setHandleRequest('getURL', function(path) {
62 if (!path.length || path[0] != '/')
64 return 'chrome-extension://' + extensionId + path;
67 // Alias several messaging deprecated APIs to their runtime counterparts.
72 'connect', 'sendMessage', 'connectNative', 'sendNativeMessage',
74 'onConnect', 'onConnectExternal', 'onMessage', 'onMessageExternal'
76 $Array.forEach(mayNeedAlias, function(alias) {
77 // Checking existence isn't enough since some functions are disabled via
78 // getters that throw exceptions. Assume that any getter is such a function.
80 $Object.hasOwnProperty(chrome.runtime, alias) &&
81 chrome.runtime.__lookupGetter__(alias) === undefined) {
82 extension[alias] = chrome.runtime[alias];
86 apiFunctions.setUpdateArgumentsPreValidate('sendRequest',
87 $Function.bind(messaging.sendMessageUpdateArguments,
88 null, 'sendRequest', false /* hasOptionsArgument */));
90 apiFunctions.setHandleRequest('sendRequest',
91 function(targetId, request, responseCallback) {
92 if (sendRequestIsDisabled)
93 throw new Error(sendRequestIsDisabled);
94 var port = chrome.runtime.connect(targetId || extensionId,
95 {name: messaging.kRequestChannel});
96 messaging.sendMessageImpl(port, request, responseCallback);
99 if (sendRequestIsDisabled) {
100 extension.onRequest.addListener = function() {
101 throw new Error(sendRequestIsDisabled);
103 if (contextType == 'BLESSED_EXTENSION') {
104 extension.onRequestExternal.addListener = function() {
105 throw new Error(sendRequestIsDisabled);
111 exports.binding = binding.generate();