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.
5 #ifndef COMPONENTS_PROXIMITY_AUTH_REMOTE_DEVICE_LIFE_CYCLE_IMPL_H
6 #define COMPONENTS_PROXIMITY_AUTH_REMOTE_DEVICE_LIFE_CYCLE_IMPL_H
8 #include "base/memory/scoped_ptr.h"
9 #include "base/memory/weak_ptr.h"
10 #include "base/observer_list.h"
11 #include "base/timer/timer.h"
12 #include "components/proximity_auth/authenticator.h"
13 #include "components/proximity_auth/client_observer.h"
14 #include "components/proximity_auth/remote_device.h"
15 #include "components/proximity_auth/remote_device_life_cycle.h"
17 namespace proximity_auth
{
19 class BluetoothThrottler
;
20 class BluetoothLowEnergyDeviceWhitelist
;
23 class ConnectionFinder
;
24 class ProximityAuthClient
;
27 // Implementation of RemoteDeviceLifeCycle.
28 class RemoteDeviceLifeCycleImpl
: public RemoteDeviceLifeCycle
,
29 public ClientObserver
{
31 // Creates the life cycle for controlling the given |remote_device|.
32 // |proximity_auth_client| is not owned.
33 RemoteDeviceLifeCycleImpl(const RemoteDevice
& remote_device
,
34 ProximityAuthClient
* proximity_auth_client
);
35 ~RemoteDeviceLifeCycleImpl() override
;
37 // RemoteDeviceLifeCycle:
38 void Start() override
;
39 RemoteDeviceLifeCycle::State
GetState() const override
;
40 Client
* GetClient() override
;
41 void AddObserver(Observer
* observer
) override
;
42 void RemoveObserver(Observer
* observer
) override
;
45 // Creates and returns a ConnectionFinder instance for |remote_device_|.
46 // Exposed for testing.
47 virtual scoped_ptr
<ConnectionFinder
> CreateConnectionFinder();
49 // Creates and returns an Authenticator instance for |connection_|.
50 // Exposed for testing.
51 virtual scoped_ptr
<Authenticator
> CreateAuthenticator();
54 // Transitions to |new_state|, and notifies observers.
55 void TransitionToState(RemoteDeviceLifeCycle::State new_state
);
57 // Transtitions to FINDING_CONNECTION state. Creates and starts
58 // |connection_finder_|.
59 void FindConnection();
61 // Called when |connection_finder_| finds a connection.
62 void OnConnectionFound(scoped_ptr
<Connection
> connection
);
64 // Callback when |authenticator_| completes authentication.
65 void OnAuthenticationResult(Authenticator::Result result
,
66 scoped_ptr
<SecureContext
> secure_context
);
68 // Creates the client which parses status updates.
72 void OnDisconnected() override
;
74 // The remote device being controlled.
75 const RemoteDevice remote_device_
;
77 // Used to grab dependencies in chrome. Not owned.
78 ProximityAuthClient
* proximity_auth_client_
;
80 // The current state in the life cycle.
81 RemoteDeviceLifeCycle::State state_
;
83 // Observers added to the life cycle.
84 base::ObserverList
<Observer
> observers_
;
86 // Rate limits Bluetooth connections to the same device. Used to in the
87 // created ConnectionFinder.
88 scoped_ptr
<BluetoothThrottler
> bluetooth_throttler_
;
90 // Used in the FINDING_CONNECTION state to establish a connection to the
92 scoped_ptr
<ConnectionFinder
> connection_finder_
;
94 // The connection that is established by |connection_finder_|.
95 scoped_ptr
<Connection
> connection_
;
97 // Authenticates the remote device after it is connected. Used in the
98 // AUTHENTICATING state.
99 scoped_ptr
<Authenticator
> authenticator_
;
101 // Context for encrypting and decrypting messages. Created after
102 // authentication succeeds. Ownership is eventually passed to |client_|.
103 scoped_ptr
<SecureContext
> secure_context_
;
105 // The client for sending and receiving messages in the
106 // SECURE_CHANNEL_ESTABLISHED state.
107 scoped_ptr
<Client
> client_
;
109 // After authentication fails, this timer waits for a period of time before
110 // retrying the connection.
111 base::OneShotTimer
<RemoteDeviceLifeCycleImpl
> authentication_recovery_timer_
;
113 base::WeakPtrFactory
<RemoteDeviceLifeCycleImpl
> weak_ptr_factory_
;
115 DISALLOW_COPY_AND_ASSIGN(RemoteDeviceLifeCycleImpl
);
118 } // namespace proximity_auth
120 #endif // COMPONENTS_PROXIMITY_AUTH_REMOTE_DEVICE_LIFE_CYCLE_IMPL_H