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 #include "mojo/edk/test/mojo_test_base.h" | 5 #include "mojo/edk/test/mojo_test_base.h" |
6 | 6 |
7 #include "base/memory/ptr_util.h" | 7 #include "base/memory/ptr_util.h" |
8 #include "base/memory/ref_counted.h" | 8 #include "base/memory/ref_counted.h" |
9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
11 #include "base/synchronization/waitable_event.h" | 11 #include "base/synchronization/waitable_event.h" |
12 #include "mojo/edk/embedder/embedder.h" | 12 #include "mojo/edk/embedder/embedder.h" |
13 #include "mojo/edk/system/handle_signals_state.h" | 13 #include "mojo/edk/system/handle_signals_state.h" |
14 #include "mojo/public/c/system/buffer.h" | 14 #include "mojo/public/c/system/buffer.h" |
15 #include "mojo/public/c/system/data_pipe.h" | 15 #include "mojo/public/c/system/data_pipe.h" |
16 #include "mojo/public/c/system/functions.h" | 16 #include "mojo/public/c/system/functions.h" |
17 #include "mojo/public/c/system/watcher.h" | 17 #include "mojo/public/c/system/watcher.h" |
| 18 #include "mojo/public/cpp/system/wait.h" |
18 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
19 | 20 |
20 #if defined(OS_MACOSX) && !defined(OS_IOS) | 21 #if defined(OS_MACOSX) && !defined(OS_IOS) |
21 #include "base/mac/mach_port_broker.h" | 22 #include "base/mac/mach_port_broker.h" |
22 #endif | 23 #endif |
23 | 24 |
24 namespace mojo { | 25 namespace mojo { |
25 namespace edk { | 26 namespace edk { |
26 namespace test { | 27 namespace test { |
27 | 28 |
28 namespace { | |
29 | |
30 class Waiter { | |
31 public: | |
32 Waiter() {} | |
33 ~Waiter() {} | |
34 | |
35 MojoResult Wait(MojoHandle handle, | |
36 MojoHandleSignals signals, | |
37 MojoHandleSignalsState* state) { | |
38 MojoHandle watcher; | |
39 MojoCreateWatcher(&Context::OnNotification, &watcher); | |
40 | |
41 context_ = new Context(); | |
42 | |
43 // Balanced by OnNotification in the |MOJO_RESULT_CANCELLED| case. | |
44 context_->AddRef(); | |
45 | |
46 MojoResult rv = MojoWatch(watcher, handle, signals, | |
47 reinterpret_cast<uintptr_t>(context_.get())); | |
48 DCHECK_EQ(MOJO_RESULT_OK, rv); | |
49 | |
50 uint32_t num_ready_contexts = 1; | |
51 uintptr_t ready_context; | |
52 MojoResult ready_result; | |
53 MojoHandleSignalsState ready_state; | |
54 rv = MojoArmWatcher(watcher, &num_ready_contexts, &ready_context, | |
55 &ready_result, &ready_state); | |
56 if (rv == MOJO_RESULT_FAILED_PRECONDITION) { | |
57 MojoClose(watcher); | |
58 DCHECK_EQ(1u, num_ready_contexts); | |
59 if (state) | |
60 *state = ready_state; | |
61 return ready_result; | |
62 } | |
63 | |
64 // Wait for the first notification. | |
65 context_->event().Wait(); | |
66 | |
67 ready_result = context_->wait_result(); | |
68 DCHECK_NE(MOJO_RESULT_UNKNOWN, ready_result); | |
69 | |
70 if (state) | |
71 *state = context_->wait_state(); | |
72 | |
73 MojoClose(watcher); | |
74 | |
75 return ready_result; | |
76 } | |
77 | |
78 private: | |
79 class Context : public base::RefCountedThreadSafe<Context> { | |
80 public: | |
81 Context() | |
82 : event_(base::WaitableEvent::ResetPolicy::AUTOMATIC, | |
83 base::WaitableEvent::InitialState::NOT_SIGNALED) {} | |
84 | |
85 base::WaitableEvent& event() { return event_; } | |
86 MojoResult wait_result() const { return wait_result_; } | |
87 MojoHandleSignalsState wait_state() const { return wait_state_; } | |
88 | |
89 static void OnNotification(uintptr_t context_value, | |
90 MojoResult result, | |
91 MojoHandleSignalsState state, | |
92 MojoWatcherNotificationFlags flags) { | |
93 auto* context = reinterpret_cast<Context*>(context_value); | |
94 context->Notify(result, state); | |
95 if (result == MOJO_RESULT_CANCELLED) | |
96 context->Release(); | |
97 } | |
98 | |
99 private: | |
100 friend class base::RefCountedThreadSafe<Context>; | |
101 | |
102 ~Context() {} | |
103 | |
104 void Notify(MojoResult result, MojoHandleSignalsState state) { | |
105 if (wait_result_ == MOJO_RESULT_UNKNOWN) { | |
106 wait_result_ = result; | |
107 wait_state_ = state; | |
108 } | |
109 event_.Signal(); | |
110 } | |
111 | |
112 base::WaitableEvent event_; | |
113 MojoResult wait_result_ = MOJO_RESULT_UNKNOWN; | |
114 MojoHandleSignalsState wait_state_ = {0, 0}; | |
115 | |
116 DISALLOW_COPY_AND_ASSIGN(Context); | |
117 }; | |
118 | |
119 scoped_refptr<Context> context_; | |
120 | |
121 DISALLOW_COPY_AND_ASSIGN(Waiter); | |
122 }; | |
123 | |
124 } // namespace | |
125 | |
126 #if defined(OS_MACOSX) && !defined(OS_IOS) | 29 #if defined(OS_MACOSX) && !defined(OS_IOS) |
127 namespace { | 30 namespace { |
128 base::MachPortBroker* g_mach_broker = nullptr; | 31 base::MachPortBroker* g_mach_broker = nullptr; |
129 } | 32 } |
130 #endif | 33 #endif |
131 | 34 |
132 MojoTestBase::MojoTestBase() { | 35 MojoTestBase::MojoTestBase() { |
133 #if defined(OS_MACOSX) && !defined(OS_IOS) | 36 #if defined(OS_MACOSX) && !defined(OS_IOS) |
134 if (!g_mach_broker) { | 37 if (!g_mach_broker) { |
135 g_mach_broker = new base::MachPortBroker("mojo_test"); | 38 g_mach_broker = new base::MachPortBroker("mojo_test"); |
(...skipping 273 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
409 MojoHandleSignalsState MojoTestBase::GetSignalsState(MojoHandle handle) { | 312 MojoHandleSignalsState MojoTestBase::GetSignalsState(MojoHandle handle) { |
410 MojoHandleSignalsState signals_state; | 313 MojoHandleSignalsState signals_state; |
411 CHECK_EQ(MOJO_RESULT_OK, MojoQueryHandleSignalsState(handle, &signals_state)); | 314 CHECK_EQ(MOJO_RESULT_OK, MojoQueryHandleSignalsState(handle, &signals_state)); |
412 return signals_state; | 315 return signals_state; |
413 } | 316 } |
414 | 317 |
415 // static | 318 // static |
416 MojoResult MojoTestBase::WaitForSignals(MojoHandle handle, | 319 MojoResult MojoTestBase::WaitForSignals(MojoHandle handle, |
417 MojoHandleSignals signals, | 320 MojoHandleSignals signals, |
418 MojoHandleSignalsState* state) { | 321 MojoHandleSignalsState* state) { |
419 Waiter waiter; | 322 return Wait(Handle(handle), signals, state); |
420 return waiter.Wait(handle, signals, state); | |
421 } | 323 } |
422 | 324 |
423 } // namespace test | 325 } // namespace test |
424 } // namespace edk | 326 } // namespace edk |
425 } // namespace mojo | 327 } // namespace mojo |
OLD | NEW |