• 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 #include <ani.h>
16 #include "hilog_tag_wrapper.h"
17 
18 namespace OHOS {
19 namespace AbilityRuntime {
ContextConstructor()20 void ContextConstructor() {}
21 
AbilityStageContextConstructor()22 void AbilityStageContextConstructor() {}
23 
ExtensionContextConstructor()24 void ExtensionContextConstructor() {}
25 
UIAbilityContextConstructor()26 void UIAbilityContextConstructor() {}
27 
28 extern "C" {
ANI_Constructor(ani_vm * vm,uint32_t * result)29 ANI_EXPORT ani_status ANI_Constructor(ani_vm *vm, uint32_t *result)
30 {
31     ani_env *env = nullptr;
32     if (vm == nullptr || result == nullptr) {
33         TAG_LOGE(AAFwkTag::ETSRUNTIME, "Illegal VM or result");
34         return ANI_ERROR;
35     }
36     if (ANI_OK != vm->GetEnv(ANI_VERSION_1, &env)) {
37         TAG_LOGE(AAFwkTag::ETSRUNTIME, "Unsupported ANI_VERSION_1");
38         return ANI_ERROR;
39     }
40     ani_class contextClass = nullptr;
41     static const char *contextClassName = "Lapplication/Context/Context;";
42     if (ANI_OK != env->FindClass(contextClassName, &contextClass)) {
43         TAG_LOGE(AAFwkTag::ETSRUNTIME, "Not found class %{public}s.", contextClassName);
44         return ANI_NOT_FOUND;
45     }
46     std::array classMethodsContext = {
47         ani_native_function {"<ctor>", ":V", reinterpret_cast<void *>(ContextConstructor)},
48     };
49     if (ANI_OK != env->Class_BindNativeMethods(contextClass, classMethodsContext.data(),
50         classMethodsContext.size())) {
51             TAG_LOGE(AAFwkTag::ETSRUNTIME, "Cannot bind native ctor to class %{public}s.", contextClassName);
52         return ANI_ERROR;
53     };
54 
55     ani_class extensionContextClass = nullptr;
56     static const char *extensionContextClassName = "Lapplication/ExtensionContext/ExtensionContext;";
57     if (ANI_OK != env->FindClass(extensionContextClassName, &extensionContextClass)) {
58         TAG_LOGE(AAFwkTag::ETSRUNTIME, "Not found class %{public}s.", extensionContextClassName);
59         return ANI_NOT_FOUND;
60     }
61     std::array classMethodsExtensionContext = {
62         ani_native_function {"<ctor>", ":V", reinterpret_cast<void *>(ExtensionContextConstructor)},
63     };
64     if (ANI_OK != env->Class_BindNativeMethods(extensionContextClass, classMethodsExtensionContext.data(),
65         classMethodsExtensionContext.size())) {
66         TAG_LOGE(AAFwkTag::ETSRUNTIME, "Cannot bind native ctor to class %{public}s.", extensionContextClassName);
67         return ANI_ERROR;
68     };
69 
70     ani_class uiAbilityClass = nullptr;
71     static const char *uiAbilityClassName = "Lapplication/UIAbilityContext/UIAbilityContext;";
72     if (ANI_OK != env->FindClass(uiAbilityClassName, &uiAbilityClass)) {
73         TAG_LOGE(AAFwkTag::ETSRUNTIME, "Not found class %{public}s.", uiAbilityClassName);
74         return ANI_NOT_FOUND;
75     }
76     std::array classMethodsUiAbility = {
77         ani_native_function {"<ctor>", ":V", reinterpret_cast<void *>(UIAbilityContextConstructor)},
78     };
79     if (ANI_OK != env->Class_BindNativeMethods(uiAbilityClass, classMethodsUiAbility.data(),
80         classMethodsUiAbility.size())) {
81         TAG_LOGE(AAFwkTag::ETSRUNTIME, "Cannot bind native ctor to class %{public}s.", uiAbilityClassName);
82         return ANI_ERROR;
83     };
84 
85     ani_class abilityStageContextClass = nullptr;
86     static const char *abilityStageContextClassName = "Lapplication/AbilityStageContext/AbilityStageContext;";
87     if (ANI_OK != env->FindClass(abilityStageContextClassName, &abilityStageContextClass)) {
88         TAG_LOGE(AAFwkTag::ETSRUNTIME, "Not found class %{public}s.", abilityStageContextClassName);
89         return ANI_NOT_FOUND;
90     }
91     std::array classMethodsAbilityStage = {
92         ani_native_function {"<ctor>", ":V", reinterpret_cast<void *>(AbilityStageContextConstructor)},
93     };
94     if (ANI_OK != env->Class_BindNativeMethods(abilityStageContextClass, classMethodsAbilityStage.data(),
95         classMethodsAbilityStage.size())) {
96         TAG_LOGE(AAFwkTag::ETSRUNTIME, "Cannot bind native ctor to class %{public}s.", abilityStageContextClassName);
97         return ANI_ERROR;
98     };
99     *result = ANI_VERSION_1;
100     return ANI_OK;
101 }
102 }
103 } // namespace AbilityRuntime
104 } // namespace OHOS