| 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 #include "components/proximity_auth/remote_device_life_cycle_impl.h" | 5 #include "components/proximity_auth/remote_device_life_cycle_impl.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/command_line.h" | |
| 11 #include "base/location.h" | 10 #include "base/location.h" |
| 12 #include "base/memory/ptr_util.h" | 11 #include "base/memory/ptr_util.h" |
| 13 #include "base/threading/thread_task_runner_handle.h" | 12 #include "base/threading/thread_task_runner_handle.h" |
| 14 #include "base/time/default_tick_clock.h" | 13 #include "base/time/default_tick_clock.h" |
| 15 #include "components/cryptauth/bluetooth_throttler_impl.h" | 14 #include "components/cryptauth/bluetooth_throttler_impl.h" |
| 16 #include "components/cryptauth/connection_finder.h" | 15 #include "components/cryptauth/connection_finder.h" |
| 17 #include "components/cryptauth/device_to_device_authenticator.h" | 16 #include "components/cryptauth/device_to_device_authenticator.h" |
| 18 #include "components/cryptauth/secure_context.h" | 17 #include "components/cryptauth/secure_context.h" |
| 19 #include "components/cryptauth/secure_message_delegate.h" | 18 #include "components/cryptauth/secure_message_delegate.h" |
| 19 #include "components/proximity_auth/ble/bluetooth_low_energy_connection.h" |
| 20 #include "components/proximity_auth/ble/bluetooth_low_energy_connection_finder.h
" |
| 20 #include "components/proximity_auth/bluetooth_connection.h" | 21 #include "components/proximity_auth/bluetooth_connection.h" |
| 21 #include "components/proximity_auth/bluetooth_connection_finder.h" | 22 #include "components/proximity_auth/bluetooth_connection_finder.h" |
| 22 #include "components/proximity_auth/bluetooth_low_energy_connection_finder.h" | |
| 23 #include "components/proximity_auth/logging/logging.h" | 23 #include "components/proximity_auth/logging/logging.h" |
| 24 #include "components/proximity_auth/messenger_impl.h" | 24 #include "components/proximity_auth/messenger_impl.h" |
| 25 #include "components/proximity_auth/proximity_auth_client.h" | 25 #include "components/proximity_auth/proximity_auth_client.h" |
| 26 #include "components/proximity_auth/switches.h" | |
| 27 | 26 |
| 28 namespace proximity_auth { | 27 namespace proximity_auth { |
| 29 | 28 |
| 30 namespace { | 29 namespace { |
| 31 | 30 |
| 32 // The UUID of the Smart Lock classic Bluetooth service. | 31 // The UUID of the Smart Lock classic Bluetooth service. |
| 33 const char kClassicBluetoothServiceUUID[] = | 32 const char kClassicBluetoothServiceUUID[] = |
| 34 "704EE561-3782-405A-A14B-2D47A2DDCDDF"; | 33 "704EE561-3782-405A-A14B-2D47A2DDCDDF"; |
| 35 | 34 |
| 35 // The UUID of the Bluetooth Low Energy service. |
| 36 const char kBLESmartLockServiceUUID[] = "b3b7e28e-a000-3e17-bd86-6e97b9e28c11"; |
| 37 |
| 36 // The time to wait, in seconds, after authentication fails, before retrying | 38 // The time to wait, in seconds, after authentication fails, before retrying |
| 37 // another connection. | 39 // another connection. |
| 38 const int kAuthenticationRecoveryTimeSeconds = 10; | 40 const int kAuthenticationRecoveryTimeSeconds = 10; |
| 39 | 41 |
| 40 } // namespace | 42 } // namespace |
| 41 | 43 |
| 42 RemoteDeviceLifeCycleImpl::RemoteDeviceLifeCycleImpl( | 44 RemoteDeviceLifeCycleImpl::RemoteDeviceLifeCycleImpl( |
| 43 const cryptauth::RemoteDevice& remote_device, | 45 const cryptauth::RemoteDevice& remote_device, |
| 44 ProximityAuthClient* proximity_auth_client) | 46 ProximityAuthClient* proximity_auth_client) |
| 45 : remote_device_(remote_device), | 47 : remote_device_(remote_device), |
| (...skipping 27 matching lines...) Expand all Loading... |
| 73 void RemoteDeviceLifeCycleImpl::AddObserver(Observer* observer) { | 75 void RemoteDeviceLifeCycleImpl::AddObserver(Observer* observer) { |
| 74 observers_.AddObserver(observer); | 76 observers_.AddObserver(observer); |
| 75 } | 77 } |
| 76 | 78 |
| 77 void RemoteDeviceLifeCycleImpl::RemoveObserver(Observer* observer) { | 79 void RemoteDeviceLifeCycleImpl::RemoveObserver(Observer* observer) { |
| 78 observers_.RemoveObserver(observer); | 80 observers_.RemoveObserver(observer); |
| 79 } | 81 } |
| 80 | 82 |
| 81 std::unique_ptr<cryptauth::ConnectionFinder> | 83 std::unique_ptr<cryptauth::ConnectionFinder> |
| 82 RemoteDeviceLifeCycleImpl::CreateConnectionFinder() { | 84 RemoteDeviceLifeCycleImpl::CreateConnectionFinder() { |
| 83 if (base::CommandLine::ForCurrentProcess()->HasSwitch( | 85 if (remote_device_.bluetooth_address.empty()) { |
| 84 proximity_auth::switches::kEnableBluetoothLowEnergyDiscovery)) { | |
| 85 cryptauth::RemoteBeaconSeedFetcher fetcher( | |
| 86 proximity_auth_client_->GetCryptAuthDeviceManager()); | |
| 87 std::vector<cryptauth::BeaconSeed> beacon_seeds; | |
| 88 if (!fetcher.FetchSeedsForDevice(remote_device_, &beacon_seeds)) { | |
| 89 PA_LOG(ERROR) << "Unable to fetch BeaconSeeds for " | |
| 90 << remote_device_.name; | |
| 91 return nullptr; | |
| 92 } | |
| 93 | |
| 94 return base::MakeUnique<BluetoothLowEnergyConnectionFinder>( | 86 return base::MakeUnique<BluetoothLowEnergyConnectionFinder>( |
| 95 remote_device_, beacon_seeds, bluetooth_throttler_); | 87 remote_device_, kBLESmartLockServiceUUID, |
| 88 BluetoothLowEnergyConnectionFinder::FinderStrategy::FIND_PAIRED_DEVICE, |
| 89 nullptr, bluetooth_throttler_, 3); |
| 96 } else { | 90 } else { |
| 97 return base::MakeUnique<BluetoothConnectionFinder>( | 91 return base::MakeUnique<BluetoothConnectionFinder>( |
| 98 remote_device_, device::BluetoothUUID(kClassicBluetoothServiceUUID), | 92 remote_device_, device::BluetoothUUID(kClassicBluetoothServiceUUID), |
| 99 base::TimeDelta::FromSeconds(3)); | 93 base::TimeDelta::FromSeconds(3)); |
| 100 } | 94 } |
| 101 } | 95 } |
| 102 | 96 |
| 103 std::unique_ptr<cryptauth::Authenticator> | 97 std::unique_ptr<cryptauth::Authenticator> |
| 104 RemoteDeviceLifeCycleImpl::CreateAuthenticator() { | 98 RemoteDeviceLifeCycleImpl::CreateAuthenticator() { |
| 105 return base::MakeUnique<cryptauth::DeviceToDeviceAuthenticator>( | 99 return base::MakeUnique<cryptauth::DeviceToDeviceAuthenticator>( |
| 106 connection_.get(), remote_device_.user_id, | 100 connection_.get(), remote_device_.user_id, |
| 107 proximity_auth_client_->CreateSecureMessageDelegate()); | 101 proximity_auth_client_->CreateSecureMessageDelegate()); |
| 108 } | 102 } |
| 109 | 103 |
| 110 void RemoteDeviceLifeCycleImpl::TransitionToState( | 104 void RemoteDeviceLifeCycleImpl::TransitionToState( |
| 111 RemoteDeviceLifeCycle::State new_state) { | 105 RemoteDeviceLifeCycle::State new_state) { |
| 112 PA_LOG(INFO) << "Life cycle transition: " << static_cast<int>(state_) | 106 PA_LOG(INFO) << "Life cycle transition: " << static_cast<int>(state_) |
| 113 << " => " << static_cast<int>(new_state); | 107 << " => " << static_cast<int>(new_state); |
| 114 RemoteDeviceLifeCycle::State old_state = state_; | 108 RemoteDeviceLifeCycle::State old_state = state_; |
| 115 state_ = new_state; | 109 state_ = new_state; |
| 116 for (auto& observer : observers_) | 110 for (auto& observer : observers_) |
| 117 observer.OnLifeCycleStateChanged(old_state, new_state); | 111 observer.OnLifeCycleStateChanged(old_state, new_state); |
| 118 } | 112 } |
| 119 | 113 |
| 120 void RemoteDeviceLifeCycleImpl::FindConnection() { | 114 void RemoteDeviceLifeCycleImpl::FindConnection() { |
| 121 connection_finder_ = CreateConnectionFinder(); | 115 connection_finder_ = CreateConnectionFinder(); |
| 122 if (!connection_finder_) { | |
| 123 // TODO(tengs): We need to introduce a failed state if the RemoteDevice data | |
| 124 // is invalid. | |
| 125 TransitionToState(RemoteDeviceLifeCycleImpl::State::FINDING_CONNECTION); | |
| 126 return; | |
| 127 } | |
| 128 | |
| 129 connection_finder_->Find( | 116 connection_finder_->Find( |
| 130 base::Bind(&RemoteDeviceLifeCycleImpl::OnConnectionFound, | 117 base::Bind(&RemoteDeviceLifeCycleImpl::OnConnectionFound, |
| 131 weak_ptr_factory_.GetWeakPtr())); | 118 weak_ptr_factory_.GetWeakPtr())); |
| 132 TransitionToState(RemoteDeviceLifeCycle::State::FINDING_CONNECTION); | 119 TransitionToState(RemoteDeviceLifeCycle::State::FINDING_CONNECTION); |
| 133 } | 120 } |
| 134 | 121 |
| 135 void RemoteDeviceLifeCycleImpl::OnConnectionFound( | 122 void RemoteDeviceLifeCycleImpl::OnConnectionFound( |
| 136 std::unique_ptr<cryptauth::Connection> connection) { | 123 std::unique_ptr<cryptauth::Connection> connection) { |
| 137 DCHECK(state_ == RemoteDeviceLifeCycle::State::FINDING_CONNECTION); | 124 DCHECK(state_ == RemoteDeviceLifeCycle::State::FINDING_CONNECTION); |
| 138 connection_ = std::move(connection); | 125 connection_ = std::move(connection); |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 179 TransitionToState(RemoteDeviceLifeCycle::State::SECURE_CHANNEL_ESTABLISHED); | 166 TransitionToState(RemoteDeviceLifeCycle::State::SECURE_CHANNEL_ESTABLISHED); |
| 180 } | 167 } |
| 181 | 168 |
| 182 void RemoteDeviceLifeCycleImpl::OnDisconnected() { | 169 void RemoteDeviceLifeCycleImpl::OnDisconnected() { |
| 183 DCHECK(state_ == RemoteDeviceLifeCycle::State::SECURE_CHANNEL_ESTABLISHED); | 170 DCHECK(state_ == RemoteDeviceLifeCycle::State::SECURE_CHANNEL_ESTABLISHED); |
| 184 messenger_.reset(); | 171 messenger_.reset(); |
| 185 FindConnection(); | 172 FindConnection(); |
| 186 } | 173 } |
| 187 | 174 |
| 188 } // namespace proximity_auth | 175 } // namespace proximity_auth |
| OLD | NEW |