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 "ani_class_name.h"
17 #include "cloud_enhancement_ani.h"
18 #include "medialibrary_ani_utils.h"
19 #include "medialibrary_errno.h"
20
21 namespace OHOS {
22 namespace Media {
CloudEnhancementAniInit(ani_env * env)23 ani_status CloudEnhancementAni::CloudEnhancementAniInit(ani_env *env)
24 {
25 static const char *className = ANI_CLASS_CLOUD_ENHANCEMENT.c_str();
26 ani_class cls;
27 ani_status status = env->FindClass(className, &cls);
28 if (status != ANI_OK) {
29 ANI_ERR_LOG("Failed to find class: %{public}s", className);
30 return status;
31 }
32
33 std::array methods = {
34 ani_native_function {"create", nullptr, reinterpret_cast<void *>(CloudEnhancementAni::Constructor)},
35 };
36
37 status = env->Class_BindNativeMethods(cls, methods.data(), methods.size());
38 if (status != ANI_OK) {
39 ANI_ERR_LOG("Failed to bind native methods to: %{public}s", className);
40 return status;
41 }
42 return ANI_OK;
43 }
44
Constructor(ani_env * env,ani_class clazz,ani_object context)45 ani_object CloudEnhancementAni::Constructor(ani_env *env, [[maybe_unused]] ani_class clazz, ani_object context)
46 {
47 if (!MediaLibraryAniUtils::IsSystemApp()) {
48 AniError::ThrowError(env, E_CHECK_SYSTEMAPP_FAIL,
49 "The cloud enhancement instance can be called only by system apps");
50 return nullptr;
51 }
52
53 std::unique_ptr<CloudEnhancementAni> nativeHandle = std::make_unique<CloudEnhancementAni>();
54 static const char *className = ANI_CLASS_CLOUD_ENHANCEMENT.c_str();
55 ani_class cls;
56 if (ANI_OK != env->FindClass(className, &cls)) {
57 ANI_ERR_LOG("Failed to find class: %{public}s", className);
58 return nullptr;
59 }
60
61 ani_method ctor;
62 if (ANI_OK != env->Class_FindMethod(cls, "<ctor>", "J:V", &ctor)) {
63 ANI_ERR_LOG("Failed to find method: %{public}s", "ctor");
64 return nullptr;
65 }
66
67 ani_object aniObject;
68 if (ANI_OK !=env->Object_New(cls, ctor, &aniObject, reinterpret_cast<ani_long>(nativeHandle.release()))) {
69 ANI_ERR_LOG("New MediaAssetChangeRequest Fail");
70 return nullptr;
71 }
72
73 return aniObject;
74 }
75
Unwrap(ani_env * env,ani_object aniObject)76 CloudEnhancementAni* CloudEnhancementAni::Unwrap(ani_env *env, ani_object aniObject)
77 {
78 ani_long context;
79 if (ANI_OK != env->Object_GetFieldByName_Long(aniObject, "nativeHandle", &context)) {
80 return nullptr;
81 }
82 return reinterpret_cast<CloudEnhancementAni*>(context);
83 }
84 } // namespace Media
85 } // namespace OHOS
86