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_BUNDLEWRAPPER_H_
18 #define PACKAGES_SCRIPTEXECUTOR_SRC_BUNDLEWRAPPER_H_
19 
20 #include "jni.h"
21 
22 #include <android-base/result.h>
23 
24 #include <string>
25 #include <vector>
26 
27 namespace com {
28 namespace android {
29 namespace car {
30 namespace scriptexecutor {
31 
32 // Used to create a java bundle object and populate its fields one at a time.
33 class BundleWrapper {
34 public:
35     explicit BundleWrapper(JNIEnv* env);
36     // BundleWrapper is not copyable.
37     BundleWrapper(const BundleWrapper&) = delete;
38     BundleWrapper& operator=(const BundleWrapper&) = delete;
39 
40     virtual ~BundleWrapper();
41 
42     // Family of methods that puts the provided 'value' into the PersistableBundle
43     // under provided 'key'.
44     ::android::base::Result<void> putBoolean(const char* key, bool value);
45     ::android::base::Result<void> putLong(const char* key, int64_t value);
46     ::android::base::Result<void> putDouble(const char* key, double value);
47     ::android::base::Result<void> putString(const char* key, const char* value);
48     ::android::base::Result<void> putLongArray(const char* key, const std::vector<int64_t>& value);
49     ::android::base::Result<void> putStringArray(const char* key,
50                                                  const std::vector<std::string>& value);
51 
52     jobject getBundle();
53 
54 private:
55     // The class asks Java to create PersistableBundle object and stores the reference.
56     // When the instance of this class is destroyed the actual Java PersistableBundle object behind
57     // this reference stays on and is managed by Java.
58     jobject mBundle;
59 
60     // Reference to java PersistableBundle class cached for performance reasons.
61     jclass mBundleClass;
62 
63     // Stores a JNIEnv* pointer.
64     JNIEnv* mJNIEnv;  // not owned
65 };
66 
67 }  // namespace scriptexecutor
68 }  // namespace car
69 }  // namespace android
70 }  // namespace com
71 
72 #endif  // PACKAGES_SCRIPTEXECUTOR_SRC_BUNDLEWRAPPER_H_
73