1 // Copyright 2015 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_COMPONENTS_PROXIMITY_AUTH_REMOTE_DEVICE_LIFE_CYCLE_H_
6 #define CHROMEOS_COMPONENTS_PROXIMITY_AUTH_REMOTE_DEVICE_LIFE_CYCLE_H_
7 
8 #include <ostream>
9 
10 #include "base/macros.h"
11 #include "chromeos/components/multidevice/remote_device_ref.h"
12 
13 namespace chromeos {
14 namespace secure_channel {
15 class ClientChannel;
16 }  // namespace secure_channel
17 }  // namespace chromeos
18 
19 namespace proximity_auth {
20 
21 class Messenger;
22 
23 // Controls the life cycle of connecting and authenticating to a remote device.
24 // After the life cycle is started, it can be in the following states:
25 //   FINDING_CONNECTION:
26 //       Continuiously attempts to create a connection to the remote device.
27 //       After connecting, transitions to the AUTHENTICATING state.
28 //   AUTHENTICATING:
29 //       Verifies that the connected device has the correct credentials. On
30 //       success, transitions to SECURE_CHANNEL_ESTABLISHED; otherwise,
31 //       transitions to AUTHENTICATION_FAILED.
32 //   SECURE_CHANNEL_ESTABLISHED:
33 //       Can send and receive messages securely from the remote device. Upon
34 //       disconnection, transitions to FINDING_CONNECTION.
35 //   AUTHENTICATION_FAILED:
36 //       Recovery state after authentication fails. After a brief wait,
37 //       transition to FINDING_CONNECTION.
38 // To stop the life cycle and clean up the connection, simply destroying this
39 // object.
40 class RemoteDeviceLifeCycle {
41  public:
42   // The possible states in the life cycle.
43   enum class State {
44     STOPPED,
45     FINDING_CONNECTION,
46     AUTHENTICATING,
47     SECURE_CHANNEL_ESTABLISHED,
48     AUTHENTICATION_FAILED,
49   };
50 
51   // Interface for observing changes to the life cycle.
52   class Observer {
53    public:
~Observer()54     virtual ~Observer() {}
55 
56     // Called when the state in the life cycle changes.
57     virtual void OnLifeCycleStateChanged(State old_state, State new_state) = 0;
58   };
59 
~RemoteDeviceLifeCycle()60   virtual ~RemoteDeviceLifeCycle() {}
61 
62   // Starts the life cycle.
63   virtual void Start() = 0;
64 
65   // Returns the RemoteDeviceRef instance that this life cycle manages.
66   virtual chromeos::multidevice::RemoteDeviceRef GetRemoteDevice() const = 0;
67 
68   // Returns the active channel to the remote device, or null if the device is
69   // not yet connected.
70   virtual chromeos::secure_channel::ClientChannel* GetChannel() const = 0;
71 
72   // Returns the current state of in the life cycle.
73   virtual State GetState() const = 0;
74 
75   // Returns the client for sending and receiving messages. This function will
76   // only return an instance if the state is SECURE_CHANNEL_ESTABLISHED;
77   // otherwise, it will return nullptr.
78   virtual Messenger* GetMessenger() = 0;
79 
80   // Adds an observer.
81   virtual void AddObserver(Observer* observer) = 0;
82 
83   // Removes an observer.
84   virtual void RemoveObserver(Observer* observer) = 0;
85 };
86 
87 std::ostream& operator<<(std::ostream& stream,
88                          const RemoteDeviceLifeCycle::State& state);
89 
90 }  // namespace proximity_auth
91 
92 #endif  // CHROMEOS_COMPONENTS_PROXIMITY_AUTH_REMOTE_DEVICE_LIFE_CYCLE_H_
93