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_ability_delegator_registry.h"
17
18 #include <memory>
19 #include "ability_delegator.h"
20 #include "ability_delegator_registry.h"
21 #include "ets_ability_delegator.h"
22 #include "ets_ability_delegator_utils.h"
23 #include "ets_native_reference.h"
24 #include "hilog_tag_wrapper.h"
25
26 namespace OHOS {
27 namespace AbilityDelegatorEts {
28 std::unique_ptr<AppExecFwk::ETSNativeReference> etsReference;
29 std::mutex etsReferenceMutex;
30
31 namespace {
32 constexpr const char* ETS_DELEGATOR_REGISTRY_NAMESPACE =
33 "L@ohos/app/ability/abilityDelegatorRegistry/abilityDelegatorRegistry;";
34 constexpr const char* ETS_DELEGATOR_REGISTRY_SIGNATURE_DELEAGTOR = ":Lapplication/AbilityDelegator/AbilityDelegator;";
35 constexpr const char* ETS_DELEGATOR_REGISTRY_SIGNATURE_ATGS =
36 ":Lapplication/abilityDelegatorArgs/AbilityDelegatorArgs;";;
37 }
GetAbilityDelegator(ani_env * env)38 static ani_object GetAbilityDelegator(ani_env *env)
39 {
40 if (env == nullptr) {
41 TAG_LOGE(AAFwkTag::DELEGATOR, "null env");
42 return {};
43 }
44
45 std::lock_guard<std::mutex> lock(etsReferenceMutex);
46 auto delegator = AppExecFwk::AbilityDelegatorRegistry::GetAbilityDelegator(AbilityRuntime::Runtime::Language::ETS);
47 if (delegator == nullptr) {
48 TAG_LOGE(AAFwkTag::DELEGATOR, "null delegator");
49 return {};
50 }
51
52 if (etsReference == nullptr) {
53 ani_object value = CreateEtsAbilityDelegator(env);
54 if (value == nullptr) {
55 TAG_LOGE(AAFwkTag::DELEGATOR, "value is nullptr");
56 return {};
57 }
58 ani_boolean isValue = false;
59 env->Reference_IsNullishValue(value, &isValue);
60 if (isValue) {
61 TAG_LOGE(AAFwkTag::DELEGATOR, "Reference_IsNullishValue");
62 return {};
63 }
64 etsReference = std::make_unique<AppExecFwk::ETSNativeReference>();
65 ani_ref result = nullptr;
66 auto status = env->GlobalReference_Create(value, &(result));
67 if (status != ANI_OK) {
68 TAG_LOGE(AAFwkTag::DELEGATOR, "Create Gloabl ref for delegator failed %{public}d", status);
69 return {};
70 }
71 etsReference->aniObj = static_cast<ani_object>(result);
72 return etsReference->aniObj;
73 } else {
74 return etsReference->aniObj;
75 }
76 }
77
GetArguments(ani_env * env)78 static ani_object GetArguments(ani_env *env)
79 {
80 if (env == nullptr) {
81 TAG_LOGE(AAFwkTag::DELEGATOR, "null env");
82 return {};
83 }
84
85 auto abilityDelegatorArgs = AppExecFwk::AbilityDelegatorRegistry::GetArguments();
86 if (abilityDelegatorArgs == nullptr) {
87 TAG_LOGE(AAFwkTag::DELEGATOR, "get argument failed");
88 return {};
89 }
90
91 return CreateEtsAbilityDelegatorArguments(env, abilityDelegatorArgs);
92 }
93
EtsAbilityDelegatorRegistryInit(ani_env * env)94 void EtsAbilityDelegatorRegistryInit(ani_env *env)
95 {
96 TAG_LOGD(AAFwkTag::DELEGATOR, "EtsAbilityDelegatorRegistryInit call");
97 if (env == nullptr) {
98 TAG_LOGE(AAFwkTag::DELEGATOR, "null env");
99 return;
100 }
101 ani_status status = ANI_ERROR;
102 if (env->ResetError() != ANI_OK) {
103 TAG_LOGE(AAFwkTag::DELEGATOR, "ResetError failed");
104 }
105
106 ani_namespace ns = nullptr;
107 status = env->FindNamespace(ETS_DELEGATOR_REGISTRY_NAMESPACE, &ns);
108 if (status != ANI_OK) {
109 TAG_LOGE(AAFwkTag::DELEGATOR, "FindNamespace abilityDelegatorRegistry failed status: %{public}d", status);
110 return;
111 }
112
113 std::array kitFunctions = {
114 ani_native_function {"getAbilityDelegator", ETS_DELEGATOR_REGISTRY_SIGNATURE_DELEAGTOR,
115 reinterpret_cast<void *>(GetAbilityDelegator)},
116 ani_native_function {"getArguments", ETS_DELEGATOR_REGISTRY_SIGNATURE_ATGS,
117 reinterpret_cast<void *>(GetArguments)},
118 };
119
120 status = env->Namespace_BindNativeFunctions(ns, kitFunctions.data(), kitFunctions.size());
121 if (status != ANI_OK) {
122 TAG_LOGE(AAFwkTag::DELEGATOR, "Namespace_BindNativeFunctions failed status: %{public}d", status);
123 }
124
125 if (env->ResetError() != ANI_OK) {
126 TAG_LOGE(AAFwkTag::DELEGATOR, "ResetError failed");
127 }
128 TAG_LOGD(AAFwkTag::DELEGATOR, "EtsAbilityDelegatorRegistryInit end");
129 }
130
131 extern "C" {
ANI_Constructor(ani_vm * vm,uint32_t * result)132 ANI_EXPORT ani_status ANI_Constructor(ani_vm *vm, uint32_t *result)
133 {
134 TAG_LOGD(AAFwkTag::DELEGATOR, "ANI_Constructor");
135 ani_env *env = nullptr;
136 ani_status status = ANI_ERROR;
137 if (vm == nullptr) {
138 TAG_LOGE(AAFwkTag::DELEGATOR, "null vm");
139 return ANI_ERROR;
140 }
141 status = vm->GetEnv(ANI_VERSION_1, &env);
142 if (status != ANI_OK) {
143 TAG_LOGE(AAFwkTag::DELEGATOR, "GetEnv failed status: %{public}d", status);
144 return ANI_NOT_FOUND;
145 }
146
147 EtsAbilityDelegatorRegistryInit(env);
148 *result = ANI_VERSION_1;
149 TAG_LOGD(AAFwkTag::DELEGATOR, "ANI_Constructor finish");
150 return ANI_OK;
151 }
152 }
153 } // namespace AbilityDelegatorEts
154 } // namespace OHOS
155