cast_bluetooth_chooser.h 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // Copyright 2018 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 CHROMECAST_BROWSER_BLUETOOTH_CAST_BLUETOOTH_CHOOSER_H_
  5. #define CHROMECAST_BROWSER_BLUETOOTH_CAST_BLUETOOTH_CHOOSER_H_
  6. #include <string>
  7. #include <unordered_set>
  8. #include "chromecast/browser/bluetooth/public/mojom/web_bluetooth.mojom.h"
  9. #include "content/public/browser/bluetooth_chooser.h"
  10. #include "mojo/public/cpp/bindings/pending_remote.h"
  11. #include "mojo/public/cpp/bindings/receiver.h"
  12. namespace chromecast {
  13. // This class requests access from a remote BluetoothDeviceAccessProvider
  14. // implemented by the Activity's host. The host will update this client with
  15. // whitelisted devices via GrantAccess(). Meanwhile, the WebBluetooth stack will
  16. // add devices that match the application's filter via AddOrUpdateDevice(). The
  17. // first device which matches both will be selected.
  18. class CastBluetoothChooser : public content::BluetoothChooser,
  19. public mojom::BluetoothDeviceAccessProviderClient {
  20. public:
  21. // |event_handler| is called when an approved device is discovered, or when
  22. // the |provider| destroys the connection to this client. |this| may destroy
  23. // |provider| immediately after requesting access.
  24. CastBluetoothChooser(content::BluetoothChooser::EventHandler event_handler,
  25. mojo::PendingRemote<mojom::BluetoothDeviceAccessProvider>
  26. pending_provider);
  27. CastBluetoothChooser(const CastBluetoothChooser&) = delete;
  28. CastBluetoothChooser& operator=(const CastBluetoothChooser&) = delete;
  29. ~CastBluetoothChooser() override;
  30. private:
  31. // mojom::BluetoothDeviceAccessProviderClient implementation:
  32. void GrantAccess(const std::string& address) override;
  33. void GrantAccessToAllDevices() override;
  34. // content::BluetoothChooser implementation:
  35. void AddOrUpdateDevice(const std::string& device_id,
  36. bool should_update_name,
  37. const std::u16string& device_name,
  38. bool is_gatt_connected,
  39. bool is_paired,
  40. int signal_strength_level) override;
  41. // Runs the event_handler and resets the client receiver. After this is
  42. // called, this class should not be used.
  43. void RunEventHandlerAndResetReceiver(content::BluetoothChooserEvent event,
  44. std::string address);
  45. // Called when the remote connection held by |receiver_| is torn down.
  46. void OnClientConnectionError();
  47. content::BluetoothChooser::EventHandler event_handler_;
  48. mojo::Receiver<mojom::BluetoothDeviceAccessProviderClient> receiver_{this};
  49. std::unordered_set<std::string> available_devices_;
  50. std::unordered_set<std::string> approved_devices_;
  51. bool all_devices_approved_ = false;
  52. };
  53. } // namespace chromecast
  54. #endif // CHROMECAST_BROWSER_BLUETOOTH_CAST_BLUETOOTH_CHOOSER_H_