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/task/single_thread_task_runner.h>
32 #include <base/test/task_environment.h>
33 #include <base/threading/thread.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::SingleThreadTaskRunner::GetCurrentDefault();
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::SingleThreadTaskRunner::GetCurrentDefault();
54   }
55 };
56 
57 // Initialize the test task environment
58 #define DEFINE_TEST_TASK_ENV(var) \
59   base::AbstractTestMessageLoop var { base::test::TaskEnvironment::ThreadingMode::MAIN_THREAD_ONLY }
60 
set_message_loop_type_IO(base::Thread::Options & options)61 inline void set_message_loop_type_IO(base::Thread::Options& options) {
62   options.message_pump_type = base::MessagePumpType::IO;
63 }
64 
65 #else
66 class AbstractMessageLoop : public base::MessageLoop {
67 public:
68   static scoped_refptr<base::SingleThreadTaskRunner> current_task_runner() {
69     return base::MessageLoop::current()->task_runner();
70   }
71 };
72 
73 class AbstractTestMessageLoop : public AbstractMessageLoop {};
74 
75 #define DEFINE_TEST_TASK_ENV(var) base::AbstractTestMessageLoop* var
76 
77 inline void set_message_loop_type_IO(base::Thread::Options& options) {
78   options.message_loop_type = base::MessageLoop::TYPE_IO;
79 }
80 
81 #endif
82 
83 }  // namespace btbase
84