File: messenger_impl.h

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (104 lines) | stat: -rw-r--r-- 3,843 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROMEOS_ASH_COMPONENTS_PROXIMITY_AUTH_MESSENGER_IMPL_H_
#define CHROMEOS_ASH_COMPONENTS_PROXIMITY_AUTH_MESSENGER_IMPL_H_

#include <memory>

#include "base/containers/circular_deque.h"
#include "base/memory/weak_ptr.h"
#include "base/observer_list.h"
#include "base/values.h"
#include "chromeos/ash/components/proximity_auth/messenger.h"
#include "chromeos/ash/services/secure_channel/public/cpp/client/client_channel.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 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 "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  // CHROMEOS_ASH_COMPONENTS_PROXIMITY_AUTH_MESSENGER_IMPL_H_