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 Bluetooth API. | 5 // Custom binding for the Bluetooth API. |
6 | 6 |
7 var binding = require('binding').Binding.create('bluetooth'); | 7 var binding = require('binding').Binding.create('bluetooth'); |
8 | 8 |
9 var chromeHidden = requireNative('chrome_hidden').GetChromeHidden(); | 9 var chromeHidden = requireNative('chrome_hidden').GetChromeHidden(); |
10 var chrome = requireNative('chrome').GetChrome(); | 10 var chrome = requireNative('chrome').GetChrome(); |
11 var sendRequest = require('sendRequest').sendRequest; | 11 var sendRequest = require('sendRequest').sendRequest; |
12 var lastError = require('lastError'); | 12 var lastError = require('lastError'); |
13 | 13 |
14 // Use custom binding to create an undocumented event listener that will | 14 // Use custom binding to create an undocumented event listener that will |
15 // receive events about device discovery and call the event listener that was | 15 // receive events about device discovery and call the event listener that was |
16 // provided with the request to begin discovery. | 16 // provided with the request to begin discovery. |
17 binding.registerCustomHook(function(api) { | 17 binding.registerCustomHook(function(api) { |
18 var apiFunctions = api.apiFunctions; | 18 var apiFunctions = api.apiFunctions; |
19 | 19 |
20 chromeHidden.bluetooth = {}; | 20 chromeHidden.bluetooth = {}; |
21 | 21 |
22 function callCallbackIfPresent(args) { | 22 function callCallbackIfPresent(args, error) { |
23 if (typeof(args[args.length-1]) == "function") { | 23 var callback = args[args.length - 1]; |
24 args[args.length-1](); | 24 if (typeof(callback) == "function") |
25 } | 25 lastError.run(error, callback); |
26 } | 26 } |
27 | 27 |
28 chromeHidden.bluetooth.deviceDiscoveredHandler = null; | 28 chromeHidden.bluetooth.deviceDiscoveredHandler = null; |
29 chromeHidden.bluetooth.onDeviceDiscovered = | 29 chromeHidden.bluetooth.onDeviceDiscovered = |
30 new chrome.Event("bluetooth.onDeviceDiscovered"); | 30 new chrome.Event("bluetooth.onDeviceDiscovered"); |
31 function clearDeviceDiscoveredHandler() { | 31 function clearDeviceDiscoveredHandler() { |
32 chromeHidden.bluetooth.onDeviceDiscovered.removeListener( | 32 chromeHidden.bluetooth.onDeviceDiscovered.removeListener( |
33 chromeHidden.bluetooth.deviceDiscoveredHandler); | 33 chromeHidden.bluetooth.deviceDiscoveredHandler); |
34 chromeHidden.bluetooth.deviceDiscoveredHandler = null; | 34 chromeHidden.bluetooth.deviceDiscoveredHandler = null; |
35 } | 35 } |
36 apiFunctions.setHandleRequest('startDiscovery', | 36 apiFunctions.setHandleRequest('startDiscovery', |
37 function() { | 37 function() { |
38 var args = arguments; | 38 var args = arguments; |
39 if (args.length > 0 && args[0] && args[0].deviceCallback) { | 39 if (args.length > 0 && args[0] && args[0].deviceCallback) { |
40 if (chromeHidden.bluetooth.deviceDiscoveredHandler != null) { | 40 if (chromeHidden.bluetooth.deviceDiscoveredHandler != null) { |
41 lastError.set("Concurrent discovery is not allowed."); | 41 callCallbackIfPresent(args, "Concurrent discovery is not allowed."); |
42 callCallbackIfPresent(args); | |
43 return; | 42 return; |
44 } | 43 } |
45 | 44 |
46 chromeHidden.bluetooth.deviceDiscoveredHandler = | 45 chromeHidden.bluetooth.deviceDiscoveredHandler = |
47 args[0].deviceCallback; | 46 args[0].deviceCallback; |
48 chromeHidden.bluetooth.onDeviceDiscovered.addListener( | 47 chromeHidden.bluetooth.onDeviceDiscovered.addListener( |
49 chromeHidden.bluetooth.deviceDiscoveredHandler); | 48 chromeHidden.bluetooth.deviceDiscoveredHandler); |
50 sendRequest(this.name, | 49 sendRequest(this.name, |
51 args, | 50 args, |
52 this.definition.parameters, | 51 this.definition.parameters, |
53 {customCallback:this.customCallback}); | 52 {customCallback:this.customCallback}); |
54 } else { | 53 } else { |
55 lastError.set("deviceCallback is required in the options object"); | 54 callCallbackIfPresent( |
56 callCallbackIfPresent(args); | 55 args, "deviceCallback is required in the options object"); |
| 56 return; |
57 } | 57 } |
58 }); | 58 }); |
59 apiFunctions.setCustomCallback('startDiscovery', | 59 apiFunctions.setCustomCallback('startDiscovery', |
60 function(name, request, response) { | 60 function(name, request, response) { |
61 if (chrome.runtime.lastError) { | 61 if (chrome.runtime.lastError) { |
62 clearDeviceDiscoveredHandler(); | 62 clearDeviceDiscoveredHandler(); |
63 return; | 63 return; |
64 } | 64 } |
65 }); | 65 }); |
66 apiFunctions.setHandleRequest('stopDiscovery', | 66 apiFunctions.setHandleRequest('stopDiscovery', |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
152 } | 152 } |
153 | 153 |
154 chromeHidden.bluetooth.getDevicesState = state; | 154 chromeHidden.bluetooth.getDevicesState = state; |
155 addDeviceSearchListeners(); | 155 addDeviceSearchListeners(); |
156 | 156 |
157 return args; | 157 return args; |
158 }); | 158 }); |
159 }); | 159 }); |
160 | 160 |
161 exports.binding = binding.generate(); | 161 exports.binding = binding.generate(); |
OLD | NEW |