bluetooth_scanning_prompt_controller.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // Copyright 2019 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_SCANNING_PROMPT_CONTROLLER_H_
  5. #define COMPONENTS_PERMISSIONS_BLUETOOTH_SCANNING_PROMPT_CONTROLLER_H_
  6. #include <stddef.h>
  7. #include <string>
  8. #include <unordered_map>
  9. #include <utility>
  10. #include <vector>
  11. #include "base/callback.h"
  12. #include "base/memory/weak_ptr.h"
  13. #include "components/permissions/chooser_controller.h"
  14. #include "content/public/browser/bluetooth_scanning_prompt.h"
  15. namespace content {
  16. class RenderFrameHost;
  17. }
  18. namespace permissions {
  19. // BluetoothScanningPromptController is a prompt that presents a list of
  20. // Bluetooth device names. It can be used by Bluetooth Scanning API to
  21. // show example nearby Bluetooth devices to user.
  22. class BluetoothScanningPromptController : public ChooserController {
  23. public:
  24. BluetoothScanningPromptController(
  25. content::RenderFrameHost* owner,
  26. const content::BluetoothScanningPrompt::EventHandler& event_handler,
  27. std::u16string title);
  28. BluetoothScanningPromptController(const BluetoothScanningPromptController&) =
  29. delete;
  30. BluetoothScanningPromptController& operator=(
  31. const BluetoothScanningPromptController&) = delete;
  32. ~BluetoothScanningPromptController() override;
  33. // ChooserController:
  34. bool ShouldShowHelpButton() const override;
  35. std::u16string GetNoOptionsText() const override;
  36. std::u16string GetOkButtonLabel() const override;
  37. std::u16string GetCancelButtonLabel() const override;
  38. std::pair<std::u16string, std::u16string> GetThrobberLabelAndTooltip()
  39. const override;
  40. bool BothButtonsAlwaysEnabled() const override;
  41. bool TableViewAlwaysDisabled() const override;
  42. size_t NumOptions() const override;
  43. std::u16string GetOption(size_t index) const override;
  44. void Select(const std::vector<size_t>& indices) override;
  45. void Cancel() override;
  46. void Close() override;
  47. void OpenHelpCenterUrl() const override;
  48. // Shows a new device in the permission prompt or updates its information.
  49. void AddOrUpdateDevice(const std::string& device_id,
  50. bool should_update_name,
  51. const std::u16string& device_name);
  52. // Called when |event_handler_| is no longer valid and should not be used
  53. // any more.
  54. void ResetEventHandler();
  55. // Get a weak pointer to this controller.
  56. base::WeakPtr<BluetoothScanningPromptController> GetWeakPtr();
  57. private:
  58. std::vector<std::string> device_ids_;
  59. std::unordered_map<std::string, std::u16string> device_id_to_name_map_;
  60. // Maps from device name to number of devices with that name.
  61. std::unordered_map<std::u16string, int> device_name_counts_;
  62. content::BluetoothScanningPrompt::EventHandler event_handler_;
  63. base::WeakPtrFactory<BluetoothScanningPromptController> weak_factory_{this};
  64. };
  65. } // namespace permissions
  66. #endif // COMPONENTS_PERMISSIONS_BLUETOOTH_SCANNING_PROMPT_CONTROLLER_H_