• 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 
16 #include "fs_watcher_wrapper.h"
17 
18 #include "ani_signature.h"
19 #include "filemgmt_libhilog.h"
20 
21 namespace OHOS {
22 namespace FileManagement {
23 namespace ModuleFileIO {
24 namespace ANI {
25 using namespace std;
26 using namespace OHOS::FileManagement::ModuleFileIO;
27 using namespace OHOS::FileManagement::ModuleFileIO::ANI::AniSignature;
28 
Unwrap(ani_env * env,ani_object object)29 FsWatcher *FsWatcherWrapper::Unwrap(ani_env *env, ani_object object)
30 {
31     ani_long nativePtr;
32     auto ret = env->Object_GetFieldByName_Long(object, "nativePtr", &nativePtr);
33     if (ret != ANI_OK) {
34         HILOGE("Unwrap fsWatcher err: %{public}d", ret);
35         return nullptr;
36     }
37     uintptr_t ptrValue = static_cast<uintptr_t>(nativePtr);
38     FsWatcher *watcher = reinterpret_cast<FsWatcher *>(ptrValue);
39     return watcher;
40 }
41 
Wrap(ani_env * env,const FsWatcher * watcher)42 ani_object FsWatcherWrapper::Wrap(ani_env *env, const FsWatcher *watcher)
43 {
44     if (watcher == nullptr) {
45         HILOGE("FsWatcher pointer is null!");
46         return nullptr;
47     }
48     auto classDesc = FS::WatcherInner::classDesc.c_str();
49     ani_class cls;
50     if (ANI_OK != env->FindClass(classDesc, &cls)) {
51         HILOGE("Cannot find class %s", classDesc);
52         return nullptr;
53     }
54     auto ctorDesc = FS::WatcherInner::ctorDesc.c_str();
55     auto ctorSig = FS::WatcherInner::ctorSig.c_str();
56     ani_method ctor;
57     if (ANI_OK != env->Class_FindMethod(cls, ctorDesc, ctorSig, &ctor)) {
58         HILOGE("Cannot find constructor method for class %{public}s", classDesc);
59         return nullptr;
60     }
61     ani_long ptr = static_cast<ani_long>(reinterpret_cast<std::uintptr_t>(watcher));
62     ani_object obj;
63     if (ANI_OK != env->Object_New(cls, ctor, &obj, ptr)) {
64         HILOGE("New %{public}s obj Failed!", classDesc);
65         return nullptr;
66     }
67     return obj;
68 }
69 
70 } // namespace ANI
71 } // namespace ModuleFileIO
72 } // namespace FileManagement
73 } // namespace OHOS