Add new certificateProvider extension API.
[chromium-blink-merge.git] / chrome / renderer / resources / extensions / tabs_custom_bindings.js
blob0eb2946c0ca414a1084038916baf8806f47754b6
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) {
20     var name = '';
21     var frameId = -1;
22     if (connectInfo) {
23       name = connectInfo.name || name;
24       frameId = connectInfo.frameId;
25       if (typeof frameId == 'undefined' || frameId < 0)
26         frameId = -1;
27     }
28     var portId = OpenChannelToTab(tabId, frameId, extensionId, name);
29     return messaging.createPort(portId, name);
30   });
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);
38   });
40   apiFunctions.setHandleRequest('sendMessage',
41       function(tabId, message, options, responseCallback) {
42     var connectInfo = {
43       name: messaging.kMessageChannel
44     };
45     if (options) {
46       forEach(options, function(k, v) {
47         connectInfo[k] = v;
48       });
49     }
51     var port = tabs.connect(tabId, connectInfo);
52     messaging.sendMessageImpl(port, message, responseCallback);
53   });
54 });
56 exports.binding = binding.generate();