Replace usage of base::Callback with std::function

base::Callback comes from libchrome which is undermaintained. Since
C++11 there's standard library support for function objects. Migrate to
a more well knowned solution for function objects.

Test: th
Change-Id: Id19bcd7e92691f57d97520f8f1f4909ca9c25b33
This commit is contained in:
Kelvin Zhang 2023-07-05 12:48:13 -07:00
parent db15b6f93d
commit 40e78443e9
8 changed files with 26 additions and 36 deletions

View file

@ -26,13 +26,10 @@ cc_defaults {
"-Werror",
"-Wno-unused-parameter",
// for libchrome
"-Wno-sign-promo",
],
export_include_dirs: ["include"],
shared_libs: [
"libbinder",
"libchrome",
"libutils",
],
}

View file

@ -16,7 +16,7 @@
#include <binderwrapper/binder_wrapper.h>
#include <base/logging.h>
#include <android-base/logging.h>
#include "real_binder_wrapper.h"

View file

@ -17,7 +17,6 @@
#ifndef SYSTEM_CORE_INCLUDE_BINDERWRAPPER_BINDER_TEST_BASE_H_
#define SYSTEM_CORE_INCLUDE_BINDERWRAPPER_BINDER_TEST_BASE_H_
#include <base/macros.h>
#include <gtest/gtest.h>
namespace android {
@ -37,7 +36,7 @@ class BinderTestBase : public ::testing::Test {
StubBinderWrapper* binder_wrapper_; // Not owned.
private:
DISALLOW_COPY_AND_ASSIGN(BinderTestBase);
BinderTestBase(const BinderTestBase&) = delete;
};
} // namespace android

View file

@ -19,9 +19,9 @@
#include <sys/types.h>
#include <functional>
#include <string>
#include <base/callback.h>
#include <utils/StrongPointer.h>
namespace android {
@ -68,9 +68,8 @@ class BinderWrapper {
// Registers |callback| to be invoked when |binder| dies. If another callback
// is currently registered for |binder|, it will be replaced.
virtual bool RegisterForDeathNotifications(
const sp<IBinder>& binder,
const ::base::Closure& callback) = 0;
virtual bool RegisterForDeathNotifications(const sp<IBinder>& binder,
const std::function<void()>&) = 0;
// Unregisters the callback, if any, for |binder|.
virtual bool UnregisterForDeathNotifications(const sp<IBinder>& binder) = 0;

View file

@ -21,7 +21,6 @@
#include <string>
#include <vector>
#include <base/macros.h>
#include <binder/Binder.h>
#include <binder/IBinder.h>
#include <binderwrapper/binder_wrapper.h>
@ -98,7 +97,7 @@ class StubBinderWrapper : public BinderWrapper {
const sp<IBinder>& binder) override;
sp<BBinder> CreateLocalBinder() override;
bool RegisterForDeathNotifications(const sp<IBinder>& binder,
const ::base::Closure& callback) override;
const std::function<void()>& callback) override;
bool UnregisterForDeathNotifications(const sp<IBinder>& binder) override;
uid_t GetCallingUid() override;
pid_t GetCallingPid() override;
@ -119,13 +118,13 @@ class StubBinderWrapper : public BinderWrapper {
// Map from binder handle to the callback that should be invoked on binder
// death.
std::map<sp<IBinder>, ::base::Closure> death_callbacks_;
std::map<sp<IBinder>, std::function<void()>> death_callbacks_;
// Values to return from GetCallingUid() and GetCallingPid();
uid_t calling_uid_;
pid_t calling_pid_;
DISALLOW_COPY_AND_ASSIGN(StubBinderWrapper);
StubBinderWrapper(const StubBinderWrapper&) = delete;
};
} // namespace android

View file

@ -16,7 +16,8 @@
#include "real_binder_wrapper.h"
#include <base/logging.h>
#include <android-base/logging.h>
#include <binder/Binder.h>
#include <binder/IBinder.h>
#include <binder/IPCThreadState.h>
@ -29,20 +30,18 @@ namespace android {
// be awkward.
class RealBinderWrapper::DeathRecipient : public IBinder::DeathRecipient {
public:
explicit DeathRecipient(const ::base::Closure& callback)
: callback_(callback) {}
~DeathRecipient() = default;
explicit DeathRecipient(const std::function<void()>& callback)
: callback_(std::move(callback)) {}
~DeathRecipient() = default;
// IBinder::DeathRecipient:
void binderDied(const wp<IBinder>& who) override {
callback_.Run();
}
// IBinder::DeathRecipient:
void binderDied(const wp<IBinder>& who) override { callback_(); }
private:
// Callback to run in response to binder death.
::base::Closure callback_;
std::function<void()> callback_;
DISALLOW_COPY_AND_ASSIGN(DeathRecipient);
DISALLOW_COPY_AND_ASSIGN(DeathRecipient);
};
RealBinderWrapper::RealBinderWrapper() = default;
@ -83,9 +82,8 @@ sp<BBinder> RealBinderWrapper::CreateLocalBinder() {
return sp<BBinder>(new BBinder());
}
bool RealBinderWrapper::RegisterForDeathNotifications(
const sp<IBinder>& binder,
const ::base::Closure& callback) {
bool RealBinderWrapper::RegisterForDeathNotifications(const sp<IBinder>& binder,
const std::function<void()>& callback) {
sp<DeathRecipient> recipient(new DeathRecipient(callback));
if (binder->linkToDeath(recipient) != OK) {
LOG(ERROR) << "Failed to register for death notifications on "

View file

@ -19,7 +19,6 @@
#include <map>
#include <base/macros.h>
#include <binderwrapper/binder_wrapper.h>
namespace android {
@ -38,7 +37,7 @@ class RealBinderWrapper : public BinderWrapper {
const sp<IBinder>& binder) override;
sp<BBinder> CreateLocalBinder() override;
bool RegisterForDeathNotifications(const sp<IBinder>& binder,
const ::base::Closure& callback) override;
const std::function<void()>& callback) override;
bool UnregisterForDeathNotifications(const sp<IBinder>& binder) override;
uid_t GetCallingUid() override;
pid_t GetCallingPid() override;
@ -50,7 +49,7 @@ class RealBinderWrapper : public BinderWrapper {
// death.
std::map<sp<IBinder>, sp<DeathRecipient>> death_recipients_;
DISALLOW_COPY_AND_ASSIGN(RealBinderWrapper);
RealBinderWrapper(const RealBinderWrapper&) = delete;
};
} // namespace android

View file

@ -16,7 +16,8 @@
#include <binderwrapper/stub_binder_wrapper.h>
#include <base/logging.h>
#include <android-base/logging.h>
#include <binder/Binder.h>
#include <binder/IBinder.h>
@ -41,8 +42,7 @@ sp<IBinder> StubBinderWrapper::GetRegisteredService(
void StubBinderWrapper::NotifyAboutBinderDeath(const sp<IBinder>& binder) {
const auto it = death_callbacks_.find(binder);
if (it != death_callbacks_.end())
it->second.Run();
if (it != death_callbacks_.end()) it->second();
}
sp<IBinder> StubBinderWrapper::GetService(const std::string& service_name) {
@ -62,9 +62,8 @@ sp<BBinder> StubBinderWrapper::CreateLocalBinder() {
return binder;
}
bool StubBinderWrapper::RegisterForDeathNotifications(
const sp<IBinder>& binder,
const ::base::Closure& callback) {
bool StubBinderWrapper::RegisterForDeathNotifications(const sp<IBinder>& binder,
const std::function<void()>& callback) {
death_callbacks_[binder] = callback;
return true;
}