Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(890)

Unified Diff: mojo/edk/base_edk/platform_message_loop_for_io_impl.cc

Issue 1507903006: EDK: Remove mojo::platform::MessageLoopForIO and add PlatformHandleWatcher. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: mojo/edk/base_edk/platform_message_loop_for_io_impl.cc
diff --git a/mojo/edk/base_edk/platform_message_loop_for_io_impl.cc b/mojo/edk/base_edk/platform_message_loop_for_io_impl.cc
index baea46e0c034a774de1accc4c5ba3120d527ea73..f1c36b173b64aac669d6da114411e639448da482 100644
--- a/mojo/edk/base_edk/platform_message_loop_for_io_impl.cc
+++ b/mojo/edk/base_edk/platform_message_loop_for_io_impl.cc
@@ -6,14 +6,57 @@
#include <utility>
+#include "base/logging.h"
+#include "base/macros.h"
#include "mojo/edk/base_edk/platform_task_runner_impl.h"
+#include "mojo/edk/util/make_unique.h"
+using mojo::platform::PlatformHandle;
+using mojo::platform::PlatformHandleWatcher;
using mojo::platform::TaskRunner;
using mojo::util::MakeRefCounted;
+using mojo::util::MakeUnique;
using mojo::util::RefPtr;
namespace base_edk {
+namespace {
+
+class WatchTokenImpl : public PlatformHandleWatcher::WatchToken,
+ public base::MessageLoopForIO::Watcher {
+ public:
+ WatchTokenImpl(std::function<void()>&& read_callback,
+ std::function<void()>&& write_callback)
+ : read_callback_(std::move(read_callback)),
+ write_callback_(std::move(write_callback)) {}
+ ~WatchTokenImpl() override {}
+
+ base::MessageLoopForIO::FileDescriptorWatcher* base_fd_watcher() {
+ return &base_fd_watcher_;
+ }
+
+ private:
+ // |base::MessageLoopForIO::Watcher| implementation:
+ void OnFileCanReadWithoutBlocking(int /*fd*/) override {
+ DCHECK(read_callback_);
+ read_callback_();
+ }
+
+ void OnFileCanWriteWithoutBlocking(int /*fd*/) override {
+ DCHECK(write_callback_);
+ write_callback_();
+ }
+
+ const std::function<void()> read_callback_;
+ const std::function<void()> write_callback_;
+
+ base::MessageLoopForIO::FileDescriptorWatcher base_fd_watcher_;
+
+ DISALLOW_COPY_AND_ASSIGN(WatchTokenImpl);
+};
+
+} // namespace
+
PlatformMessageLoopForIOImpl::PlatformMessageLoopForIOImpl()
: task_runner_(MakeRefCounted<PlatformTaskRunnerImpl>(
base_message_loop_for_io_.task_runner())) {}
@@ -45,4 +88,28 @@ bool PlatformMessageLoopForIOImpl::IsRunningOnCurrentThread() const {
base_message_loop_for_io_.is_running();
}
+std::unique_ptr<PlatformHandleWatcher::WatchToken>
+PlatformMessageLoopForIOImpl::Watch(PlatformHandle platform_handle,
+ bool persistent,
+ std::function<void()>&& read_callback,
+ std::function<void()>&& write_callback) {
+ DCHECK(platform_handle.is_valid());
+ DCHECK(read_callback || write_callback);
+
+ base::MessageLoopForIO::Mode mode;
+ if (read_callback && write_callback) {
+ mode = base::MessageLoopForIO::WATCH_READ_WRITE;
+ } else if (read_callback) {
+ mode = base::MessageLoopForIO::WATCH_READ;
+ } else {
+ DCHECK(write_callback);
+ mode = base::MessageLoopForIO::WATCH_WRITE;
+ }
+ auto rv = MakeUnique<WatchTokenImpl>(std::move(read_callback),
+ std::move(write_callback));
+ CHECK(base_message_loop_for_io_.WatchFileDescriptor(
+ platform_handle.fd, persistent, mode, rv->base_fd_watcher(), rv.get()));
+ return std::move(rv);
+}
+
} // namespace base_edk

Powered by Google App Engine
This is Rietveld 408576698