diff options
author | Alex Deymo <deymo@google.com> | 2016-02-03 17:16:57 -0800 |
---|---|---|
committer | Alex Deymo <deymo@google.com> | 2016-02-04 23:38:54 +0000 |
commit | ba882304e0dee418bb05fa19b6cf8c30cecb59d0 (patch) | |
tree | c1c06906eceed5752ef7e3479a0752541a429d81 | |
parent | 2a6e3f7c9dfe0ed3c3be143d97588ac8ea9c898a (diff) | |
download | platform_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
-rw-r--r-- | Android.mk | 7 | ||||
-rw-r--r-- | brillo/binder_watcher.cc | 56 | ||||
-rw-r--r-- | brillo/binder_watcher.h | 20 |
3 files changed, 55 insertions, 28 deletions
@@ -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); }; |