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();
13 var forEach
= require('utils').forEach
;
15 binding
.registerCustomHook(function(bindingsAPI
, extensionId
) {
16 var apiFunctions
= bindingsAPI
.apiFunctions
;
17 var tabs
= bindingsAPI
.compiledApi
;
19 apiFunctions
.setHandleRequest('connect', function(tabId
, connectInfo
) {
23 name
= connectInfo
.name
|| name
;
24 frameId
= connectInfo
.frameId
;
25 if (typeof frameId
== 'undefined' || frameId
< 0)
28 var portId
= OpenChannelToTab(tabId
, frameId
, extensionId
, name
);
29 return messaging
.createPort(portId
, name
);
32 apiFunctions
.setHandleRequest('sendRequest',
33 function(tabId
, request
, responseCallback
) {
34 if (sendRequestIsDisabled
)
35 throw new Error(sendRequestIsDisabled
);
36 var port
= tabs
.connect(tabId
, {name
: messaging
.kRequestChannel
});
37 messaging
.sendMessageImpl(port
, request
, responseCallback
);
40 apiFunctions
.setHandleRequest('sendMessage',
41 function(tabId
, message
, options
, responseCallback
) {
43 name
: messaging
.kMessageChannel
46 forEach(options
, function(k
, v
) {
51 var port
= tabs
.connect(tabId
, connectInfo
);
52 messaging
.sendMessageImpl(port
, message
, responseCallback
);
56 exports
.binding
= binding
.generate();