1 // Copyright 2012 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include <android/looper.h>
6 #include <stdarg.h>
7 #include <string.h>
8
9 #include "base/android/path_utils.h"
10 #include "base/files/file_path.h"
11 #include "base/logging.h"
12 #include "base/memory/raw_ptr.h"
13 #include "base/memory/singleton.h"
14 #include "base/message_loop/message_pump.h"
15 #include "base/message_loop/message_pump_android.h"
16 #include "base/path_service.h"
17 #include "base/synchronization/waitable_event.h"
18 #include "base/test/multiprocess_test.h"
19
20 namespace {
21
22 base::FilePath* g_test_data_dir = nullptr;
23
24 struct RunState {
RunState__anon4c7e9c480111::RunState25 RunState(base::MessagePump::Delegate* delegate, int run_depth)
26 : delegate(delegate),
27 run_depth(run_depth),
28 should_quit(false) {
29 }
30
31 raw_ptr<base::MessagePump::Delegate> delegate;
32
33 // Used to count how many Run() invocations are on the stack.
34 int run_depth;
35
36 // Used to flag that the current Run() invocation should return ASAP.
37 bool should_quit;
38 };
39
40 RunState* g_state = nullptr;
41
42 // A singleton WaitableEvent wrapper so we avoid a busy loop in
43 // MessagePumpForUIStub. Other platforms use the native event loop which blocks
44 // when there are no pending messages.
45 class Waitable {
46 public:
GetInstance()47 static Waitable* GetInstance() {
48 return base::Singleton<Waitable,
49 base::LeakySingletonTraits<Waitable>>::get();
50 }
51
52 Waitable(const Waitable&) = delete;
53 Waitable& operator=(const Waitable&) = delete;
54
55 // Signals that there are more work to do.
Signal()56 void Signal() { waitable_event_.Signal(); }
57
58 // Blocks until more work is scheduled.
Block()59 void Block() { waitable_event_.Wait(); }
60
Quit()61 void Quit() {
62 g_state->should_quit = true;
63 Signal();
64 }
65
66 private:
67 friend struct base::DefaultSingletonTraits<Waitable>;
68
Waitable()69 Waitable()
70 : waitable_event_(base::WaitableEvent::ResetPolicy::AUTOMATIC,
71 base::WaitableEvent::InitialState::NOT_SIGNALED) {}
72
73 base::WaitableEvent waitable_event_;
74 };
75
76 // The MessagePumpForUI implementation for test purpose.
77 class MessagePumpForUIStub : public base::MessagePumpForUI {
78 public:
MessagePumpForUIStub()79 MessagePumpForUIStub() : base::MessagePumpForUI() { Waitable::GetInstance(); }
~MessagePumpForUIStub()80 ~MessagePumpForUIStub() override {}
81
82 // In tests, there isn't a native thread, as such RunLoop::Run() should be
83 // used to run the loop instead of attaching and delegating to the native
84 // loop. As such, this override ignores the Attach() request.
Attach(base::MessagePump::Delegate * delegate)85 void Attach(base::MessagePump::Delegate* delegate) override {}
86
Run(base::MessagePump::Delegate * delegate)87 void Run(base::MessagePump::Delegate* delegate) override {
88 // The following was based on message_pump_glib.cc, except we're using a
89 // WaitableEvent since there are no native message loop to use.
90 RunState state(delegate, g_state ? g_state->run_depth + 1 : 1);
91
92 RunState* previous_state = g_state;
93 g_state = &state;
94
95 // When not nested we can use the looper, otherwise fall back
96 // to the stub implementation.
97 if (g_state->run_depth > 1) {
98 RunNested(delegate);
99 } else {
100 SetQuit(false);
101 SetDelegate(delegate);
102
103 // Pump the loop once in case we're starting off idle as ALooper_pollOnce
104 // will never return in that case.
105 ScheduleWork();
106 while (true) {
107 // Waits for either the delayed, or non-delayed fds to be signalled,
108 // calling either OnDelayedLooperCallback, or
109 // OnNonDelayedLooperCallback, respectively. This uses Android's Looper
110 // implementation, which is based off of epoll.
111 ALooper_pollOnce(-1, nullptr, nullptr, nullptr);
112 if (ShouldQuit())
113 break;
114 }
115 }
116
117 g_state = previous_state;
118 }
119
RunNested(base::MessagePump::Delegate * delegate)120 void RunNested(base::MessagePump::Delegate* delegate) {
121 bool more_work_is_plausible = true;
122
123 for (;;) {
124 if (!more_work_is_plausible) {
125 Waitable::GetInstance()->Block();
126 if (g_state->should_quit)
127 break;
128 }
129
130 Delegate::NextWorkInfo next_work_info = g_state->delegate->DoWork();
131 more_work_is_plausible = next_work_info.is_immediate();
132 if (g_state->should_quit)
133 break;
134
135 if (more_work_is_plausible)
136 continue;
137
138 more_work_is_plausible = g_state->delegate->DoIdleWork();
139 if (g_state->should_quit)
140 break;
141
142 more_work_is_plausible |= !next_work_info.delayed_run_time.is_max();
143 }
144 }
145
Quit()146 void Quit() override {
147 CHECK(g_state);
148 if (g_state->run_depth > 1) {
149 Waitable::GetInstance()->Quit();
150 } else {
151 MessagePumpForUI::Quit();
152 }
153 }
154
ScheduleWork()155 void ScheduleWork() override {
156 if (g_state && g_state->run_depth > 1) {
157 Waitable::GetInstance()->Signal();
158 } else {
159 MessagePumpForUI::ScheduleWork();
160 }
161 }
162
ScheduleDelayedWork(const Delegate::NextWorkInfo & next_work_info)163 void ScheduleDelayedWork(
164 const Delegate::NextWorkInfo& next_work_info) override {
165 if (g_state && g_state->run_depth > 1) {
166 Waitable::GetInstance()->Signal();
167 } else {
168 MessagePumpForUI::ScheduleDelayedWork(next_work_info);
169 }
170 }
171 };
172
CreateMessagePumpForUIStub()173 std::unique_ptr<base::MessagePump> CreateMessagePumpForUIStub() {
174 return std::unique_ptr<base::MessagePump>(new MessagePumpForUIStub());
175 }
176
177 // Provides the test path for paths overridden during tests.
GetTestProviderPath(int key,base::FilePath * result)178 bool GetTestProviderPath(int key, base::FilePath* result) {
179 switch (key) {
180 // On Android, our tests don't have permission to write to DIR_MODULE.
181 // gtest/test_runner.py pushes data to external storage.
182 // TODO(agrieve): Stop overriding DIR_ANDROID_APP_DATA.
183 // https://crbug.com/617734
184 // Instead DIR_ASSETS should be used to discover assets file location in
185 // tests.
186 case base::DIR_ANDROID_APP_DATA:
187 case base::DIR_ASSETS:
188 case base::DIR_SRC_TEST_DATA_ROOT:
189 case base::DIR_OUT_TEST_DATA_ROOT:
190 CHECK(g_test_data_dir != nullptr);
191 *result = *g_test_data_dir;
192 return true;
193 default:
194 return false;
195 }
196 }
197
InitPathProvider(int key)198 void InitPathProvider(int key) {
199 base::FilePath path;
200 // If failed to override the key, that means the way has not been registered.
201 if (GetTestProviderPath(key, &path) &&
202 !base::PathService::Override(key, path)) {
203 base::PathService::RegisterProvider(&GetTestProviderPath, key, key + 1);
204 }
205 }
206
207 } // namespace
208
209 namespace base {
210
InitAndroidTestPaths(const FilePath & test_data_dir)211 void InitAndroidTestPaths(const FilePath& test_data_dir) {
212 if (g_test_data_dir) {
213 CHECK(test_data_dir == *g_test_data_dir);
214 return;
215 }
216 g_test_data_dir = new FilePath(test_data_dir);
217 InitPathProvider(DIR_ANDROID_APP_DATA);
218 InitPathProvider(DIR_ASSETS);
219 InitPathProvider(DIR_SRC_TEST_DATA_ROOT);
220 InitPathProvider(DIR_OUT_TEST_DATA_ROOT);
221 }
222
InitAndroidTestMessageLoop()223 void InitAndroidTestMessageLoop() {
224 // NOTE something else such as a JNI call may have already overridden the UI
225 // factory.
226 if (!MessagePump::IsMessagePumpForUIFactoryOveridden())
227 MessagePump::OverrideMessagePumpForUIFactory(&CreateMessagePumpForUIStub);
228 }
229
230 } // namespace base
231