remote_device_life_cycle_impl.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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_IMPL_H_
  5. #define ASH_COMPONENTS_PROXIMITY_AUTH_REMOTE_DEVICE_LIFE_CYCLE_IMPL_H_
  6. #include <memory>
  7. #include "ash/components/multidevice/remote_device_ref.h"
  8. #include "ash/components/proximity_auth/messenger_observer.h"
  9. #include "ash/components/proximity_auth/remote_device_life_cycle.h"
  10. #include "ash/services/secure_channel/public/cpp/client/connection_attempt.h"
  11. #include "ash/services/secure_channel/public/cpp/client/secure_channel_client.h"
  12. #include "ash/services/secure_channel/public/mojom/secure_channel.mojom.h"
  13. #include "base/memory/weak_ptr.h"
  14. #include "base/observer_list.h"
  15. #include "base/timer/timer.h"
  16. namespace ash {
  17. namespace secure_channel {
  18. class ClientChannel;
  19. class SecureChannelClient;
  20. } // namespace secure_channel
  21. } // namespace ash
  22. namespace proximity_auth {
  23. class Messenger;
  24. // Implementation of RemoteDeviceLifeCycle.
  25. class RemoteDeviceLifeCycleImpl
  26. : public RemoteDeviceLifeCycle,
  27. public ash::secure_channel::ConnectionAttempt::Delegate,
  28. public MessengerObserver {
  29. public:
  30. // Creates the life cycle for controlling the given |remote_device|.
  31. // |proximity_auth_client| is not owned.
  32. RemoteDeviceLifeCycleImpl(
  33. ash::multidevice::RemoteDeviceRef remote_device,
  34. absl::optional<ash::multidevice::RemoteDeviceRef> local_device,
  35. ash::secure_channel::SecureChannelClient* secure_channel_client);
  36. RemoteDeviceLifeCycleImpl(const RemoteDeviceLifeCycleImpl&) = delete;
  37. RemoteDeviceLifeCycleImpl& operator=(const RemoteDeviceLifeCycleImpl&) =
  38. delete;
  39. ~RemoteDeviceLifeCycleImpl() override;
  40. // RemoteDeviceLifeCycle:
  41. void Start() override;
  42. ash::multidevice::RemoteDeviceRef GetRemoteDevice() const override;
  43. ash::secure_channel::ClientChannel* GetChannel() const override;
  44. RemoteDeviceLifeCycle::State GetState() const override;
  45. Messenger* GetMessenger() override;
  46. void AddObserver(Observer* observer) override;
  47. void RemoveObserver(Observer* observer) override;
  48. private:
  49. // Transitions to |new_state|, and notifies observers.
  50. void TransitionToState(RemoteDeviceLifeCycle::State new_state);
  51. // Transtitions to FINDING_CONNECTION state. Creates and starts
  52. // |connection_finder_|.
  53. void FindConnection();
  54. // Creates the messenger which parses status updates.
  55. void CreateMessenger();
  56. // ash::secure_channel::ConnectionAttempt::Delegate:
  57. void OnConnectionAttemptFailure(
  58. ash::secure_channel::mojom::ConnectionAttemptFailureReason reason)
  59. override;
  60. void OnConnection(
  61. std::unique_ptr<ash::secure_channel::ClientChannel> channel) override;
  62. // MessengerObserver:
  63. void OnDisconnected() override;
  64. // The remote device being controlled.
  65. const ash::multidevice::RemoteDeviceRef remote_device_;
  66. // Represents this device (i.e. this Chromebook) for a particular profile.
  67. absl::optional<ash::multidevice::RemoteDeviceRef> local_device_;
  68. // The entrypoint to the SecureChannel API.
  69. ash::secure_channel::SecureChannelClient* secure_channel_client_;
  70. // The current state in the life cycle.
  71. RemoteDeviceLifeCycle::State state_;
  72. // Observers added to the life cycle.
  73. base::ObserverList<Observer>::Unchecked observers_{
  74. base::ObserverListPolicy::EXISTING_ONLY};
  75. // The messenger for sending and receiving messages in the
  76. // SECURE_CHANNEL_ESTABLISHED state.
  77. std::unique_ptr<Messenger> messenger_;
  78. std::unique_ptr<ash::secure_channel::ConnectionAttempt> connection_attempt_;
  79. // Ownership is eventually passed to |messenger_|.
  80. std::unique_ptr<ash::secure_channel::ClientChannel> channel_;
  81. // After authentication fails, this timer waits for a period of time before
  82. // retrying the connection.
  83. base::OneShotTimer authentication_recovery_timer_;
  84. base::WeakPtrFactory<RemoteDeviceLifeCycleImpl> weak_ptr_factory_{this};
  85. };
  86. } // namespace proximity_auth
  87. #endif // ASH_COMPONENTS_PROXIMITY_AUTH_REMOTE_DEVICE_LIFE_CYCLE_IMPL_H_