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