Convert AsyncDnsResolver to use absl::AnyInvocable
Bug: webrtc:12598
Change-Id: I0950231d6de7cf53116a573dcd97a3cf5514946c
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/318400
Commit-Queue: Harald Alvestrand <hta@webrtc.org>
Reviewed-by: Tomas Gunnarsson <tommi@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#40670}
diff --git a/api/BUILD.gn b/api/BUILD.gn
index b2a7947..f2d10ec 100644
--- a/api/BUILD.gn
+++ b/api/BUILD.gn
@@ -410,6 +410,7 @@
"../rtc_base:socket_address",
"../rtc_base/system:rtc_export",
]
+ absl_deps = [ "//third_party/abseil-cpp/absl/functional:any_invocable" ]
}
rtc_source_set("wrapping_async_dns_resolver") {
diff --git a/api/async_dns_resolver.h b/api/async_dns_resolver.h
index 82d80de..db02cd1 100644
--- a/api/async_dns_resolver.h
+++ b/api/async_dns_resolver.h
@@ -14,6 +14,7 @@
#include <functional>
#include <memory>
+#include "absl/functional/any_invocable.h"
#include "rtc_base/checks.h"
#include "rtc_base/socket_address.h"
#include "rtc_base/system/rtc_export.h"
@@ -63,11 +64,11 @@
// Start address resolution of the hostname in `addr`.
virtual void Start(const rtc::SocketAddress& addr,
- std::function<void()> callback) = 0;
+ absl::AnyInvocable<void()> callback) = 0;
// Start address resolution of the hostname in `addr` matching `family`.
virtual void Start(const rtc::SocketAddress& addr,
int family,
- std::function<void()> callback) = 0;
+ absl::AnyInvocable<void()> callback) = 0;
virtual const AsyncDnsResolverResult& result() const = 0;
};
@@ -83,7 +84,7 @@
// The callback will be called on the sequence that the caller runs on.
virtual std::unique_ptr<webrtc::AsyncDnsResolverInterface> CreateAndResolve(
const rtc::SocketAddress& addr,
- std::function<void()> callback) = 0;
+ absl::AnyInvocable<void()> callback) = 0;
// Creates an AsyncDnsResolver and starts resolving the name to an address
// matching the specified family. The callback will be called when resolution
// is finished. The callback will be called on the sequence that the caller
@@ -91,7 +92,7 @@
virtual std::unique_ptr<webrtc::AsyncDnsResolverInterface> CreateAndResolve(
const rtc::SocketAddress& addr,
int family,
- std::function<void()> callback) = 0;
+ absl::AnyInvocable<void()> callback) = 0;
// Creates an AsyncDnsResolver and does not start it.
// For backwards compatibility, will be deprecated and removed.
// One has to do a separate Start() call on the
diff --git a/api/test/mock_async_dns_resolver.h b/api/test/mock_async_dns_resolver.h
index 81132c9..26127eb 100644
--- a/api/test/mock_async_dns_resolver.h
+++ b/api/test/mock_async_dns_resolver.h
@@ -32,11 +32,13 @@
public:
MOCK_METHOD(void,
Start,
- (const rtc::SocketAddress&, std::function<void()>),
+ (const rtc::SocketAddress&, absl::AnyInvocable<void()>),
(override));
MOCK_METHOD(void,
Start,
- (const rtc::SocketAddress&, int family, std::function<void()>),
+ (const rtc::SocketAddress&,
+ int family,
+ absl::AnyInvocable<void()>),
(override));
MOCK_METHOD(AsyncDnsResolverResult&, result, (), (const, override));
};
@@ -45,11 +47,11 @@
public:
MOCK_METHOD(std::unique_ptr<webrtc::AsyncDnsResolverInterface>,
CreateAndResolve,
- (const rtc::SocketAddress&, std::function<void()>),
+ (const rtc::SocketAddress&, absl::AnyInvocable<void()>),
(override));
MOCK_METHOD(std::unique_ptr<webrtc::AsyncDnsResolverInterface>,
CreateAndResolve,
- (const rtc::SocketAddress&, int, std::function<void()>),
+ (const rtc::SocketAddress&, int, absl::AnyInvocable<void()>),
(override));
MOCK_METHOD(std::unique_ptr<webrtc::AsyncDnsResolverInterface>,
Create,
diff --git a/api/wrapping_async_dns_resolver.h b/api/wrapping_async_dns_resolver.h
index 5155b0f..d07f146 100644
--- a/api/wrapping_async_dns_resolver.h
+++ b/api/wrapping_async_dns_resolver.h
@@ -67,7 +67,7 @@
}
void Start(const rtc::SocketAddress& addr,
- std::function<void()> callback) override {
+ absl::AnyInvocable<void()> callback) override {
RTC_DCHECK_RUN_ON(&sequence_checker_);
PrepareToResolve(std::move(callback));
wrapped_->Start(addr);
@@ -75,7 +75,7 @@
void Start(const rtc::SocketAddress& addr,
int family,
- std::function<void()> callback) override {
+ absl::AnyInvocable<void()> callback) override {
RTC_DCHECK_RUN_ON(&sequence_checker_);
PrepareToResolve(std::move(callback));
wrapped_->Start(addr, family);
@@ -97,7 +97,7 @@
return wrapped_.get();
}
- void PrepareToResolve(std::function<void()> callback) {
+ void PrepareToResolve(absl::AnyInvocable<void()> callback) {
RTC_DCHECK_RUN_ON(&sequence_checker_);
RTC_DCHECK_EQ(State::kNotStarted, state_);
state_ = State::kStarted;
@@ -118,7 +118,7 @@
// The class variables need to be accessed on a single thread.
SequenceChecker sequence_checker_;
- std::function<void()> callback_ RTC_GUARDED_BY(sequence_checker_);
+ absl::AnyInvocable<void()> callback_ RTC_GUARDED_BY(sequence_checker_);
std::unique_ptr<rtc::AsyncResolverInterface> wrapped_
RTC_GUARDED_BY(sequence_checker_);
State state_ RTC_GUARDED_BY(sequence_checker_) = State::kNotStarted;
diff --git a/p2p/base/basic_async_resolver_factory.cc b/p2p/base/basic_async_resolver_factory.cc
index b351121..67c8167 100644
--- a/p2p/base/basic_async_resolver_factory.cc
+++ b/p2p/base/basic_async_resolver_factory.cc
@@ -32,17 +32,19 @@
}
std::unique_ptr<webrtc::AsyncDnsResolverInterface>
-BasicAsyncDnsResolverFactory::CreateAndResolve(const rtc::SocketAddress& addr,
- std::function<void()> callback) {
+BasicAsyncDnsResolverFactory::CreateAndResolve(
+ const rtc::SocketAddress& addr,
+ absl::AnyInvocable<void()> callback) {
std::unique_ptr<webrtc::AsyncDnsResolverInterface> resolver = Create();
resolver->Start(addr, std::move(callback));
return resolver;
}
std::unique_ptr<webrtc::AsyncDnsResolverInterface>
-BasicAsyncDnsResolverFactory::CreateAndResolve(const rtc::SocketAddress& addr,
- int family,
- std::function<void()> callback) {
+BasicAsyncDnsResolverFactory::CreateAndResolve(
+ const rtc::SocketAddress& addr,
+ int family,
+ absl::AnyInvocable<void()> callback) {
std::unique_ptr<webrtc::AsyncDnsResolverInterface> resolver = Create();
resolver->Start(addr, family, std::move(callback));
return resolver;
@@ -56,7 +58,7 @@
std::unique_ptr<webrtc::AsyncDnsResolverInterface>
WrappingAsyncDnsResolverFactory::CreateAndResolve(
const rtc::SocketAddress& addr,
- std::function<void()> callback) {
+ absl::AnyInvocable<void()> callback) {
std::unique_ptr<webrtc::AsyncDnsResolverInterface> resolver = Create();
resolver->Start(addr, std::move(callback));
return resolver;
@@ -66,7 +68,7 @@
WrappingAsyncDnsResolverFactory::CreateAndResolve(
const rtc::SocketAddress& addr,
int family,
- std::function<void()> callback) {
+ absl::AnyInvocable<void()> callback) {
std::unique_ptr<webrtc::AsyncDnsResolverInterface> resolver = Create();
resolver->Start(addr, family, std::move(callback));
return resolver;
diff --git a/p2p/base/basic_async_resolver_factory.h b/p2p/base/basic_async_resolver_factory.h
index 147a71c..9c1af6a 100644
--- a/p2p/base/basic_async_resolver_factory.h
+++ b/p2p/base/basic_async_resolver_factory.h
@@ -34,12 +34,12 @@
std::unique_ptr<webrtc::AsyncDnsResolverInterface> CreateAndResolve(
const rtc::SocketAddress& addr,
- std::function<void()> callback) override;
+ absl::AnyInvocable<void()> callback) override;
std::unique_ptr<webrtc::AsyncDnsResolverInterface> CreateAndResolve(
const rtc::SocketAddress& addr,
int family,
- std::function<void()> callback) override;
+ absl::AnyInvocable<void()> callback) override;
std::unique_ptr<webrtc::AsyncDnsResolverInterface> Create() override;
};
@@ -61,12 +61,12 @@
std::unique_ptr<webrtc::AsyncDnsResolverInterface> CreateAndResolve(
const rtc::SocketAddress& addr,
- std::function<void()> callback) override;
+ absl::AnyInvocable<void()> callback) override;
std::unique_ptr<webrtc::AsyncDnsResolverInterface> CreateAndResolve(
const rtc::SocketAddress& addr,
int family,
- std::function<void()> callback) override;
+ absl::AnyInvocable<void()> callback) override;
std::unique_ptr<webrtc::AsyncDnsResolverInterface> Create() override;
diff --git a/p2p/base/p2p_transport_channel_unittest.cc b/p2p/base/p2p_transport_channel_unittest.cc
index ca8ca8d..e0b837c 100644
--- a/p2p/base/p2p_transport_channel_unittest.cc
+++ b/p2p/base/p2p_transport_channel_unittest.cc
@@ -63,7 +63,6 @@
using ::testing::Contains;
using ::testing::DoAll;
using ::testing::InSequence;
-using ::testing::InvokeArgument;
using ::testing::InvokeWithoutArgs;
using ::testing::MockFunction;
using ::testing::Return;
@@ -203,7 +202,9 @@
ResolverFactoryFixture() {
mock_async_dns_resolver_ = std::make_unique<webrtc::MockAsyncDnsResolver>();
EXPECT_CALL(*mock_async_dns_resolver_, Start(_, _))
- .WillRepeatedly(InvokeArgument<1>());
+ .WillRepeatedly(
+ [](const rtc::SocketAddress& addr,
+ absl::AnyInvocable<void()> callback) { callback(); });
EXPECT_CALL(*mock_async_dns_resolver_, result())
.WillOnce(ReturnRef(mock_async_dns_resolver_result_));
@@ -227,7 +228,10 @@
// This function must be called before Create().
ASSERT_TRUE(!!mock_async_dns_resolver_);
EXPECT_CALL(*mock_async_dns_resolver_, Start(_, _))
- .WillOnce(SaveArg<1>(&saved_callback_));
+ .WillOnce([this](const rtc::SocketAddress& addr,
+ absl::AnyInvocable<void()> callback) {
+ saved_callback_ = std::move(callback);
+ });
}
void FireDelayedResolution() {
// This function must be called after Create().
@@ -238,7 +242,7 @@
private:
std::unique_ptr<webrtc::MockAsyncDnsResolver> mock_async_dns_resolver_;
webrtc::MockAsyncDnsResolverResult mock_async_dns_resolver_result_;
- std::function<void()> saved_callback_;
+ absl::AnyInvocable<void()> saved_callback_;
};
bool HasLocalAddress(const cricket::CandidatePairInterface* pair,
diff --git a/p2p/base/stun_port_unittest.cc b/p2p/base/stun_port_unittest.cc
index eb7d25e..bf51151 100644
--- a/p2p/base/stun_port_unittest.cc
+++ b/p2p/base/stun_port_unittest.cc
@@ -30,7 +30,6 @@
using rtc::SocketAddress;
using ::testing::_;
using ::testing::DoAll;
-using ::testing::InvokeArgument;
using ::testing::Return;
using ::testing::ReturnPointee;
using ::testing::SetArgPointee;
@@ -318,7 +317,9 @@
[](webrtc::MockAsyncDnsResolver* resolver,
webrtc::MockAsyncDnsResolverResult* resolver_result) {
EXPECT_CALL(*resolver, Start(kValidHostnameAddr, /*family=*/AF_INET, _))
- .WillOnce(InvokeArgument<2>());
+ .WillOnce([](const rtc::SocketAddress& addr, int family,
+ absl::AnyInvocable<void()> callback) { callback(); });
+
EXPECT_CALL(*resolver, result)
.WillRepeatedly(ReturnPointee(resolver_result));
EXPECT_CALL(*resolver_result, GetError).WillOnce(Return(0));
@@ -342,7 +343,8 @@
[](webrtc::MockAsyncDnsResolver* resolver,
webrtc::MockAsyncDnsResolverResult* resolver_result) {
EXPECT_CALL(*resolver, Start(kValidHostnameAddr, /*family=*/AF_INET, _))
- .WillOnce(InvokeArgument<2>());
+ .WillOnce([](const rtc::SocketAddress& addr, int family,
+ absl::AnyInvocable<void()> callback) { callback(); });
EXPECT_CALL(*resolver, result)
.WillRepeatedly(ReturnPointee(resolver_result));
EXPECT_CALL(*resolver_result, GetError).WillOnce(Return(0));
@@ -693,7 +695,9 @@
webrtc::MockAsyncDnsResolverResult* resolver_result) {
EXPECT_CALL(*resolver,
Start(kValidHostnameAddr, /*family=*/AF_INET6, _))
- .WillOnce(InvokeArgument<2>());
+ .WillOnce([](const rtc::SocketAddress& addr, int family,
+ absl::AnyInvocable<void()> callback) { callback(); });
+
EXPECT_CALL(*resolver, result)
.WillRepeatedly(ReturnPointee(resolver_result));
EXPECT_CALL(*resolver_result, GetError).WillOnce(Return(0));
@@ -719,7 +723,8 @@
webrtc::MockAsyncDnsResolverResult* resolver_result) {
EXPECT_CALL(*resolver,
Start(kValidHostnameAddr, /*family=*/AF_INET6, _))
- .WillOnce(InvokeArgument<2>());
+ .WillOnce([](const rtc::SocketAddress& addr, int family,
+ absl::AnyInvocable<void()> callback) { callback(); });
EXPECT_CALL(*resolver, result)
.WillRepeatedly(ReturnPointee(resolver_result));
EXPECT_CALL(*resolver_result, GetError).WillOnce(Return(0));
diff --git a/p2p/base/turn_port_unittest.cc b/p2p/base/turn_port_unittest.cc
index c77a8c0..a570fc7 100644
--- a/p2p/base/turn_port_unittest.cc
+++ b/p2p/base/turn_port_unittest.cc
@@ -50,7 +50,6 @@
using ::testing::_;
using ::testing::DoAll;
-using ::testing::InvokeArgument;
using ::testing::Return;
using ::testing::ReturnPointee;
using ::testing::SetArgPointee;
@@ -1913,7 +1912,8 @@
[](webrtc::MockAsyncDnsResolver* resolver,
webrtc::MockAsyncDnsResolverResult* resolver_result) {
EXPECT_CALL(*resolver, Start(kTurnValidAddr, /*family=*/AF_INET, _))
- .WillOnce(InvokeArgument<2>());
+ .WillOnce([](const rtc::SocketAddress& addr, int family,
+ absl::AnyInvocable<void()> callback) { callback(); });
EXPECT_CALL(*resolver, result)
.WillRepeatedly(ReturnPointee(resolver_result));
EXPECT_CALL(*resolver_result, GetError).WillRepeatedly(Return(0));
@@ -1933,7 +1933,8 @@
[](webrtc::MockAsyncDnsResolver* resolver,
webrtc::MockAsyncDnsResolverResult* resolver_result) {
EXPECT_CALL(*resolver, Start(kTurnValidAddr, /*family=*/AF_INET6, _))
- .WillOnce(InvokeArgument<2>());
+ .WillOnce([](const rtc::SocketAddress& addr, int family,
+ absl::AnyInvocable<void()> callback) { callback(); });
EXPECT_CALL(*resolver, result)
.WillRepeatedly(ReturnPointee(resolver_result));
EXPECT_CALL(*resolver_result, GetError).WillRepeatedly(Return(0));
diff --git a/pc/test/integration_test_helpers.h b/pc/test/integration_test_helpers.h
index 740e063..c450ccb 100644
--- a/pc/test/integration_test_helpers.h
+++ b/pc/test/integration_test_helpers.h
@@ -1132,7 +1132,8 @@
remote_async_dns_resolved_addr_ = local_candidate.address();
remote_async_dns_resolved_addr_.SetResolvedIP(resolved_ip);
EXPECT_CALL(*remote_async_dns_resolver_, Start(_, _))
- .WillOnce(InvokeArgument<1>());
+ .WillOnce([](const rtc::SocketAddress& addr,
+ absl::AnyInvocable<void()> callback) { callback(); });
EXPECT_CALL(*remote_async_dns_resolver_, result())
.WillOnce(ReturnRef(remote_async_dns_resolver_result_));
EXPECT_CALL(remote_async_dns_resolver_result_, GetResolvedAddress(_, _))
diff --git a/rtc_base/async_dns_resolver.cc b/rtc_base/async_dns_resolver.cc
index 5abe969..918eeb7 100644
--- a/rtc_base/async_dns_resolver.cc
+++ b/rtc_base/async_dns_resolver.cc
@@ -99,28 +99,28 @@
} // namespace
void AsyncDnsResolver::Start(const rtc::SocketAddress& addr,
- std::function<void()> callback) {
+ absl::AnyInvocable<void()> callback) {
Start(addr, addr.family(), std::move(callback));
}
// Start address resolution of the hostname in `addr` matching `family`.
void AsyncDnsResolver::Start(const rtc::SocketAddress& addr,
int family,
- std::function<void()> callback) {
+ absl::AnyInvocable<void()> callback) {
RTC_DCHECK_RUN_ON(&result_.sequence_checker_);
result_.addr_ = addr;
+ callback_ = std::move(callback);
auto thread_function = [this, addr, family, flag = safety_.flag(),
- caller_task_queue = webrtc::TaskQueueBase::Current(),
- callback = std::move(callback)] {
+ caller_task_queue =
+ webrtc::TaskQueueBase::Current()] {
std::vector<rtc::IPAddress> addresses;
int error = ResolveHostname(addr.hostname(), family, addresses);
caller_task_queue->PostTask(
- SafeTask(flag, [this, error, addresses = std::move(addresses),
- callback = std::move(callback)] {
+ SafeTask(flag, [this, error, addresses = std::move(addresses)] {
RTC_DCHECK_RUN_ON(&result_.sequence_checker_);
result_.addresses_ = addresses;
result_.error_ = error;
- callback();
+ callback_();
}));
};
#if defined(WEBRTC_MAC) || defined(WEBRTC_IOS)
diff --git a/rtc_base/async_dns_resolver.h b/rtc_base/async_dns_resolver.h
index 7511245..f8d60b9 100644
--- a/rtc_base/async_dns_resolver.h
+++ b/rtc_base/async_dns_resolver.h
@@ -40,16 +40,17 @@
public:
// Start address resolution of the hostname in `addr`.
void Start(const rtc::SocketAddress& addr,
- std::function<void()> callback) override;
+ absl::AnyInvocable<void()> callback) override;
// Start address resolution of the hostname in `addr` matching `family`.
void Start(const rtc::SocketAddress& addr,
int family,
- std::function<void()> callback) override;
+ absl::AnyInvocable<void()> callback) override;
const AsyncDnsResolverResult& result() const override;
private:
ScopedTaskSafety safety_;
AsyncDnsResolverResultImpl result_;
+ absl::AnyInvocable<void()> callback_;
};
} // namespace webrtc