aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlex Deymo <deymo@google.com>2016-02-04 23:57:51 +0000
committerandroid-build-merger <android-build-merger@google.com>2016-02-04 23:57:51 +0000
commit955cddc958a23ec0c09d9d4b79396744f83cd24e (patch)
treec1c06906eceed5752ef7e3479a0752541a429d81
parent598f8a226cf0b5f8d2702ba28aae51222249aa82 (diff)
parentba882304e0dee418bb05fa19b6cf8c30cecb59d0 (diff)
downloadplatform_external_libbrillo-nougat-dev.tar.gz
platform_external_libbrillo-nougat-dev.tar.bz2
platform_external_libbrillo-nougat-dev.zip
Make BinderWatcher use brillo::MessageLoop.nougat-dev
am: ba882304e0 * commit 'ba882304e0dee418bb05fa19b6cf8c30cecb59d0': Make BinderWatcher use brillo::MessageLoop.
-rw-r--r--Android.mk7
-rw-r--r--brillo/binder_watcher.cc56
-rw-r--r--brillo/binder_watcher.h20
3 files changed, 55 insertions, 28 deletions
diff --git a/Android.mk b/Android.mk
index 0df3362..59a9b9b 100644
--- a/Android.mk
+++ b/Android.mk
@@ -177,8 +177,11 @@ LOCAL_CPP_EXTENSION := $(libbrillo_cpp_extension)
LOCAL_MODULE := libbrillo-binder
LOCAL_SRC_FILES := $(libbrillo_binder_sources)
LOCAL_C_INCLUDES := $(libbrillo_includes)
-LOCAL_SHARED_LIBRARIES := $(libbrillo_shared_libraries) \
- libbinder libutils
+LOCAL_SHARED_LIBRARIES := \
+ $(libbrillo_shared_libraries) \
+ libbinder \
+ libbrillo \
+ libutils
LOCAL_CFLAGS := $(libbrillo_CFLAGS)
LOCAL_CPPFLAGS := $(libbrillo_CPPFLAGS)
LOCAL_CLANG := true
diff --git a/brillo/binder_watcher.cc b/brillo/binder_watcher.cc
index 32ab101..9752204 100644
--- a/brillo/binder_watcher.cc
+++ b/brillo/binder_watcher.cc
@@ -16,6 +16,7 @@
#include <brillo/binder_watcher.h>
+#include <base/bind.h>
#include <base/logging.h>
#include <binder/IPCThreadState.h>
#include <binder/ProcessState.h>
@@ -23,36 +24,59 @@
using android::IPCThreadState;
using android::ProcessState;
+namespace {
+// Called from the message loop whenever the binder file descriptor is ready.
+void OnBinderReadReady() {
+ IPCThreadState::self()->handlePolledCommands();
+}
+} // namespace
+
namespace brillo {
-BinderWatcher::BinderWatcher() = default;
+BinderWatcher::BinderWatcher(MessageLoop* message_loop)
+ : message_loop_(message_loop) {}
+
+BinderWatcher::BinderWatcher() : message_loop_(nullptr) {}
-BinderWatcher::~BinderWatcher() = default;
+BinderWatcher::~BinderWatcher() {
+ if (task_id_ != MessageLoop::kTaskIdNull)
+ message_loop_->CancelTask(task_id_);
+}
bool BinderWatcher::Init() {
+ if (!message_loop_)
+ message_loop_ = MessageLoop::current();
+ if (!message_loop_) {
+ LOG(ERROR) << "Must initialize a brillo::MessageLoop to use BinderWatcher";
+ return false;
+ }
+
int binder_fd = -1;
ProcessState::self()->setThreadPoolMaxThreadCount(0);
IPCThreadState::self()->disableBackgroundScheduling(true);
- IPCThreadState::self()->setupPolling(&binder_fd);
- LOG(INFO) << "Got binder FD " << binder_fd;
- if (binder_fd < 0)
+ int err = IPCThreadState::self()->setupPolling(&binder_fd);
+ if (err != 0) {
+ LOG(ERROR) << "Error setting up binder polling: "
+ << logging::SystemErrorCodeToString(err);
return false;
+ }
+ if (binder_fd < 0) {
+ LOG(ERROR) << "Invalid binder FD " << binder_fd;
+ return false;
+ }
+ VLOG(1) << "Got binder FD " << binder_fd;
- if (!base::MessageLoopForIO::current()->WatchFileDescriptor(
- binder_fd, true /* persistent */, base::MessageLoopForIO::WATCH_READ,
- &watcher_, this)) {
+ task_id_ = message_loop_->WatchFileDescriptor(
+ FROM_HERE,
+ binder_fd,
+ MessageLoop::kWatchRead,
+ true /* persistent */,
+ base::Bind(&OnBinderReadReady));
+ if (task_id_ == MessageLoop::kTaskIdNull) {
LOG(ERROR) << "Failed to watch binder FD";
return false;
}
return true;
}
-void BinderWatcher::OnFileCanReadWithoutBlocking(int /* fd */) {
- IPCThreadState::self()->handlePolledCommands();
-}
-
-void BinderWatcher::OnFileCanWriteWithoutBlocking(int fd) {
- NOTREACHED() << "Unexpected writable notification for FD " << fd;
-}
-
} // namespace brillo
diff --git a/brillo/binder_watcher.h b/brillo/binder_watcher.h
index 3683270..ece999d 100644
--- a/brillo/binder_watcher.h
+++ b/brillo/binder_watcher.h
@@ -18,26 +18,26 @@
#define LIBBRILLO_BRILLO_BINDER_WATCHER_H_
#include <base/macros.h>
-#include <base/message_loop/message_loop.h>
+#include <brillo/message_loops/message_loop.h>
namespace brillo {
-// Bridge between libbinder and base::MessageLoop. Construct at startup to make
-// the message loop watch for binder events and pass them to libbinder.
-class BinderWatcher : public base::MessageLoopForIO::Watcher {
+// Bridge between libbinder and brillo::MessageLoop. Construct at startup to
+// make the message loop watch for binder events and pass them to libbinder.
+class BinderWatcher final {
public:
+ // Construct the BinderWatcher using the passed |message_loop| if not null or
+ // the current MessageLoop otherwise.
+ explicit BinderWatcher(MessageLoop* message_loop);
BinderWatcher();
- ~BinderWatcher() override;
+ ~BinderWatcher();
// Initializes the object, returning true on success.
bool Init();
- // base::MessageLoopForIO::Watcher:
- void OnFileCanReadWithoutBlocking(int fd) override;
- void OnFileCanWriteWithoutBlocking(int fd) override;
-
private:
- base::MessageLoopForIO::FileDescriptorWatcher watcher_;
+ MessageLoop::TaskId task_id_{MessageLoop::kTaskIdNull};
+ MessageLoop* message_loop_;
DISALLOW_COPY_AND_ASSIGN(BinderWatcher);
};