bluetooth_chooser_controller.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // Copyright 2015 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef COMPONENTS_PERMISSIONS_BLUETOOTH_CHOOSER_CONTROLLER_H_
  5. #define COMPONENTS_PERMISSIONS_BLUETOOTH_CHOOSER_CONTROLLER_H_
  6. #include <stddef.h>
  7. #include <string>
  8. #include <unordered_map>
  9. #include <utility>
  10. #include <vector>
  11. #include "base/memory/weak_ptr.h"
  12. #include "components/permissions/chooser_controller.h"
  13. #include "content/public/browser/bluetooth_chooser.h"
  14. namespace content {
  15. class RenderFrameHost;
  16. }
  17. namespace permissions {
  18. // BluetoothChooserController is a chooser that presents a list of
  19. // Bluetooth device names, which come from |bluetooth_chooser_desktop_|.
  20. // It can be used by WebBluetooth API to get the user's permission to
  21. // access a Bluetooth device.
  22. class BluetoothChooserController : public ChooserController {
  23. public:
  24. BluetoothChooserController(
  25. content::RenderFrameHost* owner,
  26. const content::BluetoothChooser::EventHandler& event_handler,
  27. std::u16string title);
  28. BluetoothChooserController(const BluetoothChooserController&) = delete;
  29. BluetoothChooserController& operator=(const BluetoothChooserController&) =
  30. delete;
  31. ~BluetoothChooserController() override;
  32. // ChooserController:
  33. bool ShouldShowIconBeforeText() const override;
  34. bool ShouldShowReScanButton() const override;
  35. std::u16string GetNoOptionsText() const override;
  36. std::u16string GetOkButtonLabel() const override;
  37. std::pair<std::u16string, std::u16string> GetThrobberLabelAndTooltip()
  38. const override;
  39. size_t NumOptions() const override;
  40. int GetSignalStrengthLevel(size_t index) const override;
  41. bool IsConnected(size_t index) const override;
  42. bool IsPaired(size_t index) const override;
  43. std::u16string GetOption(size_t index) const override;
  44. void RefreshOptions() override;
  45. void Select(const std::vector<size_t>& indices) override;
  46. void Cancel() override;
  47. void Close() override;
  48. // Update the state of the Bluetooth adapter.
  49. void OnAdapterPresenceChanged(
  50. content::BluetoothChooser::AdapterPresence presence);
  51. // Update the Bluetooth discovery state and let the user know whether
  52. // discovery is happening.
  53. void OnDiscoveryStateChanged(content::BluetoothChooser::DiscoveryState state);
  54. // Shows a new device in the chooser or updates its information.
  55. // The range of |signal_strength_level| is -1 to 4 inclusively.
  56. void AddOrUpdateDevice(const std::string& device_id,
  57. bool should_update_name,
  58. const std::u16string& device_name,
  59. bool is_gatt_connected,
  60. bool is_paired,
  61. int signal_strength_level);
  62. // Tells the chooser that a device is no longer available.
  63. void RemoveDevice(const std::string& device_id);
  64. // Called when |event_handler_| is no longer valid and should not be used
  65. // any more.
  66. void ResetEventHandler();
  67. // Get a weak pointer to this controller.
  68. base::WeakPtr<BluetoothChooserController> GetWeakPtr();
  69. private:
  70. struct BluetoothDeviceInfo {
  71. std::string id;
  72. int signal_strength_level;
  73. bool is_connected;
  74. bool is_paired;
  75. };
  76. // Clears |device_names_and_ids_| and |device_name_counts_|. Called when
  77. // Bluetooth adapter is turned on or off, or when re-scan happens.
  78. void ClearAllDevices();
  79. std::vector<BluetoothDeviceInfo> devices_;
  80. std::unordered_map<std::string, std::u16string> device_id_to_name_map_;
  81. // Maps from device name to number of devices with that name.
  82. std::unordered_map<std::u16string, int> device_name_counts_;
  83. content::BluetoothChooser::EventHandler event_handler_;
  84. base::WeakPtrFactory<BluetoothChooserController> weak_factory_{this};
  85. };
  86. } // namespace permissions
  87. #endif // COMPONENTS_PERMISSIONS_BLUETOOTH_CHOOSER_CONTROLLER_H_