OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 #include "mojo/common/handle_watcher.h" | 5 #include "mojo/common/handle_watcher.h" |
6 | 6 |
7 #include "base/auto_reset.h" | 7 #include "base/auto_reset.h" |
8 #include "base/bind.h" | 8 #include "base/bind.h" |
9 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
10 #include "base/test/simple_test_tick_clock.h" | 10 #include "base/test/simple_test_tick_clock.h" |
(...skipping 14 matching lines...) Expand all Loading... |
25 uint32_t num_handles = 0; | 25 uint32_t num_handles = 0; |
26 return MojoReadMessage(handle, NULL, &num_bytes, NULL, &num_handles, | 26 return MojoReadMessage(handle, NULL, &num_bytes, NULL, &num_handles, |
27 MOJO_READ_MESSAGE_FLAG_MAY_DISCARD); | 27 MOJO_READ_MESSAGE_FLAG_MAY_DISCARD); |
28 } | 28 } |
29 | 29 |
30 void RunUntilIdle() { | 30 void RunUntilIdle() { |
31 base::RunLoop run_loop; | 31 base::RunLoop run_loop; |
32 run_loop.RunUntilIdle(); | 32 run_loop.RunUntilIdle(); |
33 } | 33 } |
34 | 34 |
35 void DeleteWatcherAndForwardResult( | |
36 HandleWatcher* watcher, | |
37 base::Callback<void(MojoResult)> next_callback, | |
38 MojoResult result) { | |
39 delete watcher; | |
40 next_callback.Run(result); | |
41 } | |
42 | |
43 // Helper class to manage the callback and running the message loop waiting for | 35 // Helper class to manage the callback and running the message loop waiting for |
44 // message to be received. Typical usage is something like: | 36 // message to be received. Typical usage is something like: |
45 // Schedule callback returned from GetCallback(). | 37 // Schedule callback returned from GetCallback(). |
46 // RunUntilGotCallback(); | 38 // RunUntilGotCallback(); |
47 // EXPECT_TRUE(got_callback()); | 39 // EXPECT_TRUE(got_callback()); |
48 // clear_callback(); | 40 // clear_callback(); |
49 class CallbackHelper { | 41 class CallbackHelper { |
50 public: | 42 public: |
51 CallbackHelper() | 43 CallbackHelper() |
52 : got_callback_(false), | 44 : got_callback_(false), |
53 run_loop_(NULL), | 45 run_loop_(NULL), |
54 weak_factory_(this) {} | 46 weak_factory_(this) {} |
55 ~CallbackHelper() {} | 47 ~CallbackHelper() {} |
56 | 48 |
57 // See description above |got_callback_|. | 49 // See description above |got_callback_|. |
58 bool got_callback() const { return got_callback_; } | 50 bool got_callback() const { return got_callback_; } |
59 void clear_callback() { got_callback_ = false; } | 51 void clear_callback() { got_callback_ = false; } |
60 | 52 |
61 // Runs the current MessageLoop until the callback returned from GetCallback() | 53 // Runs the current MessageLoop until the callback returned from GetCallback() |
62 // is notified. | 54 // is notified. |
63 void RunUntilGotCallback() { | 55 void RunUntilGotCallback() { |
64 ASSERT_TRUE(run_loop_ == NULL); | 56 ASSERT_TRUE(run_loop_ == NULL); |
65 base::RunLoop run_loop; | 57 base::RunLoop run_loop; |
66 base::AutoReset<base::RunLoop*> reseter(&run_loop_, &run_loop); | 58 base::AutoReset<base::RunLoop*> reseter(&run_loop_, &run_loop); |
67 run_loop.Run(); | 59 run_loop.Run(); |
68 } | 60 } |
69 | 61 |
70 base::Callback<void(MojoResult)> GetCallback() { | 62 base::Closure GetCallback() { |
71 return base::Bind(&CallbackHelper::OnCallback, weak_factory_.GetWeakPtr()); | 63 return base::Bind(&CallbackHelper::OnCallback, weak_factory_.GetWeakPtr()); |
72 } | 64 } |
73 | 65 |
74 void Start(HandleWatcher* watcher, MojoHandle handle) { | 66 void Start(HandleWatcher* watcher, MojoHandle handle) { |
75 StartWithCallback(watcher, handle, GetCallback()); | |
76 } | |
77 | |
78 void StartWithCallback(HandleWatcher* watcher, MojoHandle handle, | |
79 const base::Callback<void(MojoResult)>& callback) { | |
80 watcher->Start(handle, MOJO_WAIT_FLAG_READABLE, MOJO_DEADLINE_INDEFINITE, | 67 watcher->Start(handle, MOJO_WAIT_FLAG_READABLE, MOJO_DEADLINE_INDEFINITE, |
81 callback); | 68 GetCallback()); |
82 } | 69 } |
83 | 70 |
84 private: | 71 private: |
85 void OnCallback(MojoResult result) { | 72 void OnCallback() { |
86 got_callback_ = true; | 73 got_callback_ = true; |
87 if (run_loop_) | 74 if (run_loop_) |
88 run_loop_->Quit(); | 75 run_loop_->Quit(); |
89 } | 76 } |
90 | 77 |
91 // Set to true when the callback is called. | 78 // Set to true when the callback is called. |
92 bool got_callback_; | 79 bool got_callback_; |
93 | 80 |
94 // If non-NULL we're in RunUntilGotCallback(). | 81 // If non-NULL we're in RunUntilGotCallback(). |
95 base::RunLoop* run_loop_; | 82 base::RunLoop* run_loop_; |
(...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
281 | 268 |
282 HandleWatcher watcher3; | 269 HandleWatcher watcher3; |
283 callback_helper3.Start(&watcher3, test_pipe3.handle_0()); | 270 callback_helper3.Start(&watcher3, test_pipe3.handle_0()); |
284 | 271 |
285 callback_helper2.RunUntilGotCallback(); | 272 callback_helper2.RunUntilGotCallback(); |
286 EXPECT_FALSE(callback_helper1.got_callback()); | 273 EXPECT_FALSE(callback_helper1.got_callback()); |
287 EXPECT_TRUE(callback_helper2.got_callback()); | 274 EXPECT_TRUE(callback_helper2.got_callback()); |
288 EXPECT_FALSE(callback_helper3.got_callback()); | 275 EXPECT_FALSE(callback_helper3.got_callback()); |
289 } | 276 } |
290 | 277 |
291 TEST_F(HandleWatcherTest, DeleteInCallback) { | |
292 ScopedMessagePipe test_pipe; | |
293 CallbackHelper callback_helper; | |
294 | |
295 HandleWatcher* watcher = new HandleWatcher(); | |
296 callback_helper.StartWithCallback(watcher, test_pipe.handle_1(), | |
297 base::Bind(&DeleteWatcherAndForwardResult, | |
298 watcher, | |
299 callback_helper.GetCallback())); | |
300 WriteToHandle(test_pipe.handle_0()); | |
301 callback_helper.RunUntilGotCallback(); | |
302 EXPECT_TRUE(callback_helper.got_callback()); | |
303 } | |
304 | |
305 } // namespace test | 278 } // namespace test |
306 } // namespace common | 279 } // namespace common |
307 } // namespace mojo | 280 } // namespace mojo |
OLD | NEW |