| 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 bindings for the tabs API. | 5 // Custom bindings for the tabs API. |
| 6 | 6 |
| 7 var tabsNatives = requireNative('tabs'); | 7 var tabsNatives = requireNative('tabs'); |
| 8 var OpenChannelToTab = tabsNatives.OpenChannelToTab; | 8 var OpenChannelToTab = tabsNatives.OpenChannelToTab; |
| 9 | 9 |
| 10 var chromeHidden = requireNative('chrome_hidden').GetChromeHidden(); | 10 var chromeHidden = requireNative('chrome_hidden').GetChromeHidden(); |
| 11 | 11 |
| 12 chromeHidden.registerCustomHook('tabs', function(bindingsAPI, extensionId) { | 12 chromeHidden.registerCustomHook('tabs', function(bindingsAPI, extensionId) { |
| 13 var apiFunctions = bindingsAPI.apiFunctions; | 13 var apiFunctions = bindingsAPI.apiFunctions; |
| 14 | 14 |
| 15 apiFunctions.setHandleRequest('connect', function(tabId, connectInfo) { | 15 apiFunctions.setHandleRequest('connect', function(tabId, connectInfo) { |
| 16 var name = ''; | 16 var name = ''; |
| 17 if (connectInfo) { | 17 if (connectInfo) { |
| 18 name = connectInfo.name || name; | 18 name = connectInfo.name || name; |
| 19 } | 19 } |
| 20 var portId = OpenChannelToTab(tabId, extensionId, name); | 20 var portId = OpenChannelToTab(tabId, extensionId, name); |
| 21 return chromeHidden.Port.createPort(portId, name); | 21 return chromeHidden.Port.createPort(portId, name); |
| 22 }); | 22 }); |
| 23 | 23 |
| 24 apiFunctions.setHandleRequest('sendRequest', | 24 apiFunctions.setHandleRequest('sendRequest', |
| 25 function(tabId, request, responseCallback) { | 25 function(tabId, request, responseCallback) { |
| 26 var port = chrome.tabs.connect(tabId, | 26 var port = chrome.tabs.connect(tabId, {name: chromeHidden.kRequestChannel}); |
| 27 {name: chromeHidden.kRequestChannel}); | 27 chromeHidden.Port.sendMessageImpl(port, request, responseCallback); |
| 28 port.postMessage(request); | 28 }); |
| 29 port.onDisconnect.addListener(function() { | 29 |
| 30 // For onDisconnects, we only notify the callback if there was an error. | 30 apiFunctions.setHandleRequest('sendMessage', |
| 31 if (chrome.extension.lastError && responseCallback) | 31 function(tabId, message, responseCallback) { |
| 32 responseCallback(); | 32 var port = chrome.tabs.connect(tabId, {name: chromeHidden.kMessageChannel}); |
| 33 }); | 33 chromeHidden.Port.sendMessageImpl(port, message, responseCallback); |
| 34 port.onMessage.addListener(function(response) { | |
| 35 try { | |
| 36 if (responseCallback) | |
| 37 responseCallback(response); | |
| 38 } finally { | |
| 39 port.disconnect(); | |
| 40 port = null; | |
| 41 } | |
| 42 }); | |
| 43 }); | 34 }); |
| 44 }); | 35 }); |
| OLD | NEW |