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 #include "ability_delegator_registry.h"
16 #include "ets_ability_monitor.h"
17 #include "ets_native_reference.h"
18 #include "hilog_tag_wrapper.h"
19
20 namespace OHOS {
21 namespace AbilityDelegatorEts {
22 using namespace OHOS::AbilityRuntime;
EtsAbilityMonitor(const std::string & abilityName)23 EtsAbilityMonitor::EtsAbilityMonitor(const std::string &abilityName)
24 : IAbilityMonitor(abilityName), abilityName_(abilityName)
25 {}
26
EtsAbilityMonitor(const std::string & abilityName,const std::string & moduleName)27 EtsAbilityMonitor::EtsAbilityMonitor(const std::string &abilityName, const std::string &moduleName)
28 : IAbilityMonitor(abilityName), abilityName_(abilityName), moduleName_(moduleName)
29 {}
30
OnAbilityStart(const std::weak_ptr<AppExecFwk::BaseDelegatorAbilityProperty> & abilityObj)31 void EtsAbilityMonitor::OnAbilityStart(const std::weak_ptr<AppExecFwk::BaseDelegatorAbilityProperty> &abilityObj)
32 {
33 TAG_LOGI(AAFwkTag::DELEGATOR, "called OnAbilityStart");
34 auto runtimeObj = GetRuntimeObject(abilityObj);
35 if (!runtimeObj) {
36 return;
37 }
38 CallLifecycleCBFunction("onAbilityCreate", runtimeObj);
39 }
40
OnAbilityForeground(const std::weak_ptr<AppExecFwk::BaseDelegatorAbilityProperty> & abilityObj)41 void EtsAbilityMonitor::OnAbilityForeground(const std::weak_ptr<AppExecFwk::BaseDelegatorAbilityProperty> &abilityObj)
42 {
43 TAG_LOGI(AAFwkTag::DELEGATOR, "called OnAbilityForeground");
44 auto runtimeObj = GetRuntimeObject(abilityObj);
45 if (!runtimeObj) {
46 return;
47 }
48 CallLifecycleCBFunction("onAbilityForeground", runtimeObj);
49 }
50
OnAbilityBackground(const std::weak_ptr<AppExecFwk::BaseDelegatorAbilityProperty> & abilityObj)51 void EtsAbilityMonitor::OnAbilityBackground(const std::weak_ptr<AppExecFwk::BaseDelegatorAbilityProperty> &abilityObj)
52 {
53 TAG_LOGI(AAFwkTag::DELEGATOR, "called OnAbilityBackground");
54 auto runtimeObj = GetRuntimeObject(abilityObj);
55 if (!runtimeObj) {
56 return;
57 }
58 CallLifecycleCBFunction("onAbilityBackground", runtimeObj);
59 }
60
OnAbilityStop(const std::weak_ptr<AppExecFwk::BaseDelegatorAbilityProperty> & abilityObj)61 void EtsAbilityMonitor::OnAbilityStop(const std::weak_ptr<AppExecFwk::BaseDelegatorAbilityProperty> &abilityObj)
62 {
63 TAG_LOGI(AAFwkTag::DELEGATOR, "called OnAbilityStop");
64 auto runtimeObj = GetRuntimeObject(abilityObj);
65 if (!runtimeObj) {
66 return;
67 }
68 CallLifecycleCBFunction("onAbilityDestroy", runtimeObj);
69 }
70
OnWindowStageCreate(const std::weak_ptr<AppExecFwk::BaseDelegatorAbilityProperty> & abilityObj)71 void EtsAbilityMonitor::OnWindowStageCreate(const std::weak_ptr<AppExecFwk::BaseDelegatorAbilityProperty> &abilityObj)
72 {
73 TAG_LOGI(AAFwkTag::DELEGATOR, "called OnWindowStageCreate");
74 auto runtimeObj = GetRuntimeObject(abilityObj);
75 if (!runtimeObj) {
76 return;
77 }
78 CallLifecycleCBFunction("onWindowStageCreate", runtimeObj);
79 }
80
OnWindowStageRestore(const std::weak_ptr<AppExecFwk::BaseDelegatorAbilityProperty> & abilityObj)81 void EtsAbilityMonitor::OnWindowStageRestore(const std::weak_ptr<AppExecFwk::BaseDelegatorAbilityProperty> &abilityObj)
82 {
83 TAG_LOGI(AAFwkTag::DELEGATOR, "called OnWindowStageRestore");
84 auto runtimeObj = GetRuntimeObject(abilityObj);
85 if (!runtimeObj) {
86 return;
87 }
88 CallLifecycleCBFunction("onWindowStageRestore", runtimeObj);
89 }
90
OnWindowStageDestroy(const std::weak_ptr<AppExecFwk::BaseDelegatorAbilityProperty> & abilityObj)91 void EtsAbilityMonitor::OnWindowStageDestroy(const std::weak_ptr<AppExecFwk::BaseDelegatorAbilityProperty> &abilityObj)
92 {
93 TAG_LOGI(AAFwkTag::DELEGATOR, "called OnWindowStageDestroy");
94 auto runtimeObj = GetRuntimeObject(abilityObj);
95 if (!runtimeObj) {
96 return;
97 }
98 CallLifecycleCBFunction("onWindowStageDestroy", runtimeObj);
99 }
100
SetEtsAbilityMonitor(ani_env * env,ani_object & abilityMonitorObj)101 void EtsAbilityMonitor::SetEtsAbilityMonitor(ani_env *env, ani_object &abilityMonitorObj)
102 {
103 TAG_LOGI(AAFwkTag::DELEGATOR, "called SetEtsAbilityMonitor");
104 if (env == nullptr) {
105 TAG_LOGE(AAFwkTag::DELEGATOR, "null env");
106 return;
107 }
108 etsAbilityMonitor_ = std::make_unique<AppExecFwk::ETSNativeReference>();
109 ani_ref objRef = nullptr;
110 if (env->GlobalReference_Create(abilityMonitorObj, &objRef) != ANI_OK) {
111 TAG_LOGE(AAFwkTag::DELEGATOR, "GlobalReference_Create failed");
112 return;
113 }
114
115 ani_vm *aniVM = nullptr;
116 if (env->GetVM(&aniVM) != ANI_OK) {
117 TAG_LOGE(AAFwkTag::DELEGATOR, "GetVM failed");
118 return;
119 }
120 vm_ = aniVM;
121 if (etsAbilityMonitor_ == nullptr) {
122 TAG_LOGE(AAFwkTag::DELEGATOR, "null etsAbilityMonitor_");
123 return;
124 }
125 etsAbilityMonitor_->aniObj = abilityMonitorObj;
126 etsAbilityMonitor_->aniRef = objRef;
127 }
128
CallLifecycleCBFunction(const std::string & functionName,const std::shared_ptr<AppExecFwk::ETSNativeReference> & abilityObj)129 void EtsAbilityMonitor::CallLifecycleCBFunction(const std::string &functionName,
130 const std::shared_ptr<AppExecFwk::ETSNativeReference> &abilityObj)
131 {
132 TAG_LOGI(AAFwkTag::DELEGATOR, "CallLifecycleCBFunction, name: %{public}s start", functionName.c_str());
133 if (functionName.empty()) {
134 TAG_LOGE(AAFwkTag::DELEGATOR, "empty funcName");
135 return;
136 }
137
138 if (abilityObj == nullptr) {
139 TAG_LOGE(AAFwkTag::DELEGATOR, "null EtsAbilityMonitor");
140 return;
141 }
142
143 ani_env *env = GetAniEnv();
144 if (env == nullptr || etsAbilityMonitor_ == nullptr) {
145 TAG_LOGE(AAFwkTag::DELEGATOR, "null env or etsAbilityMonitor_");
146 return;
147 }
148
149 ani_status status = ANI_OK;
150 ani_object monitorObj = reinterpret_cast<ani_object>(etsAbilityMonitor_->aniRef);
151 ani_ref funRef;
152 status = env->Object_GetPropertyByName_Ref(monitorObj, functionName.c_str(), &funRef);
153 if (status != ANI_OK) {
154 TAG_LOGE(AAFwkTag::DELEGATOR, "Object_GetField_Ref failed status: %{public}d", status);
155 return;
156 }
157
158 ani_fn_object onFn = reinterpret_cast<ani_fn_object>(funRef);
159 ani_ref resutlt;
160 std::vector<ani_ref> argv = { abilityObj->aniRef };
161 if ((status = env->FunctionalObject_Call(onFn, 1, argv.data(), &resutlt)) != ANI_OK) {
162 TAG_LOGE(AAFwkTag::DELEGATOR, "FunctionalObject_Call failed, status: %{public}d", status);
163 return;
164 }
165 }
166
GetAniEnv()167 ani_env* EtsAbilityMonitor::GetAniEnv()
168 {
169 if (vm_ == nullptr) {
170 return nullptr;
171 }
172 ani_env* aniEnv = nullptr;
173 if (vm_->GetEnv(ANI_VERSION_1, &aniEnv) != ANI_OK) {
174 return nullptr;
175 }
176 return aniEnv;
177 }
178
GetRuntimeObject(const std::weak_ptr<AppExecFwk::BaseDelegatorAbilityProperty> & abilityObj)179 std::shared_ptr<AppExecFwk::ETSNativeReference> EtsAbilityMonitor::GetRuntimeObject(
180 const std::weak_ptr<AppExecFwk::BaseDelegatorAbilityProperty> &abilityObj)
181 {
182 auto baseProperty = abilityObj.lock();
183 if (!baseProperty) {
184 TAG_LOGE(AAFwkTag::DELEGATOR, "abilityObj is expired");
185 return nullptr;
186 }
187 auto etsbaseProperty = std::static_pointer_cast<AppExecFwk::EtsDelegatorAbilityProperty>(baseProperty);
188 auto runtimeObj = etsbaseProperty->object_.lock();
189 if (!runtimeObj) {
190 TAG_LOGE(AAFwkTag::DELEGATOR, "runtimeObj is nullptr");
191 return nullptr;
192 }
193 return runtimeObj;
194 }
195 } // namespace AbilityDelegatorJs
196 } // namespace OHOS
197