diff --git a/shell/browser/serial/electron_serial_delegate.cc b/shell/browser/serial/electron_serial_delegate.cc index 98a5a3f803a7..2306be383db4 100644 --- a/shell/browser/serial/electron_serial_delegate.cc +++ b/shell/browser/serial/electron_serial_delegate.cc @@ -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); } @@ -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( diff --git a/shell/browser/serial/serial_chooser_context.cc b/shell/browser/serial/serial_chooser_context.cc index 57798ccf5692..080a1030d431 100644 --- a/shell/browser/serial/serial_chooser_context.cc +++ b/shell/browser/serial/serial_chooser_context.cc @@ -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( browser_context_->GetPermissionControllerDelegate()); return permission_manager->GrantDevicePermission( @@ -190,6 +192,9 @@ base::WeakPtr 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); } @@ -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() { @@ -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 ports) { + for (auto& port : ports) + port_info_.insert({port->token, std::move(port)}); + is_initialized_ = true; } void SerialChooserContext::OnPortManagerConnectionError() { diff --git a/shell/browser/serial/serial_chooser_context.h b/shell/browser/serial/serial_chooser_context.h index 2424ed1e64f1..148bf14996ca 100644 --- a/shell/browser/serial/serial_chooser_context.h +++ b/shell/browser/serial/serial_chooser_context.h @@ -97,6 +97,7 @@ class SerialChooserContext : public KeyedService, void EnsurePortManagerConnection(); void SetUpPortManagerConnection( mojo::PendingRemote manager); + void OnGetDevices(std::vector ports); void OnPortManagerConnectionError(); void RevokeObjectPermissionInternal(const url::Origin& origin, const base::Value& object,