Home | History | Annotate | Download | only in extensions
      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.
      4 
      5 // Custom binding for the tabs API.
      6 
      7 var binding = require('binding').Binding.create('tabs');
      8 
      9 var messaging = require('messaging');
     10 var tabsNatives = requireNative('tabs');
     11 var OpenChannelToTab = tabsNatives.OpenChannelToTab;
     12 var sendRequestIsDisabled = requireNative('process').IsSendRequestDisabled();
     13 
     14 binding.registerCustomHook(function(bindingsAPI, extensionId) {
     15   var apiFunctions = bindingsAPI.apiFunctions;
     16   var tabs = bindingsAPI.compiledApi;
     17 
     18   apiFunctions.setHandleRequest('connect', function(tabId, connectInfo) {
     19     var name = '';
     20     if (connectInfo) {
     21       name = connectInfo.name || name;
     22     }
     23     var portId = OpenChannelToTab(tabId, extensionId, name);
     24     return messaging.createPort(portId, name);
     25   });
     26 
     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);
     33   });
     34 
     35   apiFunctions.setHandleRequest('sendMessage',
     36                                 function(tabId, message, responseCallback) {
     37     var port = tabs.connect(tabId, {name: messaging.kMessageChannel});
     38     messaging.sendMessageImpl(port, message, responseCallback);
     39   });
     40 });
     41 
     42 exports.binding = binding.generate();
     43