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" | 8 #include "base/callback.h" |
9 #include "base/macros.h" | 9 #include "base/macros.h" |
10 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
11 #include "base/memory/weak_ptr.h" | 11 #include "base/memory/weak_ptr.h" |
12 #include "base/single_thread_task_runner.h" | 12 #include "base/sequence_checker.h" |
13 #include "base/threading/thread_checker.h" | 13 #include "base/sequenced_task_runner.h" |
14 #include "base/threading/thread_task_runner_handle.h" | 14 #include "base/threading/sequenced_task_runner_handle.h" |
15 #include "mojo/public/c/system/types.h" | 15 #include "mojo/public/c/system/types.h" |
16 #include "mojo/public/cpp/system/handle.h" | 16 #include "mojo/public/cpp/system/handle.h" |
17 #include "mojo/public/cpp/system/system_export.h" | 17 #include "mojo/public/cpp/system/system_export.h" |
18 | 18 |
19 namespace mojo { | 19 namespace mojo { |
20 | 20 |
21 // A Watcher watches a single Mojo handle for signal state changes. | 21 // A Watcher watches a single Mojo handle for signal state changes. |
22 // | 22 // |
23 // 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. |
24 class MOJO_CPP_SYSTEM_EXPORT Watcher { | 24 class MOJO_CPP_SYSTEM_EXPORT Watcher { |
25 public: | 25 public: |
26 // 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 |
27 // interesting way. The |result| argument indicates one of the following | 27 // interesting way. The |result| argument indicates one of the following |
28 // conditions depending on its value: | 28 // conditions depending on its value: |
29 // | 29 // |
30 // |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. |
31 // | 31 // |
32 // |MOJO_RESULT_FAILED_PRECONDITION|: None of the signals being watched can | 32 // |MOJO_RESULT_FAILED_PRECONDITION|: None of the signals being watched can |
33 // ever be satisfied again. | 33 // ever be satisfied again. |
34 // | 34 // |
35 // |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 |
36 // been cancelled implicitly. | 36 // been cancelled implicitly. |
37 using ReadyCallback = base::Callback<void(MojoResult result)>; | 37 using ReadyCallback = base::Callback<void(MojoResult result)>; |
38 | 38 |
39 Watcher(const tracked_objects::Location& from_here, | 39 Watcher(const tracked_objects::Location& from_here, |
40 scoped_refptr<base::SingleThreadTaskRunner> runner = | 40 scoped_refptr<base::SequencedTaskRunner> runner = |
41 base::ThreadTaskRunnerHandle::Get()); | 41 base::SequencedTaskRunnerHandle::Get()); |
42 | 42 |
43 // NOTE: This destructor automatically calls |Cancel()| if the Watcher is | 43 // NOTE: This destructor automatically calls |Cancel()| if the Watcher is |
44 // still active. | 44 // still active. |
45 ~Watcher(); | 45 ~Watcher(); |
46 | 46 |
47 // Indicates if the Watcher is currently watching a handle. | 47 // Indicates if the Watcher is currently watching a handle. |
48 bool IsWatching() const; | 48 bool IsWatching() const; |
49 | 49 |
50 // Starts watching |handle|. A Watcher may only watch one handle at a time, | 50 // Starts watching |handle|. A Watcher may only watch one handle at a time, |
51 // but it is safe to call this more than once as long as the previous watch | 51 // but it is safe to call this more than once as long as the previous watch |
(...skipping 30 matching lines...) Expand all Loading... |
82 } | 82 } |
83 | 83 |
84 private: | 84 private: |
85 void OnHandleReady(MojoResult result); | 85 void OnHandleReady(MojoResult result); |
86 | 86 |
87 static void CallOnHandleReady(uintptr_t context, | 87 static void CallOnHandleReady(uintptr_t context, |
88 MojoResult result, | 88 MojoResult result, |
89 MojoHandleSignalsState signals_state, | 89 MojoHandleSignalsState signals_state, |
90 MojoWatchNotificationFlags flags); | 90 MojoWatchNotificationFlags flags); |
91 | 91 |
92 base::ThreadChecker thread_checker_; | 92 base::SequenceChecker sequence_checker_; |
93 | 93 |
94 // The TaskRunner of this Watcher's owning thread. This field is safe to | 94 // The TaskRunner of this Watcher's owning thread. This field is safe to |
95 // access from any thread. | 95 // access from any thread. |
96 const scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | 96 const scoped_refptr<base::SequencedTaskRunner> task_runner_; |
97 // Whether |task_runner_| is the same as base::ThreadTaskRunnerHandle::Get() | 97 // Whether |task_runner_| is the same as base::ThreadTaskRunnerHandle::Get() |
98 // for the thread. | 98 // for the thread. |
99 const bool is_default_task_runner_; | 99 const bool is_default_task_runner_; |
100 | 100 |
101 // A persistent weak reference to this Watcher which can be passed to the | 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 | 102 // Dispatcher any time this object should be signalled. Safe to access (but |
103 // not to dereference!) from any thread. | 103 // not to dereference!) from any thread. |
104 base::WeakPtr<Watcher> weak_self_; | 104 base::WeakPtr<Watcher> weak_self_; |
105 | 105 |
106 // Fields below must only be accessed on the Watcher's owning thread. | 106 // Fields below must only be accessed on the Watcher's owning thread. |
107 | 107 |
108 // The handle currently under watch. Not owned. | 108 // The handle currently under watch. Not owned. |
109 Handle handle_; | 109 Handle handle_; |
110 | 110 |
111 // The callback to call when the handle is signaled. | 111 // The callback to call when the handle is signaled. |
112 ReadyCallback callback_; | 112 ReadyCallback callback_; |
113 | 113 |
114 // Tag used to ID memory allocations that originated from notifications in | 114 // Tag used to ID memory allocations that originated from notifications in |
115 // this watcher. | 115 // this watcher. |
116 const char* heap_profiler_tag_ = nullptr; | 116 const char* heap_profiler_tag_ = nullptr; |
117 | 117 |
118 base::WeakPtrFactory<Watcher> weak_factory_; | 118 base::WeakPtrFactory<Watcher> weak_factory_; |
119 | 119 |
120 DISALLOW_COPY_AND_ASSIGN(Watcher); | 120 DISALLOW_COPY_AND_ASSIGN(Watcher); |
121 }; | 121 }; |
122 | 122 |
123 } // namespace mojo | 123 } // namespace mojo |
124 | 124 |
125 #endif // MOJO_PUBLIC_CPP_SYSTEM_WATCHER_H_ | 125 #endif // MOJO_PUBLIC_CPP_SYSTEM_WATCHER_H_ |
OLD | NEW |