aboutsummaryrefslogtreecommitdiffstats
path: root/brillo/binder_watcher.cc
diff options
context:
space:
mode:
authorAlex Deymo <deymo@google.com>2016-02-03 17:16:57 -0800
committerAlex Deymo <deymo@google.com>2016-02-04 23:38:54 +0000
commitba882304e0dee418bb05fa19b6cf8c30cecb59d0 (patch)
treec1c06906eceed5752ef7e3479a0752541a429d81 /brillo/binder_watcher.cc
parent2a6e3f7c9dfe0ed3c3be143d97588ac8ea9c898a (diff)
downloadplatform_external_libbrillo-ba882304e0dee418bb05fa19b6cf8c30cecb59d0.tar.gz
platform_external_libbrillo-ba882304e0dee418bb05fa19b6cf8c30cecb59d0.tar.bz2
platform_external_libbrillo-ba882304e0dee418bb05fa19b6cf8c30cecb59d0.zip
Make BinderWatcher use brillo::MessageLoop.
This patch breaks the dependency between the BinderWatcher and the base::MessageLoopForIO implementation, making it use the brillo::MessageLoop class instead. Bug: 26356682 TEST=Binder based `update_engine_client` still works. Change-Id: I46a8a9c8d00d536c2d54889017f109c581eb0bc7
Diffstat (limited to 'brillo/binder_watcher.cc')
-rw-r--r--brillo/binder_watcher.cc56
1 files changed, 40 insertions, 16 deletions
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