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 (function() { | 7 (function() { |
8 | 8 |
9 native function GetChromeHidden(); | 9 native function GetChromeHidden(); |
10 native function OpenChannelToTab(); | 10 native function OpenChannelToTab(); |
11 | 11 |
12 var chromeHidden = GetChromeHidden(); | 12 var chromeHidden = GetChromeHidden(); |
13 | 13 |
14 chromeHidden.registerCustomHook('tabs', function(bindingsAPI, extensionId) { | 14 chromeHidden.registerCustomHook('tabs', function(bindingsAPI, extensionId) { |
15 var apiFunctions = bindingsAPI.apiFunctions; | 15 var apiFunctions = bindingsAPI.apiFunctions; |
16 | 16 |
17 apiFunctions.setHandleRequest('tabs.connect', function(tabId, connectInfo) { | 17 apiFunctions.setHandleRequest('connect', function(tabId, connectInfo) { |
18 var name = ''; | 18 var name = ''; |
19 if (connectInfo) { | 19 if (connectInfo) { |
20 name = connectInfo.name || name; | 20 name = connectInfo.name || name; |
21 } | 21 } |
22 var portId = OpenChannelToTab(tabId, extensionId, name); | 22 var portId = OpenChannelToTab(tabId, extensionId, name); |
23 return chromeHidden.Port.createPort(portId, name); | 23 return chromeHidden.Port.createPort(portId, name); |
24 }); | 24 }); |
25 | 25 |
26 apiFunctions.setHandleRequest('tabs.sendRequest', | 26 apiFunctions.setHandleRequest('sendRequest', |
27 function(tabId, request, responseCallback) { | 27 function(tabId, request, responseCallback) { |
28 var port = chrome.tabs.connect(tabId, | 28 var port = chrome.tabs.connect(tabId, |
29 {name: chromeHidden.kRequestChannel}); | 29 {name: chromeHidden.kRequestChannel}); |
30 port.postMessage(request); | 30 port.postMessage(request); |
31 port.onDisconnect.addListener(function() { | 31 port.onDisconnect.addListener(function() { |
32 // For onDisconnects, we only notify the callback if there was an error. | 32 // For onDisconnects, we only notify the callback if there was an error. |
33 if (chrome.extension.lastError && responseCallback) | 33 if (chrome.extension.lastError && responseCallback) |
34 responseCallback(); | 34 responseCallback(); |
35 }); | 35 }); |
36 port.onMessage.addListener(function(response) { | 36 port.onMessage.addListener(function(response) { |
37 try { | 37 try { |
38 if (responseCallback) | 38 if (responseCallback) |
39 responseCallback(response); | 39 responseCallback(response); |
40 } finally { | 40 } finally { |
41 port.disconnect(); | 41 port.disconnect(); |
42 port = null; | 42 port = null; |
43 } | 43 } |
44 }); | 44 }); |
45 }); | 45 }); |
46 | 46 |
47 // TODO(skerner,mtytel): The next step to omitting optional arguments is the | 47 // TODO(skerner,mtytel): The next step to omitting optional arguments is the |
48 // replacement of this code with code that matches arguments by type. | 48 // replacement of this code with code that matches arguments by type. |
49 // Once this is working for captureVisibleTab() it can be enabled for | 49 // Once this is working for captureVisibleTab() it can be enabled for |
50 // the rest of the API. See crbug/29215 . | 50 // the rest of the API. See crbug/29215 . |
51 apiFunctions.setUpdateArgumentsPreValidate('tabs.captureVisibleTab', | 51 apiFunctions.setUpdateArgumentsPreValidate('captureVisibleTab', function() { |
52 function() { | |
53 // Old signature: | 52 // Old signature: |
54 // captureVisibleTab(int windowId, function callback); | 53 // captureVisibleTab(int windowId, function callback); |
55 // New signature: | 54 // New signature: |
56 // captureVisibleTab(int windowId, object details, function callback); | 55 // captureVisibleTab(int windowId, object details, function callback); |
57 var newArgs; | 56 var newArgs; |
58 if (arguments.length == 2 && typeof(arguments[1]) == 'function') { | 57 if (arguments.length == 2 && typeof(arguments[1]) == 'function') { |
59 // If the old signature is used, add a null details object. | 58 // If the old signature is used, add a null details object. |
60 newArgs = [arguments[0], null, arguments[1]]; | 59 newArgs = [arguments[0], null, arguments[1]]; |
61 } else { | 60 } else { |
62 newArgs = arguments; | 61 newArgs = arguments; |
63 } | 62 } |
64 return newArgs; | 63 return newArgs; |
65 }); | 64 }); |
66 }); | 65 }); |
67 | 66 |
68 })(); | 67 })(); |
OLD | NEW |