OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef COMPONENTS_PROXIMITY_AUTH_REMOTE_DEVICE_LIFE_CYCLE_IMPL_H | 5 #ifndef COMPONENTS_PROXIMITY_AUTH_REMOTE_DEVICE_LIFE_CYCLE_IMPL_H |
6 #define COMPONENTS_PROXIMITY_AUTH_REMOTE_DEVICE_LIFE_CYCLE_IMPL_H | 6 #define COMPONENTS_PROXIMITY_AUTH_REMOTE_DEVICE_LIFE_CYCLE_IMPL_H |
7 | 7 |
8 #include <memory> | 8 #include <memory> |
9 | 9 |
10 #include "base/macros.h" | 10 #include "base/macros.h" |
11 #include "base/memory/weak_ptr.h" | 11 #include "base/memory/weak_ptr.h" |
12 #include "base/observer_list.h" | 12 #include "base/observer_list.h" |
13 #include "base/timer/timer.h" | 13 #include "base/timer/timer.h" |
| 14 #include "components/cryptauth/bluetooth_throttler.h" |
| 15 #include "components/cryptauth/connection.h" |
| 16 #include "components/cryptauth/connection_finder.h" |
14 #include "components/cryptauth/remote_device.h" | 17 #include "components/cryptauth/remote_device.h" |
15 #include "components/proximity_auth/authenticator.h" | 18 #include "components/proximity_auth/authenticator.h" |
16 #include "components/proximity_auth/messenger_observer.h" | 19 #include "components/proximity_auth/messenger_observer.h" |
17 #include "components/proximity_auth/remote_device_life_cycle.h" | 20 #include "components/proximity_auth/remote_device_life_cycle.h" |
18 | 21 |
19 namespace proximity_auth { | 22 namespace proximity_auth { |
20 | 23 |
21 class BluetoothThrottler; | |
22 class Messenger; | 24 class Messenger; |
23 class Connection; | |
24 class ConnectionFinder; | |
25 class ProximityAuthClient; | 25 class ProximityAuthClient; |
26 class SecureContext; | 26 class SecureContext; |
27 | 27 |
28 // Implementation of RemoteDeviceLifeCycle. | 28 // Implementation of RemoteDeviceLifeCycle. |
29 class RemoteDeviceLifeCycleImpl : public RemoteDeviceLifeCycle, | 29 class RemoteDeviceLifeCycleImpl : public RemoteDeviceLifeCycle, |
30 public MessengerObserver { | 30 public MessengerObserver { |
31 public: | 31 public: |
32 // Creates the life cycle for controlling the given |remote_device|. | 32 // Creates the life cycle for controlling the given |remote_device|. |
33 // |proximity_auth_client| is not owned. | 33 // |proximity_auth_client| is not owned. |
34 RemoteDeviceLifeCycleImpl(const cryptauth::RemoteDevice& remote_device, | 34 RemoteDeviceLifeCycleImpl(const cryptauth::RemoteDevice& remote_device, |
35 ProximityAuthClient* proximity_auth_client); | 35 ProximityAuthClient* proximity_auth_client); |
36 ~RemoteDeviceLifeCycleImpl() override; | 36 ~RemoteDeviceLifeCycleImpl() override; |
37 | 37 |
38 // RemoteDeviceLifeCycle: | 38 // RemoteDeviceLifeCycle: |
39 void Start() override; | 39 void Start() override; |
40 cryptauth::RemoteDevice GetRemoteDevice() const override; | 40 cryptauth::RemoteDevice GetRemoteDevice() const override; |
41 RemoteDeviceLifeCycle::State GetState() const override; | 41 RemoteDeviceLifeCycle::State GetState() const override; |
42 Messenger* GetMessenger() override; | 42 Messenger* GetMessenger() override; |
43 void AddObserver(Observer* observer) override; | 43 void AddObserver(Observer* observer) override; |
44 void RemoveObserver(Observer* observer) override; | 44 void RemoveObserver(Observer* observer) override; |
45 | 45 |
46 protected: | 46 protected: |
47 // Creates and returns a ConnectionFinder instance for |remote_device_|. | 47 // Creates and returns a cryptauth::ConnectionFinder instance for |
| 48 // |remote_device_|. |
48 // Exposed for testing. | 49 // Exposed for testing. |
49 virtual std::unique_ptr<ConnectionFinder> CreateConnectionFinder(); | 50 virtual std::unique_ptr<cryptauth::ConnectionFinder> CreateConnectionFinder(); |
50 | 51 |
51 // Creates and returns an Authenticator instance for |connection_|. | 52 // Creates and returns an Authenticator instance for |connection_|. |
52 // Exposed for testing. | 53 // Exposed for testing. |
53 virtual std::unique_ptr<Authenticator> CreateAuthenticator(); | 54 virtual std::unique_ptr<Authenticator> CreateAuthenticator(); |
54 | 55 |
55 private: | 56 private: |
56 // Transitions to |new_state|, and notifies observers. | 57 // Transitions to |new_state|, and notifies observers. |
57 void TransitionToState(RemoteDeviceLifeCycle::State new_state); | 58 void TransitionToState(RemoteDeviceLifeCycle::State new_state); |
58 | 59 |
59 // Transtitions to FINDING_CONNECTION state. Creates and starts | 60 // Transtitions to FINDING_CONNECTION state. Creates and starts |
60 // |connection_finder_|. | 61 // |connection_finder_|. |
61 void FindConnection(); | 62 void FindConnection(); |
62 | 63 |
63 // Called when |connection_finder_| finds a connection. | 64 // Called when |connection_finder_| finds a connection. |
64 void OnConnectionFound(std::unique_ptr<Connection> connection); | 65 void OnConnectionFound(std::unique_ptr<cryptauth::Connection> connection); |
65 | 66 |
66 // Callback when |authenticator_| completes authentication. | 67 // Callback when |authenticator_| completes authentication. |
67 void OnAuthenticationResult(Authenticator::Result result, | 68 void OnAuthenticationResult(Authenticator::Result result, |
68 std::unique_ptr<SecureContext> secure_context); | 69 std::unique_ptr<SecureContext> secure_context); |
69 | 70 |
70 // Creates the messenger which parses status updates. | 71 // Creates the messenger which parses status updates. |
71 void CreateMessenger(); | 72 void CreateMessenger(); |
72 | 73 |
73 // MessengerObserver: | 74 // MessengerObserver: |
74 void OnDisconnected() override; | 75 void OnDisconnected() override; |
75 | 76 |
76 // The remote device being controlled. | 77 // The remote device being controlled. |
77 const cryptauth::RemoteDevice remote_device_; | 78 const cryptauth::RemoteDevice remote_device_; |
78 | 79 |
79 // Used to grab dependencies in chrome. Not owned. | 80 // Used to grab dependencies in chrome. Not owned. |
80 ProximityAuthClient* proximity_auth_client_; | 81 ProximityAuthClient* proximity_auth_client_; |
81 | 82 |
82 // The current state in the life cycle. | 83 // The current state in the life cycle. |
83 RemoteDeviceLifeCycle::State state_; | 84 RemoteDeviceLifeCycle::State state_; |
84 | 85 |
85 // Observers added to the life cycle. Configured as NOTIFY_EXISTING_ONLY. | 86 // Observers added to the life cycle. Configured as NOTIFY_EXISTING_ONLY. |
86 base::ObserverList<Observer> observers_; | 87 base::ObserverList<Observer> observers_; |
87 | 88 |
88 // The connection that is established by |connection_finder_|. | 89 // The connection that is established by |connection_finder_|. |
89 std::unique_ptr<Connection> connection_; | 90 std::unique_ptr<cryptauth::Connection> connection_; |
90 | 91 |
91 // Context for encrypting and decrypting messages. Created after | 92 // Context for encrypting and decrypting messages. Created after |
92 // authentication succeeds. Ownership is eventually passed to |messenger_|. | 93 // authentication succeeds. Ownership is eventually passed to |messenger_|. |
93 std::unique_ptr<SecureContext> secure_context_; | 94 std::unique_ptr<SecureContext> secure_context_; |
94 | 95 |
95 // The messenger for sending and receiving messages in the | 96 // The messenger for sending and receiving messages in the |
96 // SECURE_CHANNEL_ESTABLISHED state. | 97 // SECURE_CHANNEL_ESTABLISHED state. |
97 std::unique_ptr<Messenger> messenger_; | 98 std::unique_ptr<Messenger> messenger_; |
98 | 99 |
99 // Authenticates the remote device after it is connected. Used in the | 100 // Authenticates the remote device after it is connected. Used in the |
100 // AUTHENTICATING state. | 101 // AUTHENTICATING state. |
101 std::unique_ptr<Authenticator> authenticator_; | 102 std::unique_ptr<Authenticator> authenticator_; |
102 | 103 |
103 // Used in the FINDING_CONNECTION state to establish a connection to the | 104 // Used in the FINDING_CONNECTION state to establish a connection to the |
104 // remote device. | 105 // remote device. |
105 std::unique_ptr<ConnectionFinder> connection_finder_; | 106 std::unique_ptr<cryptauth::ConnectionFinder> connection_finder_; |
106 | 107 |
107 // Rate limits Bluetooth connections to the same device. Used to in the | 108 // Rate limits Bluetooth connections to the same device. Used to in the |
108 // created ConnectionFinder. | 109 // created cryptauth::ConnectionFinder. |
109 std::unique_ptr<BluetoothThrottler> bluetooth_throttler_; | 110 std::unique_ptr<cryptauth::BluetoothThrottler> bluetooth_throttler_; |
110 | 111 |
111 // After authentication fails, this timer waits for a period of time before | 112 // After authentication fails, this timer waits for a period of time before |
112 // retrying the connection. | 113 // retrying the connection. |
113 base::OneShotTimer authentication_recovery_timer_; | 114 base::OneShotTimer authentication_recovery_timer_; |
114 | 115 |
115 base::WeakPtrFactory<RemoteDeviceLifeCycleImpl> weak_ptr_factory_; | 116 base::WeakPtrFactory<RemoteDeviceLifeCycleImpl> weak_ptr_factory_; |
116 | 117 |
117 DISALLOW_COPY_AND_ASSIGN(RemoteDeviceLifeCycleImpl); | 118 DISALLOW_COPY_AND_ASSIGN(RemoteDeviceLifeCycleImpl); |
118 }; | 119 }; |
119 | 120 |
120 } // namespace proximity_auth | 121 } // namespace proximity_auth |
121 | 122 |
122 #endif // COMPONENTS_PROXIMITY_AUTH_REMOTE_DEVICE_LIFE_CYCLE_IMPL_H | 123 #endif // COMPONENTS_PROXIMITY_AUTH_REMOTE_DEVICE_LIFE_CYCLE_IMPL_H |
OLD | NEW |