Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: serialPort.open() failing #35306

Merged
merged 1 commit into from
Aug 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 2 additions & 6 deletions shell/browser/serial/electron_serial_delegate.cc
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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