2016-05-30 12:38:09 +00: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 20:46:59 +00:00
|
|
|
#include "shell/browser/lib/bluetooth_chooser.h"
|
2016-05-30 12:38:09 +00:00
|
|
|
#include "native_mate/dictionary.h"
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "shell/common/native_mate_converters/once_callback.h"
|
|
|
|
#include "shell/common/native_mate_converters/string16_converter.h"
|
2016-05-30 12:38:09 +00:00
|
|
|
|
|
|
|
namespace mate {
|
|
|
|
|
2018-04-18 01:55:30 +00:00
|
|
|
template <>
|
2019-06-19 21:23:04 +00:00
|
|
|
struct Converter<electron::BluetoothChooser::DeviceInfo> {
|
2016-05-30 12:38:09 +00:00
|
|
|
static v8::Local<v8::Value> ToV8(
|
2018-04-18 01:55:30 +00:00
|
|
|
v8::Isolate* isolate,
|
2019-06-19 21:23:04 +00:00
|
|
|
const electron::BluetoothChooser::DeviceInfo& val) {
|
2016-05-30 12:38:09 +00:00
|
|
|
mate::Dictionary dict = mate::Dictionary::CreateEmpty(isolate);
|
|
|
|
dict.Set("deviceName", val.device_name);
|
|
|
|
dict.Set("deviceId", val.device_id);
|
|
|
|
return mate::ConvertToV8(isolate, dict);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace mate
|
|
|
|
|
2019-06-19 21:23:04 +00:00
|
|
|
namespace electron {
|
2016-05-30 12:38:09 +00:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
const int kMaxScanRetries = 5;
|
|
|
|
|
2018-04-18 01:55:30 +00:00
|
|
|
void OnDeviceChosen(const content::BluetoothChooser::EventHandler& handler,
|
|
|
|
const std::string& device_id) {
|
2016-05-30 12:38:09 +00:00
|
|
|
if (device_id.empty()) {
|
|
|
|
handler.Run(content::BluetoothChooser::Event::CANCELLED, device_id);
|
|
|
|
} else {
|
|
|
|
handler.Run(content::BluetoothChooser::Event::SELECTED, device_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2018-04-18 01:55:30 +00:00
|
|
|
BluetoothChooser::BluetoothChooser(api::WebContents* contents,
|
|
|
|
const EventHandler& event_handler)
|
2018-05-21 22:18:38 +00:00
|
|
|
: api_web_contents_(contents), event_handler_(event_handler) {}
|
2016-05-30 12:38:09 +00:00
|
|
|
|
2018-04-18 01:55:30 +00:00
|
|
|
BluetoothChooser::~BluetoothChooser() {}
|
2016-05-30 12:38:09 +00:00
|
|
|
|
|
|
|
void BluetoothChooser::SetAdapterPresence(AdapterPresence presence) {
|
|
|
|
switch (presence) {
|
|
|
|
case AdapterPresence::ABSENT:
|
|
|
|
case AdapterPresence::POWERED_OFF:
|
|
|
|
event_handler_.Run(Event::CANCELLED, "");
|
|
|
|
break;
|
|
|
|
case AdapterPresence::POWERED_ON:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void BluetoothChooser::ShowDiscoveryState(DiscoveryState state) {
|
|
|
|
switch (state) {
|
|
|
|
case DiscoveryState::FAILED_TO_START:
|
|
|
|
event_handler_.Run(Event::CANCELLED, "");
|
|
|
|
break;
|
|
|
|
case DiscoveryState::IDLE:
|
2018-11-28 16:36:00 +00:00
|
|
|
if (device_map_.empty()) {
|
2018-04-18 01:55:30 +00:00
|
|
|
auto event =
|
|
|
|
++num_retries_ > kMaxScanRetries ? Event::CANCELLED : Event::RESCAN;
|
2016-05-30 12:38:09 +00:00
|
|
|
event_handler_.Run(event, "");
|
|
|
|
} else {
|
2018-04-18 01:55:30 +00:00
|
|
|
bool prevent_default = api_web_contents_->Emit(
|
2018-11-28 16:36:00 +00:00
|
|
|
"select-bluetooth-device", GetDeviceList(),
|
2019-05-02 23:49:27 +00:00
|
|
|
base::BindOnce(&OnDeviceChosen, event_handler_));
|
2016-05-30 12:38:09 +00:00
|
|
|
if (!prevent_default) {
|
2018-11-28 16:36:00 +00:00
|
|
|
auto it = device_map_.begin();
|
|
|
|
auto device_id = it->first;
|
2016-05-30 12:38:09 +00:00
|
|
|
event_handler_.Run(Event::SELECTED, device_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case DiscoveryState::DISCOVERING:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-30 07:30:03 +00:00
|
|
|
void BluetoothChooser::AddOrUpdateDevice(const std::string& device_id,
|
|
|
|
bool should_update_name,
|
|
|
|
const base::string16& device_name,
|
|
|
|
bool is_gatt_connected,
|
|
|
|
bool is_paired,
|
|
|
|
int signal_strength_level) {
|
2018-11-28 16:36:00 +00:00
|
|
|
bool changed = false;
|
|
|
|
auto entry = device_map_.find(device_id);
|
|
|
|
if (entry == device_map_.end()) {
|
|
|
|
device_map_[device_id] = device_name;
|
|
|
|
changed = true;
|
|
|
|
} else if (should_update_name) {
|
|
|
|
entry->second = device_name;
|
|
|
|
changed = true;
|
2017-12-18 10:46:06 +00:00
|
|
|
}
|
2016-05-30 12:38:09 +00:00
|
|
|
|
2018-11-28 16:36:00 +00:00
|
|
|
if (changed) {
|
|
|
|
// Emit a select-bluetooth-device handler to allow for user to listen for
|
|
|
|
// bluetooth device found.
|
2019-05-02 23:49:27 +00:00
|
|
|
bool prevent_default = api_web_contents_->Emit(
|
|
|
|
"select-bluetooth-device", GetDeviceList(),
|
|
|
|
base::BindOnce(&OnDeviceChosen, event_handler_));
|
2018-11-28 16:36:00 +00:00
|
|
|
|
|
|
|
// If emit not implimented select first device that matches the filters
|
|
|
|
// provided.
|
|
|
|
if (!prevent_default) {
|
|
|
|
event_handler_.Run(Event::SELECTED, device_id);
|
2016-05-30 12:38:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-19 21:23:04 +00:00
|
|
|
std::vector<electron::BluetoothChooser::DeviceInfo>
|
2018-11-28 16:36:00 +00:00
|
|
|
BluetoothChooser::GetDeviceList() {
|
2019-06-19 21:23:04 +00:00
|
|
|
std::vector<electron::BluetoothChooser::DeviceInfo> vec;
|
2018-11-28 16:36:00 +00:00
|
|
|
for (const auto& it : device_map_) {
|
|
|
|
DeviceInfo info = {it.first, it.second};
|
|
|
|
vec.push_back(info);
|
|
|
|
}
|
|
|
|
|
|
|
|
return vec;
|
|
|
|
}
|
|
|
|
|
2019-06-19 21:23:04 +00:00
|
|
|
} // namespace electron
|