2016-05-30 06:38:09 -06:00
|
|
|
// Copyright (c) 2016 GitHub, Inc.
|
|
|
|
// Use of this source code is governed by the MIT license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2019-06-19 14:46:59 -06:00
|
|
|
#include "shell/browser/lib/bluetooth_chooser.h"
|
2016-05-30 06:38:09 -06:00
|
|
|
|
2019-10-25 07:03:28 -06:00
|
|
|
#include "shell/common/gin_converters/callback_converter.h"
|
|
|
|
#include "shell/common/gin_helper/dictionary.h"
|
|
|
|
|
|
|
|
namespace gin {
|
2016-05-30 06:38:09 -06:00
|
|
|
|
2018-04-17 19:55:30 -06:00
|
|
|
template <>
|
2019-06-19 15:23:04 -06:00
|
|
|
struct Converter<electron::BluetoothChooser::DeviceInfo> {
|
2016-05-30 06:38:09 -06:00
|
|
|
static v8::Local<v8::Value> ToV8(
|
2018-04-17 19:55:30 -06:00
|
|
|
v8::Isolate* isolate,
|
2019-06-19 15:23:04 -06:00
|
|
|
const electron::BluetoothChooser::DeviceInfo& val) {
|
2023-08-20 19:43:41 -06:00
|
|
|
auto dict = gin_helper::Dictionary::CreateEmpty(isolate);
|
2016-05-30 06:38:09 -06:00
|
|
|
dict.Set("deviceName", val.device_name);
|
|
|
|
dict.Set("deviceId", val.device_id);
|
2019-10-25 07:03:28 -06:00
|
|
|
return gin::ConvertToV8(isolate, dict);
|
2016-05-30 06:38:09 -06:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-10-25 07:03:28 -06:00
|
|
|
} // namespace gin
|
2016-05-30 06:38:09 -06:00
|
|
|
|
2019-06-19 15:23:04 -06:00
|
|
|
namespace electron {
|
2016-05-30 06:38:09 -06:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2018-04-17 19:55:30 -06:00
|
|
|
void OnDeviceChosen(const content::BluetoothChooser::EventHandler& handler,
|
|
|
|
const std::string& device_id) {
|
2016-05-30 06:38:09 -06:00
|
|
|
if (device_id.empty()) {
|
2020-10-15 19:30:41 -06:00
|
|
|
handler.Run(content::BluetoothChooserEvent::CANCELLED, device_id);
|
2016-05-30 06:38:09 -06:00
|
|
|
} else {
|
2020-10-15 19:30:41 -06:00
|
|
|
handler.Run(content::BluetoothChooserEvent::SELECTED, device_id);
|
2016-05-30 06:38:09 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2018-04-17 19:55:30 -06:00
|
|
|
BluetoothChooser::BluetoothChooser(api::WebContents* contents,
|
|
|
|
const EventHandler& event_handler)
|
2018-05-21 16:18:38 -06:00
|
|
|
: api_web_contents_(contents), event_handler_(event_handler) {}
|
2016-05-30 06:38:09 -06:00
|
|
|
|
2021-02-26 12:10:27 -07:00
|
|
|
BluetoothChooser::~BluetoothChooser() {
|
|
|
|
event_handler_.Reset();
|
|
|
|
}
|
2016-05-30 06:38:09 -06:00
|
|
|
|
|
|
|
void BluetoothChooser::SetAdapterPresence(AdapterPresence presence) {
|
|
|
|
switch (presence) {
|
|
|
|
case AdapterPresence::ABSENT:
|
|
|
|
case AdapterPresence::POWERED_OFF:
|
2021-02-09 13:16:21 -07:00
|
|
|
// Chrome currently directs the user to system preferences
|
|
|
|
// to grant bluetooth permission for this case, should we
|
|
|
|
// do something similar ?
|
|
|
|
// https://chromium-review.googlesource.com/c/chromium/src/+/2617129
|
|
|
|
case AdapterPresence::UNAUTHORIZED:
|
2020-10-15 19:30:41 -06:00
|
|
|
event_handler_.Run(content::BluetoothChooserEvent::CANCELLED, "");
|
2016-05-30 06:38:09 -06:00
|
|
|
break;
|
|
|
|
case AdapterPresence::POWERED_ON:
|
2021-06-09 08:48:18 -06:00
|
|
|
rescan_ = true;
|
2016-05-30 06:38:09 -06:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void BluetoothChooser::ShowDiscoveryState(DiscoveryState state) {
|
2023-03-27 07:31:15 -06:00
|
|
|
bool idle_state = false;
|
2016-05-30 06:38:09 -06:00
|
|
|
switch (state) {
|
|
|
|
case DiscoveryState::FAILED_TO_START:
|
2021-02-26 12:10:27 -07:00
|
|
|
refreshing_ = false;
|
2020-10-15 19:30:41 -06:00
|
|
|
event_handler_.Run(content::BluetoothChooserEvent::CANCELLED, "");
|
2023-03-27 07:31:15 -06:00
|
|
|
return;
|
2016-05-30 06:38:09 -06:00
|
|
|
case DiscoveryState::IDLE:
|
2021-02-26 12:10:27 -07:00
|
|
|
refreshing_ = false;
|
2023-03-27 07:31:15 -06:00
|
|
|
idle_state = true;
|
2016-05-30 06:38:09 -06:00
|
|
|
break;
|
|
|
|
case DiscoveryState::DISCOVERING:
|
2021-02-26 12:10:27 -07:00
|
|
|
// The first time this state fires is due to a rescan triggering so set a
|
|
|
|
// flag to ignore devices
|
2021-06-09 08:48:18 -06:00
|
|
|
if (rescan_ && !refreshing_) {
|
2021-02-26 12:10:27 -07:00
|
|
|
refreshing_ = true;
|
|
|
|
} else {
|
|
|
|
// The second time this state fires we are now safe to pick a device
|
|
|
|
refreshing_ = false;
|
|
|
|
}
|
2016-05-30 06:38:09 -06:00
|
|
|
break;
|
|
|
|
}
|
2023-03-27 07:31:15 -06:00
|
|
|
bool prevent_default =
|
|
|
|
api_web_contents_->Emit("select-bluetooth-device", GetDeviceList(),
|
|
|
|
base::BindOnce(&OnDeviceChosen, event_handler_));
|
|
|
|
if (!prevent_default && idle_state) {
|
|
|
|
if (device_map_.empty()) {
|
|
|
|
event_handler_.Run(content::BluetoothChooserEvent::CANCELLED, "");
|
|
|
|
} else {
|
|
|
|
auto it = device_map_.begin();
|
|
|
|
auto device_id = it->first;
|
|
|
|
event_handler_.Run(content::BluetoothChooserEvent::SELECTED, device_id);
|
|
|
|
}
|
|
|
|
}
|
2016-05-30 06:38:09 -06:00
|
|
|
}
|
|
|
|
|
2016-11-30 00:30:03 -07:00
|
|
|
void BluetoothChooser::AddOrUpdateDevice(const std::string& device_id,
|
|
|
|
bool should_update_name,
|
2021-03-16 10:18:45 -06:00
|
|
|
const std::u16string& device_name,
|
2016-11-30 00:30:03 -07:00
|
|
|
bool is_gatt_connected,
|
|
|
|
bool is_paired,
|
|
|
|
int signal_strength_level) {
|
2021-02-26 12:10:27 -07:00
|
|
|
if (refreshing_) {
|
|
|
|
// If the list of bluetooth devices is currently being generated don't fire
|
|
|
|
// an event
|
|
|
|
return;
|
|
|
|
}
|
2023-07-13 02:16:25 -06:00
|
|
|
|
|
|
|
auto [iter, changed] = device_map_.try_emplace(device_id, device_name);
|
|
|
|
if (!changed && should_update_name) {
|
|
|
|
iter->second = device_name;
|
2018-11-28 09:36:00 -07:00
|
|
|
changed = true;
|
2017-12-18 03:46:06 -07:00
|
|
|
}
|
2016-05-30 06:38:09 -06:00
|
|
|
|
2018-11-28 09:36:00 -07:00
|
|
|
if (changed) {
|
|
|
|
// Emit a select-bluetooth-device handler to allow for user to listen for
|
|
|
|
// bluetooth device found.
|
2019-05-02 17:49:27 -06:00
|
|
|
bool prevent_default = api_web_contents_->Emit(
|
|
|
|
"select-bluetooth-device", GetDeviceList(),
|
|
|
|
base::BindOnce(&OnDeviceChosen, event_handler_));
|
2018-11-28 09:36:00 -07:00
|
|
|
|
2022-06-16 01:46:11 -06:00
|
|
|
// If emit not implemented select first device that matches the filters
|
2018-11-28 09:36:00 -07:00
|
|
|
// provided.
|
|
|
|
if (!prevent_default) {
|
2020-10-15 19:30:41 -06:00
|
|
|
event_handler_.Run(content::BluetoothChooserEvent::SELECTED, device_id);
|
2016-05-30 06:38:09 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-19 15:23:04 -06:00
|
|
|
std::vector<electron::BluetoothChooser::DeviceInfo>
|
2018-11-28 09:36:00 -07:00
|
|
|
BluetoothChooser::GetDeviceList() {
|
2019-06-19 15:23:04 -06:00
|
|
|
std::vector<electron::BluetoothChooser::DeviceInfo> vec;
|
2019-09-13 08:26:59 -06:00
|
|
|
vec.reserve(device_map_.size());
|
2023-07-13 02:16:25 -06:00
|
|
|
for (const auto& [device_id, device_name] : device_map_)
|
|
|
|
vec.emplace_back(device_id, device_name);
|
2018-11-28 09:36:00 -07:00
|
|
|
return vec;
|
|
|
|
}
|
|
|
|
|
2019-06-19 15:23:04 -06:00
|
|
|
} // namespace electron
|