OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Custom binding for the tabs API. | 5 // Custom binding for the tabs API. |
6 | 6 |
7 var binding = require('binding').Binding.create('tabs'); | 7 var binding = require('binding').Binding.create('tabs'); |
8 | 8 |
9 var tabsNatives = requireNative('tabs'); | 9 var tabsNatives = requireNative('tabs'); |
10 var OpenChannelToTab = tabsNatives.OpenChannelToTab; | 10 var OpenChannelToTab = tabsNatives.OpenChannelToTab; |
11 var sendRequestIsDisabled = requireNative('process').IsSendRequestDisabled(); | 11 var sendRequestIsDisabled = requireNative('process').IsSendRequestDisabled(); |
12 | 12 |
13 var chromeHidden = requireNative('chrome_hidden').GetChromeHidden(); | 13 var chromeHidden = requireNative('chrome_hidden').GetChromeHidden(); |
14 | 14 |
15 binding.registerCustomHook(function(bindingsAPI, extensionId) { | 15 binding.registerCustomHook(function(bindingsAPI, extensionId) { |
16 var apiFunctions = bindingsAPI.apiFunctions; | 16 var apiFunctions = bindingsAPI.apiFunctions; |
17 var tabs = bindingsAPI.compiledApi; | 17 var tabs = bindingsAPI.compiledApi; |
18 | 18 |
19 apiFunctions.setHandleRequest('connect', function(tabId, connectInfo) { | 19 apiFunctions.setHandleRequest('connect', function(tabId, connectInfo) { |
20 var name = ''; | 20 var name = ''; |
21 if (connectInfo) { | 21 if (connectInfo) { |
22 name = connectInfo.name || name; | 22 name = connectInfo.name || name; |
23 } | 23 } |
24 var portId = OpenChannelToTab(tabId, extensionId, name); | 24 var portId = OpenChannelToTab(tabId, extensionId, name); |
25 return chromeHidden.Port.createPort(portId, name); | 25 return chromeHidden.Port.createPort(portId, name); |
26 }); | 26 }, false); |
27 | 27 |
28 apiFunctions.setHandleRequest('sendRequest', | 28 apiFunctions.setHandleRequest('sendRequest', |
29 function(tabId, request, responseCallback) { | 29 function(tabId, request, responseCallback) { |
30 if (sendRequestIsDisabled) | 30 if (sendRequestIsDisabled) |
31 throw new Error(sendRequestIsDisabled); | 31 throw new Error(sendRequestIsDisabled); |
32 var port = tabs.connect(tabId, {name: chromeHidden.kRequestChannel}); | 32 var port = tabs.connect(tabId, {name: chromeHidden.kRequestChannel}); |
33 chromeHidden.Port.sendMessageImpl(port, request, responseCallback); | 33 chromeHidden.Port.sendMessageImpl(port, request, responseCallback); |
34 }); | 34 }, false); |
35 | 35 |
36 apiFunctions.setHandleRequest('sendMessage', | 36 apiFunctions.setHandleRequest('sendMessage', |
37 function(tabId, message, responseCallback) { | 37 function(tabId, message, responseCallback) { |
38 var port = tabs.connect(tabId, {name: chromeHidden.kMessageChannel}); | 38 var port = tabs.connect(tabId, {name: chromeHidden.kMessageChannel}); |
39 chromeHidden.Port.sendMessageImpl(port, message, responseCallback); | 39 chromeHidden.Port.sendMessageImpl(port, message, responseCallback); |
40 }); | 40 }, false); |
41 }); | 41 }); |
42 | 42 |
43 exports.binding = binding.generate(); | 43 exports.binding = binding.generate(); |
OLD | NEW |