| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef MOJO_PUBLIC_CPP_SYSTEM_WATCHER_H_ | 5 #ifndef MOJO_PUBLIC_CPP_SYSTEM_WATCHER_H_ |
| 6 #define MOJO_PUBLIC_CPP_SYSTEM_WATCHER_H_ | 6 #define MOJO_PUBLIC_CPP_SYSTEM_WATCHER_H_ |
| 7 | 7 |
| 8 #include <memory> | |
| 9 | |
| 10 #include "base/callback.h" | 8 #include "base/callback.h" |
| 11 #include "base/macros.h" | 9 #include "base/macros.h" |
| 12 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
| 13 #include "base/memory/weak_ptr.h" | 11 #include "base/memory/weak_ptr.h" |
| 14 #include "base/single_thread_task_runner.h" | 12 #include "base/single_thread_task_runner.h" |
| 15 #include "base/threading/thread_checker.h" | 13 #include "base/threading/thread_checker.h" |
| 16 #include "base/threading/thread_task_runner_handle.h" | 14 #include "base/threading/thread_task_runner_handle.h" |
| 17 #include "mojo/public/c/system/types.h" | 15 #include "mojo/public/c/system/types.h" |
| 18 #include "mojo/public/cpp/system/handle.h" | 16 #include "mojo/public/cpp/system/handle.h" |
| 19 #include "mojo/public/cpp/system/system_export.h" | 17 #include "mojo/public/cpp/system/system_export.h" |
| 20 | 18 |
| 21 namespace mojo { | 19 namespace mojo { |
| 22 | 20 |
| 23 // A Watcher watches a single Mojo handle for signal state changes. | 21 // A Watcher watches a single Mojo handle for signal state changes. |
| 24 // | 22 // |
| 25 // NOTE: Watchers may only be used on threads which have a running MessageLoop. | 23 // NOTE: Watchers may only be used on threads which have a running MessageLoop. |
| 26 class MOJO_CPP_SYSTEM_EXPORT Watcher { | 24 class MOJO_CPP_SYSTEM_EXPORT Watcher { |
| 27 public: | 25 public: |
| 28 // A callback to be called any time a watched handle changes state in some | 26 // A callback to be called any time a watched handle changes state in some |
| 29 // interesting way. The |result| argument indicates one of the following | 27 // interesting way. The |result| argument indicates one of the following |
| 30 // conditions depending on its value: | 28 // conditions depending on its value: |
| 31 // | 29 // |
| 32 // |MOJO_RESULT_OK|: One or more of the signals being watched is satisfied. | 30 // |MOJO_RESULT_OK|: One or more of the signals being watched is satisfied. |
| 33 // | 31 // |
| 34 // |MOJO_RESULT_FAILED_PRECONDITION|: None of the signals being watched can | 32 // |MOJO_RESULT_FAILED_PRECONDITION|: None of the signals being watched can |
| 35 // ever be satisfied again. | 33 // ever be satisfied again. |
| 36 // | 34 // |
| 37 // |MOJO_RESULT_CANCELLED|: The handle has been closed and the watch has | 35 // |MOJO_RESULT_CANCELLED|: The handle has been closed and the watch has |
| 38 // been cancelled implicitly. | 36 // been cancelled implicitly. |
| 39 // | |
| 40 // |MOJO_RESULT_ABORTED|: Notifications can no longer be delivered for this | |
| 41 // watcher for some unspecified reason, e.g., the watching thread may | |
| 42 // be shutting down soon. Note that it is still necessary to explicitly | |
| 43 // Cancel() the watch in this case. | |
| 44 using ReadyCallback = base::Callback<void(MojoResult result)>; | 37 using ReadyCallback = base::Callback<void(MojoResult result)>; |
| 45 | 38 |
| 46 explicit Watcher(scoped_refptr<base::SingleThreadTaskRunner> runner = | 39 explicit Watcher(scoped_refptr<base::SingleThreadTaskRunner> runner = |
| 47 base::ThreadTaskRunnerHandle::Get()); | 40 base::ThreadTaskRunnerHandle::Get()); |
| 48 | 41 |
| 49 // NOTE: This destructor automatically calls |Cancel()| if the Watcher is | 42 // NOTE: This destructor automatically calls |Cancel()| if the Watcher is |
| 50 // still active. | 43 // still active. |
| 51 ~Watcher(); | 44 ~Watcher(); |
| 52 | 45 |
| 53 // Indicates if the Watcher is currently watching a handle. | 46 // Indicates if the Watcher is currently watching a handle. |
| (...skipping 21 matching lines...) Expand all Loading... |
| 75 const ReadyCallback& callback); | 68 const ReadyCallback& callback); |
| 76 | 69 |
| 77 // Cancels the current watch. Once this returns, the callback previously | 70 // Cancels the current watch. Once this returns, the callback previously |
| 78 // passed to |Start()| will never be called again for this Watcher. | 71 // passed to |Start()| will never be called again for this Watcher. |
| 79 void Cancel(); | 72 void Cancel(); |
| 80 | 73 |
| 81 Handle handle() const { return handle_; } | 74 Handle handle() const { return handle_; } |
| 82 ReadyCallback ready_callback() const { return callback_; } | 75 ReadyCallback ready_callback() const { return callback_; } |
| 83 | 76 |
| 84 private: | 77 private: |
| 85 class MessageLoopObserver; | |
| 86 friend class MessageLoopObserver; | |
| 87 | |
| 88 void OnHandleReady(MojoResult result); | 78 void OnHandleReady(MojoResult result); |
| 89 | 79 |
| 90 static void CallOnHandleReady(uintptr_t context, | 80 static void CallOnHandleReady(uintptr_t context, |
| 91 MojoResult result, | 81 MojoResult result, |
| 92 MojoHandleSignalsState signals_state, | 82 MojoHandleSignalsState signals_state, |
| 93 MojoWatchNotificationFlags flags); | 83 MojoWatchNotificationFlags flags); |
| 94 | 84 |
| 95 base::ThreadChecker thread_checker_; | 85 base::ThreadChecker thread_checker_; |
| 96 | 86 |
| 97 // The TaskRunner of this Watcher's owning thread. This field is safe to | 87 // The TaskRunner of this Watcher's owning thread. This field is safe to |
| 98 // access from any thread. | 88 // access from any thread. |
| 99 const scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | 89 const scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
| 100 // Whether |task_runner_| is the same as base::ThreadTaskRunnerHandle::Get() | 90 // Whether |task_runner_| is the same as base::ThreadTaskRunnerHandle::Get() |
| 101 // for the thread. | 91 // for the thread. |
| 102 const bool is_default_task_runner_; | 92 const bool is_default_task_runner_; |
| 103 | 93 |
| 104 std::unique_ptr<MessageLoopObserver> message_loop_observer_; | |
| 105 | |
| 106 // A persistent weak reference to this Watcher which can be passed to the | 94 // A persistent weak reference to this Watcher which can be passed to the |
| 107 // Dispatcher any time this object should be signalled. Safe to access (but | 95 // Dispatcher any time this object should be signalled. Safe to access (but |
| 108 // not to dereference!) from any thread. | 96 // not to dereference!) from any thread. |
| 109 base::WeakPtr<Watcher> weak_self_; | 97 base::WeakPtr<Watcher> weak_self_; |
| 110 | 98 |
| 111 // Fields below must only be accessed on the Watcher's owning thread. | 99 // Fields below must only be accessed on the Watcher's owning thread. |
| 112 | 100 |
| 113 // The handle currently under watch. Not owned. | 101 // The handle currently under watch. Not owned. |
| 114 Handle handle_; | 102 Handle handle_; |
| 115 | 103 |
| 116 // The callback to call when the handle is signaled. | 104 // The callback to call when the handle is signaled. |
| 117 ReadyCallback callback_; | 105 ReadyCallback callback_; |
| 118 | 106 |
| 119 base::WeakPtrFactory<Watcher> weak_factory_; | 107 base::WeakPtrFactory<Watcher> weak_factory_; |
| 120 | 108 |
| 121 DISALLOW_COPY_AND_ASSIGN(Watcher); | 109 DISALLOW_COPY_AND_ASSIGN(Watcher); |
| 122 }; | 110 }; |
| 123 | 111 |
| 124 } // namespace mojo | 112 } // namespace mojo |
| 125 | 113 |
| 126 #endif // MOJO_PUBLIC_CPP_SYSTEM_WATCHER_H_ | 114 #endif // MOJO_PUBLIC_CPP_SYSTEM_WATCHER_H_ |
| OLD | NEW |