1 // Copyright 2020 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 CHROME_BROWSER_CHROMEOS_SECURE_CHANNEL_NEARBY_CONNECTION_BROKER_H_
6 #define CHROME_BROWSER_CHROMEOS_SECURE_CHANNEL_NEARBY_CONNECTION_BROKER_H_
7 
8 #include <string>
9 #include <vector>
10 
11 #include "base/callback.h"
12 #include "chromeos/services/secure_channel/public/mojom/nearby_connector.mojom.h"
13 #include "mojo/public/cpp/bindings/pending_receiver.h"
14 #include "mojo/public/cpp/bindings/pending_remote.h"
15 #include "mojo/public/cpp/bindings/receiver.h"
16 #include "mojo/public/cpp/bindings/remote.h"
17 
18 namespace chromeos {
19 namespace secure_channel {
20 
21 // Attempts to create a Nearby Connection to a remote device and exchange
22 // messages on behalf of the SecureChannel service. Implements the
23 // mojom::NearbyMessageSender interface so that SecureChannel can send messages
24 // to Nearby Connections, and uses the mojom::NearbyMessageReceiver interface to
25 // relay messages received from Nearby Connections back to SecureChannel.
26 //
27 // An instance of this class is only meant to be used for one connection
28 // request to a single device. To make a new request, create a new object.
29 class NearbyConnectionBroker : public mojom::NearbyMessageSender {
30  public:
31   ~NearbyConnectionBroker() override;
32 
33  protected:
34   NearbyConnectionBroker(
35       const std::vector<uint8_t>& bluetooth_public_address,
36       mojo::PendingReceiver<mojom::NearbyMessageSender> message_sender_receiver,
37       mojo::PendingRemote<mojom::NearbyMessageReceiver> message_receiver_remote,
38       base::OnceClosure on_connected_callback,
39       base::OnceClosure on_disconnected_callback);
40 
bluetooth_public_address()41   const std::vector<uint8_t>& bluetooth_public_address() const {
42     return bluetooth_public_address_;
43   }
44 
45   // Can be overridden by derived classes to handle MessageSender and
46   // MessageReceiver Mojo pipes being disconnected.
OnMojoDisconnection()47   virtual void OnMojoDisconnection() {}
48 
49   void InvokeDisconnectedCallback();
50   void NotifyConnected();
51   void NotifyMessageReceived(const std::string& received_message);
52 
53  private:
54   std::vector<uint8_t> bluetooth_public_address_;
55   mojo::Receiver<mojom::NearbyMessageSender> message_sender_receiver_;
56   mojo::Remote<mojom::NearbyMessageReceiver> message_receiver_remote_;
57   base::OnceClosure on_connected_callback_;
58   base::OnceClosure on_disconnected_callback_;
59 };
60 
61 }  // namespace secure_channel
62 }  // namespace chromeos
63 
64 #endif  // CHROME_BROWSER_CHROMEOS_SECURE_CHANNEL_NEARBY_CONNECTION_BROKER_H_
65