| 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 "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/location.h" | 8 #include "base/location.h" |
| 9 #include "base/thread_task_runner_handle.h" | 9 #include "base/thread_task_runner_handle.h" |
| 10 #include "base/time/default_tick_clock.h" | 10 #include "base/time/default_tick_clock.h" |
| (...skipping 25 matching lines...) Expand all Loading... |
| 36 const int kAuthenticationRecoveryTimeSeconds = 10; | 36 const int kAuthenticationRecoveryTimeSeconds = 10; |
| 37 | 37 |
| 38 } // namespace | 38 } // namespace |
| 39 | 39 |
| 40 RemoteDeviceLifeCycleImpl::RemoteDeviceLifeCycleImpl( | 40 RemoteDeviceLifeCycleImpl::RemoteDeviceLifeCycleImpl( |
| 41 const RemoteDevice& remote_device, | 41 const RemoteDevice& remote_device, |
| 42 ProximityAuthClient* proximity_auth_client) | 42 ProximityAuthClient* proximity_auth_client) |
| 43 : remote_device_(remote_device), | 43 : remote_device_(remote_device), |
| 44 proximity_auth_client_(proximity_auth_client), | 44 proximity_auth_client_(proximity_auth_client), |
| 45 state_(RemoteDeviceLifeCycle::State::STOPPED), | 45 state_(RemoteDeviceLifeCycle::State::STOPPED), |
| 46 observers_(base::ObserverList<Observer>::NOTIFY_EXISTING_ONLY), |
| 46 bluetooth_throttler_(new BluetoothThrottlerImpl( | 47 bluetooth_throttler_(new BluetoothThrottlerImpl( |
| 47 make_scoped_ptr(new base::DefaultTickClock()))), | 48 make_scoped_ptr(new base::DefaultTickClock()))), |
| 48 weak_ptr_factory_(this) {} | 49 weak_ptr_factory_(this) {} |
| 49 | 50 |
| 50 RemoteDeviceLifeCycleImpl::~RemoteDeviceLifeCycleImpl() {} | 51 RemoteDeviceLifeCycleImpl::~RemoteDeviceLifeCycleImpl() {} |
| 51 | 52 |
| 52 void RemoteDeviceLifeCycleImpl::Start() { | 53 void RemoteDeviceLifeCycleImpl::Start() { |
| 53 PA_LOG(INFO) << "Life cycle started."; | 54 PA_LOG(INFO) << "Life cycle started."; |
| 54 DCHECK(state_ == RemoteDeviceLifeCycle::State::STOPPED); | 55 DCHECK(state_ == RemoteDeviceLifeCycle::State::STOPPED); |
| 55 FindConnection(); | 56 FindConnection(); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 66 void RemoteDeviceLifeCycleImpl::AddObserver(Observer* observer) { | 67 void RemoteDeviceLifeCycleImpl::AddObserver(Observer* observer) { |
| 67 observers_.AddObserver(observer); | 68 observers_.AddObserver(observer); |
| 68 } | 69 } |
| 69 | 70 |
| 70 void RemoteDeviceLifeCycleImpl::RemoveObserver(Observer* observer) { | 71 void RemoteDeviceLifeCycleImpl::RemoveObserver(Observer* observer) { |
| 71 observers_.RemoveObserver(observer); | 72 observers_.RemoveObserver(observer); |
| 72 } | 73 } |
| 73 | 74 |
| 74 scoped_ptr<ConnectionFinder> | 75 scoped_ptr<ConnectionFinder> |
| 75 RemoteDeviceLifeCycleImpl::CreateConnectionFinder() { | 76 RemoteDeviceLifeCycleImpl::CreateConnectionFinder() { |
| 76 // TODO(tengs): We should not rely on the assumption that an empty Bluetooth | 77 if (remote_device_.bluetooth_type == RemoteDevice::BLUETOOTH_LE) { |
| 77 // address means that the device is BLE. | |
| 78 if (remote_device_.bluetooth_address.empty()) { | |
| 79 return make_scoped_ptr(new BluetoothLowEnergyConnectionFinder( | 78 return make_scoped_ptr(new BluetoothLowEnergyConnectionFinder( |
| 80 remote_device_, kBLESmartLockServiceUUID, | 79 remote_device_, kBLESmartLockServiceUUID, |
| 81 BluetoothLowEnergyConnectionFinder::FinderStrategy::FIND_PAIRED_DEVICE, | 80 BluetoothLowEnergyConnectionFinder::FinderStrategy::FIND_PAIRED_DEVICE, |
| 82 nullptr, bluetooth_throttler_.get(), 3)); | 81 nullptr, bluetooth_throttler_.get(), 3)); |
| 83 } else { | 82 } else { |
| 84 return make_scoped_ptr(new BluetoothConnectionFinder( | 83 return make_scoped_ptr(new BluetoothConnectionFinder( |
| 85 remote_device_, device::BluetoothUUID(kClassicBluetoothServiceUUID), | 84 remote_device_, device::BluetoothUUID(kClassicBluetoothServiceUUID), |
| 86 base::TimeDelta::FromSeconds(3))); | 85 base::TimeDelta::FromSeconds(3))); |
| 87 } | 86 } |
| 88 } | 87 } |
| (...skipping 30 matching lines...) Expand all Loading... |
| 119 authenticator_->Authenticate( | 118 authenticator_->Authenticate( |
| 120 base::Bind(&RemoteDeviceLifeCycleImpl::OnAuthenticationResult, | 119 base::Bind(&RemoteDeviceLifeCycleImpl::OnAuthenticationResult, |
| 121 weak_ptr_factory_.GetWeakPtr())); | 120 weak_ptr_factory_.GetWeakPtr())); |
| 122 TransitionToState(RemoteDeviceLifeCycle::State::AUTHENTICATING); | 121 TransitionToState(RemoteDeviceLifeCycle::State::AUTHENTICATING); |
| 123 } | 122 } |
| 124 | 123 |
| 125 void RemoteDeviceLifeCycleImpl::OnAuthenticationResult( | 124 void RemoteDeviceLifeCycleImpl::OnAuthenticationResult( |
| 126 Authenticator::Result result, | 125 Authenticator::Result result, |
| 127 scoped_ptr<SecureContext> secure_context) { | 126 scoped_ptr<SecureContext> secure_context) { |
| 128 DCHECK(state_ == RemoteDeviceLifeCycle::State::AUTHENTICATING); | 127 DCHECK(state_ == RemoteDeviceLifeCycle::State::AUTHENTICATING); |
| 128 authenticator_.reset(); |
| 129 if (result != Authenticator::Result::SUCCESS) { | 129 if (result != Authenticator::Result::SUCCESS) { |
| 130 PA_LOG(WARNING) << "Waiting " << kAuthenticationRecoveryTimeSeconds | 130 PA_LOG(WARNING) << "Waiting " << kAuthenticationRecoveryTimeSeconds |
| 131 << " seconds to retry after authentication failure."; | 131 << " seconds to retry after authentication failure."; |
| 132 connection_->Disconnect(); | 132 connection_->Disconnect(); |
| 133 authentication_recovery_timer_.Start( | 133 authentication_recovery_timer_.Start( |
| 134 FROM_HERE, | 134 FROM_HERE, |
| 135 base::TimeDelta::FromSeconds(kAuthenticationRecoveryTimeSeconds), this, | 135 base::TimeDelta::FromSeconds(kAuthenticationRecoveryTimeSeconds), this, |
| 136 &RemoteDeviceLifeCycleImpl::FindConnection); | 136 &RemoteDeviceLifeCycleImpl::FindConnection); |
| 137 TransitionToState(RemoteDeviceLifeCycle::State::AUTHENTICATION_FAILED); | 137 TransitionToState(RemoteDeviceLifeCycle::State::AUTHENTICATION_FAILED); |
| 138 return; | 138 return; |
| 139 } | 139 } |
| 140 | 140 |
| 141 // Create the MessengerImpl asynchronously. |messenger_| registers itself as | 141 // Create the MessengerImpl asynchronously. |messenger_| registers itself as |
| 142 // an | 142 // an observer of |connection_|, so creating it synchronously would trigger |
| 143 // observer of |connection_|, so creating it synchronously would | 143 // |OnSendCompleted()| as an observer call for |messenger_|. |
| 144 // trigger |OnSendComplete()| as an observer call for |messenger_|. | |
| 145 secure_context_ = secure_context.Pass(); | 144 secure_context_ = secure_context.Pass(); |
| 146 base::ThreadTaskRunnerHandle::Get()->PostTask( | 145 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 147 FROM_HERE, base::Bind(&RemoteDeviceLifeCycleImpl::CreateMessenger, | 146 FROM_HERE, base::Bind(&RemoteDeviceLifeCycleImpl::CreateMessenger, |
| 148 weak_ptr_factory_.GetWeakPtr())); | 147 weak_ptr_factory_.GetWeakPtr())); |
| 149 } | 148 } |
| 150 | 149 |
| 151 void RemoteDeviceLifeCycleImpl::CreateMessenger() { | 150 void RemoteDeviceLifeCycleImpl::CreateMessenger() { |
| 152 DCHECK(state_ == RemoteDeviceLifeCycle::State::AUTHENTICATING); | 151 DCHECK(state_ == RemoteDeviceLifeCycle::State::AUTHENTICATING); |
| 153 DCHECK(secure_context_); | 152 DCHECK(secure_context_); |
| 154 messenger_.reset( | 153 messenger_.reset( |
| 155 new MessengerImpl(connection_.Pass(), secure_context_.Pass())); | 154 new MessengerImpl(connection_.Pass(), secure_context_.Pass())); |
| 156 messenger_->AddObserver(this); | 155 messenger_->AddObserver(this); |
| 157 | 156 |
| 158 TransitionToState(RemoteDeviceLifeCycle::State::SECURE_CHANNEL_ESTABLISHED); | 157 TransitionToState(RemoteDeviceLifeCycle::State::SECURE_CHANNEL_ESTABLISHED); |
| 159 } | 158 } |
| 160 | 159 |
| 161 void RemoteDeviceLifeCycleImpl::OnDisconnected() { | 160 void RemoteDeviceLifeCycleImpl::OnDisconnected() { |
| 162 DCHECK(state_ == RemoteDeviceLifeCycle::State::SECURE_CHANNEL_ESTABLISHED); | 161 DCHECK(state_ == RemoteDeviceLifeCycle::State::SECURE_CHANNEL_ESTABLISHED); |
| 162 messenger_.reset(); |
| 163 FindConnection(); | 163 FindConnection(); |
| 164 } | 164 } |
| 165 | 165 |
| 166 } // namespace proximity_auth | 166 } // namespace proximity_auth |
| OLD | NEW |