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