2020-09-28 16:22:03 +00:00
|
|
|
// Copyright 2019 The Chromium Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
#include "shell/browser/serial/serial_chooser_context.h"
|
|
|
|
|
2021-06-01 01:34:44 +00:00
|
|
|
#include <string>
|
2020-09-28 16:22:03 +00:00
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
#include "base/base64.h"
|
2021-06-03 08:05:04 +00:00
|
|
|
#include "base/containers/contains.h"
|
chore: bump chromium to 128.0.6611.0 (main) (#42779)
* chore: bump chromium in DEPS to 128.0.6577.0
* chore: bump chromium in DEPS to 128.0.6579.0
* 5675706: Reland "Reland "Reland "Reland "Add toolchains without PartitionAlloc-Everywhere for dump_syms et al""""
https://chromium-review.googlesource.com/c/chromium/src/+/5675706
* 5668597: [PDF Ink Signatures] Prompt download menu on save when there are edits
https://chromium-review.googlesource.com/c/chromium/src/+/5668597
* 5677014: Reland "Pull data_sharing_sdk from CIPD"
https://chromium-review.googlesource.com/c/chromium/src/+/5677014
* chore: fixup patch indices
* chore: bump chromium in DEPS to 128.0.6581.0
* chore: bump chromium in DEPS to 128.0.6583.0
* update patches
* 5455480: [Extensions] Allow service worker requests to continue without a cert
https://chromium-review.googlesource.com/c/chromium/src/+/5455480
* try to get some debugging output from script/push-patch.js
* chore: bump chromium in DEPS to 128.0.6585.0
* chore: bump chromium in DEPS to 128.0.6587.0
* update patches
* chore: bump chromium in DEPS to 128.0.6589.0
* more patch work
* maybe over here?
* chore: update patches
* 5673207: [HTTPS Upgrades] Disable in captive portal login webview
https://chromium-review.googlesource.com/c/chromium/src/+/5673207
* 5636785: Extensions: WAR: manifest.json's use_dynamic_url requires a dynamic url
https://chromium-review.googlesource.com/c/chromium/src/+/5636785
* chore: bump chromium in DEPS to 128.0.6591.0
* 5665458: Trigger WN2 page when feature is enabled
https://chromium-review.googlesource.com/c/chromium/src/+/5665458
* update patches
* chore: bump chromium in DEPS to 128.0.6593.0
* chore: bump chromium in DEPS to 128.0.6595.0
* chore: bump chromium in DEPS to 128.0.6597.0
* (patch update) 5694586: [compile hints] Remove the usage of v8::Isolate::SetJavaScriptCompileHintsMagicEnabledCallback API
https://chromium-review.googlesource.com/c/chromium/src/+/5694586
* update patches
* 5691287: Reland "Change blink::WebKeyboardEvent to use std::array in is members"
https://chromium-review.googlesource.com/c/chromium/src/+/5691287
The code changed here is modeled after code in `content/renderer/pepper/event_conversion.cc` that was also modified in this CL, so I took the same approach.
* 5529018: Cleanup EnableWebHidOnExtensionServiceWorker flag
https://chromium-review.googlesource.com/c/chromium/src/+/5529018
* 5526324: [Code Health] Add deprecation comment for base::SupportsWeakPtr.
https://chromium-review.googlesource.com/c/chromium/src/+/5526324
Note that this CL actually does make `SupportsWeakPtr` strictly restricted to existing implementations, no new ones. We could add a patch to add ourselves to this list, but it looks like we'll have to refactor this anyways in the near future. Since the refactor seems straightforward, let's try that first.
* chore: bump chromium in DEPS to 128.0.6598.0
* chore: update patches
* 5704737: Rename ExclusiveAccessContext::GetActiveWebContents to avoid conflict
https://chromium-review.googlesource.com/c/chromium/src/+/5704737
* chore: bump chromium in DEPS to 128.0.6601.0
* chore: update patches
* Add `base::StringPiece` header includes
Chromium is working on replacing `base::StringPiece` with `std::string_view`. (See the Chromium Bug below.) They're currently running mass codemods (across many multiple changes) to replace uses of `StringPiece` with `string_view`, including removing the header include for `StringPiece` in those files. This cascades down to our files that were implicitly depending on those includes through some other include.
They're on track to eventually deprecate and remove `StringPiece` so our code should be converted, but that can be done as an upgrade follow-up task. For now, adding the header back to files that need it should suffice for minimal upgrade changes.
Chromium Bug: https://issues.chromium.org/issues/40506050
* 5702737: GlobalRequestID: Avoid unwanted inlining and narrowing int conversions
https://chromium-review.googlesource.com/c/chromium/src/+/5702737
contender for smallest commit 2024
* 5706534: Rename GlobalFeatures to GlobalDesktopFeatures.
https://chromium-review.googlesource.com/c/chromium/src/+/5706534
* 5691321: ui: remove params variants of SelectFile listener functions
https://chromium-review.googlesource.com/c/chromium/src/+/5691321
* 5714949: [Extensions] Display re-enable dialog for MV2 disabled stage
https://chromium-review.googlesource.com/c/chromium/src/+/5714949
* chore: update libc++ filenames
* patch: disable scope reuse & associated dchecks in v8 (hopefully temp, upgrade follow-up)
* fixup! Add `base::StringPiece` header includes
* update MAS patch
5710330: Add crash keys to debug NativeWidgetMacNSWindowBorderlessFrame exception
https://chromium-review.googlesource.com/c/chromium/src/+/5710330
* chore: bump chromium in DEPS to 128.0.6603.0
* chore: update patches
* 5713258: Reland "Preparation for decoupling creation/initialization of context"
https://chromium-review.googlesource.com/c/chromium/src/+/5713258
When destroying a context, it must already be shutdown, and this change enforces it with a new CHECK.
We were overriding `BrowserContextKeyedServiceFactory::BrowserContextShutdown` with an empty implementation, which differed from the default implementation that notifies the `KeyedServiceFactory` that the context has shutdown. Since we were missing this notification, the CHECK would later trip when the service was being destoryed because it was not registered as shutdown when it was shutdown.
* chore: bump chromium in DEPS to 128.0.6605.2
* chore: update patches
* refactor: linux open/save dialog patch
Our existing implementation was relying on an opaque `void* params` parameter that was passed through `ui::SelectFileDialog`.
Recently, that parameter has been getting removed:
- 5691321: ui: remove params variants of SelectFile listener functions | https://chromium-review.googlesource.com/c/chromium/src/+/5691321
- 5709097: ui: remove SelectFileDialog impl params | https://chromium-review.googlesource.com/c/chromium/src/+/5709097
- https://issues.chromium.org/issues/340178601 "reconsider SelectFileDialog"
This restructures the patch to work with mostly the same mechanics, but directly on the `ui::SelectFileDialog` object. This nets us some wins in terms of a smaller patch.
* 5713262: DevTools UI binding AIDA client event returns response
https://chromium-review.googlesource.com/c/chromium/src/+/5713262
* fixup! refactor: linux open/save dialog patch
* chore: bump chromium in DEPS to 128.0.6606.0
* chore: update patches
* fixup! refactor: linux open/save dialog patch
* chore: bump chromium in DEPS to 128.0.6607.0
* chore: update printing.patch
Xref: https://chromium-review.googlesource.com/c/chromium/src/+/5722937
* fix: pwd error in electron-test, nan-test
fix: unshallow depot_tools before 3-way apply
* chore: e patches all
* fixup! fix: pwd error in electron-test, nan-test
* chore: bump chromium in DEPS to 128.0.6609.0
* chore: bump chromium in DEPS to 128.0.6611.0
* chore: update patches
* chore: update libcxx filenames
---------
Co-authored-by: electron-roller[bot] <84116207+electron-roller[bot]@users.noreply.github.com>
Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>
Co-authored-by: Jeremy Rose <nornagon@nornagon.net>
Co-authored-by: PatchUp <73610968+patchup[bot]@users.noreply.github.com>
Co-authored-by: clavin <clavin@electronjs.org>
Co-authored-by: Charles Kerr <charles@charleskerr.com>
Co-authored-by: Alice Zhao <alice@makenotion.com>
2024-07-23 15:59:44 +00:00
|
|
|
#include "base/strings/string_piece.h"
|
2020-09-28 16:22:03 +00:00
|
|
|
#include "base/values.h"
|
|
|
|
#include "content/public/browser/device_service.h"
|
2021-10-06 20:18:00 +00:00
|
|
|
#include "content/public/browser/web_contents.h"
|
2020-09-28 16:22:03 +00:00
|
|
|
#include "mojo/public/cpp/bindings/pending_remote.h"
|
2022-10-18 09:22:32 +00:00
|
|
|
#include "shell/browser/api/electron_api_session.h"
|
2024-07-25 09:25:45 +00:00
|
|
|
#include "shell/browser/electron_browser_context.h"
|
2022-06-27 20:50:08 +00:00
|
|
|
#include "shell/browser/electron_permission_manager.h"
|
2021-10-06 20:18:00 +00:00
|
|
|
#include "shell/browser/web_contents_permission_helper.h"
|
2022-10-18 09:22:32 +00:00
|
|
|
#include "shell/common/gin_converters/frame_converter.h"
|
|
|
|
#include "shell/common/gin_converters/serial_port_info_converter.h"
|
2020-09-28 16:22:03 +00:00
|
|
|
|
|
|
|
namespace electron {
|
|
|
|
|
|
|
|
constexpr char kPortNameKey[] = "name";
|
|
|
|
constexpr char kTokenKey[] = "token";
|
2022-02-10 02:58:52 +00:00
|
|
|
#if BUILDFLAG(IS_WIN)
|
2021-10-06 20:18:00 +00:00
|
|
|
const char kDeviceInstanceIdKey[] = "device_instance_id";
|
2020-10-16 01:30:41 +00:00
|
|
|
#else
|
2021-10-06 20:18:00 +00:00
|
|
|
const char kVendorIdKey[] = "vendor_id";
|
|
|
|
const char kProductIdKey[] = "product_id";
|
|
|
|
const char kSerialNumberKey[] = "serial_number";
|
2022-02-10 02:58:52 +00:00
|
|
|
#if BUILDFLAG(IS_MAC)
|
2021-10-06 20:18:00 +00:00
|
|
|
const char kUsbDriverKey[] = "usb_driver";
|
2022-02-10 02:58:52 +00:00
|
|
|
#endif // BUILDFLAG(IS_MAC)
|
|
|
|
#endif // BUILDFLAG(IS_WIN)
|
2020-09-28 16:22:03 +00:00
|
|
|
|
|
|
|
std::string EncodeToken(const base::UnguessableToken& token) {
|
|
|
|
const uint64_t data[2] = {token.GetHighForSerialization(),
|
|
|
|
token.GetLowForSerialization()};
|
2024-01-15 10:01:35 +00:00
|
|
|
return base::Base64Encode(
|
|
|
|
base::StringPiece(reinterpret_cast<const char*>(&data[0]), sizeof(data)));
|
2020-09-28 16:22:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
base::Value PortInfoToValue(const device::mojom::SerialPortInfo& port) {
|
2022-10-18 09:22:32 +00:00
|
|
|
base::Value::Dict value;
|
2020-09-28 16:22:03 +00:00
|
|
|
if (port.display_name && !port.display_name->empty())
|
2022-10-18 09:22:32 +00:00
|
|
|
value.Set(kPortNameKey, *port.display_name);
|
2020-09-28 16:22:03 +00:00
|
|
|
else
|
2022-10-18 09:22:32 +00:00
|
|
|
value.Set(kPortNameKey, port.path.LossyDisplayName());
|
2020-10-16 01:30:41 +00:00
|
|
|
|
|
|
|
if (!SerialChooserContext::CanStorePersistentEntry(port)) {
|
2022-10-18 09:22:32 +00:00
|
|
|
value.Set(kTokenKey, EncodeToken(port.token));
|
|
|
|
return base::Value(std::move(value));
|
2020-10-16 01:30:41 +00:00
|
|
|
}
|
|
|
|
|
2022-02-10 02:58:52 +00:00
|
|
|
#if BUILDFLAG(IS_WIN)
|
2020-10-16 01:30:41 +00:00
|
|
|
// Windows provides a handy device identifier which we can rely on to be
|
|
|
|
// sufficiently stable for identifying devices across restarts.
|
2022-10-18 09:22:32 +00:00
|
|
|
value.Set(kDeviceInstanceIdKey, port.device_instance_id);
|
2020-10-16 01:30:41 +00:00
|
|
|
#else
|
|
|
|
DCHECK(port.has_vendor_id);
|
2022-10-18 09:22:32 +00:00
|
|
|
value.Set(kVendorIdKey, port.vendor_id);
|
2020-10-16 01:30:41 +00:00
|
|
|
DCHECK(port.has_product_id);
|
2022-10-18 09:22:32 +00:00
|
|
|
value.Set(kProductIdKey, port.product_id);
|
2020-10-16 01:30:41 +00:00
|
|
|
DCHECK(port.serial_number);
|
2022-10-18 09:22:32 +00:00
|
|
|
value.Set(kSerialNumberKey, *port.serial_number);
|
2020-10-16 01:30:41 +00:00
|
|
|
|
2022-02-10 02:58:52 +00:00
|
|
|
#if BUILDFLAG(IS_MAC)
|
2020-10-16 01:30:41 +00:00
|
|
|
DCHECK(port.usb_driver_name && !port.usb_driver_name->empty());
|
2022-10-18 09:22:32 +00:00
|
|
|
value.Set(kUsbDriverKey, *port.usb_driver_name);
|
2022-02-10 02:58:52 +00:00
|
|
|
#endif // BUILDFLAG(IS_MAC)
|
|
|
|
#endif // BUILDFLAG(IS_WIN)
|
2022-10-18 09:22:32 +00:00
|
|
|
return base::Value(std::move(value));
|
2020-09-28 16:22:03 +00:00
|
|
|
}
|
|
|
|
|
2022-06-27 20:50:08 +00:00
|
|
|
SerialChooserContext::SerialChooserContext(ElectronBrowserContext* context)
|
|
|
|
: browser_context_(context) {}
|
2021-07-26 17:10:57 +00:00
|
|
|
|
2022-07-25 14:50:19 +00:00
|
|
|
SerialChooserContext::~SerialChooserContext() {
|
|
|
|
// Notify observers that the chooser context is about to be destroyed.
|
|
|
|
// Observers must remove themselves from the observer lists.
|
|
|
|
for (auto& observer : port_observer_list_) {
|
|
|
|
observer.OnSerialChooserContextShutdown();
|
|
|
|
DCHECK(!port_observer_list_.HasObserver(&observer));
|
|
|
|
}
|
2022-05-17 16:48:40 +00:00
|
|
|
}
|
|
|
|
|
2020-09-28 16:22:03 +00:00
|
|
|
void SerialChooserContext::GrantPortPermission(
|
2021-07-26 17:10:57 +00:00
|
|
|
const url::Origin& origin,
|
2021-10-06 20:18:00 +00:00
|
|
|
const device::mojom::SerialPortInfo& port,
|
|
|
|
content::RenderFrameHost* render_frame_host) {
|
2022-08-15 15:49:20 +00:00
|
|
|
port_info_.insert({port.token, port.Clone()});
|
|
|
|
|
2022-10-18 09:22:32 +00:00
|
|
|
if (CanStorePersistentEntry(port)) {
|
|
|
|
auto* permission_manager = static_cast<ElectronPermissionManager*>(
|
|
|
|
browser_context_->GetPermissionControllerDelegate());
|
|
|
|
permission_manager->GrantDevicePermission(
|
|
|
|
static_cast<blink::PermissionType>(
|
|
|
|
WebContentsPermissionHelper::PermissionType::SERIAL),
|
|
|
|
origin, PortInfoToValue(port), browser_context_);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ephemeral_ports_[origin].insert(port.token);
|
2020-09-28 16:22:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool SerialChooserContext::HasPortPermission(
|
2021-07-26 17:10:57 +00:00
|
|
|
const url::Origin& origin,
|
2021-10-06 20:18:00 +00:00
|
|
|
const device::mojom::SerialPortInfo& port,
|
|
|
|
content::RenderFrameHost* render_frame_host) {
|
2022-10-18 09:22:32 +00:00
|
|
|
auto it = ephemeral_ports_.find(origin);
|
|
|
|
if (it != ephemeral_ports_.end()) {
|
|
|
|
const std::set<base::UnguessableToken>& ports = it->second;
|
|
|
|
if (base::Contains(ports, port.token))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!CanStorePersistentEntry(port))
|
|
|
|
return false;
|
|
|
|
|
2022-06-27 20:50:08 +00:00
|
|
|
auto* permission_manager = static_cast<ElectronPermissionManager*>(
|
|
|
|
browser_context_->GetPermissionControllerDelegate());
|
|
|
|
return permission_manager->CheckDevicePermission(
|
|
|
|
static_cast<blink::PermissionType>(
|
|
|
|
WebContentsPermissionHelper::PermissionType::SERIAL),
|
|
|
|
origin, PortInfoToValue(port), browser_context_);
|
2020-09-28 16:22:03 +00:00
|
|
|
}
|
|
|
|
|
2022-05-17 16:48:40 +00:00
|
|
|
void SerialChooserContext::RevokePortPermissionWebInitiated(
|
|
|
|
const url::Origin& origin,
|
2022-10-18 09:22:32 +00:00
|
|
|
const base::UnguessableToken& token,
|
|
|
|
content::RenderFrameHost* render_frame_host) {
|
2022-05-17 16:48:40 +00:00
|
|
|
auto it = port_info_.find(token);
|
2022-10-18 09:22:32 +00:00
|
|
|
if (it != port_info_.end()) {
|
|
|
|
auto* permission_manager = static_cast<ElectronPermissionManager*>(
|
|
|
|
browser_context_->GetPermissionControllerDelegate());
|
|
|
|
permission_manager->RevokeDevicePermission(
|
|
|
|
static_cast<blink::PermissionType>(
|
|
|
|
WebContentsPermissionHelper::PermissionType::SERIAL),
|
|
|
|
origin, PortInfoToValue(*it->second), browser_context_);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto ephemeral = ephemeral_ports_.find(origin);
|
|
|
|
if (ephemeral != ephemeral_ports_.end()) {
|
|
|
|
std::set<base::UnguessableToken>& ports = ephemeral->second;
|
|
|
|
ports.erase(token);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto* web_contents =
|
|
|
|
content::WebContents::FromRenderFrameHost(render_frame_host);
|
|
|
|
api::Session* session =
|
|
|
|
api::Session::FromBrowserContext(web_contents->GetBrowserContext());
|
|
|
|
|
|
|
|
if (session) {
|
|
|
|
v8::Isolate* isolate = JavascriptEnvironment::GetIsolate();
|
|
|
|
v8::HandleScope scope(isolate);
|
2023-10-10 10:45:44 +00:00
|
|
|
auto details = gin_helper::Dictionary::CreateEmpty(isolate);
|
2022-10-18 09:22:32 +00:00
|
|
|
details.Set("port", it->second);
|
|
|
|
details.SetGetter("frame", render_frame_host);
|
|
|
|
details.Set("origin", origin.Serialize());
|
|
|
|
session->Emit("serial-port-revoked", details);
|
|
|
|
}
|
2022-05-17 16:48:40 +00:00
|
|
|
}
|
|
|
|
|
2020-09-28 16:22:03 +00:00
|
|
|
// static
|
|
|
|
bool SerialChooserContext::CanStorePersistentEntry(
|
|
|
|
const device::mojom::SerialPortInfo& port) {
|
|
|
|
// If there is no display name then the path name will be used instead. The
|
|
|
|
// path name is not guaranteed to be stable. For example, on Linux the name
|
|
|
|
// "ttyUSB0" is reused for any USB serial device. A name like that would be
|
|
|
|
// confusing to show in settings when the device is disconnected.
|
|
|
|
if (!port.display_name || port.display_name->empty())
|
|
|
|
return false;
|
|
|
|
|
2022-02-10 02:58:52 +00:00
|
|
|
#if BUILDFLAG(IS_WIN)
|
2020-10-16 01:30:41 +00:00
|
|
|
return !port.device_instance_id.empty();
|
|
|
|
#else
|
|
|
|
if (!port.has_vendor_id || !port.has_product_id || !port.serial_number ||
|
|
|
|
port.serial_number->empty()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-02-10 02:58:52 +00:00
|
|
|
#if BUILDFLAG(IS_MAC)
|
2020-10-16 01:30:41 +00:00
|
|
|
// The combination of the standard USB vendor ID, product ID and serial
|
|
|
|
// number properties should be enough to uniquely identify a device
|
|
|
|
// however recent versions of macOS include built-in drivers for common
|
|
|
|
// types of USB-to-serial adapters while their manufacturers still
|
|
|
|
// recommend installing their custom drivers. When both are loaded two
|
|
|
|
// IOSerialBSDClient instances are found for each device. Including the
|
|
|
|
// USB driver name allows us to distinguish between the two.
|
|
|
|
if (!port.usb_driver_name || port.usb_driver_name->empty())
|
|
|
|
return false;
|
2022-02-10 02:58:52 +00:00
|
|
|
#endif // BUILDFLAG(IS_MAC)
|
2020-10-16 01:30:41 +00:00
|
|
|
|
|
|
|
return true;
|
2022-02-10 02:58:52 +00:00
|
|
|
#endif // BUILDFLAG(IS_WIN)
|
2020-09-28 16:22:03 +00:00
|
|
|
}
|
|
|
|
|
2022-05-17 16:48:40 +00:00
|
|
|
const device::mojom::SerialPortInfo* SerialChooserContext::GetPortInfo(
|
|
|
|
const base::UnguessableToken& token) {
|
|
|
|
DCHECK(is_initialized_);
|
|
|
|
auto it = port_info_.find(token);
|
|
|
|
return it == port_info_.end() ? nullptr : it->second.get();
|
|
|
|
}
|
|
|
|
|
2020-09-28 16:22:03 +00:00
|
|
|
device::mojom::SerialPortManager* SerialChooserContext::GetPortManager() {
|
|
|
|
EnsurePortManagerConnection();
|
|
|
|
return port_manager_.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SerialChooserContext::AddPortObserver(PortObserver* observer) {
|
|
|
|
port_observer_list_.AddObserver(observer);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SerialChooserContext::RemovePortObserver(PortObserver* observer) {
|
|
|
|
port_observer_list_.RemoveObserver(observer);
|
|
|
|
}
|
|
|
|
|
|
|
|
base::WeakPtr<SerialChooserContext> SerialChooserContext::AsWeakPtr() {
|
|
|
|
return weak_factory_.GetWeakPtr();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SerialChooserContext::OnPortAdded(device::mojom::SerialPortInfoPtr port) {
|
2022-08-15 15:49:20 +00:00
|
|
|
if (!base::Contains(port_info_, port->token))
|
|
|
|
port_info_.insert({port->token, port->Clone()});
|
|
|
|
|
2022-10-18 09:22:32 +00:00
|
|
|
for (auto& map_entry : ephemeral_ports_) {
|
|
|
|
std::set<base::UnguessableToken>& ports = map_entry.second;
|
|
|
|
ports.erase(port->token);
|
|
|
|
}
|
|
|
|
|
2020-09-28 16:22:03 +00:00
|
|
|
for (auto& observer : port_observer_list_)
|
|
|
|
observer.OnPortAdded(*port);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SerialChooserContext::OnPortRemoved(
|
|
|
|
device::mojom::SerialPortInfoPtr port) {
|
|
|
|
for (auto& observer : port_observer_list_)
|
|
|
|
observer.OnPortRemoved(*port);
|
2022-08-15 15:49:20 +00:00
|
|
|
|
|
|
|
port_info_.erase(port->token);
|
2020-09-28 16:22:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SerialChooserContext::EnsurePortManagerConnection() {
|
|
|
|
if (port_manager_)
|
|
|
|
return;
|
|
|
|
|
|
|
|
mojo::PendingRemote<device::mojom::SerialPortManager> manager;
|
|
|
|
content::GetDeviceService().BindSerialPortManager(
|
|
|
|
manager.InitWithNewPipeAndPassReceiver());
|
|
|
|
SetUpPortManagerConnection(std::move(manager));
|
|
|
|
}
|
|
|
|
|
|
|
|
void SerialChooserContext::SetUpPortManagerConnection(
|
|
|
|
mojo::PendingRemote<device::mojom::SerialPortManager> manager) {
|
|
|
|
port_manager_.Bind(std::move(manager));
|
|
|
|
port_manager_.set_disconnect_handler(
|
|
|
|
base::BindOnce(&SerialChooserContext::OnPortManagerConnectionError,
|
|
|
|
base::Unretained(this)));
|
|
|
|
|
|
|
|
port_manager_->SetClient(client_receiver_.BindNewPipeAndPassRemote());
|
2022-08-15 15:49:20 +00:00
|
|
|
port_manager_->GetDevices(base::BindOnce(&SerialChooserContext::OnGetDevices,
|
|
|
|
weak_factory_.GetWeakPtr()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void SerialChooserContext::OnGetDevices(
|
|
|
|
std::vector<device::mojom::SerialPortInfoPtr> ports) {
|
|
|
|
for (auto& port : ports)
|
|
|
|
port_info_.insert({port->token, std::move(port)});
|
|
|
|
is_initialized_ = true;
|
2020-09-28 16:22:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SerialChooserContext::OnPortManagerConnectionError() {
|
|
|
|
port_manager_.reset();
|
|
|
|
client_receiver_.reset();
|
|
|
|
|
2022-10-18 09:22:32 +00:00
|
|
|
port_info_.clear();
|
|
|
|
ephemeral_ports_.clear();
|
|
|
|
}
|
2020-09-28 16:22:03 +00:00
|
|
|
} // namespace electron
|