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 "medialibrary_ani_log.h"
18 #include "photo_proxy_ani.h"
19
20 namespace OHOS {
21 namespace Media {
22 thread_local sptr<PhotoProxy> PhotoProxyAni::sPhotoProxy_ = nullptr;
PhotoProxyAni()23 PhotoProxyAni::PhotoProxyAni() : env_(nullptr), wrapper_(nullptr)
24 {
25 }
26
~PhotoProxyAni()27 PhotoProxyAni::~PhotoProxyAni()
28 {
29 if (wrapper_ != nullptr) {
30 env_->GlobalReference_Delete(wrapper_);
31 wrapper_ = nullptr;
32 }
33 if (photoProxy_) {
34 photoProxy_ = nullptr;
35 }
36 }
37
PhotoProxyAniInit(ani_env * env)38 ani_status PhotoProxyAni::PhotoProxyAniInit(ani_env *env)
39 {
40 static const char *className = ANI_CLASS_PHOTO_PROXY.c_str();
41 ani_class cls;
42 ani_status status = env->FindClass(className, &cls);
43 if (status != ANI_OK) {
44 ANI_ERR_LOG("Failed to find class: %{public}s", className);
45 return status;
46 }
47
48 std::array methods = {
49 ani_native_function {"create", nullptr,
50 reinterpret_cast<void *>(PhotoProxyAni::PhotoProxyAniConstructor)},
51 };
52
53 status = env->Class_BindNativeMethods(cls, methods.data(), methods.size());
54 if (status != ANI_OK) {
55 ANI_ERR_LOG("Failed to bind native methods to: %{public}s", className);
56 return status;
57 }
58 return ANI_OK;
59 }
60
61 // Constructor callback
PhotoProxyAniConstructor(ani_env * env,ani_class clazz)62 ani_object PhotoProxyAni::PhotoProxyAniConstructor(ani_env *env, [[maybe_unused]] ani_class clazz)
63 {
64 ANI_DEBUG_LOG("PhotoProxyAniConstructor is called");
65 std::unique_ptr<PhotoProxyAni> nativeHandle = std::make_unique<PhotoProxyAni>();
66 nativeHandle->env_ = env;
67 nativeHandle->photoProxy_ = sPhotoProxy_;
68
69 static const char *className = ANI_CLASS_PHOTO_PROXY.c_str();
70 ani_class cls;
71 if (ANI_OK != env->FindClass(className, &cls)) {
72 ANI_ERR_LOG("Failed to find class: %{public}s", className);
73 return nullptr;
74 }
75
76 ani_method ctor;
77 if (ANI_OK != env->Class_FindMethod(cls, "<ctor>", "J:V", &ctor)) {
78 ANI_ERR_LOG("Failed to find method: %{public}s", "ctor");
79 return nullptr;
80 }
81
82 // wrap nativeHandle to aniObject
83 ani_object aniObject;
84 if (ANI_OK !=env->Object_New(cls, ctor, &aniObject, reinterpret_cast<ani_long>(nativeHandle.release()))) {
85 ANI_ERR_LOG("New PhotoProxy Fail");
86 return nullptr;
87 }
88
89 return aniObject;
90 }
91
PhotoProxyAniDestructor(ani_env * env,void * nativeObject,void * finalize_hint)92 void PhotoProxyAni::PhotoProxyAniDestructor(ani_env *env, void *nativeObject, void *finalize_hint)
93 {
94 ANI_DEBUG_LOG("PhotoProxyAniDestructor is called");
95 PhotoProxyAni* deferredPhotoProxy = reinterpret_cast<PhotoProxyAni*>(nativeObject);
96 if (deferredPhotoProxy != nullptr) {
97 delete deferredPhotoProxy;
98 }
99 }
100 } // namespace Media
101 } // namespace OHOS