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