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 tabs API.
7 var binding = require('binding').Binding.create('tabs');
9 var messaging = require('messaging');
10 var tabsNatives = requireNative('tabs');
11 var OpenChannelToTab = tabsNatives.OpenChannelToTab;
12 var sendRequestIsDisabled = requireNative('process').IsSendRequestDisabled();
14 binding.registerCustomHook(function(bindingsAPI, extensionId) {
15 var apiFunctions = bindingsAPI.apiFunctions;
16 var tabs = bindingsAPI.compiledApi;
18 apiFunctions.setHandleRequest('connect', function(tabId, connectInfo) {
21 name = connectInfo.name || name;
23 var portId = OpenChannelToTab(tabId, extensionId, name);
24 return messaging.createPort(portId, name);
27 apiFunctions.setHandleRequest('sendRequest',
28 function(tabId, request, responseCallback) {
29 if (sendRequestIsDisabled)
30 throw new Error(sendRequestIsDisabled);
31 var port = tabs.connect(tabId, {name: messaging.kRequestChannel});
32 messaging.sendMessageImpl(port, request, responseCallback);
35 apiFunctions.setHandleRequest('sendMessage',
36 function(tabId, message, responseCallback) {
37 var port = tabs.connect(tabId, {name: messaging.kMessageChannel});
38 messaging.sendMessageImpl(port, message, responseCallback);
42 exports.binding = binding.generate();