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 bindings for the Bluetooth API. |
6 | 6 |
7 var chromeHidden = requireNative('chrome_hidden').GetChromeHidden(); | 7 var chromeHidden = requireNative('chrome_hidden').GetChromeHidden(); |
8 var sendRequest = require('sendRequest').sendRequest; | 8 var sendRequest = require('sendRequest').sendRequest; |
9 | 9 |
10 // Use custom bindings to create an undocumented event listener that will | 10 // Use custom bindings to create an undocumented event listener that will |
11 // receive events about device discovery and call the event listener that was | 11 // receive events about device discovery and call the event listener that was |
12 // provided with the request to begin discovery. | 12 // provided with the request to begin discovery. |
13 chromeHidden.registerCustomHook('experimental.bluetooth', function(api) { | 13 chromeHidden.registerCustomHook('experimental.bluetooth', function(api) { |
14 var apiFunctions = api.apiFunctions; | 14 var apiFunctions = api.apiFunctions; |
15 | 15 |
16 chromeHidden.bluetooth = {}; | 16 chromeHidden.bluetooth = {}; |
17 chromeHidden.bluetooth.handler = null; | 17 chromeHidden.bluetooth.deviceDiscoveredHandler = null; |
18 chromeHidden.bluetooth.onDeviceDiscovered = | 18 chromeHidden.bluetooth.onDeviceDiscovered = |
19 new chrome.Event("experimental.bluetooth.onDeviceDiscovered"); | 19 new chrome.Event("experimental.bluetooth.onDeviceDiscovered"); |
20 | 20 |
21 function deviceDiscoveredListener(device) { | 21 var clearDeviceDiscoveredHandler = function() { |
asargent_no_longer_on_chrome
2012/07/24 20:29:45
style nit: no need to assign this to a var - inste
bryeung
2012/08/03 20:08:37
Done.
| |
22 if (chromeHidden.bluetooth.handler != null) | 22 chromeHidden.bluetooth.onDeviceDiscovered.removeListener( |
23 chromeHidden.bluetooth.handler(device); | 23 chromeHidden.bluetooth.deviceDiscoveredHandler); |
24 } | 24 chromeHidden.bluetooth.deviceDiscoveredHandler = null; |
25 chromeHidden.bluetooth.onDeviceDiscovered.addListener( | 25 }; |
26 deviceDiscoveredListener); | |
27 | 26 |
28 apiFunctions.setHandleRequest('startDiscovery', function() { | 27 apiFunctions.setHandleRequest('startDiscovery', |
29 var args = arguments; | 28 function() { |
asargent_no_longer_on_chrome
2012/07/24 20:29:45
nit: my personal preference is to leave the openin
bryeung
2012/08/03 20:08:37
I'd prefer to keep all of the functions indented s
asargent_no_longer_on_chrome
2012/08/03 23:35:23
I think it's fine to leave it as you have it. Vi v
| |
30 if (args.length > 0 && args[0] && args[0].deviceCallback) { | 29 var args = arguments; |
31 chromeHidden.bluetooth.handler = args[0].deviceCallback; | 30 if (args.length > 0 && args[0] && args[0].deviceCallback) { |
32 } | 31 chromeHidden.bluetooth.deviceDiscoveredHandler = |
33 sendRequest(this.name, args, this.definition.parameters); | 32 args[0].deviceCallback; |
34 }); | 33 chromeHidden.bluetooth.onDeviceDiscovered.addListener( |
35 apiFunctions.setHandleRequest('stopDiscovery', function() { | 34 chromeHidden.bluetooth.deviceDiscoveredHandler); |
36 chromeHidden.bluetooth.handler = null; | 35 sendRequest(this.name, |
37 sendRequest(this.name, arguments, this.definition.parameters); | 36 args, |
38 }); | 37 this.definition.parameters, |
38 {customCallback:this.customCallback}); | |
39 } else { | |
40 throw new Error("deviceCallback is required in the options object"); | |
asargent_no_longer_on_chrome
2012/07/24 20:29:45
nit: the canonical way to report errors is to set
bryeung
2012/08/03 20:08:37
Done.
| |
41 } | |
42 }); | |
43 apiFunctions.setCustomCallback('startDiscovery', | |
44 function(name, request, response) { | |
45 if (chrome.runtime.lastError) { | |
46 clearDeviceDiscoveredHandler(); | |
47 return; | |
48 } | |
49 }); | |
50 apiFunctions.setHandleRequest('stopDiscovery', | |
51 function() { | |
52 clearDeviceDiscoveredHandler(); | |
53 sendRequest(this.name, arguments, this.definition.parameters); | |
54 }); | |
39 }); | 55 }); |
OLD | NEW |