2020-09-28 16:22:03 +00:00
|
|
|
// Copyright (c) 2020 Microsoft, Inc.
|
|
|
|
// Use of this source code is governed by the MIT license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
#include "shell/browser/serial/serial_chooser_controller.h"
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
#include "base/files/file_path.h"
|
2023-02-03 11:43:42 +00:00
|
|
|
#include "base/functional/bind.h"
|
2020-09-28 16:22:03 +00:00
|
|
|
#include "base/strings/stringprintf.h"
|
|
|
|
#include "base/strings/utf_string_conversions.h"
|
|
|
|
#include "shell/browser/api/electron_api_session.h"
|
|
|
|
#include "shell/browser/serial/serial_chooser_context.h"
|
|
|
|
#include "shell/browser/serial/serial_chooser_context_factory.h"
|
|
|
|
#include "shell/common/gin_converters/callback_converter.h"
|
|
|
|
#include "shell/common/gin_converters/content_converter.h"
|
|
|
|
#include "shell/common/gin_helper/dictionary.h"
|
|
|
|
#include "ui/base/l10n/l10n_util.h"
|
|
|
|
|
|
|
|
namespace gin {
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct Converter<device::mojom::SerialPortInfoPtr> {
|
|
|
|
static v8::Local<v8::Value> ToV8(
|
|
|
|
v8::Isolate* isolate,
|
|
|
|
const device::mojom::SerialPortInfoPtr& port) {
|
2023-08-21 01:43:41 +00:00
|
|
|
auto dict = gin_helper::Dictionary::CreateEmpty(isolate);
|
2020-09-28 16:22:03 +00:00
|
|
|
dict.Set("portId", port->token.ToString());
|
|
|
|
dict.Set("portName", port->path.BaseName().LossyDisplayName());
|
|
|
|
if (port->display_name && !port->display_name->empty()) {
|
|
|
|
dict.Set("displayName", *port->display_name);
|
|
|
|
}
|
|
|
|
if (port->has_vendor_id) {
|
|
|
|
dict.Set("vendorId", base::StringPrintf("%u", port->vendor_id));
|
|
|
|
}
|
|
|
|
if (port->has_product_id) {
|
|
|
|
dict.Set("productId", base::StringPrintf("%u", port->product_id));
|
|
|
|
}
|
2020-10-16 01:30:41 +00:00
|
|
|
if (port->serial_number && !port->serial_number->empty()) {
|
|
|
|
dict.Set("serialNumber", *port->serial_number);
|
|
|
|
}
|
2022-02-10 02:58:52 +00:00
|
|
|
#if BUILDFLAG(IS_MAC)
|
2020-10-16 01:30:41 +00:00
|
|
|
if (port->usb_driver_name && !port->usb_driver_name->empty()) {
|
|
|
|
dict.Set("usbDriverName", *port->usb_driver_name);
|
|
|
|
}
|
2022-02-10 02:58:52 +00:00
|
|
|
#elif BUILDFLAG(IS_WIN)
|
2020-10-16 01:30:41 +00:00
|
|
|
if (!port->device_instance_id.empty()) {
|
|
|
|
dict.Set("deviceInstanceId", port->device_instance_id);
|
|
|
|
}
|
|
|
|
#endif
|
2020-09-28 16:22:03 +00:00
|
|
|
return gin::ConvertToV8(isolate, dict);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace gin
|
|
|
|
|
|
|
|
namespace electron {
|
|
|
|
|
|
|
|
SerialChooserController::SerialChooserController(
|
|
|
|
content::RenderFrameHost* render_frame_host,
|
|
|
|
std::vector<blink::mojom::SerialPortFilterPtr> filters,
|
|
|
|
content::SerialChooser::Callback callback,
|
|
|
|
content::WebContents* web_contents,
|
|
|
|
base::WeakPtr<ElectronSerialDelegate> serial_delegate)
|
|
|
|
: WebContentsObserver(web_contents),
|
|
|
|
filters_(std::move(filters)),
|
|
|
|
callback_(std::move(callback)),
|
2021-10-06 20:18:00 +00:00
|
|
|
serial_delegate_(serial_delegate),
|
|
|
|
render_frame_host_id_(render_frame_host->GetGlobalId()) {
|
2022-06-27 20:50:08 +00:00
|
|
|
origin_ = web_contents->GetPrimaryMainFrame()->GetLastCommittedOrigin();
|
2020-09-28 16:22:03 +00:00
|
|
|
|
|
|
|
chooser_context_ = SerialChooserContextFactory::GetForBrowserContext(
|
|
|
|
web_contents->GetBrowserContext())
|
|
|
|
->AsWeakPtr();
|
|
|
|
DCHECK(chooser_context_);
|
|
|
|
chooser_context_->GetPortManager()->GetDevices(base::BindOnce(
|
|
|
|
&SerialChooserController::OnGetDevices, weak_factory_.GetWeakPtr()));
|
2022-07-25 14:50:19 +00:00
|
|
|
observation_.Observe(chooser_context_.get());
|
2020-09-28 16:22:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SerialChooserController::~SerialChooserController() {
|
|
|
|
RunCallback(/*port=*/nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
api::Session* SerialChooserController::GetSession() {
|
|
|
|
if (!web_contents()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return api::Session::FromBrowserContext(web_contents()->GetBrowserContext());
|
|
|
|
}
|
|
|
|
|
|
|
|
void SerialChooserController::OnPortAdded(
|
|
|
|
const device::mojom::SerialPortInfo& port) {
|
|
|
|
ports_.push_back(port.Clone());
|
|
|
|
api::Session* session = GetSession();
|
|
|
|
if (session) {
|
|
|
|
session->Emit("serial-port-added", port.Clone(), web_contents());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SerialChooserController::OnPortRemoved(
|
|
|
|
const device::mojom::SerialPortInfo& port) {
|
|
|
|
const auto it = std::find_if(
|
|
|
|
ports_.begin(), ports_.end(),
|
|
|
|
[&port](const auto& ptr) { return ptr->token == port.token; });
|
|
|
|
if (it != ports_.end()) {
|
|
|
|
api::Session* session = GetSession();
|
|
|
|
if (session) {
|
|
|
|
session->Emit("serial-port-removed", port.Clone(), web_contents());
|
|
|
|
}
|
|
|
|
ports_.erase(it);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-17 16:48:40 +00:00
|
|
|
void SerialChooserController::OnPortManagerConnectionError() {
|
2022-07-25 14:50:19 +00:00
|
|
|
observation_.Reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SerialChooserController::OnSerialChooserContextShutdown() {
|
|
|
|
observation_.Reset();
|
2022-05-17 16:48:40 +00:00
|
|
|
}
|
|
|
|
|
2020-09-28 16:22:03 +00:00
|
|
|
void SerialChooserController::OnDeviceChosen(const std::string& port_id) {
|
|
|
|
if (port_id.empty()) {
|
|
|
|
RunCallback(/*port=*/nullptr);
|
|
|
|
} else {
|
|
|
|
const auto it =
|
|
|
|
std::find_if(ports_.begin(), ports_.end(), [&port_id](const auto& ptr) {
|
|
|
|
return ptr->token.ToString() == port_id;
|
|
|
|
});
|
2021-04-12 13:18:39 +00:00
|
|
|
if (it != ports_.end()) {
|
2021-10-06 20:18:00 +00:00
|
|
|
auto* rfh = content::RenderFrameHost::FromID(render_frame_host_id_);
|
|
|
|
chooser_context_->GrantPortPermission(origin_, *it->get(), rfh);
|
2021-04-12 13:18:39 +00:00
|
|
|
RunCallback(it->Clone());
|
|
|
|
} else {
|
|
|
|
RunCallback(/*port=*/nullptr);
|
|
|
|
}
|
2020-09-28 16:22:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SerialChooserController::OnGetDevices(
|
|
|
|
std::vector<device::mojom::SerialPortInfoPtr> ports) {
|
|
|
|
// Sort ports by file paths.
|
|
|
|
std::sort(ports.begin(), ports.end(),
|
|
|
|
[](const auto& port1, const auto& port2) {
|
|
|
|
return port1->path.BaseName() < port2->path.BaseName();
|
|
|
|
});
|
|
|
|
|
|
|
|
for (auto& port : ports) {
|
|
|
|
if (FilterMatchesAny(*port))
|
|
|
|
ports_.push_back(std::move(port));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool prevent_default = false;
|
|
|
|
api::Session* session = GetSession();
|
|
|
|
if (session) {
|
2023-05-18 21:01:44 +00:00
|
|
|
prevent_default = session->Emit(
|
|
|
|
"select-serial-port", ports_, web_contents(),
|
|
|
|
base::BindRepeating(&SerialChooserController::OnDeviceChosen,
|
|
|
|
weak_factory_.GetWeakPtr()));
|
2020-09-28 16:22:03 +00:00
|
|
|
}
|
|
|
|
if (!prevent_default) {
|
|
|
|
RunCallback(/*port=*/nullptr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SerialChooserController::FilterMatchesAny(
|
|
|
|
const device::mojom::SerialPortInfo& port) const {
|
|
|
|
if (filters_.empty())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
for (const auto& filter : filters_) {
|
|
|
|
if (filter->has_vendor_id &&
|
|
|
|
(!port.has_vendor_id || filter->vendor_id != port.vendor_id)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (filter->has_product_id &&
|
|
|
|
(!port.has_product_id || filter->product_id != port.product_id)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SerialChooserController::RunCallback(
|
|
|
|
device::mojom::SerialPortInfoPtr port) {
|
|
|
|
if (callback_) {
|
|
|
|
std::move(callback_).Run(std::move(port));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace electron
|