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