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

Side by Side Diff: mojo/public/cpp/system/watcher.h

Issue 2480153004: Revert of Remove MessageLoop destruction observer from mojo::Watcher. (Closed)
Patch Set: Created 4 years, 1 month 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 unified diff | Download patch
« no previous file with comments | « mojo/public/cpp/system/tests/watcher_unittest.cc ('k') | mojo/public/cpp/system/watcher.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
8 #include "base/callback.h" 10 #include "base/callback.h"
9 #include "base/macros.h" 11 #include "base/macros.h"
10 #include "base/memory/ref_counted.h" 12 #include "base/memory/ref_counted.h"
11 #include "base/memory/weak_ptr.h" 13 #include "base/memory/weak_ptr.h"
12 #include "base/single_thread_task_runner.h" 14 #include "base/single_thread_task_runner.h"
13 #include "base/threading/thread_checker.h" 15 #include "base/threading/thread_checker.h"
14 #include "base/threading/thread_task_runner_handle.h" 16 #include "base/threading/thread_task_runner_handle.h"
15 #include "mojo/public/c/system/types.h" 17 #include "mojo/public/c/system/types.h"
16 #include "mojo/public/cpp/system/handle.h" 18 #include "mojo/public/cpp/system/handle.h"
17 #include "mojo/public/cpp/system/system_export.h" 19 #include "mojo/public/cpp/system/system_export.h"
18 20
19 namespace mojo { 21 namespace mojo {
20 22
21 // A Watcher watches a single Mojo handle for signal state changes. 23 // A Watcher watches a single Mojo handle for signal state changes.
22 // 24 //
23 // NOTE: Watchers may only be used on threads which have a running MessageLoop. 25 // NOTE: Watchers may only be used on threads which have a running MessageLoop.
24 class MOJO_CPP_SYSTEM_EXPORT Watcher { 26 class MOJO_CPP_SYSTEM_EXPORT Watcher {
25 public: 27 public:
26 // A callback to be called any time a watched handle changes state in some 28 // 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 29 // interesting way. The |result| argument indicates one of the following
28 // conditions depending on its value: 30 // conditions depending on its value:
29 // 31 //
30 // |MOJO_RESULT_OK|: One or more of the signals being watched is satisfied. 32 // |MOJO_RESULT_OK|: One or more of the signals being watched is satisfied.
31 // 33 //
32 // |MOJO_RESULT_FAILED_PRECONDITION|: None of the signals being watched can 34 // |MOJO_RESULT_FAILED_PRECONDITION|: None of the signals being watched can
33 // ever be satisfied again. 35 // ever be satisfied again.
34 // 36 //
35 // |MOJO_RESULT_CANCELLED|: The handle has been closed and the watch has 37 // |MOJO_RESULT_CANCELLED|: The handle has been closed and the watch has
36 // been cancelled implicitly. 38 // 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.
37 using ReadyCallback = base::Callback<void(MojoResult result)>; 44 using ReadyCallback = base::Callback<void(MojoResult result)>;
38 45
39 explicit Watcher(scoped_refptr<base::SingleThreadTaskRunner> runner = 46 explicit Watcher(scoped_refptr<base::SingleThreadTaskRunner> runner =
40 base::ThreadTaskRunnerHandle::Get()); 47 base::ThreadTaskRunnerHandle::Get());
41 48
42 // NOTE: This destructor automatically calls |Cancel()| if the Watcher is 49 // NOTE: This destructor automatically calls |Cancel()| if the Watcher is
43 // still active. 50 // still active.
44 ~Watcher(); 51 ~Watcher();
45 52
46 // Indicates if the Watcher is currently watching a handle. 53 // Indicates if the Watcher is currently watching a handle.
(...skipping 21 matching lines...) Expand all
68 const ReadyCallback& callback); 75 const ReadyCallback& callback);
69 76
70 // Cancels the current watch. Once this returns, the callback previously 77 // Cancels the current watch. Once this returns, the callback previously
71 // passed to |Start()| will never be called again for this Watcher. 78 // passed to |Start()| will never be called again for this Watcher.
72 void Cancel(); 79 void Cancel();
73 80
74 Handle handle() const { return handle_; } 81 Handle handle() const { return handle_; }
75 ReadyCallback ready_callback() const { return callback_; } 82 ReadyCallback ready_callback() const { return callback_; }
76 83
77 private: 84 private:
85 class MessageLoopObserver;
86 friend class MessageLoopObserver;
87
78 void OnHandleReady(MojoResult result); 88 void OnHandleReady(MojoResult result);
79 89
80 static void CallOnHandleReady(uintptr_t context, 90 static void CallOnHandleReady(uintptr_t context,
81 MojoResult result, 91 MojoResult result,
82 MojoHandleSignalsState signals_state, 92 MojoHandleSignalsState signals_state,
83 MojoWatchNotificationFlags flags); 93 MojoWatchNotificationFlags flags);
84 94
85 base::ThreadChecker thread_checker_; 95 base::ThreadChecker thread_checker_;
86 96
87 // The TaskRunner of this Watcher's owning thread. This field is safe to 97 // The TaskRunner of this Watcher's owning thread. This field is safe to
88 // access from any thread. 98 // access from any thread.
89 const scoped_refptr<base::SingleThreadTaskRunner> task_runner_; 99 const scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
90 // Whether |task_runner_| is the same as base::ThreadTaskRunnerHandle::Get() 100 // Whether |task_runner_| is the same as base::ThreadTaskRunnerHandle::Get()
91 // for the thread. 101 // for the thread.
92 const bool is_default_task_runner_; 102 const bool is_default_task_runner_;
93 103
104 std::unique_ptr<MessageLoopObserver> message_loop_observer_;
105
94 // A persistent weak reference to this Watcher which can be passed to the 106 // A persistent weak reference to this Watcher which can be passed to the
95 // Dispatcher any time this object should be signalled. Safe to access (but 107 // Dispatcher any time this object should be signalled. Safe to access (but
96 // not to dereference!) from any thread. 108 // not to dereference!) from any thread.
97 base::WeakPtr<Watcher> weak_self_; 109 base::WeakPtr<Watcher> weak_self_;
98 110
99 // Fields below must only be accessed on the Watcher's owning thread. 111 // Fields below must only be accessed on the Watcher's owning thread.
100 112
101 // The handle currently under watch. Not owned. 113 // The handle currently under watch. Not owned.
102 Handle handle_; 114 Handle handle_;
103 115
104 // The callback to call when the handle is signaled. 116 // The callback to call when the handle is signaled.
105 ReadyCallback callback_; 117 ReadyCallback callback_;
106 118
107 base::WeakPtrFactory<Watcher> weak_factory_; 119 base::WeakPtrFactory<Watcher> weak_factory_;
108 120
109 DISALLOW_COPY_AND_ASSIGN(Watcher); 121 DISALLOW_COPY_AND_ASSIGN(Watcher);
110 }; 122 };
111 123
112 } // namespace mojo 124 } // namespace mojo
113 125
114 #endif // MOJO_PUBLIC_CPP_SYSTEM_WATCHER_H_ 126 #endif // MOJO_PUBLIC_CPP_SYSTEM_WATCHER_H_
OLDNEW
« no previous file with comments | « mojo/public/cpp/system/tests/watcher_unittest.cc ('k') | mojo/public/cpp/system/watcher.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698