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 
5 #ifndef CHROMEOS_SERVICES_SECURE_CHANNEL_AUTHENTICATED_CHANNEL_IMPL_H_
6 #define CHROMEOS_SERVICES_SECURE_CHANNEL_AUTHENTICATED_CHANNEL_IMPL_H_
7 
8 #include <string>
9 #include <vector>
10 
11 #include "base/macros.h"
12 #include "chromeos/services/secure_channel/authenticated_channel.h"
13 #include "chromeos/services/secure_channel/secure_channel.h"
14 
15 namespace chromeos {
16 
17 namespace secure_channel {
18 
19 class SecureChannel;
20 
21 // Concrete AuthenticatedChannel implementation, whose send/receive mechanisms
22 // are implemented via SecureChannel.
23 class AuthenticatedChannelImpl : public AuthenticatedChannel,
24                                  public SecureChannel::Observer {
25  public:
26   class Factory {
27    public:
28     static std::unique_ptr<AuthenticatedChannel> Create(
29         const std::vector<mojom::ConnectionCreationDetail>&
30             connection_creation_details,
31         std::unique_ptr<SecureChannel> secure_channel);
32     static void SetFactoryForTesting(Factory* test_factory);
33 
34    protected:
35     virtual ~Factory();
36     virtual std::unique_ptr<AuthenticatedChannel> CreateInstance(
37         const std::vector<mojom::ConnectionCreationDetail>&
38             connection_creation_details,
39         std::unique_ptr<SecureChannel> secure_channel) = 0;
40 
41    private:
42     static Factory* test_factory_;
43   };
44 
45   ~AuthenticatedChannelImpl() override;
46 
47  private:
48   AuthenticatedChannelImpl(const std::vector<mojom::ConnectionCreationDetail>&
49                                connection_creation_details,
50                            std::unique_ptr<SecureChannel> secure_channel);
51 
52   // AuthenticatedChannel:
53   void GetConnectionMetadata(
54       base::OnceCallback<void(mojom::ConnectionMetadataPtr)> callback) override;
55   void PerformSendMessage(const std::string& feature,
56                           const std::string& payload,
57                           base::OnceClosure on_sent_callback) final;
58   void PerformDisconnection() override;
59 
60   // SecureChannel::Observer:
61   void OnSecureChannelStatusChanged(
62       SecureChannel* secure_channel,
63       const SecureChannel::Status& old_status,
64       const SecureChannel::Status& new_status) override;
65   void OnMessageReceived(SecureChannel* secure_channel,
66                          const std::string& feature,
67                          const std::string& payload) override;
68   void OnMessageSent(SecureChannel* secure_channel,
69                      int sequence_number) override;
70 
71   void OnRssiFetched(
72       base::OnceCallback<void(mojom::ConnectionMetadataPtr)> callback,
73       base::Optional<int32_t> current_rssi);
74 
75   const std::vector<mojom::ConnectionCreationDetail>
76       connection_creation_details_;
77   std::unique_ptr<SecureChannel> secure_channel_;
78   std::unordered_map<int, base::OnceClosure> sequence_number_to_callback_map_;
79 
80   DISALLOW_COPY_AND_ASSIGN(AuthenticatedChannelImpl);
81 };
82 
83 }  // namespace secure_channel
84 
85 }  // namespace chromeos
86 
87 #endif  // CHROMEOS_SERVICES_SECURE_CHANNEL_AUTHENTICATED_CHANNEL_IMPL_H_
88