| 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 Event = require('event_bindings').Event; |
| 12 var lastError = require('lastError'); |
| 11 var sendRequest = require('sendRequest').sendRequest; | 13 var sendRequest = require('sendRequest').sendRequest; |
| 12 var lastError = require('lastError'); | |
| 13 | 14 |
| 14 // Use custom binding to create an undocumented event listener that will | 15 // Use custom binding to create an undocumented event listener that will |
| 15 // receive events about device discovery and call the event listener that was | 16 // receive events about device discovery and call the event listener that was |
| 16 // provided with the request to begin discovery. | 17 // provided with the request to begin discovery. |
| 17 binding.registerCustomHook(function(api) { | 18 binding.registerCustomHook(function(api) { |
| 18 var apiFunctions = api.apiFunctions; | 19 var apiFunctions = api.apiFunctions; |
| 19 | 20 |
| 20 chromeHidden.bluetooth = {}; | 21 chromeHidden.bluetooth = {}; |
| 21 | 22 |
| 22 function callCallbackIfPresent(name, args, error) { | 23 function callCallbackIfPresent(name, args, error) { |
| 23 var callback = args[args.length - 1]; | 24 var callback = args[args.length - 1]; |
| 24 if (typeof(callback) == 'function') | 25 if (typeof(callback) == 'function') |
| 25 lastError.run(name, error, callback); | 26 lastError.run(name, error, callback); |
| 26 } | 27 } |
| 27 | 28 |
| 28 chromeHidden.bluetooth.deviceDiscoveredHandler = null; | 29 chromeHidden.bluetooth.deviceDiscoveredHandler = null; |
| 29 chromeHidden.bluetooth.onDeviceDiscovered = | 30 chromeHidden.bluetooth.onDeviceDiscovered = |
| 30 new chrome.Event('bluetooth.onDeviceDiscovered'); | 31 new Event('bluetooth.onDeviceDiscovered'); |
| 31 function clearDeviceDiscoveredHandler() { | 32 function clearDeviceDiscoveredHandler() { |
| 32 chromeHidden.bluetooth.onDeviceDiscovered.removeListener( | 33 chromeHidden.bluetooth.onDeviceDiscovered.removeListener( |
| 33 chromeHidden.bluetooth.deviceDiscoveredHandler); | 34 chromeHidden.bluetooth.deviceDiscoveredHandler); |
| 34 chromeHidden.bluetooth.deviceDiscoveredHandler = null; | 35 chromeHidden.bluetooth.deviceDiscoveredHandler = null; |
| 35 } | 36 } |
| 36 apiFunctions.setHandleRequest('startDiscovery', | 37 apiFunctions.setHandleRequest('startDiscovery', |
| 37 function() { | 38 function() { |
| 38 var args = arguments; | 39 var args = arguments; |
| 39 if (args.length > 0 && args[0] && args[0].deviceCallback) { | 40 if (args.length > 0 && args[0] && args[0].deviceCallback) { |
| 40 if (chromeHidden.bluetooth.deviceDiscoveredHandler != null) { | 41 if (chromeHidden.bluetooth.deviceDiscoveredHandler != null) { |
| (...skipping 30 matching lines...) Expand all Loading... |
| 71 function() { | 72 function() { |
| 72 clearDeviceDiscoveredHandler(); | 73 clearDeviceDiscoveredHandler(); |
| 73 sendRequest(this.name, arguments, this.definition.parameters); | 74 sendRequest(this.name, arguments, this.definition.parameters); |
| 74 }); | 75 }); |
| 75 | 76 |
| 76 // An object to hold state during one call to getDevices. | 77 // An object to hold state during one call to getDevices. |
| 77 chromeHidden.bluetooth.getDevicesState = null; | 78 chromeHidden.bluetooth.getDevicesState = null; |
| 78 | 79 |
| 79 // Hidden events used to deliver getDevices data to the client callbacks | 80 // Hidden events used to deliver getDevices data to the client callbacks |
| 80 chromeHidden.bluetooth.onDeviceSearchResult = | 81 chromeHidden.bluetooth.onDeviceSearchResult = |
| 81 new chrome.Event('bluetooth.onDeviceSearchResult'); | 82 new Event('bluetooth.onDeviceSearchResult'); |
| 82 chromeHidden.bluetooth.onDeviceSearchFinished = | 83 chromeHidden.bluetooth.onDeviceSearchFinished = |
| 83 new chrome.Event('bluetooth.onDeviceSearchFinished'); | 84 new Event('bluetooth.onDeviceSearchFinished'); |
| 84 | 85 |
| 85 function deviceSearchResultHandler(device) { | 86 function deviceSearchResultHandler(device) { |
| 86 chromeHidden.bluetooth.getDevicesState.actualEvents++; | 87 chromeHidden.bluetooth.getDevicesState.actualEvents++; |
| 87 chromeHidden.bluetooth.getDevicesState.deviceCallback(device); | 88 chromeHidden.bluetooth.getDevicesState.deviceCallback(device); |
| 88 maybeFinishDeviceSearch(); | 89 maybeFinishDeviceSearch(); |
| 89 } | 90 } |
| 90 | 91 |
| 91 function deviceSearchFinishedHandler(info) { | 92 function deviceSearchFinishedHandler(info) { |
| 92 chromeHidden.bluetooth.getDevicesState.expectedEventCount = | 93 chromeHidden.bluetooth.getDevicesState.expectedEventCount = |
| 93 info.expectedEventCount; | 94 info.expectedEventCount; |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 156 } | 157 } |
| 157 | 158 |
| 158 chromeHidden.bluetooth.getDevicesState = state; | 159 chromeHidden.bluetooth.getDevicesState = state; |
| 159 addDeviceSearchListeners(); | 160 addDeviceSearchListeners(); |
| 160 | 161 |
| 161 return args; | 162 return args; |
| 162 }); | 163 }); |
| 163 }); | 164 }); |
| 164 | 165 |
| 165 exports.binding = binding.generate(); | 166 exports.binding = binding.generate(); |
| OLD | NEW |