| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 #ifndef CHROMEOS_COMPONENTS_TETHER_BLE_ADVERTISER_H_ | 5 #ifndef CHROMEOS_COMPONENTS_TETHER_BLE_ADVERTISER_H_ |
| 6 #define CHROMEOS_COMPONENTS_TETHER_BLE_ADVERTISER_H_ | 6 #define CHROMEOS_COMPONENTS_TETHER_BLE_ADVERTISER_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 | 9 |
| 10 #include "base/macros.h" | 10 #include "base/macros.h" |
| 11 | 11 |
| 12 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
| 13 #include "base/memory/weak_ptr.h" | 13 #include "base/memory/weak_ptr.h" |
| 14 #include "components/cryptauth/eid_generator.h" | 14 #include "components/cryptauth/eid_generator.h" |
| 15 #include "components/cryptauth/remote_device.h" | 15 #include "components/cryptauth/remote_device.h" |
| 16 #include "device/bluetooth/bluetooth_adapter.h" | 16 #include "device/bluetooth/bluetooth_adapter.h" |
| 17 #include "device/bluetooth/bluetooth_adapter_factory.h" | 17 #include "device/bluetooth/bluetooth_adapter_factory.h" |
| 18 #include "device/bluetooth/bluetooth_advertisement.h" | 18 #include "device/bluetooth/bluetooth_advertisement.h" |
| 19 | 19 |
| 20 namespace cryptauth { | 20 namespace cryptauth { |
| 21 class RemoteBeaconSeedFetcher; | 21 class RemoteBeaconSeedFetcher; |
| 22 } | 22 } |
| 23 | 23 |
| 24 namespace chromeos { | 24 namespace chromeos { |
| 25 | 25 |
| 26 namespace tether { | 26 namespace tether { |
| 27 | 27 |
| 28 class LocalDeviceDataProvider; | 28 class LocalDeviceDataProvider; |
| 29 | 29 |
| 30 // Advertises to a given device. When StartAdvertisingToDevice() is called, a |
| 31 // device-specific EID value is computed deterministically and is set as the |
| 32 // service data of the advertisement. If that device is nearby and scanning, |
| 33 // the device will have the same service data and will be able to pick up the |
| 34 // advertisement. |
| 30 class BleAdvertiser { | 35 class BleAdvertiser { |
| 31 public: | 36 public: |
| 32 BleAdvertiser( | 37 BleAdvertiser( |
| 33 scoped_refptr<device::BluetoothAdapter> adapter, | 38 scoped_refptr<device::BluetoothAdapter> adapter, |
| 34 const LocalDeviceDataProvider* local_device_data_provider, | 39 const LocalDeviceDataProvider* local_device_data_provider, |
| 35 const cryptauth::RemoteBeaconSeedFetcher* remote_beacon_seed_fetcher); | 40 const cryptauth::RemoteBeaconSeedFetcher* remote_beacon_seed_fetcher); |
| 36 virtual ~BleAdvertiser(); | 41 virtual ~BleAdvertiser(); |
| 37 | 42 |
| 38 virtual bool StartAdvertisingToDevice( | 43 virtual bool StartAdvertisingToDevice( |
| 39 const cryptauth::RemoteDevice& remote_device); | 44 const cryptauth::RemoteDevice& remote_device); |
| 40 virtual bool StopAdvertisingToDevice( | 45 virtual bool StopAdvertisingToDevice( |
| 41 const cryptauth::RemoteDevice& remote_device); | 46 const cryptauth::RemoteDevice& remote_device); |
| 42 | 47 |
| 43 private: | 48 private: |
| 44 friend class BleAdvertiserTest; | 49 friend class BleAdvertiserTest; |
| 45 | 50 |
| 51 class BleAdvertisementUnregisterHandler { |
| 52 public: |
| 53 virtual void OnAdvertisementUnregisterSuccess() = 0; |
| 54 virtual void OnAdvertisementUnregisterFailure( |
| 55 device::BluetoothAdvertisement::ErrorCode error_code) = 0; |
| 56 }; |
| 57 |
| 58 class BleAdvertisementUnregisterHandlerImpl |
| 59 : public BleAdvertiser::BleAdvertisementUnregisterHandler { |
| 60 public: |
| 61 void OnAdvertisementUnregisterSuccess() override; |
| 62 void OnAdvertisementUnregisterFailure( |
| 63 device::BluetoothAdvertisement::ErrorCode error_code) override; |
| 64 }; |
| 65 |
| 46 class IndividualAdvertisement | 66 class IndividualAdvertisement |
| 47 : public device::BluetoothAdapter::Observer, | 67 : public device::BluetoothAdapter::Observer, |
| 48 public device::BluetoothAdvertisement::Observer, | 68 public device::BluetoothAdvertisement::Observer, |
| 49 public base::RefCounted<IndividualAdvertisement> { | 69 public base::RefCounted<IndividualAdvertisement> { |
| 50 public: | 70 public: |
| 51 IndividualAdvertisement( | 71 IndividualAdvertisement( |
| 52 scoped_refptr<device::BluetoothAdapter> adapter, | 72 scoped_refptr<device::BluetoothAdapter> adapter, |
| 53 std::unique_ptr<cryptauth::EidGenerator::DataWithTimestamp> | 73 std::unique_ptr<cryptauth::EidGenerator::DataWithTimestamp> |
| 54 advertisement_data); | 74 advertisement_data, |
| 75 std::shared_ptr<BleAdvertisementUnregisterHandler> unregister_handler); |
| 55 | 76 |
| 56 // device::BluetoothAdapter::Observer | 77 // device::BluetoothAdapter::Observer |
| 57 void AdapterPoweredChanged(device::BluetoothAdapter* adapter, | 78 void AdapterPoweredChanged(device::BluetoothAdapter* adapter, |
| 58 bool powered) override; | 79 bool powered) override; |
| 59 | 80 |
| 60 // device::BluetoothAdvertisement::Observer | 81 // device::BluetoothAdvertisement::Observer |
| 61 void AdvertisementReleased( | 82 void AdvertisementReleased( |
| 62 device::BluetoothAdvertisement* advertisement) override; | 83 device::BluetoothAdvertisement* advertisement) override; |
| 63 | 84 |
| 64 private: | 85 private: |
| (...skipping 13 matching lines...) Expand all Loading... |
| 78 | 99 |
| 79 std::unique_ptr<device::BluetoothAdvertisement::ServiceData> | 100 std::unique_ptr<device::BluetoothAdvertisement::ServiceData> |
| 80 CreateServiceData() const; | 101 CreateServiceData() const; |
| 81 | 102 |
| 82 std::string ServiceDataInHex() const; | 103 std::string ServiceDataInHex() const; |
| 83 | 104 |
| 84 scoped_refptr<device::BluetoothAdapter> adapter_; | 105 scoped_refptr<device::BluetoothAdapter> adapter_; |
| 85 bool is_initializing_advertising_; | 106 bool is_initializing_advertising_; |
| 86 std::unique_ptr<cryptauth::EidGenerator::DataWithTimestamp> | 107 std::unique_ptr<cryptauth::EidGenerator::DataWithTimestamp> |
| 87 advertisement_data_; | 108 advertisement_data_; |
| 109 std::shared_ptr<BleAdvertisementUnregisterHandler> unregister_handler_; |
| 88 scoped_refptr<device::BluetoothAdvertisement> advertisement_; | 110 scoped_refptr<device::BluetoothAdvertisement> advertisement_; |
| 89 | 111 |
| 90 base::WeakPtrFactory<IndividualAdvertisement> weak_ptr_factory_; | 112 base::WeakPtrFactory<IndividualAdvertisement> weak_ptr_factory_; |
| 91 | 113 |
| 92 DISALLOW_COPY_AND_ASSIGN(IndividualAdvertisement); | 114 DISALLOW_COPY_AND_ASSIGN(IndividualAdvertisement); |
| 93 }; | 115 }; |
| 94 | 116 |
| 95 BleAdvertiser( | 117 BleAdvertiser( |
| 96 scoped_refptr<device::BluetoothAdapter> adapter, | 118 scoped_refptr<device::BluetoothAdapter> adapter, |
| 119 std::unique_ptr<BleAdvertisementUnregisterHandler> unregister_handler, |
| 97 const cryptauth::EidGenerator* eid_generator, | 120 const cryptauth::EidGenerator* eid_generator, |
| 98 const cryptauth::RemoteBeaconSeedFetcher* remote_beacon_seed_fetcher, | 121 const cryptauth::RemoteBeaconSeedFetcher* remote_beacon_seed_fetcher, |
| 99 const LocalDeviceDataProvider* local_device_data_provider); | 122 const LocalDeviceDataProvider* local_device_data_provider); |
| 100 | 123 |
| 101 scoped_refptr<device::BluetoothAdapter> adapter_; | 124 scoped_refptr<device::BluetoothAdapter> adapter_; |
| 125 std::shared_ptr<BleAdvertisementUnregisterHandler> unregister_handler_; |
| 102 | 126 |
| 103 // Not owned by this instance and must outlive it. | 127 // Not owned by this instance and must outlive it. |
| 104 const cryptauth::EidGenerator* eid_generator_; | 128 const cryptauth::EidGenerator* eid_generator_; |
| 105 const cryptauth::RemoteBeaconSeedFetcher* remote_beacon_seed_fetcher_; | 129 const cryptauth::RemoteBeaconSeedFetcher* remote_beacon_seed_fetcher_; |
| 106 const LocalDeviceDataProvider* local_device_data_provider_; | 130 const LocalDeviceDataProvider* local_device_data_provider_; |
| 107 | 131 |
| 108 std::map<std::string, scoped_refptr<IndividualAdvertisement>> | 132 std::map<std::string, scoped_refptr<IndividualAdvertisement>> |
| 109 device_id_to_advertisement_map_; | 133 device_id_to_advertisement_map_; |
| 110 | 134 |
| 111 DISALLOW_COPY_AND_ASSIGN(BleAdvertiser); | 135 DISALLOW_COPY_AND_ASSIGN(BleAdvertiser); |
| 112 }; | 136 }; |
| 113 | 137 |
| 114 } // namespace tether | 138 } // namespace tether |
| 115 | 139 |
| 116 } // namespace chromeos | 140 } // namespace chromeos |
| 117 | 141 |
| 118 #endif // CHROMEOS_COMPONENTS_TETHER_BLE_ADVERTISER_H_ | 142 #endif // CHROMEOS_COMPONENTS_TETHER_BLE_ADVERTISER_H_ |
| OLD | NEW |