123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- // Copyright 2014 The Chromium Authors. All rights reserved.
- // Use of this source code is governed by a BSD-style license that can be
- // found in the LICENSE file.
- #ifndef ASH_COMPONENTS_PROXIMITY_AUTH_MESSENGER_IMPL_H_
- #define ASH_COMPONENTS_PROXIMITY_AUTH_MESSENGER_IMPL_H_
- #include <memory>
- #include "ash/components/proximity_auth/messenger.h"
- #include "ash/services/secure_channel/public/cpp/client/client_channel.h"
- #include "base/containers/circular_deque.h"
- #include "base/memory/weak_ptr.h"
- #include "base/observer_list.h"
- #include "base/values.h"
- namespace proximity_auth {
- // Concrete implementation of the Messenger interface.
- class MessengerImpl : public Messenger,
- public ash::secure_channel::ClientChannel::Observer {
- public:
- // Constructs a messenger that sends and receives messages.
- //
- // Messages are relayed over the provided |channel|.
- //
- // The messenger begins observing messages as soon as it is constructed.
- explicit MessengerImpl(
- std::unique_ptr<ash::secure_channel::ClientChannel> channel);
- MessengerImpl(const MessengerImpl&) = delete;
- MessengerImpl& operator=(const MessengerImpl&) = delete;
- ~MessengerImpl() override;
- // Messenger:
- void AddObserver(MessengerObserver* observer) override;
- void RemoveObserver(MessengerObserver* observer) override;
- void DispatchUnlockEvent() override;
- void RequestDecryption(const std::string& challenge) override;
- void RequestUnlock() override;
- ash::secure_channel::ClientChannel* GetChannel() const override;
- private:
- // Internal data structure to represent a pending message that either hasn't
- // been sent yet or is waiting for a response from the remote device.
- struct PendingMessage {
- PendingMessage();
- explicit PendingMessage(const base::Value::Dict& message);
- explicit PendingMessage(const std::string& message);
- ~PendingMessage();
- // The message, serialized as JSON.
- const std::string json_message;
- // The message type. This is possible to parse from the |json_message|; it's
- // stored redundantly for convenience.
- const std::string type;
- };
- // Pops the first of the |queued_messages_| and sends it to the remote device.
- void ProcessMessageQueue();
- // Called when the message is encoded so it can be sent over the connection.
- void OnMessageEncoded(const std::string& encoded_message);
- // Handles an incoming "status_update" |message|, parsing and notifying
- // observers of the content.
- void HandleRemoteStatusUpdateMessage(const base::Value::Dict& message);
- // Handles an incoming "decrypt_response" message, parsing and notifying
- // observers of the decrypted content.
- void HandleDecryptResponseMessage(const base::Value::Dict& message);
- // Handles an incoming "unlock_response" message, notifying observers of the
- // response.
- void HandleUnlockResponseMessage(const base::Value::Dict& message);
- // ash::secure_channel::ClientChannel::Observer:
- void OnDisconnected() override;
- void OnMessageReceived(const std::string& payload) override;
- // Called when a message has been recevied from the remote device. The message
- // should be a valid JSON string.
- void HandleMessage(const std::string& message);
- // Called when a message has been sent to the remote device.
- void OnSendMessageResult(bool success);
- // Authenticated end-to-end channel used to communicate with the remote
- // device.
- std::unique_ptr<ash::secure_channel::ClientChannel> channel_;
- // The registered observers of |this_| messenger.
- base::ObserverList<MessengerObserver>::Unchecked observers_;
- // Queue of messages to send to the remote device.
- base::circular_deque<PendingMessage> queued_messages_;
- // The current message being sent or waiting on the remote device for a
- // response. Null if there is no message currently in this state.
- std::unique_ptr<PendingMessage> pending_message_;
- base::WeakPtrFactory<MessengerImpl> weak_ptr_factory_{this};
- };
- } // namespace proximity_auth
- #endif // ASH_COMPONENTS_PROXIMITY_AUTH_MESSENGER_IMPL_H_
|