1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
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/macros.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__anon688ab7ce0111::RunState25   RunState(base::MessagePump::Delegate* delegate, int run_depth)
26       : delegate(delegate),
27         run_depth(run_depth),
28         should_quit(false) {
29   }
30 
31   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   // Signals that there are more work to do.
Signal()53   void Signal() { waitable_event_.Signal(); }
54 
55   // Blocks until more work is scheduled.
Block()56   void Block() { waitable_event_.Wait(); }
57 
Quit()58   void Quit() {
59     g_state->should_quit = true;
60     Signal();
61   }
62 
63  private:
64   friend struct base::DefaultSingletonTraits<Waitable>;
65 
Waitable()66   Waitable()
67       : waitable_event_(base::WaitableEvent::ResetPolicy::AUTOMATIC,
68                         base::WaitableEvent::InitialState::NOT_SIGNALED) {}
69 
70   base::WaitableEvent waitable_event_;
71 
72   DISALLOW_COPY_AND_ASSIGN(Waitable);
73 };
74 
75 // The MessagePumpForUI implementation for test purpose.
76 class MessagePumpForUIStub : public base::MessagePumpForUI {
77  public:
MessagePumpForUIStub()78   MessagePumpForUIStub() : base::MessagePumpForUI() { Waitable::GetInstance(); }
~MessagePumpForUIStub()79   ~MessagePumpForUIStub() override {}
80 
81   // In tests, there isn't a native thread, as such RunLoop::Run() should be
82   // used to run the loop instead of attaching and delegating to the native
83   // loop. As such, this override ignores the Attach() request.
Attach(base::MessagePump::Delegate * delegate)84   void Attach(base::MessagePump::Delegate* delegate) override {}
85 
Run(base::MessagePump::Delegate * delegate)86   void Run(base::MessagePump::Delegate* delegate) override {
87     // The following was based on message_pump_glib.cc, except we're using a
88     // WaitableEvent since there are no native message loop to use.
89     RunState state(delegate, g_state ? g_state->run_depth + 1 : 1);
90 
91     RunState* previous_state = g_state;
92     g_state = &state;
93 
94     // When not nested we can use the looper, otherwise fall back
95     // to the stub implementation.
96     if (g_state->run_depth > 1) {
97       RunNested(delegate);
98     } else {
99       ResetShouldQuit();
100 
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 base::TimeTicks & delayed_work_time)163   void ScheduleDelayedWork(const base::TimeTicks& delayed_work_time) override {
164     if (g_state && g_state->run_depth > 1) {
165       Waitable::GetInstance()->Signal();
166     } else {
167       MessagePumpForUI::ScheduleDelayedWork(delayed_work_time);
168     }
169   }
170 };
171 
CreateMessagePumpForUIStub()172 std::unique_ptr<base::MessagePump> CreateMessagePumpForUIStub() {
173   return std::unique_ptr<base::MessagePump>(new MessagePumpForUIStub());
174 }
175 
176 // Provides the test path for DIR_SOURCE_ROOT and DIR_ANDROID_APP_DATA.
GetTestProviderPath(int key,base::FilePath * result)177 bool GetTestProviderPath(int key, base::FilePath* result) {
178   switch (key) {
179     // TODO(agrieve): Stop overriding DIR_ANDROID_APP_DATA.
180     // https://crbug.com/617734
181     // Instead DIR_ASSETS should be used to discover assets file location in
182     // tests.
183     case base::DIR_ANDROID_APP_DATA:
184     case base::DIR_ASSETS:
185     case base::DIR_SOURCE_ROOT:
186       CHECK(g_test_data_dir != nullptr);
187       *result = *g_test_data_dir;
188       return true;
189     default:
190       return false;
191   }
192 }
193 
InitPathProvider(int key)194 void InitPathProvider(int key) {
195   base::FilePath path;
196   // If failed to override the key, that means the way has not been registered.
197   if (GetTestProviderPath(key, &path) &&
198       !base::PathService::Override(key, path)) {
199     base::PathService::RegisterProvider(&GetTestProviderPath, key, key + 1);
200   }
201 }
202 
203 }  // namespace
204 
205 namespace base {
206 
InitAndroidTestLogging()207 void InitAndroidTestLogging() {
208   logging::LoggingSettings settings;
209   settings.logging_dest =
210       logging::LOG_TO_SYSTEM_DEBUG_LOG | logging::LOG_TO_STDERR;
211   logging::InitLogging(settings);
212   // To view log output with IDs and timestamps use "adb logcat -v threadtime".
213   logging::SetLogItems(false,    // Process ID
214                        false,    // Thread ID
215                        false,    // Timestamp
216                        false);   // Tick count
217 }
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_SOURCE_ROOT);
226   InitPathProvider(DIR_ANDROID_APP_DATA);
227   InitPathProvider(DIR_ASSETS);
228 }
229 
InitAndroidTestMessageLoop()230 void InitAndroidTestMessageLoop() {
231   // NOTE something else such as a JNI call may have already overridden the UI
232   // factory.
233   if (!MessagePump::IsMessagePumpForUIFactoryOveridden())
234     MessagePump::OverrideMessagePumpForUIFactory(&CreateMessagePumpForUIStub);
235 }
236 
237 }  // namespace base
238