1 /*
2 * Copyright (c) 2021 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 "environment_n_exporter.h"
17
18 #include <string>
19
20 #include "../common/napi/n_class.h"
21 #include "../common/napi/n_func_arg.h"
22 #include "../common/napi/n_val.h"
23 #include "../common/uni_error.h"
24
25 #include "../common/napi/n_async/n_async_work_callback.h"
26 #include "../common/napi/n_async/n_async_work_promise.h"
27
28 namespace OHOS {
29 namespace DistributedFS {
30 namespace ModuleEnvironment {
31 namespace {
32 const std::string STORAGE_DATA_PATH = "/data";
33 const std::string USER_DATA_PATH = "/data/storage/0";
34 }
GetStorageDataDir(napi_env env,napi_callback_info info)35 napi_value GetStorageDataDir(napi_env env, napi_callback_info info)
36 {
37 NFuncArg funcArg(env, info);
38 if (!funcArg.InitArgs(NARG_CNT::ZERO, NARG_CNT::ONE)) {
39 UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched");
40 return nullptr;
41 }
42
43 auto cbExec = [](napi_env env) -> UniError {
44 return UniError(ERRNO_NOERR);
45 };
46 auto cbComplete = [](napi_env env, UniError err) -> NVal {
47 if (err) {
48 return { env, err.GetNapiErr(env) };
49 }
50 return NVal::CreateUTF8String(env, STORAGE_DATA_PATH);
51 };
52
53 std::string procedureName = "GetStorageDataDir";
54 NVal thisVar(env, funcArg.GetThisVar());
55 if (funcArg.GetArgc() == NARG_CNT::ZERO) {
56 return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbComplete).val_;
57 } else {
58 NVal cb(env, funcArg[NARG_POS::FIRST]);
59 return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbComplete).val_;
60 }
61 return NVal::CreateUndefined(env).val_;
62 }
63
GetUserId()64 int GetUserId()
65 {
66 return 0;
67 }
68
GetUserDataDir(napi_env env,napi_callback_info info)69 napi_value GetUserDataDir(napi_env env, napi_callback_info info)
70 {
71 NFuncArg funcArg(env, info);
72 if (!funcArg.InitArgs(NARG_CNT::ZERO, NARG_CNT::ONE)) {
73 UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched");
74 return nullptr;
75 }
76
77 auto userDataPath = std::make_shared<std::string>();
78 auto cbExec = [userDataPath](napi_env env) -> UniError {
79 (*userDataPath).append("/storage/media/").append(std::to_string(GetUserId())).append("/local");
80 return UniError(ERRNO_NOERR);
81 };
82 auto cbComplete = [userDataPath](napi_env env, UniError err) -> NVal {
83 if (err) {
84 return { env, err.GetNapiErr(env) };
85 }
86 return NVal::CreateUTF8String(env, *userDataPath);
87 };
88
89 std::string procedureName = "GetUserDataDir";
90 NVal thisVar(env, funcArg.GetThisVar());
91 if (funcArg.GetArgc() == NARG_CNT::ZERO) {
92 return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbComplete).val_;
93 } else {
94 NVal cb(env, funcArg[NARG_POS::FIRST]);
95 return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbComplete).val_;
96 }
97 return NVal::CreateUndefined(env).val_;
98 }
99 } // namespace ModuleEnvironment
100 } // namespace DistributedFS
101 } // namespace OHOS