mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-16 07:36:10 -04:00
Migrate to absl::AnyInvocable in NP.
Replaces a bunch of std::function uses with absl::AnyInvocable. PiperOrigin-RevId: 502960326
This commit is contained in:
committed by
Copybara-Service
parent
9a22f71a54
commit
a29be214a3
@@ -43,7 +43,7 @@ BleV2Medium::StartAdvertising(
|
||||
api::ble_v2::AdvertiseParameters advertise_set_parameters,
|
||||
api::ble_v2::BleMedium::AdvertisingCallback callback) {
|
||||
return impl_->StartAdvertising(advertising_data, advertise_set_parameters,
|
||||
callback);
|
||||
std::move(callback));
|
||||
}
|
||||
|
||||
bool BleV2Medium::StartScanning(const Uuid& service_uuid,
|
||||
@@ -111,16 +111,18 @@ BleV2Medium::StartScanning(const Uuid& service_uuid,
|
||||
service_uuid, tx_power_level,
|
||||
api::ble_v2::BleMedium::ScanningCallback{
|
||||
.start_scanning_result =
|
||||
[this, callback](absl::Status status) {
|
||||
[this, start_scanning_result =
|
||||
std::move(callback.start_scanning_result)](
|
||||
absl::Status status) mutable {
|
||||
{
|
||||
MutexLock lock(&mutex_);
|
||||
if (status.ok()) {
|
||||
scanning_enabled_ = true;
|
||||
}
|
||||
}
|
||||
callback.start_scanning_result(status);
|
||||
start_scanning_result(status);
|
||||
},
|
||||
.advertisement_found_cb = callback.advertisement_found_cb,
|
||||
.advertisement_found_cb = std::move(callback.advertisement_found_cb),
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -370,13 +370,14 @@ bool BleMedium::StartScanning(const Uuid& service_uuid, TxPowerLevel tx_power_le
|
||||
central_ = [[GNCMBleCentral alloc] init];
|
||||
}
|
||||
|
||||
__block ScanCallback callback = std::move(scan_callback);
|
||||
[central_ startScanningWithServiceUUID:ObjCStringFromCppString(service_uuid.Get16BitAsString())
|
||||
scanResultHandler:^(NSString* peripheralID, NSData* serviceData) {
|
||||
BleAdvertisementData advertisement_data;
|
||||
advertisement_data.service_data = {{service_uuid, ByteArrayFromNSData(serviceData)}};
|
||||
BlePeripheral& peripheral = adapter_->GetPeripheral();
|
||||
peripheral.SetPeripheralId(CppStringFromObjCString(peripheralID));
|
||||
scan_callback.advertisement_found_cb(peripheral, advertisement_data);
|
||||
callback.advertisement_found_cb(peripheral, advertisement_data);
|
||||
}
|
||||
requestConnectionHandler:^(GNCMBleConnectionRequester connectionRequester) {
|
||||
BlePeripheral& peripheral = adapter_->GetPeripheral();
|
||||
@@ -393,8 +394,7 @@ bool BleMedium::StopScanning() {
|
||||
}
|
||||
|
||||
std::unique_ptr<BleMedium::ScanningSession> BleMedium::StartScanning(
|
||||
const Uuid& service_uuid, TxPowerLevel tx_power_level,
|
||||
BleMedium::ScanningCallback callback) {
|
||||
const Uuid& service_uuid, TxPowerLevel tx_power_level, BleMedium::ScanningCallback callback) {
|
||||
// TODO(hais): add real impl for windows StartScanning.
|
||||
return std::make_unique<ScanningSession>(ScanningSession{});
|
||||
}
|
||||
|
||||
@@ -224,19 +224,19 @@ class GattServer {
|
||||
struct ClientGattConnectionCallback {
|
||||
public:
|
||||
// Called when the client is disconnected from the GATT server.
|
||||
std::function<void()> disconnected_cb = DefaultCallback<>();
|
||||
absl::AnyInvocable<void()> disconnected_cb = []() {};
|
||||
};
|
||||
|
||||
// Callback for asynchronous events on the server side of a GATT connection.
|
||||
struct ServerGattConnectionCallback {
|
||||
// Called when a remote peripheral connected to us and subscribed to one of
|
||||
// our characteristics.
|
||||
std::function<void(const GattCharacteristic& characteristic)>
|
||||
absl::AnyInvocable<void(const GattCharacteristic& characteristic)>
|
||||
characteristic_subscription_cb;
|
||||
|
||||
// Called when a remote peripheral unsubscribed from one of our
|
||||
// characteristics.
|
||||
std::function<void(const GattCharacteristic& characteristic)>
|
||||
absl::AnyInvocable<void(const GattCharacteristic& characteristic)>
|
||||
characteristic_unsubscription_cb;
|
||||
};
|
||||
|
||||
@@ -298,11 +298,11 @@ class BleMedium {
|
||||
AdvertiseParameters advertise_set_parameters) = 0;
|
||||
|
||||
struct AdvertisingCallback {
|
||||
std::function<void(absl::Status)> start_advertising_result;
|
||||
absl::AnyInvocable<void(absl::Status)> start_advertising_result;
|
||||
};
|
||||
|
||||
struct AdvertisingSession {
|
||||
std::function<absl::Status()> stop_advertising;
|
||||
absl::AnyInvocable<absl::Status()> stop_advertising;
|
||||
};
|
||||
|
||||
// Async interface for StartAdertising.
|
||||
@@ -332,10 +332,9 @@ class BleMedium {
|
||||
// The peripheral is owned by platform implementation and it should outlive
|
||||
// for the whole peripheral(device) connection life cycle.
|
||||
struct ScanCallback {
|
||||
std::function<void(BlePeripheral& peripheral,
|
||||
BleAdvertisementData advertisement_data)>
|
||||
advertisement_found_cb =
|
||||
DefaultCallback<BlePeripheral&, BleAdvertisementData>();
|
||||
absl::AnyInvocable<void(BlePeripheral& peripheral,
|
||||
BleAdvertisementData advertisement_data)>
|
||||
advertisement_found_cb = [](BlePeripheral&, BleAdvertisementData) {};
|
||||
};
|
||||
|
||||
// https://developer.android.com/reference/android/bluetooth/le/BluetoothLeScanner.html#startScan(java.util.List%3Candroid.bluetooth.le.ScanFilter%3E,%20android.bluetooth.le.ScanSettings,%20android.bluetooth.le.ScanCallback)
|
||||
@@ -359,16 +358,15 @@ class BleMedium {
|
||||
virtual bool StopScanning() = 0;
|
||||
|
||||
struct ScanningSession {
|
||||
std::function<absl::Status()> stop_scanning;
|
||||
absl::AnyInvocable<absl::Status()> stop_scanning;
|
||||
};
|
||||
|
||||
struct ScanningCallback {
|
||||
std::function<void(absl::Status)> start_scanning_result =
|
||||
DefaultCallback<absl::Status>();
|
||||
std::function<void(BlePeripheral& peripheral,
|
||||
BleAdvertisementData advertisement_data)>
|
||||
advertisement_found_cb =
|
||||
DefaultCallback<BlePeripheral&, BleAdvertisementData>();
|
||||
absl::AnyInvocable<void(absl::Status)> start_scanning_result =
|
||||
[](absl::Status) {};
|
||||
absl::AnyInvocable<void(BlePeripheral& peripheral,
|
||||
BleAdvertisementData advertisement_data)>
|
||||
advertisement_found_cb = [](BlePeripheral&, BleAdvertisementData) {};
|
||||
};
|
||||
|
||||
// Async interface for StartScanning.
|
||||
|
||||
@@ -283,7 +283,8 @@ bool BleV2Medium::StartScanning(const Uuid& service_uuid,
|
||||
absl::MutexLock lock(&mutex_);
|
||||
MediumEnvironment::Instance().UpdateBleV2MediumForScanning(
|
||||
/*enabled=*/true, service_uuid, internal_session_id,
|
||||
{.advertisement_found_cb = callback.advertisement_found_cb}, *this);
|
||||
{.advertisement_found_cb = std::move(callback.advertisement_found_cb)},
|
||||
*this);
|
||||
scanning_internal_session_ids_.insert({service_uuid, internal_session_id});
|
||||
return true;
|
||||
}
|
||||
@@ -310,10 +311,10 @@ std::unique_ptr<BleV2Medium::ScanningSession> BleV2Medium::StartScanning(
|
||||
absl::MutexLock lock(&mutex_);
|
||||
|
||||
MediumEnvironment::Instance().UpdateBleV2MediumForScanning(
|
||||
/*enabled=*/true, service_uuid, internal_session_id, callback, *this);
|
||||
/*enabled=*/true, service_uuid, internal_session_id,
|
||||
std::move(callback), *this);
|
||||
scanning_internal_session_ids_.insert({service_uuid, internal_session_id});
|
||||
}
|
||||
callback.start_scanning_result(absl::OkStatus());
|
||||
return std::make_unique<ScanningSession>(ScanningSession{
|
||||
.stop_scanning =
|
||||
[this, service_uuid = service_uuid,
|
||||
|
||||
@@ -67,7 +67,7 @@ TEST(BleV2Medium, DISABLED_StartScanning) {
|
||||
};
|
||||
|
||||
EXPECT_TRUE(blev2_medium.StartScanning(
|
||||
service_uuid, api::ble_v2::TxPowerLevel::kHigh, callback));
|
||||
service_uuid, api::ble_v2::TxPowerLevel::kHigh, std::move(callback)));
|
||||
|
||||
EXPECT_TRUE(scan_response_notification.WaitForNotificationWithTimeout(
|
||||
absl::Seconds(5)));
|
||||
@@ -85,7 +85,7 @@ TEST(BleV2Medium, DISABLED_StopScanning) {
|
||||
const api::ble_v2::BleAdvertisementData& advertisement_data) {};
|
||||
|
||||
EXPECT_TRUE(blev2_medium.StartScanning(
|
||||
service_uuid, api::ble_v2::TxPowerLevel::kHigh, callback));
|
||||
service_uuid, api::ble_v2::TxPowerLevel::kHigh, std::move(callback)));
|
||||
|
||||
EXPECT_TRUE(blev2_medium.StopScanning());
|
||||
}
|
||||
|
||||
@@ -318,8 +318,7 @@ void MediumEnvironment::OnWifiLanServiceStateChanged(
|
||||
}
|
||||
}
|
||||
|
||||
void MediumEnvironment::RunOnMediumEnvironmentThread(
|
||||
std::function<void()> runnable) {
|
||||
void MediumEnvironment::RunOnMediumEnvironmentThread(Runnable runnable) {
|
||||
job_count_++;
|
||||
executor_.Execute(std::move(runnable));
|
||||
}
|
||||
@@ -582,7 +581,8 @@ void MediumEnvironment::UpdateBleV2MediumForScanning(
|
||||
RunOnMediumEnvironmentThread([this, &medium,
|
||||
scanning_service_uuid = scanning_service_uuid,
|
||||
internal_session_id = internal_session_id,
|
||||
callback = std::move(callback), enabled]() {
|
||||
callback = std::move(callback),
|
||||
enabled]() mutable {
|
||||
auto it = ble_v2_mediums_.find(&medium);
|
||||
if (it == ble_v2_mediums_.end()) {
|
||||
NEARBY_LOGS(INFO)
|
||||
@@ -598,8 +598,8 @@ void MediumEnvironment::UpdateBleV2MediumForScanning(
|
||||
if (enabled) {
|
||||
context.scanning = true;
|
||||
callback.start_scanning_result(absl::OkStatus());
|
||||
context.scan_callback_map.insert(
|
||||
{{scanning_service_uuid, internal_session_id}, std::move(callback)});
|
||||
context.scan_callback_map[{scanning_service_uuid, internal_session_id}] =
|
||||
std::move(callback);
|
||||
absl::flat_hash_set<Uuid> scanning_service_uuids;
|
||||
for (auto& element : context.scan_callback_map) {
|
||||
scanning_service_uuids.insert(element.first.first);
|
||||
@@ -724,7 +724,7 @@ MediumEnvironment::GetBleV2MediumStatus(const api::ble_v2::BleMedium& medium) {
|
||||
latch.CountDown();
|
||||
return;
|
||||
}
|
||||
BleV2MediumContext context = it->second;
|
||||
BleV2MediumContext& context = it->second;
|
||||
|
||||
result = BleV2MediumStatus{.is_advertising = context.advertising,
|
||||
.is_scanning = context.scanning};
|
||||
|
||||
@@ -433,7 +433,7 @@ class MediumEnvironment {
|
||||
const NsdServiceInfo& service_info,
|
||||
bool enabled);
|
||||
|
||||
void RunOnMediumEnvironmentThread(std::function<void()> runnable);
|
||||
void RunOnMediumEnvironmentThread(Runnable runnable);
|
||||
|
||||
std::atomic_bool enabled_ = false;
|
||||
std::atomic_int job_count_ = 0;
|
||||
|
||||
@@ -52,7 +52,6 @@ cc_library(
|
||||
"device_motion.h",
|
||||
"discovery_filter.h",
|
||||
"discovery_options.h",
|
||||
"listeners.h",
|
||||
"power_mode.h",
|
||||
"presence_action.h",
|
||||
"presence_device.h",
|
||||
|
||||
@@ -33,17 +33,19 @@ using ScanSessionId = uint64_t;
|
||||
// empty functions.
|
||||
struct ScanCallback {
|
||||
// Updates client with the result of start scanning.
|
||||
std::function<void(absl::Status)> start_scan_cb = [](absl::Status) {};
|
||||
absl::AnyInvocable<void(absl::Status)> start_scan_cb = [](absl::Status) {};
|
||||
|
||||
// Reports a {@link PresenceDevice} being discovered.
|
||||
std::function<void(PresenceDevice)> on_discovered_cb = [](PresenceDevice) {};
|
||||
absl::AnyInvocable<void(PresenceDevice)> on_discovered_cb =
|
||||
[](PresenceDevice) {};
|
||||
|
||||
// Reports a {@link PresenceDevice} information(distance, and etc)
|
||||
// changed.
|
||||
std::function<void(PresenceDevice)> on_updated_cb = [](PresenceDevice) {};
|
||||
absl::AnyInvocable<void(PresenceDevice)> on_updated_cb = [](PresenceDevice) {
|
||||
};
|
||||
|
||||
// Reports a {@link PresenceDevice} is no longer within range.
|
||||
std::function<void(PresenceDevice)> on_lost_cb = [](PresenceDevice) {};
|
||||
absl::AnyInvocable<void(PresenceDevice)> on_lost_cb = [](PresenceDevice) {};
|
||||
};
|
||||
|
||||
// Unique Broadcast Session Identifier.
|
||||
@@ -53,7 +55,8 @@ using BroadcastSessionId = uint64_t;
|
||||
// don't need these signal updates, they can skip with the provided default
|
||||
// empty functions.
|
||||
struct BroadcastCallback {
|
||||
std::function<void(absl::Status)> start_broadcast_cb = [](absl::Status) {};
|
||||
absl::AnyInvocable<void(absl::Status)> start_broadcast_cb = [](absl::Status) {
|
||||
};
|
||||
};
|
||||
|
||||
} // namespace presence
|
||||
|
||||
@@ -45,12 +45,12 @@ absl::StatusOr<BroadcastSessionId> BroadcastManager::StartBroadcast(
|
||||
RunOnServiceControllerThread(
|
||||
"start-broadcast",
|
||||
[this, id, power_mode = broadcast_request.power_mode, request = *request,
|
||||
broadcast_callback = std::move(callback)]()
|
||||
ABSL_EXCLUSIVE_LOCKS_REQUIRED(*executor_) {
|
||||
sessions_.insert(
|
||||
{id, BroadcastSessionState(broadcast_callback, power_mode)});
|
||||
FetchCredentials(id, std::move(request));
|
||||
});
|
||||
broadcast_callback = std::move(
|
||||
callback)]() ABSL_EXCLUSIVE_LOCKS_REQUIRED(*executor_) mutable {
|
||||
sessions_.insert({id, BroadcastSessionState(
|
||||
std::move(broadcast_callback), power_mode)});
|
||||
FetchCredentials(id, std::move(request));
|
||||
});
|
||||
return id;
|
||||
}
|
||||
|
||||
|
||||
@@ -61,7 +61,8 @@ class BroadcastManager {
|
||||
public:
|
||||
explicit BroadcastSessionState(BroadcastCallback broadcast_callback,
|
||||
PowerMode power_mode)
|
||||
: broadcast_callback_(broadcast_callback), power_mode_(power_mode) {}
|
||||
: broadcast_callback_(std::move(broadcast_callback)),
|
||||
power_mode_(power_mode) {}
|
||||
|
||||
void SetAdvertisingSession(std::unique_ptr<AdvertisingSession> session);
|
||||
void CallStartedCallback(absl::Status status);
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
#include "internal/platform/ble_v2.h"
|
||||
#include "internal/platform/bluetooth_adapter.h"
|
||||
@@ -70,7 +71,7 @@ class Ble {
|
||||
.is_connectable = true,
|
||||
};
|
||||
return medium_.StartAdvertising(advertising_data, advertise_set_parameters,
|
||||
callback);
|
||||
std::move(callback));
|
||||
}
|
||||
|
||||
// Starts scanning for NP advertisements. The caller should use the returned
|
||||
@@ -79,7 +80,8 @@ class Ble {
|
||||
ScanningCallback callback) {
|
||||
return medium_.StartScanning(
|
||||
kPresenceServiceUuid,
|
||||
ConvertPowerModeToPowerLevel(scan_request.power_mode), callback);
|
||||
ConvertPowerModeToPowerLevel(scan_request.power_mode),
|
||||
std::move(callback));
|
||||
}
|
||||
|
||||
// Provides access to platform implementation. It's used in tests.
|
||||
|
||||
@@ -48,34 +48,37 @@ ScanSessionId ScanManager::StartScan(ScanRequest scan_request,
|
||||
ScanCallback cb) {
|
||||
ScanSessionId id = ::crypto::RandData<ScanSessionId>();
|
||||
RunOnServiceControllerThread(
|
||||
"start-scan", [this, id, scan_request,
|
||||
scan_callback = std::move(
|
||||
cb)]() ABSL_EXCLUSIVE_LOCKS_REQUIRED(*executor_) {
|
||||
ScanningCallback callback = ScanningCallback{
|
||||
.start_scanning_result =
|
||||
[start_scan_client = std::move(scan_callback.start_scan_cb)](
|
||||
absl::Status ble_status) { start_scan_client(ble_status); },
|
||||
// TODO(b/256686710): Track known devices
|
||||
.advertisement_found_cb =
|
||||
[this, id](BlePeripheral& peripheral,
|
||||
BleAdvertisementData data) {
|
||||
RunOnServiceControllerThread(
|
||||
"notify-found-ble",
|
||||
[this, id, data = std::move(data),
|
||||
address = peripheral.GetAddress()]()
|
||||
ABSL_EXCLUSIVE_LOCKS_REQUIRED(*executor_) {
|
||||
NotifyFoundBle(id, data, address);
|
||||
});
|
||||
}};
|
||||
FetchCredentials(id, scan_request);
|
||||
scan_sessions_.insert(
|
||||
{id, ScanSessionState{
|
||||
.request = scan_request,
|
||||
.callback = std::move(scan_callback),
|
||||
.decoder = AdvertisementDecoder(scan_request),
|
||||
.scanning_session = mediums_->GetBle().StartScanning(
|
||||
scan_request, std::move(callback))}});
|
||||
});
|
||||
"start-scan",
|
||||
[this, id, scan_request, scan_callback = std::move(cb)]()
|
||||
ABSL_EXCLUSIVE_LOCKS_REQUIRED(*executor_) mutable {
|
||||
ScanningCallback callback = ScanningCallback{
|
||||
.start_scanning_result =
|
||||
[start_scan_client =
|
||||
std::move(scan_callback.start_scan_cb)](
|
||||
absl::Status ble_status) mutable {
|
||||
start_scan_client(ble_status);
|
||||
},
|
||||
// TODO(b/256686710): Track known devices
|
||||
.advertisement_found_cb =
|
||||
[this, id](BlePeripheral& peripheral,
|
||||
BleAdvertisementData data) {
|
||||
RunOnServiceControllerThread(
|
||||
"notify-found-ble",
|
||||
[this, id, data = std::move(data),
|
||||
address = peripheral.GetAddress()]()
|
||||
ABSL_EXCLUSIVE_LOCKS_REQUIRED(*executor_) {
|
||||
NotifyFoundBle(id, data, address);
|
||||
});
|
||||
}};
|
||||
FetchCredentials(id, scan_request);
|
||||
scan_sessions_.insert(
|
||||
{id, ScanSessionState{
|
||||
.request = scan_request,
|
||||
.callback = std::move(scan_callback),
|
||||
.decoder = AdvertisementDecoder(scan_request),
|
||||
.scanning_session = mediums_->GetBle().StartScanning(
|
||||
scan_request, std::move(callback))}});
|
||||
});
|
||||
return id;
|
||||
}
|
||||
|
||||
|
||||
@@ -206,7 +206,8 @@ TEST_F(ScanManagerTest, PresenceDeviceMetadataIsRetained) {
|
||||
// Start scanning
|
||||
ScanRequest scan_request_no_filter = MakeDefaultScanRequest();
|
||||
scan_request_no_filter.scan_filters.clear();
|
||||
auto scan_session = manager.StartScan(scan_request_no_filter, callback);
|
||||
auto scan_session =
|
||||
manager.StartScan(scan_request_no_filter, std::move(callback));
|
||||
|
||||
ASSERT_EQ(manager.ScanningCallbacksLengthForTest(), 1);
|
||||
ASSERT_TRUE(mediums.GetBle().IsAvailable());
|
||||
|
||||
@@ -14,6 +14,8 @@
|
||||
|
||||
#include "presence/implementation/service_controller_impl.h"
|
||||
|
||||
#include <utility>
|
||||
|
||||
#include "absl/status/statusor.h"
|
||||
#include "presence/data_types.h"
|
||||
|
||||
@@ -22,7 +24,7 @@ namespace presence {
|
||||
|
||||
absl::StatusOr<ScanSessionId> ServiceControllerImpl::StartScan(
|
||||
ScanRequest scan_request, ScanCallback callback) {
|
||||
return scan_manager_.StartScan(scan_request, callback);
|
||||
return scan_manager_.StartScan(scan_request, std::move(callback));
|
||||
}
|
||||
void ServiceControllerImpl::StopScan(ScanSessionId id) {
|
||||
scan_manager_.StopScan(id);
|
||||
@@ -30,7 +32,8 @@ void ServiceControllerImpl::StopScan(ScanSessionId id) {
|
||||
|
||||
absl::StatusOr<BroadcastSessionId> ServiceControllerImpl::StartBroadcast(
|
||||
BroadcastRequest broadcast_request, BroadcastCallback callback) {
|
||||
return broadcast_manager_.StartBroadcast(broadcast_request, callback);
|
||||
return broadcast_manager_.StartBroadcast(broadcast_request,
|
||||
std::move(callback));
|
||||
}
|
||||
|
||||
void ServiceControllerImpl::StopBroadcast(BroadcastSessionId id) {
|
||||
|
||||
@@ -1,39 +0,0 @@
|
||||
// Copyright 2020 Google LLC
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// https://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#ifndef THIRD_PARTY_NEARBY_PRESENCE_LISTENERS_H_
|
||||
#define THIRD_PARTY_NEARBY_PRESENCE_LISTENERS_H_
|
||||
|
||||
#include <functional>
|
||||
|
||||
#include "absl/status/status.h"
|
||||
#include "internal/platform/listeners.h"
|
||||
|
||||
namespace nearby {
|
||||
namespace presence {
|
||||
|
||||
// Common callback for asynchronously invoked methods.
|
||||
// Called after a job scheduled for execution is completed.
|
||||
struct ResultCallback {
|
||||
// Callback to access the status of the operation when available.
|
||||
// status - result of job execution;
|
||||
// Status::kSuccess, if successful; anything else indicates failure.
|
||||
std::function<void(absl::Status)> result_cb =
|
||||
nearby::DefaultCallback<absl::Status>();
|
||||
};
|
||||
|
||||
} // namespace presence
|
||||
} // namespace nearby
|
||||
|
||||
#endif // THIRD_PARTY_NEARBY_PRESENCE_LISTENERS_H_
|
||||
@@ -33,7 +33,7 @@ absl::StatusOr<ScanSessionId> PresenceClient::StartScan(
|
||||
return absl::FailedPreconditionError(
|
||||
"Can't start scan, presence service is gone");
|
||||
}
|
||||
return (*borrowed)->StartScan(scan_request, callback);
|
||||
return (*borrowed)->StartScan(scan_request, std::move(callback));
|
||||
}
|
||||
|
||||
void PresenceClient::StopScan(ScanSessionId id) {
|
||||
@@ -50,7 +50,7 @@ absl::StatusOr<BroadcastSessionId> PresenceClient::StartBroadcast(
|
||||
return absl::FailedPreconditionError(
|
||||
"Can't start broadcast, presence service is gone");
|
||||
}
|
||||
return (*borrowed)->StartBroadcast(broadcast_request, callback);
|
||||
return (*borrowed)->StartBroadcast(broadcast_request, std::move(callback));
|
||||
}
|
||||
|
||||
void PresenceClient::StopBroadcast(BroadcastSessionId session_id) {
|
||||
|
||||
@@ -43,14 +43,14 @@ class PresenceClientTest : public testing::Test {
|
||||
TEST_F(PresenceClientTest, StartBroadcastWithDefaultConstructor) {
|
||||
env_.Start();
|
||||
absl::Status broadcast_result;
|
||||
BroadcastCallback broadcast_callback = {
|
||||
.start_broadcast_cb =
|
||||
[&](absl::Status status) { broadcast_result = status; },
|
||||
};
|
||||
|
||||
PresenceService presence_service;
|
||||
PresenceClient presence_client = presence_service.CreatePresenceClient();
|
||||
auto unused = presence_client.StartBroadcast({}, broadcast_callback);
|
||||
auto unused = presence_client.StartBroadcast(
|
||||
{}, {
|
||||
.start_broadcast_cb =
|
||||
[&](absl::Status status) { broadcast_result = status; },
|
||||
});
|
||||
|
||||
EXPECT_THAT(broadcast_result, StatusIs(absl::StatusCode::kInvalidArgument));
|
||||
env_.Stop();
|
||||
@@ -59,13 +59,13 @@ TEST_F(PresenceClientTest, StartBroadcastWithDefaultConstructor) {
|
||||
TEST_F(PresenceClientTest, StartBroadcastFailsWhenPresenceServiceIsGone) {
|
||||
env_.Start();
|
||||
absl::Status broadcast_result = absl::UnknownError("");
|
||||
BroadcastCallback broadcast_callback = {
|
||||
.start_broadcast_cb =
|
||||
[&](absl::Status status) { broadcast_result = status; },
|
||||
};
|
||||
|
||||
absl::StatusOr<BroadcastSessionId> session_id =
|
||||
CreateDefunctPresenceClient().StartBroadcast({}, broadcast_callback);
|
||||
CreateDefunctPresenceClient().StartBroadcast(
|
||||
{}, {
|
||||
.start_broadcast_cb =
|
||||
[&](absl::Status status) { broadcast_result = status; },
|
||||
});
|
||||
|
||||
EXPECT_THAT(session_id, StatusIs(absl::StatusCode::kFailedPrecondition));
|
||||
EXPECT_THAT(broadcast_result, StatusIs(absl::StatusCode::kUnknown));
|
||||
@@ -81,7 +81,7 @@ TEST_F(PresenceClientTest, StartScanWithDefaultConstructor) {
|
||||
|
||||
PresenceService presence_service;
|
||||
PresenceClient presence_client = presence_service.CreatePresenceClient();
|
||||
EXPECT_OK(presence_client.StartScan({}, scan_callback));
|
||||
EXPECT_OK(presence_client.StartScan({}, std::move(scan_callback)));
|
||||
|
||||
EXPECT_TRUE(scan_result.Get().ok());
|
||||
EXPECT_OK(scan_result.Get().GetResult());
|
||||
@@ -91,12 +91,13 @@ TEST_F(PresenceClientTest, StartScanWithDefaultConstructor) {
|
||||
TEST_F(PresenceClientTest, StartScanFailsWhenPresenceServiceIsGone) {
|
||||
env_.Start();
|
||||
absl::Status scan_result = absl::UnknownError("");
|
||||
ScanCallback scan_callback = {
|
||||
.start_scan_cb = [&](absl::Status status) { scan_result = status; },
|
||||
};
|
||||
|
||||
absl::StatusOr<ScanSessionId> session_id =
|
||||
CreateDefunctPresenceClient().StartScan({}, scan_callback);
|
||||
CreateDefunctPresenceClient().StartScan(
|
||||
{}, {
|
||||
.start_scan_cb =
|
||||
[&](absl::Status status) { scan_result = status; },
|
||||
});
|
||||
|
||||
EXPECT_THAT(session_id, StatusIs(absl::StatusCode::kFailedPrecondition));
|
||||
EXPECT_THAT(scan_result, StatusIs(absl::StatusCode::kUnknown));
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
#include "presence/presence_service.h"
|
||||
|
||||
#include <memory>
|
||||
#include <utility>
|
||||
|
||||
#include "internal/platform/borrowable.h"
|
||||
#include "presence/data_types.h"
|
||||
@@ -32,7 +33,7 @@ PresenceClient PresenceService::CreatePresenceClient() {
|
||||
|
||||
absl::StatusOr<ScanSessionId> PresenceService::StartScan(
|
||||
ScanRequest scan_request, ScanCallback callback) {
|
||||
return service_controller_->StartScan(scan_request, callback);
|
||||
return service_controller_->StartScan(scan_request, std::move(callback));
|
||||
}
|
||||
void PresenceService::StopScan(ScanSessionId id) {
|
||||
service_controller_->StopScan(id);
|
||||
@@ -40,7 +41,8 @@ void PresenceService::StopScan(ScanSessionId id) {
|
||||
|
||||
absl::StatusOr<BroadcastSessionId> PresenceService::StartBroadcast(
|
||||
BroadcastRequest broadcast_request, BroadcastCallback callback) {
|
||||
return service_controller_->StartBroadcast(broadcast_request, callback);
|
||||
return service_controller_->StartBroadcast(broadcast_request,
|
||||
std::move(callback));
|
||||
}
|
||||
|
||||
void PresenceService::StopBroadcast(BroadcastSessionId session) {
|
||||
|
||||
Reference in New Issue
Block a user