1 /*
2  * Copyright (c) 2021, The Android Open Source Project
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 #ifndef PACKAGES_SCRIPTEXECUTOR_SRC_SCRIPTEXECUTORLISTENER_H_
18 #define PACKAGES_SCRIPTEXECUTOR_SRC_SCRIPTEXECUTORLISTENER_H_
19 
20 #include "jni.h"
21 
22 #include <string>
23 
24 namespace com {
25 namespace android {
26 namespace car {
27 namespace scriptexecutor {
28 
29 // Changes in this enum must also be reflected in:
30 // p/s/C/service/src/com/android/car/telemetry/scriptexecutorinterface/IScriptExecutorListener.aidl
31 // p/s/C/car-lib/src/android/car/telemetry/telemetry.proto
32 enum ErrorType {
33     /**
34      * Default error type.
35      */
36     ERROR_TYPE_UNSPECIFIED = 0,
37 
38     /**
39      * Used when an error occurs in the ScriptExecutor code.
40      */
41     ERROR_TYPE_SCRIPT_EXECUTOR_ERROR = 1,
42 
43     /**
44      * Used when an error occurs while executing the Lua script (such as
45      * errors returned by lua_pcall)
46      */
47     ERROR_TYPE_LUA_RUNTIME_ERROR = 2,
48 
49     /**
50      * Used to log errors by a script itself, for instance, when a script received
51      * inputs outside of expected range.
52      */
53     ERROR_TYPE_LUA_SCRIPT_ERROR = 3,
54 
55     /**
56      * Used to log errors for publisher failures.
57      */
58     ERROR_TYPE_PUBLISHER_FAILED = 4,
59 };
60 
61 //  Wrapper class for IScriptExecutorListener.aidl.
62 class ScriptExecutorListener {
63 public:
64     ScriptExecutorListener(JNIEnv* jni, jobject scriptExecutorListener);
65 
66     virtual ~ScriptExecutorListener();
67 
68     void onScriptFinished(jobject bundle);
69 
70     void onSuccess(jobject bundle);
71 
72     void onError(const ErrorType errorType, const char* message, const char* stackTrace);
73 
74     void onMetricsReport(jobject reportBundle, jobject stateBundle);
75 
76     JNIEnv* getCurrentJNIEnv();
77 
78 private:
79     // Stores a jni global reference to Java Script Executor listener object.
80     jobject mScriptExecutorListener;
81 
82     // Stores JavaVM pointer in order to be able to get JNIEnv pointer.
83     // This is done because JNIEnv cannot be shared between threads.
84     // https://developer.android.com/training/articles/perf-jni.html#javavm-and-jnienv
85     JavaVM* mJavaVM;
86 };
87 
88 }  // namespace scriptexecutor
89 }  // namespace car
90 }  // namespace android
91 }  // namespace com
92 
93 #endif  // PACKAGES_SCRIPTEXECUTOR_SRC_SCRIPTEXECUTORLISTENER_H_
94