Skip to content

Commit

Permalink
fix: serialPort.open() failing (#35339)
Browse files Browse the repository at this point in the history
fix: serialPort.open() failing

Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>
  • Loading branch information
trop[bot] and codebytere committed Aug 15, 2022
1 parent fe70152 commit 1978a0b
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
8 changes: 2 additions & 6 deletions shell/browser/serial/electron_serial_delegate.cc
Expand Up @@ -56,10 +56,7 @@ bool ElectronSerialDelegate::HasPortPermission(
content::RenderFrameHost* frame,
const device::mojom::SerialPortInfo& port) {
auto* web_contents = content::WebContents::FromRenderFrameHost(frame);
auto* browser_context = web_contents->GetBrowserContext();
auto* chooser_context =
SerialChooserContextFactory::GetForBrowserContext(browser_context);
return chooser_context->HasPortPermission(
return GetChooserContext(frame)->HasPortPermission(
web_contents->GetPrimaryMainFrame()->GetLastCommittedOrigin(), port,
frame);
}
Expand Down Expand Up @@ -91,8 +88,7 @@ void ElectronSerialDelegate::RevokePortPermissionWebInitiated(
const device::mojom::SerialPortInfo* ElectronSerialDelegate::GetPortInfo(
content::RenderFrameHost* frame,
const base::UnguessableToken& token) {
// TODO(nornagon/jkleinsc): pass this on to the chooser context
return nullptr;
return GetChooserContext(frame)->GetPortInfo(token);
}

SerialChooserController* ElectronSerialDelegate::ControllerForFrame(
Expand Down
16 changes: 16 additions & 0 deletions shell/browser/serial/serial_chooser_context.cc
Expand Up @@ -103,6 +103,8 @@ void SerialChooserContext::GrantPortPermission(
const url::Origin& origin,
const device::mojom::SerialPortInfo& port,
content::RenderFrameHost* render_frame_host) {
port_info_.insert({port.token, port.Clone()});

auto* permission_manager = static_cast<ElectronPermissionManager*>(
browser_context_->GetPermissionControllerDelegate());
return permission_manager->GrantDevicePermission(
Expand Down Expand Up @@ -190,6 +192,9 @@ base::WeakPtr<SerialChooserContext> SerialChooserContext::AsWeakPtr() {
}

void SerialChooserContext::OnPortAdded(device::mojom::SerialPortInfoPtr port) {
if (!base::Contains(port_info_, port->token))
port_info_.insert({port->token, port->Clone()});

for (auto& observer : port_observer_list_)
observer.OnPortAdded(*port);
}
Expand All @@ -198,6 +203,8 @@ void SerialChooserContext::OnPortRemoved(
device::mojom::SerialPortInfoPtr port) {
for (auto& observer : port_observer_list_)
observer.OnPortRemoved(*port);

port_info_.erase(port->token);
}

void SerialChooserContext::EnsurePortManagerConnection() {
Expand All @@ -218,6 +225,15 @@ void SerialChooserContext::SetUpPortManagerConnection(
base::Unretained(this)));

port_manager_->SetClient(client_receiver_.BindNewPipeAndPassRemote());
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;
}

void SerialChooserContext::OnPortManagerConnectionError() {
Expand Down
1 change: 1 addition & 0 deletions shell/browser/serial/serial_chooser_context.h
Expand Up @@ -97,6 +97,7 @@ class SerialChooserContext : public KeyedService,
void EnsurePortManagerConnection();
void SetUpPortManagerConnection(
mojo::PendingRemote<device::mojom::SerialPortManager> manager);
void OnGetDevices(std::vector<device::mojom::SerialPortInfoPtr> ports);
void OnPortManagerConnectionError();
void RevokeObjectPermissionInternal(const url::Origin& origin,
const base::Value& object,
Expand Down

0 comments on commit 1978a0b

Please sign in to comment.