1 //
2 //  Copyright 2021 Google, Inc.
3 //
4 //  Licensed under the Apache License, Version 2.0 (the "License");
5 //  you may not use this file except in compliance with the License.
6 //  You may obtain a copy of the License at:
7 //
8 //  http://www.apache.org/licenses/LICENSE-2.0
9 //
10 //  Unless required by applicable law or agreed to in writing, software
11 //  distributed under the License is distributed on an "AS IS" BASIS,
12 //  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 //  See the License for the specific language governing permissions and
14 //  limitations under the License.
15 //
16 
17 #pragma once
18 
19 #if defined(BASE_VER) && BASE_VER > 780000
20 /* libchrome version < 780000 still has the old message loop. Android still uses
21  * the old libchrome version so use the basic messageloop where that's required.
22  * Elsewhere, use the SingleThreadTaskExecutor instead.
23  */
24 #if BASE_VER >= 822064
25 #include <base/task/current_thread.h>
26 #else
27 #include <base/message_loop/message_loop_current.h>
28 #endif
29 #include <base/message_loop/message_pump.h>
30 #include <base/task/single_thread_task_executor.h>
31 #include <base/test/task_environment.h>
32 #include <base/threading/thread.h>
33 #include <base/threading/thread_task_runner_handle.h>
34 #else
35 #include <base/message_loop/message_loop.h>
36 #include <base/threading/thread.h>
37 #endif
38 
39 namespace btbase {
40 
41 #if defined(BASE_VER) && BASE_VER > 780000
42 
43 class AbstractMessageLoop : public base::SingleThreadTaskExecutor {
44  public:
current_task_runner()45   static scoped_refptr<base::SingleThreadTaskRunner> current_task_runner() {
46     return base::ThreadTaskRunnerHandle::Get();
47   }
48 };
49 
50 class AbstractTestMessageLoop : public base::test::TaskEnvironment {
51  public:
current_task_runner()52   static scoped_refptr<base::SingleThreadTaskRunner> current_task_runner() {
53     return base::ThreadTaskRunnerHandle::Get();
54   }
55 };
56 
57 // Initialize the test task environment
58 #define DEFINE_TEST_TASK_ENV(var)                                \
59   base::AbstractTestMessageLoop var {                            \
60     base::test::TaskEnvironment::ThreadingMode::MAIN_THREAD_ONLY \
61   }
62 
set_message_loop_type_IO(base::Thread::Options & options)63 inline void set_message_loop_type_IO(base::Thread::Options& options) {
64   options.message_pump_type = base::MessagePumpType::IO;
65 }
66 
67 #else
68 class AbstractMessageLoop : public base::MessageLoop {
69  public:
70   static scoped_refptr<base::SingleThreadTaskRunner> current_task_runner() {
71     return base::MessageLoop::current()->task_runner();
72   }
73 };
74 
75 class AbstractTestMessageLoop : public AbstractMessageLoop {};
76 
77 #define DEFINE_TEST_TASK_ENV(var) base::AbstractTestMessageLoop* var
78 
79 inline void set_message_loop_type_IO(base::Thread::Options& options) {
80   options.message_loop_type = base::MessageLoop::TYPE_IO;
81 }
82 
83 #endif
84 
85 }  // namespace btbase
86