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 #include "ani_utils.h"
17
18 #include <ani_signature_builder.h>
19 #include "fs_error.h"
20 #include "utils_log.h"
21
22 namespace OHOS::FileManagement::CloudSync {
23 using namespace arkts::ani_signature;
AniString2String(ani_env * env,ani_string str,std::string & res)24 ani_status ANIUtils::AniString2String(ani_env *env, ani_string str, std::string &res)
25 {
26 ani_size strSize;
27 ani_status ret = env->String_GetUTF8Size(str, &strSize);
28 if (ret != ANI_OK) {
29 LOGE("ani string get size failed. ret = %{public}d", static_cast<int32_t>(ret));
30 return ret;
31 }
32 std::vector<char> buffer(strSize + 1);
33 char *utf8Buffer = buffer.data();
34 ani_size byteWrite = 0;
35 ret = env->String_GetUTF8(str, utf8Buffer, strSize + 1, &byteWrite);
36 if (ret != ANI_OK) {
37 LOGE("ani string to string failed. ret = %{public}d", static_cast<int32_t>(ret));
38 return ret;
39 }
40 utf8Buffer[byteWrite] = '\0';
41 res = std::string(utf8Buffer);
42 return ANI_OK;
43 }
44
ToAniString(ani_env * env,const std::string & str)45 std::tuple<bool, ani_string> ANIUtils::ToAniString(ani_env *env, const std::string &str)
46 {
47 ani_string result;
48 bool succ = (ANI_OK == env->String_NewUTF8(str.c_str(), str.size(), &result));
49 return {succ, std::move(result)};
50 }
51
EnumToInt32(ani_env * env,ani_enum_item enumItem)52 std::tuple<bool, int32_t> ANIUtils::EnumToInt32(ani_env *env, ani_enum_item enumItem)
53 {
54 ani_int result;
55 bool succ = (ANI_OK == env->EnumItem_GetValue_Int(enumItem, &result));
56 return {succ, result};
57 }
58
AniToStringArray(ani_env * env,ani_array_ref strArray)59 std::tuple<bool, std::vector<std::string>> ANIUtils::AniToStringArray(ani_env *env, ani_array_ref strArray)
60 {
61 ani_size arraySize;
62 ani_status ret;
63 if ((ret = env->Array_GetLength(strArray, &arraySize)) != ANI_OK) {
64 LOGE("Failed to get array size, %{public}d", ret);
65 return {false, {}};
66 }
67 std::vector<std::string> res;
68 for (ani_size i = 0; i < arraySize; ++i) {
69 ani_ref strRef;
70 if ((ret = env->Array_Get_Ref(strArray, i, &strRef)) != ANI_OK) {
71 LOGE("Failed to get string item, %{public}d", ret);
72 return {false, {}};
73 }
74 std::string strItem;
75 if ((ret = ANIUtils::AniString2String(env, static_cast<ani_string>(strRef), strItem)) != ANI_OK) {
76 LOGE("Failed to get string item, %{public}d", ret);
77 return {false, {}};
78 }
79 res.emplace_back(strItem);
80 }
81 return {true, res};
82 }
83
ToAniStringArray(ani_env * env,const std::vector<std::string> & strList)84 std::tuple<bool, ani_array_ref> ANIUtils::ToAniStringArray(ani_env *env, const std::vector<std::string> &strList)
85 {
86 size_t length = strList.size();
87 const std::string *strArray = strList.data();
88 std::string classDesc = Builder::BuildClass("std.core.String").Descriptor();
89 ani_class cls = nullptr;
90 if (env->FindClass(classDesc.c_str(), &cls) != ANI_OK) {
91 return {false, nullptr};
92 }
93 ani_array_ref result = nullptr;
94 if (env->Array_New_Ref(cls, length, nullptr, &result) != ANI_OK) {
95 return {false, nullptr};
96 }
97 for (size_t i = 0; i < length; ++i) {
98 auto [succ, item] = ANIUtils::ToAniString(env, strArray[i]);
99 if (!succ) {
100 LOGE("Failed to conver to ani string");
101 return {false, nullptr};
102 }
103 if (env->Array_Set_Ref(result, i, item) != ANI_OK) {
104 LOGE("Failed to set element for array");
105 return {false, nullptr};
106 }
107 }
108 return {true, result};
109 }
110
SendEventToMainThread(const std::function<void ()> func)111 bool ANIUtils::SendEventToMainThread(const std::function<void()> func)
112 {
113 if (func == nullptr) {
114 LOGE("func is nullptr!");
115 return false;
116 }
117
118 if (!mainHandler) {
119 std::shared_ptr<OHOS::AppExecFwk::EventRunner> runner = OHOS::AppExecFwk::EventRunner::GetMainEventRunner();
120 if (!runner) {
121 LOGE("get main event runner failed!");
122 return false;
123 }
124 mainHandler = std::make_shared<OHOS::AppExecFwk::EventHandler>(runner);
125 }
126 mainHandler->PostTask(func, "", 0, OHOS::AppExecFwk::EventQueue::Priority::HIGH, {});
127 return true;
128 }
129 } // namespace OHOS::FileManagement::CloudSync