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

Unified Diff: mojo/public/cpp/system/watcher.h

Issue 2725133002: Mojo: Armed Watchers (Closed)
Patch Set: . Created 3 years, 9 months 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/public/cpp/system/watcher.h
diff --git a/mojo/public/cpp/system/watcher.h b/mojo/public/cpp/system/watcher.h
index 236788b1c853139367df8b0a56b44859064219ca..d0a257814d74bfe4c618ed4371d35a22e4ff9e27 100644
--- a/mojo/public/cpp/system/watcher.h
+++ b/mojo/public/cpp/system/watcher.h
@@ -5,120 +5,32 @@
#ifndef MOJO_PUBLIC_CPP_SYSTEM_WATCHER_H_
#define MOJO_PUBLIC_CPP_SYSTEM_WATCHER_H_
-#include "base/callback.h"
-#include "base/macros.h"
-#include "base/memory/ref_counted.h"
-#include "base/memory/weak_ptr.h"
-#include "base/single_thread_task_runner.h"
-#include "base/threading/thread_checker.h"
-#include "base/threading/thread_task_runner_handle.h"
#include "mojo/public/c/system/types.h"
+#include "mojo/public/c/system/watcher.h"
#include "mojo/public/cpp/system/handle.h"
#include "mojo/public/cpp/system/system_export.h"
namespace mojo {
-// A Watcher watches a single Mojo handle for signal state changes.
-//
-// NOTE: Watchers may only be used on threads which have a running MessageLoop.
-class MOJO_CPP_SYSTEM_EXPORT Watcher {
+// A strongly-typed representation of a |MojoHandle| for a watcher.
+class WatcherHandle : public Handle {
public:
- // A callback to be called any time a watched handle changes state in some
- // interesting way. The |result| argument indicates one of the following
- // conditions depending on its value:
- //
- // |MOJO_RESULT_OK|: One or more of the signals being watched is satisfied.
- //
- // |MOJO_RESULT_FAILED_PRECONDITION|: None of the signals being watched can
- // ever be satisfied again.
- //
- // |MOJO_RESULT_CANCELLED|: The handle has been closed and the watch has
- // been cancelled implicitly.
- using ReadyCallback = base::Callback<void(MojoResult result)>;
+ WatcherHandle() = default;
+ explicit WatcherHandle(MojoHandle value) : Handle(value) {}
- Watcher(const tracked_objects::Location& from_here,
- scoped_refptr<base::SingleThreadTaskRunner> runner =
- base::ThreadTaskRunnerHandle::Get());
-
- // NOTE: This destructor automatically calls |Cancel()| if the Watcher is
- // still active.
- ~Watcher();
-
- // Indicates if the Watcher is currently watching a handle.
- bool IsWatching() const;
-
- // Starts watching |handle|. A Watcher may only watch one handle at a time,
- // but it is safe to call this more than once as long as the previous watch
- // has been cancelled (i.e. |is_watching()| returns |false|.)
- //
- // If no signals in |signals| can ever be satisfied for |handle|, this returns
- // |MOJO_RESULT_FAILED_PRECONDITION|.
- //
- // If |handle| is not a valid watchable (message or data pipe) handle, this
- // returns |MOJO_RESULT_INVALID_ARGUMENT|.
- //
- // Otherwise |MOJO_RESULT_OK| is returned and the handle will be watched until
- // closure or cancellation.
- //
- // Once the watch is started, |callback| may be called at any time on the
- // current thread until |Cancel()| is called or the handle is closed.
- //
- // Destroying the Watcher implicitly calls |Cancel()|.
- MojoResult Start(Handle handle,
- MojoHandleSignals signals,
- const ReadyCallback& callback);
-
- // Cancels the current watch. Once this returns, the callback previously
- // passed to |Start()| will never be called again for this Watcher.
- void Cancel();
-
- Handle handle() const { return handle_; }
- ReadyCallback ready_callback() const { return callback_; }
-
- // Sets the tag used by the heap profiler.
- // |tag| must be a const string literal.
- void set_heap_profiler_tag(const char* heap_profiler_tag) {
- heap_profiler_tag_ = heap_profiler_tag;
- }
-
- private:
- void OnHandleReady(MojoResult result);
-
- static void CallOnHandleReady(uintptr_t context,
- MojoResult result,
- MojoHandleSignalsState signals_state,
- MojoWatchNotificationFlags flags);
-
- base::ThreadChecker thread_checker_;
-
- // The TaskRunner of this Watcher's owning thread. This field is safe to
- // access from any thread.
- const scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
- // Whether |task_runner_| is the same as base::ThreadTaskRunnerHandle::Get()
- // for the thread.
- const bool is_default_task_runner_;
-
- // A persistent weak reference to this Watcher which can be passed to the
- // Dispatcher any time this object should be signalled. Safe to access (but
- // not to dereference!) from any thread.
- base::WeakPtr<Watcher> weak_self_;
-
- // Fields below must only be accessed on the Watcher's owning thread.
-
- // The handle currently under watch. Not owned.
- Handle handle_;
-
- // The callback to call when the handle is signaled.
- ReadyCallback callback_;
+ // Copying and assignment allowed.
+};
- // Tag used to ID memory allocations that originated from notifications in
- // this watcher.
- const char* heap_profiler_tag_ = nullptr;
+static_assert(sizeof(WatcherHandle) == sizeof(Handle),
+ "Bad size for C++ WatcherHandle");
- base::WeakPtrFactory<Watcher> weak_factory_;
+typedef ScopedHandleBase<WatcherHandle> ScopedWatcherHandle;
+static_assert(sizeof(ScopedWatcherHandle) == sizeof(WatcherHandle),
+ "Bad size for C++ ScopedWatcherHandle");
- DISALLOW_COPY_AND_ASSIGN(Watcher);
-};
+MOJO_CPP_SYSTEM_EXPORT MojoResult
+CreateWatcher(MojoWatcherCallback callback,
+ ScopedWatcherHandle* watcher_handle);
} // namespace mojo

Powered by Google App Engine
This is Rietveld 408576698