Commit 3f45fd54 authored by msarda's avatar msarda Committed by Commit bot

Unlock Chrome when a phone with Smart Lock service is found.

This CL starts finding BLE phone devices that advertise smart
lock services when the screen is locked. It unlocks the
Chrome when such a device is found.

BUG=478073

Review URL: https://codereview.chromium.org/1102473003

Cr-Commit-Position: refs/heads/master@{#327416}
parent 3d79357f
...@@ -640,9 +640,11 @@ void EasyUnlockService::UpdateAppState() { ...@@ -640,9 +640,11 @@ void EasyUnlockService::UpdateAppState() {
if (base::CommandLine::ForCurrentProcess()->HasSwitch( if (base::CommandLine::ForCurrentProcess()->HasSwitch(
proximity_auth::switches::kEnableBluetoothLowEnergyDiscovery) && proximity_auth::switches::kEnableBluetoothLowEnergyDiscovery) &&
GetType() == EasyUnlockService::TYPE_REGULAR &&
!proximity_auth_ble_system_) { !proximity_auth_ble_system_) {
proximity_auth_ble_system_.reset( proximity_auth_ble_system_.reset(
new proximity_auth::ProximityAuthBleSystem()); new proximity_auth::ProximityAuthBleSystem(
GetScreenlockBridgeInstance(), profile_));
} }
#if defined(OS_CHROMEOS) #if defined(OS_CHROMEOS)
......
...@@ -5,6 +5,8 @@ ...@@ -5,6 +5,8 @@
{ {
'targets': [ 'targets': [
{ {
# GN version: //components/proximity_auth and
# //components/proximity_auth/ble.
'target_name': 'proximity_auth', 'target_name': 'proximity_auth',
'type': 'static_library', 'type': 'static_library',
'include_dirs': [ 'include_dirs': [
......
...@@ -154,7 +154,7 @@ bool BluetoothLowEnergyConnectionFinder::HasService( ...@@ -154,7 +154,7 @@ bool BluetoothLowEnergyConnectionFinder::HasService(
void BluetoothLowEnergyConnectionFinder::OnCreateConnectionError( void BluetoothLowEnergyConnectionFinder::OnCreateConnectionError(
BluetoothDevice::ConnectErrorCode error_code) { BluetoothDevice::ConnectErrorCode error_code) {
VLOG(1) << "Error creating connection"; VLOG(1) << "Error creating connection: " << error_code;
} }
void BluetoothLowEnergyConnectionFinder::OnConnectionCreated( void BluetoothLowEnergyConnectionFinder::OnConnectionCreated(
...@@ -162,7 +162,10 @@ void BluetoothLowEnergyConnectionFinder::OnConnectionCreated( ...@@ -162,7 +162,10 @@ void BluetoothLowEnergyConnectionFinder::OnConnectionCreated(
VLOG(1) << "Connection created"; VLOG(1) << "Connection created";
connected_ = true; connected_ = true;
StopDiscoverySession(); StopDiscoverySession();
connection_callback_.Run(connection.Pass()); if (!connection_callback_.is_null()) {
connection_callback_.Run(connection.Pass());
connection_callback_.Reset();
}
} }
void BluetoothLowEnergyConnectionFinder::CreateConnection( void BluetoothLowEnergyConnectionFinder::CreateConnection(
......
...@@ -29,7 +29,7 @@ class BluetoothLowEnergyConnectionFinder ...@@ -29,7 +29,7 @@ class BluetoothLowEnergyConnectionFinder
BluetoothLowEnergyConnectionFinder(const std::string& remote_service_uuid); BluetoothLowEnergyConnectionFinder(const std::string& remote_service_uuid);
~BluetoothLowEnergyConnectionFinder() override; ~BluetoothLowEnergyConnectionFinder() override;
// Finds a connection the remote device, only the first one is functional. // Finds a connection to the remote device. Only the first one is functional.
void Find(const device::BluetoothDevice::GattConnectionCallback& void Find(const device::BluetoothDevice::GattConnectionCallback&
connection_callback); connection_callback);
void Find(const ConnectionCallback& connection_callback) override; void Find(const ConnectionCallback& connection_callback) override;
......
...@@ -6,31 +6,76 @@ ...@@ -6,31 +6,76 @@
#include "base/bind.h" #include "base/bind.h"
#include "base/logging.h" #include "base/logging.h"
#include "base/memory/scoped_ptr.h" #include "components/proximity_auth/ble/bluetooth_low_energy_connection_finder.h"
#include "components/proximity_auth/connection.h" #include "components/proximity_auth/connection.h"
#include "device/bluetooth/bluetooth_device.h" #include "device/bluetooth/bluetooth_device.h"
#include "device/bluetooth/bluetooth_gatt_connection.h"
#include "components/proximity_auth/ble/bluetooth_low_energy_connection_finder.h"
namespace proximity_auth { namespace proximity_auth {
namespace {
// The UUID of the Bluetooth Low Energy service.
const char kSmartLockServiceUUID[] = "b3b7e28e-a000-3e17-bd86-6e97b9e28c11"; const char kSmartLockServiceUUID[] = "b3b7e28e-a000-3e17-bd86-6e97b9e28c11";
void ConnectionCallback( } // namespace
scoped_ptr<device::BluetoothGattConnection> connection) {
VLOG(1) << "Connection established";
}
ProximityAuthBleSystem::ProximityAuthBleSystem() { ProximityAuthBleSystem::ProximityAuthBleSystem(
ScreenlockBridge* screenlock_bridge,
content::BrowserContext* browser_context)
: screenlock_bridge_(screenlock_bridge),
browser_context_(browser_context),
weak_ptr_factory_(this) {
DCHECK(screenlock_bridge_);
DCHECK(browser_context_);
VLOG(1) << "Starting Proximity Auth over Bluetooth Low Energy."; VLOG(1) << "Starting Proximity Auth over Bluetooth Low Energy.";
connection_finder_ = scoped_ptr<BluetoothLowEnergyConnectionFinder>( screenlock_bridge_->AddObserver(this);
new BluetoothLowEnergyConnectionFinder(kSmartLockServiceUUID));
connection_finder_->Find(base::Bind(&ConnectionCallback));
} }
ProximityAuthBleSystem::~ProximityAuthBleSystem() { ProximityAuthBleSystem::~ProximityAuthBleSystem() {
VLOG(1) << "Stopping Proximity over Bluetooth Low Energy."; VLOG(1) << "Stopping Proximity over Bluetooth Low Energy.";
screenlock_bridge_->RemoveObserver(this);
}
void ProximityAuthBleSystem::OnScreenDidLock(
ScreenlockBridge::LockHandler::ScreenType screen_type) {
VLOG(1) << "OnScreenDidLock: " << screen_type;
switch (screen_type) {
case ScreenlockBridge::LockHandler::SIGNIN_SCREEN:
connection_finder_.reset();
break;
case ScreenlockBridge::LockHandler::LOCK_SCREEN:
DCHECK(!connection_finder_);
connection_finder_.reset(
new BluetoothLowEnergyConnectionFinder(kSmartLockServiceUUID));
connection_finder_->Find(
base::Bind(&ProximityAuthBleSystem::OnConnectionFound,
weak_ptr_factory_.GetWeakPtr()));
break;
case ScreenlockBridge::LockHandler::OTHER_SCREEN:
connection_finder_.reset();
break;
}
};
void ProximityAuthBleSystem::OnScreenDidUnlock(
ScreenlockBridge::LockHandler::ScreenType screen_type) {
VLOG(1) << "OnScreenDidUnlock: " << screen_type;
connection_finder_.reset(); connection_finder_.reset();
};
void ProximityAuthBleSystem::OnFocusedUserChanged(const std::string& user_id) {
VLOG(1) << "OnFocusedUserChanged: " << user_id;
};
void ProximityAuthBleSystem::OnConnectionFound(
scoped_ptr<device::BluetoothGattConnection> connection) {
// Unlock the screen when a connection is found.
//
// Note that this magically unlocks Chrome (no user interaction is needed).
// This user experience for this operation will be greately improved once
// the Proximity Auth Unlock Manager migration to C++ is done.
screenlock_bridge_->Unlock(browser_context_);
} }
} // namespace proximity_auth } // namespace proximity_auth
...@@ -7,23 +7,50 @@ ...@@ -7,23 +7,50 @@
#include "base/macros.h" #include "base/macros.h"
#include "base/memory/scoped_ptr.h" #include "base/memory/scoped_ptr.h"
#include "components/proximity_auth/screenlock_bridge.h"
#include "components/proximity_auth/ble/bluetooth_low_energy_connection_finder.h" namespace content {
class BrowserContext;
}
namespace device {
class BluetoothGattConnection;
}
namespace proximity_auth { namespace proximity_auth {
class BluetoothLowEnergyConnectionFinder;
// This is the main entry point to start Proximity Auth over Bluetooth Low // This is the main entry point to start Proximity Auth over Bluetooth Low
// Energy. This is the underlying system for the Smart Lock features. It will // Energy. This is the underlying system for the Smart Lock features. It will
// discover Bluetooth Low Energy phones and unlock the lock screen if the phone // discover Bluetooth Low Energy phones and unlock the lock screen if the phone
// passes an authorization and authentication protocol. // passes an authorization and authentication protocol.
class ProximityAuthBleSystem { class ProximityAuthBleSystem : public ScreenlockBridge::Observer {
public: public:
ProximityAuthBleSystem(); ProximityAuthBleSystem(ScreenlockBridge* screenlock_bridge,
~ProximityAuthBleSystem(); content::BrowserContext* browser_context);
~ProximityAuthBleSystem() override;
// ScreenlockBridge::Observer:
void OnScreenDidLock(
ScreenlockBridge::LockHandler::ScreenType screen_type) override;
void OnScreenDidUnlock(
ScreenlockBridge::LockHandler::ScreenType screen_type) override;
void OnFocusedUserChanged(const std::string& user_id) override;
private: private:
// Handler for a new connection found event.
void OnConnectionFound(
scoped_ptr<device::BluetoothGattConnection> connection);
ScreenlockBridge* screenlock_bridge_; // Not owned. Must outlive this object.
content::BrowserContext*
browser_context_; // Not owned. Must outlive this object.
scoped_ptr<BluetoothLowEnergyConnectionFinder> connection_finder_; scoped_ptr<BluetoothLowEnergyConnectionFinder> connection_finder_;
base::WeakPtrFactory<ProximityAuthBleSystem> weak_ptr_factory_;
DISALLOW_COPY_AND_ASSIGN(ProximityAuthBleSystem); DISALLOW_COPY_AND_ASSIGN(ProximityAuthBleSystem);
}; };
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment