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 <hitrace_meter.h>
17
18 #include "ani.h"
19 #include "screen_ani_utils.h"
20 #include "screen.h"
21 #include "singleton_container.h"
22 #include "screen_manager.h"
23 #include "window_manager_hilog.h"
24 #include "dm_common.h"
25 #include "refbase.h"
26
27 namespace OHOS {
28 namespace Rosen {
GetStdString(ani_env * env,ani_string ani_str,std::string & result)29 ani_status ScreenAniUtils::GetStdString(ani_env *env, ani_string ani_str, std::string &result)
30 {
31 ani_size strSize;
32 ani_status ret = env->String_GetUTF8Size(ani_str, &strSize);
33 if (ret != ANI_OK) {
34 return ret;
35 }
36 std::vector<char> buffer(strSize + 1);
37 char* utf8_buffer = buffer.data();
38 ani_size bytes_written = 0;
39 ret = env->String_GetUTF8(ani_str, utf8_buffer, strSize + 1, &bytes_written);
40 if (ret != ANI_OK) {
41 return ret;
42 }
43 utf8_buffer[bytes_written] = '\0';
44 result = std::string(utf8_buffer);
45 return ret;
46 }
47
CreateAniUndefined(ani_env * env)48 ani_object ScreenAniUtils::CreateAniUndefined(ani_env* env)
49 {
50 ani_ref aniRef;
51 env->GetUndefined(&aniRef);
52 return static_cast<ani_object>(aniRef);
53 }
54
GetAniString(ani_env * env,const std::string & str,ani_string * result)55 ani_status ScreenAniUtils::GetAniString(ani_env* env, const std::string& str, ani_string* result)
56 {
57 return env->String_NewUTF8(str.c_str(), static_cast<ani_size>(str.size()), result);
58 }
59
CallAniFunctionVoid(ani_env * env,const char * ns,const char * fn,const char * signature,...)60 ani_status ScreenAniUtils::CallAniFunctionVoid(ani_env *env, const char* ns,
61 const char* fn, const char* signature, ...)
62 {
63 TLOGI(WmsLogTag::DMS, "[ANI]CallAniFunctionVoid begin");
64 ani_status ret = ANI_OK;
65 ani_namespace aniNamespace{};
66 if ((ret = env->FindNamespace(ns, &aniNamespace)) != ANI_OK) {
67 TLOGE(WmsLogTag::DMS, "[ANI]canot find ns %{public}d", ret);
68 return ret;
69 }
70 ani_function func{};
71 if ((ret = env->Namespace_FindFunction(aniNamespace, fn, signature, &func)) != ANI_OK) {
72 TLOGE(WmsLogTag::DMS, "[ANI]canot find callBack %{public}d", ret);
73 return ret;
74 }
75 va_list args;
76 va_start(args, signature);
77 TLOGI(WmsLogTag::DMS, "[ANI]CallAniFunctionVoid begin %{public}s", signature);
78 if (func == nullptr) {
79 TLOGI(WmsLogTag::DMS, "[ANI] null func ani");
80 return ret;
81 }
82 ret = env->Function_Call_Void_V(func, args);
83 va_end(args);
84 if (ret != ANI_OK) {
85 TLOGE(WmsLogTag::DMS, "[ANI]canot run callBack %{public}d", ret);
86 return ret;
87 }
88 return ret;
89 }
90
91 }
92 }