remote_device_life_cycle.h 3.0 KB

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