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/service/src/com/android/car/telemetry/proto/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 //  Wrapper class for IScriptExecutorListener.aidl.
57 class ScriptExecutorListener {
58 public:
59     ScriptExecutorListener(JNIEnv* jni, jobject scriptExecutorListener);
60 
61     virtual ~ScriptExecutorListener();
62 
63     void onScriptFinished(jobject bundle);
64 
65     void onSuccess(jobject bundle);
66 
67     void onError(const ErrorType errorType, const char* message, const char* stackTrace);
68 
69     JNIEnv* getCurrentJNIEnv();
70 
71 private:
72     // Stores a jni global reference to Java Script Executor listener object.
73     jobject mScriptExecutorListener;
74 
75     // Stores JavaVM pointer in order to be able to get JNIEnv pointer.
76     // This is done because JNIEnv cannot be shared between threads.
77     // https://developer.android.com/training/articles/perf-jni.html#javavm-and-jnienv
78     JavaVM* mJavaVM;
79 };
80 
81 }  // namespace scriptexecutor
82 }  // namespace car
83 }  // namespace android
84 }  // namespace com
85 
86 #endif  // PACKAGES_SCRIPTEXECUTOR_SRC_SCRIPTEXECUTORLISTENER_H_
87