1 // Copyright (C) 2020 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 #include "host-common/AndroidAgentFactory.h"
15
16 #include <stdio.h>
17
18 struct QAndroidAutomationAgent;
19
20 using android::emulation::AndroidConsoleFactory;
21
22 static AndroidConsoleAgents sConsoleAgents{0};
23 static bool isInitialized = false;
24
25
getConsoleAgents()26 const AndroidConsoleAgents* getConsoleAgents() {
27 if (!isInitialized) {
28 // Let's not get involved with undefined behavior, if this happens the
29 // developer is not calling injectConsoleAgents early enough.
30 fprintf(stderr, "Accessing console agents before injecting them.\n");
31 *(uint32_t*)(1234) = 5;
32 }
33 return &sConsoleAgents;
34 }
35
36 #define ANDROID_DEFINE_CONSOLE_GETTER_IMPL(typ, name) \
37 const typ* const AndroidConsoleFactory::android_get_##typ() const { \
38 return sConsoleAgents.name; \
39 };
40
ANDROID_CONSOLE_AGENTS_LIST(ANDROID_DEFINE_CONSOLE_GETTER_IMPL)41 ANDROID_CONSOLE_AGENTS_LIST(ANDROID_DEFINE_CONSOLE_GETTER_IMPL)
42
43 #define ANDROID_CONSOLE_AGENT_SETTER(typ, name) \
44 sConsoleAgents.name = factory.android_get_##typ();
45
46 void android::emulation::injectConsoleAgents(const AndroidConsoleFactory& factory) {
47 if (isInitialized) {
48 // This is not necessarily an error, as there are cases where this might be acceptable.
49 fprintf(stderr, "Warning: console agents were already injected!\n");
50 }
51 ANDROID_CONSOLE_AGENTS_LIST(ANDROID_CONSOLE_AGENT_SETTER);
52 isInitialized = true;
53 }
54