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 "watch_event_wrapper.h"
17
18 #include "ani_signature.h"
19 #include "filemgmt_libhilog.h"
20 #include "type_converter.h"
21
22 namespace OHOS {
23 namespace FileManagement {
24 namespace ModuleFileIO {
25 namespace ANI {
26 using namespace std;
27 using namespace OHOS::FileManagement::ModuleFileIO;
28 using namespace OHOS::FileManagement::ModuleFileIO::ANI::AniSignature;
29
Wrap(ani_env * env,const WatchEvent & evt)30 ani_object WatchEventWrapper::Wrap(ani_env *env, const WatchEvent &evt)
31 {
32 auto classDesc = FS::WatchEventInner::classDesc.c_str();
33 ani_class cls;
34 if (ANI_OK != env->FindClass(classDesc, &cls)) {
35 HILOGE("Cannot find class %{public}s", classDesc);
36 return nullptr;
37 }
38 auto ctorDesc = FS::WatchEventInner::ctorDesc.c_str();
39 auto ctorSig = FS::WatchEventInner::ctorSig.c_str();
40 ani_method ctor;
41 if (ANI_OK != env->Class_FindMethod(cls, ctorDesc, ctorSig, &ctor)) {
42 HILOGE("Cannot find constructor method for class %{public}s", classDesc);
43 return nullptr;
44 }
45
46 auto [succ, fileName] = TypeConverter::ToAniString(env, evt.fileName);
47 if (!succ) {
48 HILOGE("Convert fileName to ani string failed!");
49 return nullptr;
50 }
51
52 auto event = static_cast<double>(evt.event);
53 auto cookie = static_cast<double>(evt.cookie);
54
55 ani_object obj;
56 if (ANI_OK != env->Object_New(cls, ctor, &obj, fileName, event, cookie)) {
57 HILOGE("Create %{public}s obj failed!", classDesc);
58 return nullptr;
59 }
60 return obj;
61 }
62
63 } // namespace ANI
64 } // namespace ModuleFileIO
65 } // namespace FileManagement
66 } // namespace OHOS