Repository: electron/electron
File: shell/browser/lib/bluetooth_chooser.h (C/C++)

Code before:
    std::u16string device_name;
  };

  explicit BluetoothChooser(api::WebContents* contents,
                            const EventHandler& handler);
  ~BluetoothChooser() override;

  // disable copy
  BluetoothChooser(const BluetoothChooser&) = delete;
  BluetoothChooser& operator=(const BluetoothChooser&) = delete;

  // content::BluetoothChooser:
  void SetAdapterPresence(AdapterPresence presence) override;
  void ShowDiscoveryState(DiscoveryState state) override;
  void AddOrUpdateDevice(const std::string& device_id,
                         bool should_update_name,
                         const std::u16string& device_name,
                         bool is_gatt_connected,
                         bool is_paired,
                         int signal_strength_level) override;

  void OnDeviceChosen(const std::string& device_id);
  std::vector<DeviceInfo> GetDeviceList();

 private:
  std::unordered_map<std::string, std::u16string> device_id_to_name_map_;
  raw_ptr<api::WebContents> api_web_contents_;
  EventHandler event_handler_;
  bool refreshing_ = false;
  bool rescan_ = false;

  base::WeakPtrFactory<BluetoothChooser> weak_ptr_factory_{this};
};

}  // namespace electron

#endif  // ELECTRON_SHELL_BROWSER_LIB_BLUETOOTH_CHOOSER_H_


Review comment (performance):
1. I like the new name, it's much easier to read :smile_cat: 

2. This field is a good candidate for `absl::flat_hash_map`.

    Xref: https://chromium.googlesource.com/chromium/src/+/main/base/containers/README.md#map-and-set-selection-usage-advice
    > Note that this advice never suggests the use of std::unordered_map and std::unordered_set. These containers provides similar features to the Abseil flat hash containers but with worse performance. They should only be used if absolutely required for compatibility with third-party code.

Code after:
    std::u16string device_name;
  };

  explicit BluetoothChooser(api::WebContents* contents,
                            const EventHandler& handler);
  ~BluetoothChooser() override;

  // disable copy
  BluetoothChooser(const BluetoothChooser&) = delete;
  BluetoothChooser& operator=(const BluetoothChooser&) = delete;

  // content::BluetoothChooser:
  void SetAdapterPresence(AdapterPresence presence) override;
  void ShowDiscoveryState(DiscoveryState state) override;
  void AddOrUpdateDevice(const std::string& device_id,
                         bool should_update_name,
                         const std::u16string& device_name,
                         bool is_gatt_connected,
                         bool is_paired,
                         int signal_strength_level) override;

  void OnDeviceChosen(const std::string& device_id);
  std::vector<DeviceInfo> GetDeviceList();

 private:
  absl::flat_hash_map<std::string, std::u16string> device_id_to_name_map_;
  raw_ptr<api::WebContents> api_web_contents_;
  EventHandler event_handler_;
  bool refreshing_ = false;
  bool rescan_ = false;

  base::WeakPtrFactory<BluetoothChooser> weak_ptr_factory_{this};
};

}  // namespace electron

#endif  // ELECTRON_SHELL_BROWSER_LIB_BLUETOOTH_CHOOSER_H_