summaryrefslogtreecommitdiffstats
path: root/components/proximity_auth/remote_device_life_cycle_impl.h
blob: 5746206c111990181c83fa0a0f6b78e15e02ed96 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_PROXIMITY_AUTH_REMOTE_DEVICE_LIFE_CYCLE_IMPL_H
#define COMPONENTS_PROXIMITY_AUTH_REMOTE_DEVICE_LIFE_CYCLE_IMPL_H

#include "base/macros.h"
#include "base/memory/scoped_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/observer_list.h"
#include "base/timer/timer.h"
#include "components/proximity_auth/authenticator.h"
#include "components/proximity_auth/messenger_observer.h"
#include "components/proximity_auth/remote_device.h"
#include "components/proximity_auth/remote_device_life_cycle.h"

namespace proximity_auth {

class BluetoothThrottler;
class BluetoothLowEnergyDeviceWhitelist;
class Messenger;
class Connection;
class ConnectionFinder;
class ProximityAuthClient;
class SecureContext;

// Implementation of RemoteDeviceLifeCycle.
class RemoteDeviceLifeCycleImpl : public RemoteDeviceLifeCycle,
                                  public MessengerObserver {
 public:
  // Creates the life cycle for controlling the given |remote_device|.
  // |proximity_auth_client| is not owned.
  RemoteDeviceLifeCycleImpl(const RemoteDevice& remote_device,
                            ProximityAuthClient* proximity_auth_client);
  ~RemoteDeviceLifeCycleImpl() override;

  // RemoteDeviceLifeCycle:
  void Start() override;
  RemoteDevice GetRemoteDevice() const override;
  RemoteDeviceLifeCycle::State GetState() const override;
  Messenger* GetMessenger() override;
  void AddObserver(Observer* observer) override;
  void RemoveObserver(Observer* observer) override;

 protected:
  // Creates and returns a ConnectionFinder instance for |remote_device_|.
  // Exposed for testing.
  virtual scoped_ptr<ConnectionFinder> CreateConnectionFinder();

  // Creates and returns an Authenticator instance for |connection_|.
  // Exposed for testing.
  virtual scoped_ptr<Authenticator> CreateAuthenticator();

 private:
  // Transitions to |new_state|, and notifies observers.
  void TransitionToState(RemoteDeviceLifeCycle::State new_state);

  // Transtitions to FINDING_CONNECTION state. Creates and starts
  // |connection_finder_|.
  void FindConnection();

  // Called when |connection_finder_| finds a connection.
  void OnConnectionFound(scoped_ptr<Connection> connection);

  // Callback when |authenticator_| completes authentication.
  void OnAuthenticationResult(Authenticator::Result result,
                              scoped_ptr<SecureContext> secure_context);

  // Creates the messenger which parses status updates.
  void CreateMessenger();

  // MessengerObserver:
  void OnDisconnected() override;

  // The remote device being controlled.
  const RemoteDevice remote_device_;

  // Used to grab dependencies in chrome. Not owned.
  ProximityAuthClient* proximity_auth_client_;

  // The current state in the life cycle.
  RemoteDeviceLifeCycle::State state_;

  // Observers added to the life cycle. Configured as NOTIFY_EXISTING_ONLY.
  base::ObserverList<Observer> observers_;

  // The connection that is established by |connection_finder_|.
  scoped_ptr<Connection> connection_;

  // Context for encrypting and decrypting messages. Created after
  // authentication succeeds. Ownership is eventually passed to |messenger_|.
  scoped_ptr<SecureContext> secure_context_;

  // The messenger for sending and receiving messages in the
  // SECURE_CHANNEL_ESTABLISHED state.
  scoped_ptr<Messenger> messenger_;

  // Authenticates the remote device after it is connected. Used in the
  // AUTHENTICATING state.
  scoped_ptr<Authenticator> authenticator_;

  // Used in the FINDING_CONNECTION state to establish a connection to the
  // remote device.
  scoped_ptr<ConnectionFinder> connection_finder_;

  // Rate limits Bluetooth connections to the same device. Used to in the
  // created ConnectionFinder.
  scoped_ptr<BluetoothThrottler> bluetooth_throttler_;

  // After authentication fails, this timer waits for a period of time before
  // retrying the connection.
  base::OneShotTimer authentication_recovery_timer_;

  base::WeakPtrFactory<RemoteDeviceLifeCycleImpl> weak_ptr_factory_;

  DISALLOW_COPY_AND_ASSIGN(RemoteDeviceLifeCycleImpl);
};

}  // namespace proximity_auth

#endif  // COMPONENTS_PROXIMITY_AUTH_REMOTE_DEVICE_LIFE_CYCLE_IMPL_H