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 var lastError = require('last_error'); |
9 | 10 |
10 // Use custom bindings to create an undocumented event listener that will | 11 // Use custom bindings to create an undocumented event listener that will |
11 // receive events about device discovery and call the event listener that was | 12 // receive events about device discovery and call the event listener that was |
12 // provided with the request to begin discovery. | 13 // provided with the request to begin discovery. |
13 chromeHidden.registerCustomHook('experimental.bluetooth', function(api) { | 14 chromeHidden.registerCustomHook('experimental.bluetooth', function(api) { |
14 var apiFunctions = api.apiFunctions; | 15 var apiFunctions = api.apiFunctions; |
15 | 16 |
16 chromeHidden.bluetooth = {}; | 17 chromeHidden.bluetooth = {}; |
17 chromeHidden.bluetooth.handler = null; | 18 chromeHidden.bluetooth.deviceDiscoveredHandler = null; |
18 chromeHidden.bluetooth.onDeviceDiscovered = | 19 chromeHidden.bluetooth.onDeviceDiscovered = |
19 new chrome.Event("experimental.bluetooth.onDeviceDiscovered"); | 20 new chrome.Event("experimental.bluetooth.onDeviceDiscovered"); |
20 | 21 |
21 function deviceDiscoveredListener(device) { | 22 function clearDeviceDiscoveredHandler() { |
22 if (chromeHidden.bluetooth.handler != null) | 23 chromeHidden.bluetooth.onDeviceDiscovered.removeListener( |
23 chromeHidden.bluetooth.handler(device); | 24 chromeHidden.bluetooth.deviceDiscoveredHandler); |
| 25 chromeHidden.bluetooth.deviceDiscoveredHandler = null; |
24 } | 26 } |
25 chromeHidden.bluetooth.onDeviceDiscovered.addListener( | |
26 deviceDiscoveredListener); | |
27 | 27 |
28 apiFunctions.setHandleRequest('startDiscovery', function() { | 28 apiFunctions.setHandleRequest('startDiscovery', |
29 var args = arguments; | 29 function() { |
30 if (args.length > 0 && args[0] && args[0].deviceCallback) { | 30 var args = arguments; |
31 chromeHidden.bluetooth.handler = args[0].deviceCallback; | 31 if (args.length > 0 && args[0] && args[0].deviceCallback) { |
32 } | 32 chromeHidden.bluetooth.deviceDiscoveredHandler = |
33 sendRequest(this.name, args, this.definition.parameters); | 33 args[0].deviceCallback; |
34 }); | 34 chromeHidden.bluetooth.onDeviceDiscovered.addListener( |
35 apiFunctions.setHandleRequest('stopDiscovery', function() { | 35 chromeHidden.bluetooth.deviceDiscoveredHandler); |
36 chromeHidden.bluetooth.handler = null; | 36 sendRequest(this.name, |
37 sendRequest(this.name, arguments, this.definition.parameters); | 37 args, |
38 }); | 38 this.definition.parameters, |
| 39 {customCallback:this.customCallback}); |
| 40 } else { |
| 41 if (typeof(args[args.length-1]) == "function") { |
| 42 var callback = args[args.length-1]; |
| 43 lastError.set("deviceCallback is required in the options object"); |
| 44 callback(); |
| 45 return; |
| 46 } |
| 47 } |
| 48 }); |
| 49 apiFunctions.setCustomCallback('startDiscovery', |
| 50 function(name, request, response) { |
| 51 if (chrome.runtime.lastError) { |
| 52 clearDeviceDiscoveredHandler(); |
| 53 return; |
| 54 } |
| 55 }); |
| 56 apiFunctions.setHandleRequest('stopDiscovery', |
| 57 function() { |
| 58 clearDeviceDiscoveredHandler(); |
| 59 sendRequest(this.name, arguments, this.definition.parameters); |
| 60 }); |
39 }); | 61 }); |
OLD | NEW |