| 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/location.h" | 10 #include "base/location.h" |
| 11 #include "base/memory/ptr_util.h" |
| 11 #include "base/thread_task_runner_handle.h" | 12 #include "base/thread_task_runner_handle.h" |
| 12 #include "base/time/default_tick_clock.h" | 13 #include "base/time/default_tick_clock.h" |
| 13 #include "components/proximity_auth/ble/bluetooth_low_energy_connection.h" | 14 #include "components/proximity_auth/ble/bluetooth_low_energy_connection.h" |
| 14 #include "components/proximity_auth/ble/bluetooth_low_energy_connection_finder.h
" | 15 #include "components/proximity_auth/ble/bluetooth_low_energy_connection_finder.h
" |
| 15 #include "components/proximity_auth/bluetooth_connection.h" | 16 #include "components/proximity_auth/bluetooth_connection.h" |
| 16 #include "components/proximity_auth/bluetooth_connection_finder.h" | 17 #include "components/proximity_auth/bluetooth_connection_finder.h" |
| 17 #include "components/proximity_auth/bluetooth_throttler_impl.h" | 18 #include "components/proximity_auth/bluetooth_throttler_impl.h" |
| 18 #include "components/proximity_auth/cryptauth/secure_message_delegate.h" | 19 #include "components/proximity_auth/cryptauth/secure_message_delegate.h" |
| 19 #include "components/proximity_auth/device_to_device_authenticator.h" | 20 #include "components/proximity_auth/device_to_device_authenticator.h" |
| 20 #include "components/proximity_auth/logging/logging.h" | 21 #include "components/proximity_auth/logging/logging.h" |
| (...skipping 19 matching lines...) Expand all Loading... |
| 40 } // namespace | 41 } // namespace |
| 41 | 42 |
| 42 RemoteDeviceLifeCycleImpl::RemoteDeviceLifeCycleImpl( | 43 RemoteDeviceLifeCycleImpl::RemoteDeviceLifeCycleImpl( |
| 43 const RemoteDevice& remote_device, | 44 const RemoteDevice& remote_device, |
| 44 ProximityAuthClient* proximity_auth_client) | 45 ProximityAuthClient* proximity_auth_client) |
| 45 : remote_device_(remote_device), | 46 : remote_device_(remote_device), |
| 46 proximity_auth_client_(proximity_auth_client), | 47 proximity_auth_client_(proximity_auth_client), |
| 47 state_(RemoteDeviceLifeCycle::State::STOPPED), | 48 state_(RemoteDeviceLifeCycle::State::STOPPED), |
| 48 observers_(base::ObserverList<Observer>::NOTIFY_EXISTING_ONLY), | 49 observers_(base::ObserverList<Observer>::NOTIFY_EXISTING_ONLY), |
| 49 bluetooth_throttler_(new BluetoothThrottlerImpl( | 50 bluetooth_throttler_(new BluetoothThrottlerImpl( |
| 50 make_scoped_ptr(new base::DefaultTickClock()))), | 51 base::WrapUnique(new base::DefaultTickClock()))), |
| 51 weak_ptr_factory_(this) {} | 52 weak_ptr_factory_(this) {} |
| 52 | 53 |
| 53 RemoteDeviceLifeCycleImpl::~RemoteDeviceLifeCycleImpl() {} | 54 RemoteDeviceLifeCycleImpl::~RemoteDeviceLifeCycleImpl() {} |
| 54 | 55 |
| 55 void RemoteDeviceLifeCycleImpl::Start() { | 56 void RemoteDeviceLifeCycleImpl::Start() { |
| 56 PA_LOG(INFO) << "Life cycle for " << remote_device_.bluetooth_address | 57 PA_LOG(INFO) << "Life cycle for " << remote_device_.bluetooth_address |
| 57 << " started."; | 58 << " started."; |
| 58 DCHECK(state_ == RemoteDeviceLifeCycle::State::STOPPED); | 59 DCHECK(state_ == RemoteDeviceLifeCycle::State::STOPPED); |
| 59 FindConnection(); | 60 FindConnection(); |
| 60 } | 61 } |
| (...skipping 11 matching lines...) Expand all Loading... |
| 72 } | 73 } |
| 73 | 74 |
| 74 void RemoteDeviceLifeCycleImpl::AddObserver(Observer* observer) { | 75 void RemoteDeviceLifeCycleImpl::AddObserver(Observer* observer) { |
| 75 observers_.AddObserver(observer); | 76 observers_.AddObserver(observer); |
| 76 } | 77 } |
| 77 | 78 |
| 78 void RemoteDeviceLifeCycleImpl::RemoveObserver(Observer* observer) { | 79 void RemoteDeviceLifeCycleImpl::RemoveObserver(Observer* observer) { |
| 79 observers_.RemoveObserver(observer); | 80 observers_.RemoveObserver(observer); |
| 80 } | 81 } |
| 81 | 82 |
| 82 scoped_ptr<ConnectionFinder> | 83 std::unique_ptr<ConnectionFinder> |
| 83 RemoteDeviceLifeCycleImpl::CreateConnectionFinder() { | 84 RemoteDeviceLifeCycleImpl::CreateConnectionFinder() { |
| 84 if (remote_device_.bluetooth_type == RemoteDevice::BLUETOOTH_LE) { | 85 if (remote_device_.bluetooth_type == RemoteDevice::BLUETOOTH_LE) { |
| 85 return make_scoped_ptr(new BluetoothLowEnergyConnectionFinder( | 86 return base::WrapUnique(new BluetoothLowEnergyConnectionFinder( |
| 86 remote_device_, kBLESmartLockServiceUUID, | 87 remote_device_, kBLESmartLockServiceUUID, |
| 87 BluetoothLowEnergyConnectionFinder::FinderStrategy::FIND_PAIRED_DEVICE, | 88 BluetoothLowEnergyConnectionFinder::FinderStrategy::FIND_PAIRED_DEVICE, |
| 88 nullptr, bluetooth_throttler_.get(), 3)); | 89 nullptr, bluetooth_throttler_.get(), 3)); |
| 89 } else { | 90 } else { |
| 90 return make_scoped_ptr(new BluetoothConnectionFinder( | 91 return base::WrapUnique(new BluetoothConnectionFinder( |
| 91 remote_device_, device::BluetoothUUID(kClassicBluetoothServiceUUID), | 92 remote_device_, device::BluetoothUUID(kClassicBluetoothServiceUUID), |
| 92 base::TimeDelta::FromSeconds(3))); | 93 base::TimeDelta::FromSeconds(3))); |
| 93 } | 94 } |
| 94 } | 95 } |
| 95 | 96 |
| 96 scoped_ptr<Authenticator> RemoteDeviceLifeCycleImpl::CreateAuthenticator() { | 97 std::unique_ptr<Authenticator> |
| 97 return make_scoped_ptr(new DeviceToDeviceAuthenticator( | 98 RemoteDeviceLifeCycleImpl::CreateAuthenticator() { |
| 99 return base::WrapUnique(new DeviceToDeviceAuthenticator( |
| 98 connection_.get(), remote_device_.user_id, | 100 connection_.get(), remote_device_.user_id, |
| 99 proximity_auth_client_->CreateSecureMessageDelegate())); | 101 proximity_auth_client_->CreateSecureMessageDelegate())); |
| 100 } | 102 } |
| 101 | 103 |
| 102 void RemoteDeviceLifeCycleImpl::TransitionToState( | 104 void RemoteDeviceLifeCycleImpl::TransitionToState( |
| 103 RemoteDeviceLifeCycle::State new_state) { | 105 RemoteDeviceLifeCycle::State new_state) { |
| 104 PA_LOG(INFO) << "Life cycle transition: " << static_cast<int>(state_) | 106 PA_LOG(INFO) << "Life cycle transition: " << static_cast<int>(state_) |
| 105 << " => " << static_cast<int>(new_state); | 107 << " => " << static_cast<int>(new_state); |
| 106 RemoteDeviceLifeCycle::State old_state = state_; | 108 RemoteDeviceLifeCycle::State old_state = state_; |
| 107 state_ = new_state; | 109 state_ = new_state; |
| 108 FOR_EACH_OBSERVER(Observer, observers_, | 110 FOR_EACH_OBSERVER(Observer, observers_, |
| 109 OnLifeCycleStateChanged(old_state, new_state)); | 111 OnLifeCycleStateChanged(old_state, new_state)); |
| 110 } | 112 } |
| 111 | 113 |
| 112 void RemoteDeviceLifeCycleImpl::FindConnection() { | 114 void RemoteDeviceLifeCycleImpl::FindConnection() { |
| 113 connection_finder_ = CreateConnectionFinder(); | 115 connection_finder_ = CreateConnectionFinder(); |
| 114 connection_finder_->Find( | 116 connection_finder_->Find( |
| 115 base::Bind(&RemoteDeviceLifeCycleImpl::OnConnectionFound, | 117 base::Bind(&RemoteDeviceLifeCycleImpl::OnConnectionFound, |
| 116 weak_ptr_factory_.GetWeakPtr())); | 118 weak_ptr_factory_.GetWeakPtr())); |
| 117 TransitionToState(RemoteDeviceLifeCycle::State::FINDING_CONNECTION); | 119 TransitionToState(RemoteDeviceLifeCycle::State::FINDING_CONNECTION); |
| 118 } | 120 } |
| 119 | 121 |
| 120 void RemoteDeviceLifeCycleImpl::OnConnectionFound( | 122 void RemoteDeviceLifeCycleImpl::OnConnectionFound( |
| 121 scoped_ptr<Connection> connection) { | 123 std::unique_ptr<Connection> connection) { |
| 122 DCHECK(state_ == RemoteDeviceLifeCycle::State::FINDING_CONNECTION); | 124 DCHECK(state_ == RemoteDeviceLifeCycle::State::FINDING_CONNECTION); |
| 123 connection_ = std::move(connection); | 125 connection_ = std::move(connection); |
| 124 authenticator_ = CreateAuthenticator(); | 126 authenticator_ = CreateAuthenticator(); |
| 125 authenticator_->Authenticate( | 127 authenticator_->Authenticate( |
| 126 base::Bind(&RemoteDeviceLifeCycleImpl::OnAuthenticationResult, | 128 base::Bind(&RemoteDeviceLifeCycleImpl::OnAuthenticationResult, |
| 127 weak_ptr_factory_.GetWeakPtr())); | 129 weak_ptr_factory_.GetWeakPtr())); |
| 128 TransitionToState(RemoteDeviceLifeCycle::State::AUTHENTICATING); | 130 TransitionToState(RemoteDeviceLifeCycle::State::AUTHENTICATING); |
| 129 } | 131 } |
| 130 | 132 |
| 131 void RemoteDeviceLifeCycleImpl::OnAuthenticationResult( | 133 void RemoteDeviceLifeCycleImpl::OnAuthenticationResult( |
| 132 Authenticator::Result result, | 134 Authenticator::Result result, |
| 133 scoped_ptr<SecureContext> secure_context) { | 135 std::unique_ptr<SecureContext> secure_context) { |
| 134 DCHECK(state_ == RemoteDeviceLifeCycle::State::AUTHENTICATING); | 136 DCHECK(state_ == RemoteDeviceLifeCycle::State::AUTHENTICATING); |
| 135 authenticator_.reset(); | 137 authenticator_.reset(); |
| 136 if (result != Authenticator::Result::SUCCESS) { | 138 if (result != Authenticator::Result::SUCCESS) { |
| 137 PA_LOG(WARNING) << "Waiting " << kAuthenticationRecoveryTimeSeconds | 139 PA_LOG(WARNING) << "Waiting " << kAuthenticationRecoveryTimeSeconds |
| 138 << " seconds to retry after authentication failure."; | 140 << " seconds to retry after authentication failure."; |
| 139 connection_->Disconnect(); | 141 connection_->Disconnect(); |
| 140 authentication_recovery_timer_.Start( | 142 authentication_recovery_timer_.Start( |
| 141 FROM_HERE, | 143 FROM_HERE, |
| 142 base::TimeDelta::FromSeconds(kAuthenticationRecoveryTimeSeconds), this, | 144 base::TimeDelta::FromSeconds(kAuthenticationRecoveryTimeSeconds), this, |
| 143 &RemoteDeviceLifeCycleImpl::FindConnection); | 145 &RemoteDeviceLifeCycleImpl::FindConnection); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 164 TransitionToState(RemoteDeviceLifeCycle::State::SECURE_CHANNEL_ESTABLISHED); | 166 TransitionToState(RemoteDeviceLifeCycle::State::SECURE_CHANNEL_ESTABLISHED); |
| 165 } | 167 } |
| 166 | 168 |
| 167 void RemoteDeviceLifeCycleImpl::OnDisconnected() { | 169 void RemoteDeviceLifeCycleImpl::OnDisconnected() { |
| 168 DCHECK(state_ == RemoteDeviceLifeCycle::State::SECURE_CHANNEL_ESTABLISHED); | 170 DCHECK(state_ == RemoteDeviceLifeCycle::State::SECURE_CHANNEL_ESTABLISHED); |
| 169 messenger_.reset(); | 171 messenger_.reset(); |
| 170 FindConnection(); | 172 FindConnection(); |
| 171 } | 173 } |
| 172 | 174 |
| 173 } // namespace proximity_auth | 175 } // namespace proximity_auth |
| OLD | NEW |