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 "base/callback.h" | |
9 #include "base/macros.h" | |
10 #include "base/memory/ref_counted.h" | |
11 #include "base/memory/weak_ptr.h" | |
12 #include "base/single_thread_task_runner.h" | |
13 #include "base/threading/thread_checker.h" | |
14 #include "base/threading/thread_task_runner_handle.h" | |
15 #include "mojo/public/c/system/types.h" | 8 #include "mojo/public/c/system/types.h" |
| 9 #include "mojo/public/c/system/watcher.h" |
16 #include "mojo/public/cpp/system/handle.h" | 10 #include "mojo/public/cpp/system/handle.h" |
17 #include "mojo/public/cpp/system/system_export.h" | 11 #include "mojo/public/cpp/system/system_export.h" |
18 | 12 |
19 namespace mojo { | 13 namespace mojo { |
20 | 14 |
21 // A Watcher watches a single Mojo handle for signal state changes. | 15 // A strongly-typed representation of a |MojoHandle| for a watcher. |
22 // | 16 class WatcherHandle : public Handle { |
23 // NOTE: Watchers may only be used on threads which have a running MessageLoop. | |
24 class MOJO_CPP_SYSTEM_EXPORT Watcher { | |
25 public: | 17 public: |
26 // A callback to be called any time a watched handle changes state in some | 18 WatcherHandle() = default; |
27 // interesting way. The |result| argument indicates one of the following | 19 explicit WatcherHandle(MojoHandle value) : Handle(value) {} |
28 // conditions depending on its value: | |
29 // | |
30 // |MOJO_RESULT_OK|: One or more of the signals being watched is satisfied. | |
31 // | |
32 // |MOJO_RESULT_FAILED_PRECONDITION|: None of the signals being watched can | |
33 // ever be satisfied again. | |
34 // | |
35 // |MOJO_RESULT_CANCELLED|: The handle has been closed and the watch has | |
36 // been cancelled implicitly. | |
37 using ReadyCallback = base::Callback<void(MojoResult result)>; | |
38 | 20 |
39 Watcher(const tracked_objects::Location& from_here, | 21 // Copying and assignment allowed. |
40 scoped_refptr<base::SingleThreadTaskRunner> runner = | 22 }; |
41 base::ThreadTaskRunnerHandle::Get()); | |
42 | 23 |
43 // NOTE: This destructor automatically calls |Cancel()| if the Watcher is | 24 static_assert(sizeof(WatcherHandle) == sizeof(Handle), |
44 // still active. | 25 "Bad size for C++ WatcherHandle"); |
45 ~Watcher(); | |
46 | 26 |
47 // Indicates if the Watcher is currently watching a handle. | 27 typedef ScopedHandleBase<WatcherHandle> ScopedWatcherHandle; |
48 bool IsWatching() const; | 28 static_assert(sizeof(ScopedWatcherHandle) == sizeof(WatcherHandle), |
| 29 "Bad size for C++ ScopedWatcherHandle"); |
49 | 30 |
50 // Starts watching |handle|. A Watcher may only watch one handle at a time, | 31 MOJO_CPP_SYSTEM_EXPORT MojoResult |
51 // but it is safe to call this more than once as long as the previous watch | 32 CreateWatcher(MojoWatcherCallback callback, |
52 // has been cancelled (i.e. |is_watching()| returns |false|.) | 33 ScopedWatcherHandle* watcher_handle); |
53 // | |
54 // If no signals in |signals| can ever be satisfied for |handle|, this returns | |
55 // |MOJO_RESULT_FAILED_PRECONDITION|. | |
56 // | |
57 // If |handle| is not a valid watchable (message or data pipe) handle, this | |
58 // returns |MOJO_RESULT_INVALID_ARGUMENT|. | |
59 // | |
60 // Otherwise |MOJO_RESULT_OK| is returned and the handle will be watched until | |
61 // closure or cancellation. | |
62 // | |
63 // Once the watch is started, |callback| may be called at any time on the | |
64 // current thread until |Cancel()| is called or the handle is closed. | |
65 // | |
66 // Destroying the Watcher implicitly calls |Cancel()|. | |
67 MojoResult Start(Handle handle, | |
68 MojoHandleSignals signals, | |
69 const ReadyCallback& callback); | |
70 | |
71 // Cancels the current watch. Once this returns, the callback previously | |
72 // passed to |Start()| will never be called again for this Watcher. | |
73 void Cancel(); | |
74 | |
75 Handle handle() const { return handle_; } | |
76 ReadyCallback ready_callback() const { return callback_; } | |
77 | |
78 // Sets the tag used by the heap profiler. | |
79 // |tag| must be a const string literal. | |
80 void set_heap_profiler_tag(const char* heap_profiler_tag) { | |
81 heap_profiler_tag_ = heap_profiler_tag; | |
82 } | |
83 | |
84 private: | |
85 void OnHandleReady(MojoResult result); | |
86 | |
87 static void CallOnHandleReady(uintptr_t context, | |
88 MojoResult result, | |
89 MojoHandleSignalsState signals_state, | |
90 MojoWatchNotificationFlags flags); | |
91 | |
92 base::ThreadChecker thread_checker_; | |
93 | |
94 // The TaskRunner of this Watcher's owning thread. This field is safe to | |
95 // access from any thread. | |
96 const scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | |
97 // Whether |task_runner_| is the same as base::ThreadTaskRunnerHandle::Get() | |
98 // for the thread. | |
99 const bool is_default_task_runner_; | |
100 | |
101 // A persistent weak reference to this Watcher which can be passed to the | |
102 // Dispatcher any time this object should be signalled. Safe to access (but | |
103 // not to dereference!) from any thread. | |
104 base::WeakPtr<Watcher> weak_self_; | |
105 | |
106 // Fields below must only be accessed on the Watcher's owning thread. | |
107 | |
108 // The handle currently under watch. Not owned. | |
109 Handle handle_; | |
110 | |
111 // The callback to call when the handle is signaled. | |
112 ReadyCallback callback_; | |
113 | |
114 // Tag used to ID memory allocations that originated from notifications in | |
115 // this watcher. | |
116 const char* heap_profiler_tag_ = nullptr; | |
117 | |
118 base::WeakPtrFactory<Watcher> weak_factory_; | |
119 | |
120 DISALLOW_COPY_AND_ASSIGN(Watcher); | |
121 }; | |
122 | 34 |
123 } // namespace mojo | 35 } // namespace mojo |
124 | 36 |
125 #endif // MOJO_PUBLIC_CPP_SYSTEM_WATCHER_H_ | 37 #endif // MOJO_PUBLIC_CPP_SYSTEM_WATCHER_H_ |
OLD | NEW |