• 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 #include "ets_data_struct_converter.h"
17 
18 #include "ani_enum_convert.h"
19 #include "hilog_tag_wrapper.h"
20 
21 namespace OHOS {
22 namespace AbilityRuntime {
23 namespace {
24 constexpr const char *CLASSNAME_LAUNCHPARAM = "L@ohos/app/ability/AbilityConstant/LaunchParamImpl;";
25 constexpr const char *CLASSNAME_LAUNCHREASON = "L@ohos/app/ability/AbilityConstant/AbilityConstant/LaunchReason;";
26 constexpr const char *CLASSNAME_LAST_EXITREASION = "L@ohos/app/ability/AbilityConstant/AbilityConstant/LastExitReason;";
27 constexpr const char* LAST_EXIT_DETAIL_INFO_IMPL_CLASS_NAME =
28     "L@ohos/app/ability/AbilityConstant/LastExitDetailInfoImpl;";
GetAniString(ani_env * env,const std::string & str)29 ani_string GetAniString(ani_env *env, const std::string &str)
30 {
31     if (env == nullptr) {
32         TAG_LOGE(AAFwkTag::ETSRUNTIME, "null env");
33         return nullptr;
34     }
35     ani_string aniStr = nullptr;
36     ani_status status = env->String_NewUTF8(str.c_str(), str.size(), &aniStr);
37     if (status != ANI_OK) {
38         TAG_LOGE(AAFwkTag::ETSRUNTIME, "Failed to getAniString, status: %{public}d", status);
39         return nullptr;
40     }
41     return aniStr;
42 }
43 
CreateEtsLastExitDetailInfo(ani_env * env,const AAFwk::LastExitDetailInfo & lastExitDetailInfo)44 ani_object CreateEtsLastExitDetailInfo(ani_env* env, const AAFwk::LastExitDetailInfo& lastExitDetailInfo)
45 {
46     ani_status status = ANI_ERROR;
47     ani_class cls = nullptr;
48     if ((status = env->FindClass(LAST_EXIT_DETAIL_INFO_IMPL_CLASS_NAME, &cls)) != ANI_OK) {
49         TAG_LOGE(AAFwkTag::ETSRUNTIME, "status: %{public}d", status);
50         return nullptr;
51     }
52     if (cls == nullptr) {
53         TAG_LOGE(AAFwkTag::ETSRUNTIME, "null cls");
54         return nullptr;
55     }
56     ani_method method = nullptr;
57     if ((status = env->Class_FindMethod(cls, "<ctor>", ":V", &method)) != ANI_OK) {
58         TAG_LOGE(AAFwkTag::ETSRUNTIME, "status: %{public}d", status);
59         return nullptr;
60     }
61     if (method == nullptr) {
62         TAG_LOGE(AAFwkTag::ETSRUNTIME, "null method");
63         return nullptr;
64     }
65     ani_object object = nullptr;
66     if ((status = env->Object_New(cls, method, &object)) != ANI_OK) {
67         TAG_LOGE(AAFwkTag::ETSRUNTIME, "status: %{public}d", status);
68         return nullptr;
69     }
70     if (object == nullptr) {
71         TAG_LOGE(AAFwkTag::ETSRUNTIME, "null object");
72         return nullptr;
73     }
74     env->Object_SetPropertyByName_Double(object, "pid", lastExitDetailInfo.pid);
75     env->Object_SetPropertyByName_Ref(object, "processName", GetAniString(env, lastExitDetailInfo.processName));
76     env->Object_SetPropertyByName_Double(object, "uid", lastExitDetailInfo.uid);
77     env->Object_SetPropertyByName_Double(object, "exitSubReason", lastExitDetailInfo.exitSubReason);
78     env->Object_SetPropertyByName_Ref(object, "exitMsg", GetAniString(env, lastExitDetailInfo.exitMsg));
79     env->Object_SetPropertyByName_Double(object, "rss", lastExitDetailInfo.rss);
80     env->Object_SetPropertyByName_Double(object, "pss", lastExitDetailInfo.pss);
81     env->Object_SetPropertyByName_Double(object, "timestamp", lastExitDetailInfo.timestamp);
82 
83     return object;
84 }
85 
WrapLaunchParamInner(ani_env * env,const AAFwk::LaunchParam & launchParam,ani_object & object)86 bool WrapLaunchParamInner(ani_env *env, const AAFwk::LaunchParam &launchParam, ani_object &object)
87 {
88     ani_status status = ANI_ERROR;
89     ani_enum_item launchReasonItem = nullptr;
90     OHOS::AAFwk::AniEnumConvertUtil::EnumConvert_NativeToEts(
91         env, CLASSNAME_LAUNCHREASON, launchParam.launchReason, launchReasonItem);
92     if ((status = env->Object_SetPropertyByName_Ref(object, "launchReason", launchReasonItem)) != ANI_OK) {
93         TAG_LOGE(AAFwkTag::ETSRUNTIME, "Failed to set launchReason");
94         return false;
95     }
96 
97     ani_enum_item lastExitReasonItem = nullptr;
98     OHOS::AAFwk::AniEnumConvertUtil::EnumConvert_NativeToEts(
99         env, CLASSNAME_LAST_EXITREASION, launchParam.lastExitReason, lastExitReasonItem);
100     if ((status = env->Object_SetPropertyByName_Ref(object, "lastExitReason", lastExitReasonItem)) != ANI_OK) {
101         TAG_LOGE(AAFwkTag::ETSRUNTIME, "Failed to set lastExitReason");
102         return false;
103     }
104     if ((status = env->Object_SetPropertyByName_Ref(object, "lastExitDetailInfo",
105         CreateEtsLastExitDetailInfo(env, launchParam.lastExitDetailInfo))) != ANI_OK) {
106         TAG_LOGE(AAFwkTag::ETSRUNTIME, "Failed to set lastExitDetailInfo");
107         return false;
108     }
109     return true;
110 }
111 } // namespace
112 
WrapLaunchParam(ani_env * env,const AAFwk::LaunchParam & launchParam,ani_object & object)113 bool WrapLaunchParam(ani_env *env, const AAFwk::LaunchParam &launchParam, ani_object &object)
114 {
115     ani_method method = nullptr;
116     ani_status status = ANI_ERROR;
117     ani_class cls = nullptr;
118     if (env == nullptr) {
119         TAG_LOGE(AAFwkTag::ETSRUNTIME, "null env");
120         return false;
121     }
122     if ((status = env->FindClass(CLASSNAME_LAUNCHPARAM, &cls)) != ANI_OK) {
123         TAG_LOGE(AAFwkTag::ETSRUNTIME, "Failed to find lanchParam Class, status: %{public}d", status);
124         return false;
125     }
126     if (cls == nullptr) {
127         TAG_LOGE(AAFwkTag::ETSRUNTIME, "null cls");
128         return false;
129     }
130     if ((status = env->Class_FindMethod(cls, "<ctor>", ":V", &method)) != ANI_OK) {
131         TAG_LOGE(AAFwkTag::ETSRUNTIME, "Failed to find method, status: %{public}d", status);
132         return false;
133     }
134     if (method == nullptr) {
135         TAG_LOGE(AAFwkTag::ETSRUNTIME, "null method");
136         return false;
137     }
138     if ((status = env->Object_New(cls, method, &object)) != ANI_OK) {
139         TAG_LOGE(AAFwkTag::ETSRUNTIME, "Failed to create object, status: %{public}d", status);
140         return false;
141     }
142     if (object == nullptr) {
143         TAG_LOGE(AAFwkTag::ETSRUNTIME, "null object");
144         return false;
145     }
146     if ((status = env->Object_SetPropertyByName_Ref(
147         object, "lastExitMessage", GetAniString(env, launchParam.lastExitMessage))) != ANI_OK) {
148         TAG_LOGE(AAFwkTag::ETSRUNTIME, "Failed to set lastExitMessage");
149         return false;
150     }
151     return WrapLaunchParamInner(env, launchParam, object);
152 }
153 } // namespace AbilityRuntime
154 } // namespace OHOS
155