fix: properly fire serial-port-added and serial-port-removed events (#34958)

Based on 2309652: [webhid] Notify chooser context observers on shutdown | 2309652
This commit is contained in:
John Kleinschmidt 2022-07-25 10:50:19 -04:00 committed by GitHub
parent aeba6ca973
commit 648c9934c0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 71 additions and 21 deletions

View file

@ -11,13 +11,15 @@
#include "base/memory/weak_ptr.h"
#include "content/public/browser/serial_delegate.h"
#include "shell/browser/serial/serial_chooser_context.h"
#include "shell/browser/serial/serial_chooser_controller.h"
namespace electron {
class SerialChooserController;
class ElectronSerialDelegate : public content::SerialDelegate {
class ElectronSerialDelegate : public content::SerialDelegate,
public SerialChooserContext::PortObserver {
public:
ElectronSerialDelegate();
~ElectronSerialDelegate() override;
@ -48,6 +50,13 @@ class ElectronSerialDelegate : public content::SerialDelegate {
void DeleteControllerForFrame(content::RenderFrameHost* render_frame_host);
// SerialChooserContext::PortObserver:
void OnPortAdded(const device::mojom::SerialPortInfo& port) override;
void OnPortRemoved(const device::mojom::SerialPortInfo& port) override;
void OnPortManagerConnectionError() override;
void OnPermissionRevoked(const url::Origin& origin) override {}
void OnSerialChooserContextShutdown() override;
private:
SerialChooserController* ControllerForFrame(
content::RenderFrameHost* render_frame_host);
@ -56,6 +65,13 @@ class ElectronSerialDelegate : public content::SerialDelegate {
std::vector<blink::mojom::SerialPortFilterPtr> filters,
content::SerialChooser::Callback callback);
base::ScopedObservation<SerialChooserContext,
SerialChooserContext::PortObserver,
&SerialChooserContext::AddPortObserver,
&SerialChooserContext::RemovePortObserver>
port_observation_{this};
base::ObserverList<content::SerialDelegate::Observer> observer_list_;
std::unordered_map<content::RenderFrameHost*,
std::unique_ptr<SerialChooserController>>
controller_map_;