bluetooth_chooser.cc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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_map_.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", GetDeviceList(),
  60. base::Bind(&OnDeviceChosen, event_handler_));
  61. if (!prevent_default) {
  62. auto it = device_map_.begin();
  63. auto device_id = it->first;
  64. event_handler_.Run(Event::SELECTED, device_id);
  65. }
  66. }
  67. break;
  68. case DiscoveryState::DISCOVERING:
  69. break;
  70. }
  71. }
  72. void BluetoothChooser::AddOrUpdateDevice(const std::string& device_id,
  73. bool should_update_name,
  74. const base::string16& device_name,
  75. bool is_gatt_connected,
  76. bool is_paired,
  77. int signal_strength_level) {
  78. bool changed = false;
  79. auto entry = device_map_.find(device_id);
  80. if (entry == device_map_.end()) {
  81. device_map_[device_id] = device_name;
  82. changed = true;
  83. } else if (should_update_name) {
  84. entry->second = device_name;
  85. changed = true;
  86. }
  87. if (changed) {
  88. // Emit a select-bluetooth-device handler to allow for user to listen for
  89. // bluetooth device found.
  90. bool prevent_default =
  91. api_web_contents_->Emit("select-bluetooth-device", GetDeviceList(),
  92. base::Bind(&OnDeviceChosen, event_handler_));
  93. // If emit not implimented select first device that matches the filters
  94. // provided.
  95. if (!prevent_default) {
  96. event_handler_.Run(Event::SELECTED, device_id);
  97. }
  98. }
  99. }
  100. std::vector<atom::BluetoothChooser::DeviceInfo>
  101. BluetoothChooser::GetDeviceList() {
  102. std::vector<atom::BluetoothChooser::DeviceInfo> vec;
  103. for (const auto& it : device_map_) {
  104. DeviceInfo info = {it.first, it.second};
  105. vec.push_back(info);
  106. }
  107. return vec;
  108. }
  109. } // namespace atom