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 "DriverExtensionContext.impl.hpp"
17 #include "DriverExtensionContext.proj.hpp"
18 #include "stdexcept"
19 #include "taihe/runtime.hpp"
20
21 #include "DriverExtensionContext_ani.h"
22 #include "ani_utils.h"
23 #include "ets_extension_context.h"
24 #include "hilog_wrapper.h"
25
26 namespace OHOS {
27 namespace AbilityRuntime {
28 constexpr const char *DRIVER_EXTENSION_CONTEXT_CLS = "Lapplication/DriverExtensionContext/DriverExtensionContext;";
UpdateDriverState(ani_env * env,ani_object object)29 void UpdateDriverState([[maybe_unused]] ani_env *env, [[maybe_unused]] ani_object object)
30 {
31 HILOG_DEBUG("UpdateDriverState begin");
32 DriverExtensionContext *driverExtensionContext = AniObjectUtils::Unwrap<DriverExtensionContext>(env, object);
33 if (driverExtensionContext == nullptr) {
34 return;
35 }
36 ErrCode innerErrorCode = driverExtensionContext->UpdateDriverState();
37 if (innerErrorCode != ANI_OK) {
38 HILOG_ERROR("UpdateDriverState error");
39 return;
40 }
41 HILOG_DEBUG("UpdateDriverState end");
42 }
43
CreateAniDriverExtensionContext(ani_env * env,std::shared_ptr<DriverExtensionContext> context,const std::shared_ptr<OHOS::AppExecFwk::OHOSApplication> & application)44 ani_object CreateAniDriverExtensionContext(ani_env *env, std::shared_ptr<DriverExtensionContext> context,
45 const std::shared_ptr<OHOS::AppExecFwk::OHOSApplication> &application)
46 {
47 HILOG_DEBUG("CreateAniDriverExtensionContext begin");
48 ani_class cls = nullptr;
49 ani_status status = ANI_ERROR;
50 if ((env->FindClass(DRIVER_EXTENSION_CONTEXT_CLS, &cls)) != ANI_OK) {
51 HILOG_ERROR("FindClass err: %{public}s", DRIVER_EXTENSION_CONTEXT_CLS);
52 return nullptr;
53 }
54 ani_object contextObj = AniObjectUtils::Create(env, cls);
55 std::array methods = {
56 ani_native_function {"UpdateDriverState", nullptr, reinterpret_cast<void *>(UpdateDriverState)},
57 };
58 if (ANI_OK != env->Class_BindNativeMethods(cls, methods.data(), methods.size())) {
59 HILOG_ERROR("Cannot bind native methods to %{public}s", DRIVER_EXTENSION_CONTEXT_CLS);
60 return nullptr;
61 }
62 if (ANI_OK != AniObjectUtils::Wrap(env, contextObj, context.get())) {
63 HILOG_ERROR("Cannot wrap native object");
64 return nullptr;
65 }
66 ani_field field = nullptr;
67 if ((status = env->Class_FindField(cls, "nativeContext", &field)) != ANI_OK) {
68 HILOG_ERROR("status: %{public}d", status);
69 return nullptr;
70 }
71 ani_long nativeContextLong = (ani_long)context.get();
72 if ((status = env->Object_SetField_Long(contextObj, field, nativeContextLong)) != ANI_OK) {
73 HILOG_ERROR("status: %{public}d", status);
74 return nullptr;
75 }
76 if (application == nullptr) {
77 HILOG_ERROR("application null");
78 return nullptr;
79 }
80 OHOS::AbilityRuntime::CreateEtsExtensionContext(env, cls, contextObj, context, context->GetAbilityInfo());
81 HILOG_DEBUG("CreateAniDriverExtensionContext end");
82 return contextObj;
83 }
84 } // namespace AbilityRuntime
85 } // namespace OHOS
86 // Since these macros are auto-generate, lint will cause false positive.
87 // NOLINTBEGIN
88 // NOLINTEND
89