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