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 #ifndef PACKAGES_SCRIPTEXECUTOR_SRC_JNIUTILS_H_
17 #define PACKAGES_SCRIPTEXECUTOR_SRC_JNIUTILS_H_
18 
19 #include "jni.h"
20 
21 extern "C" {
22 #include "lua.h"
23 }
24 
25 #include "BundleWrapper.h"
26 
27 #include <android-base/result.h>
28 
29 namespace com {
30 namespace android {
31 namespace car {
32 namespace scriptexecutor {
33 
34 using ::android::base::Error;
35 using ::android::base::Result;
36 
37 // Helper function which takes android.os.Bundle object in "bundle" argument
38 // and converts it to Lua table on top of Lua stack. All key-value pairs are
39 // converted to the corresponding key-value pairs of the Lua table as long as
40 // the Bundle value types are supported. At this point, we support boolean,
41 // integer, double and String types in Java.
42 void pushBundleToLuaTable(JNIEnv* env, lua_State* lua, jobject bundle);
43 
44 // Helper function that takes list of android.os.Bundle object in "bundleList"
45 // argument and converts it to Lua table on top of the Lua stack. The Lua table
46 // will be an array of converted android.os.Bundle objects in Lua table form,
47 // each having the key-value pairs as converted by pushBundleToLuaTable.
48 void pushBundleListToLuaTable(JNIEnv* env, lua_State* lua, jobject bundleList);
49 
50 // Helper function that goes over Lua table fields one by one and populates PersistableBundle
51 // object wrapped in BundleWrapper.
52 // It is assumed that Lua table is located on top of the Lua stack. There could be other
53 // items in the stack, this function will not touch them.
54 //
55 // Returns false if the conversion encountered unrecoverable error.
56 // Otherwise, returns true for success.
57 //
58 // If the function succeeds, the stack should be unchanged.
59 // In case of an error, there is no need to pop elements or clean the stack. When Lua calls C,
60 // the stack used to pass data between Lua and C is private for each call. According to
61 // https://www.lua.org/pil/26.1.html, after C function returns back to Lua, Lua
62 // removes everything that is in the stack below the returned results.
63 // TODO(b/200849134): Refactor this function.
64 Result<void> convertLuaTableToBundle(JNIEnv* env, lua_State* lua, BundleWrapper* bundleWrapper);
65 
66 }  // namespace scriptexecutor
67 }  // namespace car
68 }  // namespace android
69 }  // namespace com
70 
71 #endif  // PACKAGES_SCRIPTEXECUTOR_SRC_JNIUTILS_H_
72