Index: chromeos/binder/thread.cc |
diff --git a/chromeos/binder/thread.cc b/chromeos/binder/thread.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..5fb6623db6211a674f625b785c8c1ffe5d405211 |
--- /dev/null |
+++ b/chromeos/binder/thread.cc |
@@ -0,0 +1,67 @@ |
+// Copyright 2015 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "chromeos/binder/thread.h" |
+ |
+#include "chromeos/binder/command_broker.h" |
+#include "chromeos/binder/driver.h" |
+ |
+namespace binder { |
+ |
+Thread::Thread() |
+ : base::Thread("BinderThread"), |
+ driver_(new Driver()), |
+ watcher_(new base::MessageLoopForIO::FileDescriptorWatcher()) {} |
+ |
+Thread::~Thread() { |
+ Stop(); |
+} |
+ |
+bool Thread::Start() { |
+ base::Thread::Options options; |
+ options.message_loop_type = base::MessageLoop::TYPE_IO; |
+ return StartWithOptions(options); |
+} |
+ |
+void Thread::OnFileCanReadWithoutBlocking(int fd) { |
satorux1
2016/01/13 04:33:20
maybe add DCHECK(initialized_) here and elsewhere?
hashimoto
2016/01/13 05:46:46
Done.
|
+ bool success = command_broker_->PollCommands(); |
+ LOG_IF(ERROR, !success) << "PollCommands() failed."; |
+} |
+ |
+void Thread::OnFileCanWriteWithoutBlocking(int fd) { |
+ NOTREACHED(); |
+} |
+ |
+void Thread::Init() { |
+ if (!driver_->Initialize()) { |
+ LOG(ERROR) << "Failed to initialize driver."; |
+ return; |
+ } |
+ command_broker_.reset(new CommandBroker(driver_.get())); |
+ if (!command_broker_->EnterLooper()) { |
+ LOG(ERROR) << "Failed to enter looper."; |
+ return; |
+ } |
+ if (!base::MessageLoopForIO::current()->WatchFileDescriptor( |
+ driver_->GetFD(), true, base::MessageLoopForIO::WATCH_READ, |
+ watcher_.get(), this)) { |
+ LOG(ERROR) << "Failed to initialize watcher."; |
+ return; |
+ } |
+ initialized_ = true; |
+} |
+ |
+void Thread::CleanUp() { |
+ if (!command_broker_->ExitLooper()) { |
+ LOG(ERROR) << "Failed to exit looper."; |
+ } |
+ if (!driver_->NotifyCurrentThreadExiting()) { |
+ LOG(ERROR) << "Failed to send thread exit."; |
+ } |
+ watcher_.reset(); |
+ command_broker_.reset(); |
+ driver_.reset(); |
+} |
+ |
+} // namespace binder |