1  /*
2  * Copyright (c) 2024 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 "napi/native_api.h"
17 #include "native_common.h"
18 #include "filemanagement/environment/oh_environment.h"
19 #include "filemanagement/fileio/error_code.h"
20 #include <cstdlib>
21 #include <js_native_api_types.h>
22 #include <tuple>
23 #include <unistd.h>
24 
GetUserDownloadDir(napi_env env, napi_callback_info info)25 static napi_value GetUserDownloadDir(napi_env env, napi_callback_info info)
26 {
27     char *downloadPath = nullptr;
28     int ret = OH_Environment_GetUserDownloadDir(&downloadPath);
29     if (ret == 0) {
30         free(downloadPath);
31     }
32     napi_value result = nullptr;
33     napi_create_int32(env, ret, &result);
34     return result;
35 }
36 
GetUserDesktopDir(napi_env env, napi_callback_info info)37 static napi_value GetUserDesktopDir(napi_env env, napi_callback_info info)
38 {
39     char *desktopPath = nullptr;
40     int ret = OH_Environment_GetUserDesktopDir(&desktopPath);
41     if (ret == 0) {
42         free(desktopPath);
43     }
44     napi_value result = nullptr;
45     napi_create_int32(env, ret, &result);
46     return result;
47 }
48 
GetUserDocumentDir(napi_env env, napi_callback_info info)49 static napi_value GetUserDocumentDir(napi_env env, napi_callback_info info)
50 {
51     char *documentPath = nullptr;
52     int ret = OH_Environment_GetUserDocumentDir(&documentPath);
53     if (ret == 0) {
54         free(documentPath);
55     }
56     napi_value result = nullptr;
57     napi_create_int32(env, ret, &result);
58     return result;
59 }
60 
61 EXTERN_C_START
Init(napi_env env, napi_value exports)62 static napi_value Init(napi_env env, napi_value exports)
63 {
64     napi_property_descriptor desc[] = {
65         {"getUserDownloadDir", nullptr, GetUserDownloadDir, nullptr, nullptr, nullptr, napi_default, nullptr},
66         {"getUserDesktopDir", nullptr, GetUserDesktopDir, nullptr, nullptr, nullptr, napi_default, nullptr},
67         {"getUserDocumentDir", nullptr, GetUserDocumentDir, nullptr, nullptr, nullptr, napi_default, nullptr},
68     };
69 
70     napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
71     return exports;
72 }
73 EXTERN_C_END
74 
75 static napi_module demoModule = {
76     .nm_version = 1,
77     .nm_flags = 0,
78     .nm_filename = nullptr,
79     .nm_register_func = Init,
80     .nm_modname = "environment",
81     .nm_priv = ((void *)0),
82     .reserved = {0},
83 };
84 
RegisterModule(void)85 extern "C" __attribute__((constructor)) void RegisterModule(void) { napi_module_register(&demoModule); }