• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 Huawei Device Co., Ltd.
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  */
15 
16 #ifndef INTERFACES_ETS_ANI_COMMON_ANI_UTILS_H
17 #define INTERFACES_ETS_ANI_COMMON_ANI_UTILS_H
18 
19 #include <thread>
20 #include <string>
21 #include <vector>
22 
23 #include "ani.h"
24 
25 namespace OHOS {
26 namespace Security {
27 namespace AccessToken {
28 bool AniFindNameSpace(ani_env* env, const std::string& namespaceDescriptor, ani_namespace& out);
29 bool AniFindClass(ani_env* env, const std::string&  classDescriptor, ani_class& out);
30 bool AniClassFindMethod(
31     ani_env* env, const ani_class& aniClass, const std::string& methodDescriptor,
32     const std::string& signature, ani_method& out);
33 bool AniClassFindField(ani_env* env, const ani_class& aniClass, const std::string& fieldName, ani_field& out);
34 
35 bool AniParseCallback(ani_env* env, const ani_ref& ani_callback, ani_ref& out);
36 bool AniIsRefUndefined(ani_env* env, const ani_ref& ref);
37 bool AniParseUint32(ani_env* env, const ani_int& aniInt, uint32_t& out);
38 bool AniParseAccessTokenIDArray(ani_env* env, const ani_array_ref& array, std::vector<uint32_t>& out);
39 
40 bool GetBoolProperty(ani_env* env, const ani_object& object, const std::string& property, bool& value);
41 bool GetIntProperty(ani_env* env, const ani_object& object, const std::string& property, int32_t& value);
42 bool GetLongProperty(ani_env* env, const ani_object& object, const std::string& property, int64_t& value);
43 bool GetStringProperty(ani_env* env, const ani_object& object, const std::string& property, std::string& value);
44 bool GetEnumProperty(ani_env* env, const ani_object& object, const std::string& property, int32_t& value);
45 bool GetStringVecProperty(
46     ani_env* env, const ani_object& object, const std::string& property, std::vector<std::string>& value);
47 
48 bool SetBoolProperty(ani_env* env, ani_object& object, const std::string& property, bool in);
49 bool SetIntProperty(ani_env* env, ani_object& object, const std::string& property, int32_t in);
50 bool SetLongProperty(ani_env* env, ani_object& object, const std::string& property, int64_t in);
51 bool SetRefProperty(ani_env* env, ani_object& object, const std::string& property, const ani_ref& in);
52 bool SetStringProperty(ani_env* env, ani_object& aniObject, const std::string& property, const std::string& in);
53 bool SetEnumProperty(
54     ani_env* env, ani_object& aniObject, const std::string& enumDescription,
55     const std::string& property, uint32_t value);
56 bool SetOptionalIntProperty(ani_env* env, ani_object& aniObject, const std::string& property, int32_t in);
57 
58 bool IsCurrentThread(std::thread::id threadId);
59 bool AniIsCallbackRefEqual(ani_env* env, const ani_ref& compareRef, const ani_ref& targetRref, std::thread::id threadId,
60     bool& isEqual);
61 bool AniFunctionalObjectCall(ani_env *env, const ani_fn_object& fn, ani_size size, ani_ref* argv, ani_ref& result);
62 
63 // ani to naitive
64 std::string ParseAniString(ani_env* env, const ani_string& aniStr);
65 std::vector<std::string> ParseAniStringVector(ani_env* env, const ani_array_ref& aniStrArr);
66 
67 // native to ani
68 ani_string CreateAniString(ani_env *env, const std::string& str);
69 ani_object CreateIntObject(ani_env *env, int32_t value);
70 ani_object CreateBooleanObject(ani_env *env, bool value);
71 ani_object CreateClassObject(ani_env* env, const std::string& classDescriptor);
72 ani_object CreateArrayObject(ani_env* env, uint32_t length);
73 
74 ani_ref CreateAniArrayBool(ani_env* env, const std::vector<bool>& cArray);
75 ani_ref CreateAniArrayInt(ani_env* env, const std::vector<int32_t>& cArray);
76 ani_ref CreateAniArrayString(ani_env* env, const std::vector<std::string>& cArray);
77 ani_env* GetCurrentEnv(ani_vm* vm);
78 
79 // delete ref of GlobalReference_Create
80 void DeleteReference(ani_env* env, ani_ref& ref);
81 } // namespace AccessToken
82 } // namespace Security
83 } // namespace OHOS
84 #endif /* INTERFACES_ETS_ANI_COMMON_ANI_UTILS_H */
85