messenger_impl.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // Copyright 2014 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_COMPONENTS_PROXIMITY_AUTH_MESSENGER_IMPL_H_
  5. #define ASH_COMPONENTS_PROXIMITY_AUTH_MESSENGER_IMPL_H_
  6. #include <memory>
  7. #include "ash/components/proximity_auth/messenger.h"
  8. #include "ash/services/secure_channel/public/cpp/client/client_channel.h"
  9. #include "base/containers/circular_deque.h"
  10. #include "base/memory/weak_ptr.h"
  11. #include "base/observer_list.h"
  12. #include "base/values.h"
  13. namespace proximity_auth {
  14. // Concrete implementation of the Messenger interface.
  15. class MessengerImpl : public Messenger,
  16. public ash::secure_channel::ClientChannel::Observer {
  17. public:
  18. // Constructs a messenger that sends and receives messages.
  19. //
  20. // Messages are relayed over the provided |channel|.
  21. //
  22. // The messenger begins observing messages as soon as it is constructed.
  23. explicit MessengerImpl(
  24. std::unique_ptr<ash::secure_channel::ClientChannel> channel);
  25. MessengerImpl(const MessengerImpl&) = delete;
  26. MessengerImpl& operator=(const MessengerImpl&) = delete;
  27. ~MessengerImpl() override;
  28. // Messenger:
  29. void AddObserver(MessengerObserver* observer) override;
  30. void RemoveObserver(MessengerObserver* observer) override;
  31. void DispatchUnlockEvent() override;
  32. void RequestDecryption(const std::string& challenge) override;
  33. void RequestUnlock() override;
  34. ash::secure_channel::ClientChannel* GetChannel() const override;
  35. private:
  36. // Internal data structure to represent a pending message that either hasn't
  37. // been sent yet or is waiting for a response from the remote device.
  38. struct PendingMessage {
  39. PendingMessage();
  40. explicit PendingMessage(const base::Value::Dict& message);
  41. explicit PendingMessage(const std::string& message);
  42. ~PendingMessage();
  43. // The message, serialized as JSON.
  44. const std::string json_message;
  45. // The message type. This is possible to parse from the |json_message|; it's
  46. // stored redundantly for convenience.
  47. const std::string type;
  48. };
  49. // Pops the first of the |queued_messages_| and sends it to the remote device.
  50. void ProcessMessageQueue();
  51. // Called when the message is encoded so it can be sent over the connection.
  52. void OnMessageEncoded(const std::string& encoded_message);
  53. // Handles an incoming "status_update" |message|, parsing and notifying
  54. // observers of the content.
  55. void HandleRemoteStatusUpdateMessage(const base::Value::Dict& message);
  56. // Handles an incoming "decrypt_response" message, parsing and notifying
  57. // observers of the decrypted content.
  58. void HandleDecryptResponseMessage(const base::Value::Dict& message);
  59. // Handles an incoming "unlock_response" message, notifying observers of the
  60. // response.
  61. void HandleUnlockResponseMessage(const base::Value::Dict& message);
  62. // ash::secure_channel::ClientChannel::Observer:
  63. void OnDisconnected() override;
  64. void OnMessageReceived(const std::string& payload) override;
  65. // Called when a message has been recevied from the remote device. The message
  66. // should be a valid JSON string.
  67. void HandleMessage(const std::string& message);
  68. // Called when a message has been sent to the remote device.
  69. void OnSendMessageResult(bool success);
  70. // Authenticated end-to-end channel used to communicate with the remote
  71. // device.
  72. std::unique_ptr<ash::secure_channel::ClientChannel> channel_;
  73. // The registered observers of |this_| messenger.
  74. base::ObserverList<MessengerObserver>::Unchecked observers_;
  75. // Queue of messages to send to the remote device.
  76. base::circular_deque<PendingMessage> queued_messages_;
  77. // The current message being sent or waiting on the remote device for a
  78. // response. Null if there is no message currently in this state.
  79. std::unique_ptr<PendingMessage> pending_message_;
  80. base::WeakPtrFactory<MessengerImpl> weak_ptr_factory_{this};
  81. };
  82. } // namespace proximity_auth
  83. #endif // ASH_COMPONENTS_PROXIMITY_AUTH_MESSENGER_IMPL_H_