bluetooth_chooser.cc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // Copyright (c) 2016 GitHub, Inc.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. #include "atom/browser/lib/bluetooth_chooser.h"
  5. #include "atom/common/native_mate_converters/callback.h"
  6. #include "atom/common/native_mate_converters/string16_converter.h"
  7. #include "native_mate/dictionary.h"
  8. namespace mate {
  9. template <>
  10. struct Converter<atom::BluetoothChooser::DeviceInfo> {
  11. static v8::Local<v8::Value> ToV8(
  12. v8::Isolate* isolate,
  13. const atom::BluetoothChooser::DeviceInfo& val) {
  14. mate::Dictionary dict = mate::Dictionary::CreateEmpty(isolate);
  15. dict.Set("deviceName", val.device_name);
  16. dict.Set("deviceId", val.device_id);
  17. return mate::ConvertToV8(isolate, dict);
  18. }
  19. };
  20. } // namespace mate
  21. namespace atom {
  22. namespace {
  23. const int kMaxScanRetries = 5;
  24. void OnDeviceChosen(const content::BluetoothChooser::EventHandler& handler,
  25. const std::string& device_id) {
  26. if (device_id.empty()) {
  27. handler.Run(content::BluetoothChooser::Event::CANCELLED, device_id);
  28. } else {
  29. handler.Run(content::BluetoothChooser::Event::SELECTED, device_id);
  30. }
  31. }
  32. } // namespace
  33. BluetoothChooser::BluetoothChooser(api::WebContents* contents,
  34. const EventHandler& event_handler)
  35. : api_web_contents_(contents), event_handler_(event_handler) {}
  36. BluetoothChooser::~BluetoothChooser() {}
  37. void BluetoothChooser::SetAdapterPresence(AdapterPresence presence) {
  38. switch (presence) {
  39. case AdapterPresence::ABSENT:
  40. case AdapterPresence::POWERED_OFF:
  41. event_handler_.Run(Event::CANCELLED, "");
  42. break;
  43. case AdapterPresence::POWERED_ON:
  44. break;
  45. }
  46. }
  47. void BluetoothChooser::ShowDiscoveryState(DiscoveryState state) {
  48. switch (state) {
  49. case DiscoveryState::FAILED_TO_START:
  50. event_handler_.Run(Event::CANCELLED, "");
  51. break;
  52. case DiscoveryState::IDLE:
  53. if (device_list_.empty()) {
  54. auto event =
  55. ++num_retries_ > kMaxScanRetries ? Event::CANCELLED : Event::RESCAN;
  56. event_handler_.Run(event, "");
  57. } else {
  58. bool prevent_default = api_web_contents_->Emit(
  59. "select-bluetooth-device", device_list_,
  60. base::Bind(&OnDeviceChosen, event_handler_));
  61. if (!prevent_default) {
  62. auto device_id = device_list_[0].device_id;
  63. event_handler_.Run(Event::SELECTED, device_id);
  64. }
  65. }
  66. break;
  67. case DiscoveryState::DISCOVERING:
  68. break;
  69. }
  70. }
  71. void BluetoothChooser::AddOrUpdateDevice(const std::string& device_id,
  72. bool should_update_name,
  73. const base::string16& device_name,
  74. bool is_gatt_connected,
  75. bool is_paired,
  76. int signal_strength_level) {
  77. DeviceInfo info = {device_id, device_name};
  78. device_list_.push_back(info);
  79. // Emit a select-bluetooth-device handler to allow for user to listen for
  80. // bluetooth device found.
  81. bool prevent_default =
  82. api_web_contents_->Emit("select-bluetooth-device", device_list_,
  83. base::Bind(&OnDeviceChosen, event_handler_));
  84. // If emit not implimented select first device that matches the filters
  85. // provided.
  86. if (!prevent_default) {
  87. event_handler_.Run(Event::SELECTED, device_id);
  88. }
  89. }
  90. void BluetoothChooser::RemoveDevice(const std::string& device_id) {
  91. for (auto it = device_list_.begin(); it != device_list_.end(); ++it) {
  92. if (it->device_id == device_id) {
  93. device_list_.erase(it);
  94. return;
  95. }
  96. }
  97. }
  98. } // namespace atom