battery_update_message_handler.h 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // Copyright 2021 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 ASH_QUICK_PAIR_KEYED_SERVICE_BATTERY_UPDATE_MESSAGE_HANDLER_H_
  5. #define ASH_QUICK_PAIR_KEYED_SERVICE_BATTERY_UPDATE_MESSAGE_HANDLER_H_
  6. #include <string>
  7. #include "ash/quick_pair/message_stream/message_stream.h"
  8. #include "ash/quick_pair/message_stream/message_stream_lookup.h"
  9. #include "base/containers/flat_map.h"
  10. #include "base/memory/weak_ptr.h"
  11. #include "base/observer_list.h"
  12. #include "base/scoped_observation.h"
  13. namespace device {
  14. class BluetoothAdapter;
  15. } // namespace device
  16. namespace ash {
  17. namespace quick_pair {
  18. // Observes MessageStreams instances for devices when they are created, and
  19. // on battery update messages, adds the battery information to the bluetooth
  20. // device.
  21. class BatteryUpdateMessageHandler : public MessageStreamLookup::Observer,
  22. public MessageStream::Observer {
  23. public:
  24. explicit BatteryUpdateMessageHandler(
  25. MessageStreamLookup* message_stream_lookup);
  26. BatteryUpdateMessageHandler(const BatteryUpdateMessageHandler&) = delete;
  27. BatteryUpdateMessageHandler& operator=(const BatteryUpdateMessageHandler&) =
  28. delete;
  29. ~BatteryUpdateMessageHandler() override;
  30. private:
  31. // MessageStreamLookup::Observer
  32. void OnMessageStreamConnected(const std::string& device_address,
  33. MessageStream* message_stream) override;
  34. // MessageStream::Observer
  35. void OnBatteryUpdateMessage(
  36. const std::string& device_address,
  37. const mojom::BatteryUpdatePtr& battery_update) override;
  38. void OnDisconnected(const std::string& device_address) override;
  39. void OnMessageStreamDestroyed(const std::string& device_address) override;
  40. // Internal method called by BluetoothAdapterFactory to provide the adapter
  41. // object.
  42. void OnGetAdapter(scoped_refptr<device::BluetoothAdapter> adapter);
  43. // Parses MessageStream messages for battery update, and notifies observers
  44. // if it exists.
  45. void GetBatteryUpdateFromMessageStream(const std::string& device_address,
  46. MessageStream* message_stream);
  47. // Sets the battery information on the bluetooth device at |device_address|.
  48. void SetBatteryInfo(const std::string& device_address,
  49. const mojom::BatteryUpdatePtr& battery_update);
  50. // Cleans up memory associated with a MessageStream corresponding to
  51. // |device_address| if it exists.
  52. void CleanUpMessageStream(const std::string& device_address);
  53. // Map of the classic pairing address to their corresponding MessageStreams.
  54. base::flat_map<std::string, MessageStream*> message_streams_;
  55. scoped_refptr<device::BluetoothAdapter> adapter_;
  56. base::ScopedObservation<MessageStreamLookup, MessageStreamLookup::Observer>
  57. message_stream_lookup_observation_{this};
  58. base::WeakPtrFactory<BatteryUpdateMessageHandler> weak_ptr_factory_{this};
  59. };
  60. } // namespace quick_pair
  61. } // namespace ash
  62. #endif // ASH_QUICK_PAIR_KEYED_SERVICE_BATTERY_UPDATE_MESSAGE_HANDLER_H_