1020a203aSopenharmony_ci/*
2020a203aSopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
3020a203aSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4020a203aSopenharmony_ci * you may not use this file except in compliance with the License.
5020a203aSopenharmony_ci * You may obtain a copy of the License at
6020a203aSopenharmony_ci *
7020a203aSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8020a203aSopenharmony_ci *
9020a203aSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10020a203aSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11020a203aSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12020a203aSopenharmony_ci * See the License for the specific language governing permissions and
13020a203aSopenharmony_ci * limitations under the License.
14020a203aSopenharmony_ci */
15020a203aSopenharmony_ci
16020a203aSopenharmony_ci#include "hiview_file_info.h"
17020a203aSopenharmony_ci#include "hiview_napi_adapter.h"
18020a203aSopenharmony_ci#include "hiview_napi_util.h"
19020a203aSopenharmony_ci#include "hiview_service_agent.h"
20020a203aSopenharmony_ci#include "hiview_logger.h"
21020a203aSopenharmony_ci#include "napi/native_api.h"
22020a203aSopenharmony_ci#include "napi/native_node_api.h"
23020a203aSopenharmony_ci#include "string_util.h"
24020a203aSopenharmony_ci
25020a203aSopenharmony_cinamespace OHOS {
26020a203aSopenharmony_cinamespace HiviewDFX {
27020a203aSopenharmony_ciDEFINE_LOG_LABEL(0xD002D03, "NAPI_HIVIEW_JS");
28020a203aSopenharmony_cinamespace {
29020a203aSopenharmony_ciconstexpr size_t LOG_TYPE_INDEX = 0;
30020a203aSopenharmony_ciconstexpr size_t LOG_NAME_INDEX = 1;
31020a203aSopenharmony_ciconstexpr size_t DEST_DIR_INDEX = 2;
32020a203aSopenharmony_ci}
33020a203aSopenharmony_ci
34020a203aSopenharmony_cistatic napi_value List(napi_env env, napi_callback_info info)
35020a203aSopenharmony_ci{
36020a203aSopenharmony_ci    if (!HiviewNapiUtil::IsSystemAppCall()) {
37020a203aSopenharmony_ci        HiviewNapiUtil::ThrowSystemAppPermissionError(env);
38020a203aSopenharmony_ci        return nullptr;
39020a203aSopenharmony_ci    }
40020a203aSopenharmony_ci    constexpr size_t listParamNum = 1;
41020a203aSopenharmony_ci    size_t paramNum = listParamNum;
42020a203aSopenharmony_ci    napi_value params[listParamNum] = { 0 };
43020a203aSopenharmony_ci    napi_value result = nullptr;
44020a203aSopenharmony_ci    napi_get_undefined(env, &result);
45020a203aSopenharmony_ci    NAPI_CALL(env, napi_get_cb_info(env, info, &paramNum, params, nullptr, nullptr));
46020a203aSopenharmony_ci    if (paramNum < listParamNum) {
47020a203aSopenharmony_ci        HIVIEW_LOGE("num of params is invalid: %{public}zu", paramNum);
48020a203aSopenharmony_ci        return result;
49020a203aSopenharmony_ci    }
50020a203aSopenharmony_ci    std::string logType;
51020a203aSopenharmony_ci    if (!HiviewNapiUtil::ParseStringValue(env, "logType", params[LOG_TYPE_INDEX], logType)) {
52020a203aSopenharmony_ci        return result;
53020a203aSopenharmony_ci    }
54020a203aSopenharmony_ci    std::vector<HiviewFileInfo> fileInfos;
55020a203aSopenharmony_ci    int32_t retCode = HiviewServiceAgent::GetInstance().List(logType, fileInfos);
56020a203aSopenharmony_ci    HIVIEW_LOGI("retCode: %{public}u.", retCode);
57020a203aSopenharmony_ci    if (retCode == 0) {
58020a203aSopenharmony_ci        return HiviewNapiUtil::GenerateFileInfoResult(env, fileInfos);
59020a203aSopenharmony_ci    } else {
60020a203aSopenharmony_ci        HiviewNapiUtil::ThrowErrorByCode(env, retCode);
61020a203aSopenharmony_ci        return result;
62020a203aSopenharmony_ci    }
63020a203aSopenharmony_ci}
64020a203aSopenharmony_ci
65020a203aSopenharmony_cistatic napi_value CopyOrMoveFile(napi_env env, napi_callback_info info, bool isMove)
66020a203aSopenharmony_ci{
67020a203aSopenharmony_ci    if (isMove) {
68020a203aSopenharmony_ci        HIVIEW_LOGI("call move");
69020a203aSopenharmony_ci    } else {
70020a203aSopenharmony_ci        HIVIEW_LOGI("call copy");
71020a203aSopenharmony_ci    }
72020a203aSopenharmony_ci    constexpr size_t maxParamNum = 4;
73020a203aSopenharmony_ci    constexpr size_t paramNumWithoutCallback = 3;
74020a203aSopenharmony_ci    size_t paramNum = maxParamNum;
75020a203aSopenharmony_ci    napi_value params[maxParamNum] = { 0 };
76020a203aSopenharmony_ci    NAPI_CALL(env, napi_get_cb_info(env, info, &paramNum, params, nullptr, nullptr));
77020a203aSopenharmony_ci    napi_value result = nullptr;
78020a203aSopenharmony_ci    napi_get_undefined(env, &result);
79020a203aSopenharmony_ci    if (paramNum < paramNumWithoutCallback) {
80020a203aSopenharmony_ci        HIVIEW_LOGE("num of params is invalid %{public}zu.", paramNum);
81020a203aSopenharmony_ci        return result;
82020a203aSopenharmony_ci    }
83020a203aSopenharmony_ci    std::string logType;
84020a203aSopenharmony_ci    std::string logName;
85020a203aSopenharmony_ci    std::string destDir;
86020a203aSopenharmony_ci    if (!HiviewNapiUtil::ParseStringValue(env, "logType", params[LOG_TYPE_INDEX], logType)
87020a203aSopenharmony_ci        || !HiviewNapiUtil::ParseStringValue(env, "logName", params[LOG_NAME_INDEX], logName)
88020a203aSopenharmony_ci        || !HiviewNapiUtil::ParseStringValue(env, "dest", params[DEST_DIR_INDEX], destDir)) {
89020a203aSopenharmony_ci        return result;
90020a203aSopenharmony_ci    }
91020a203aSopenharmony_ci    HIVIEW_LOGI("type:%{public}s, name:%{public}s, dir: %{public}s",
92020a203aSopenharmony_ci        logType.c_str(), StringUtil::HideSnInfo(logName).c_str(), destDir.c_str());
93020a203aSopenharmony_ci    if (!HiviewNapiUtil::CheckDirPath(destDir)) {
94020a203aSopenharmony_ci        HIVIEW_LOGE("dest param is invalid: %{public}s", destDir.c_str());
95020a203aSopenharmony_ci        HiviewNapiUtil::ThrowParamContentError(env, "dest");
96020a203aSopenharmony_ci        return result;
97020a203aSopenharmony_ci    }
98020a203aSopenharmony_ci    HiviewFileParams* hiviewFileParams = new(std::nothrow) HiviewFileParams(logType, logName, destDir);
99020a203aSopenharmony_ci    if (hiviewFileParams == nullptr) {
100020a203aSopenharmony_ci        HIVIEW_LOGE("failed to allocate memory");
101020a203aSopenharmony_ci        return result;
102020a203aSopenharmony_ci    } else if (paramNum == maxParamNum) {
103020a203aSopenharmony_ci        if (!HiviewNapiUtil::IsMatchType(env, params[paramNumWithoutCallback], napi_function)) {
104020a203aSopenharmony_ci            HIVIEW_LOGE("no valid function param");
105020a203aSopenharmony_ci            HiviewNapiUtil::ThrowParamTypeError(env, "callback", "function");
106020a203aSopenharmony_ci            delete hiviewFileParams;
107020a203aSopenharmony_ci            return result;
108020a203aSopenharmony_ci        }
109020a203aSopenharmony_ci        napi_create_reference(env, params[paramNumWithoutCallback], 1, &hiviewFileParams->callback);
110020a203aSopenharmony_ci    } else {
111020a203aSopenharmony_ci        napi_create_promise(env, &hiviewFileParams->deferred, &result);
112020a203aSopenharmony_ci    }
113020a203aSopenharmony_ci    isMove ? HiviewNapiAdapter::Move(env, hiviewFileParams) : HiviewNapiAdapter::Copy(env, hiviewFileParams);
114020a203aSopenharmony_ci    return result;
115020a203aSopenharmony_ci}
116020a203aSopenharmony_ci
117020a203aSopenharmony_cistatic napi_value Copy(napi_env env, napi_callback_info info)
118020a203aSopenharmony_ci{
119020a203aSopenharmony_ci    if (!HiviewNapiUtil::IsSystemAppCall()) {
120020a203aSopenharmony_ci        HiviewNapiUtil::ThrowSystemAppPermissionError(env);
121020a203aSopenharmony_ci        return nullptr;
122020a203aSopenharmony_ci    }
123020a203aSopenharmony_ci    return CopyOrMoveFile(env, info, false);
124020a203aSopenharmony_ci}
125020a203aSopenharmony_ci
126020a203aSopenharmony_cistatic napi_value Move(napi_env env, napi_callback_info info)
127020a203aSopenharmony_ci{
128020a203aSopenharmony_ci    if (!HiviewNapiUtil::IsSystemAppCall()) {
129020a203aSopenharmony_ci        HiviewNapiUtil::ThrowSystemAppPermissionError(env);
130020a203aSopenharmony_ci        return nullptr;
131020a203aSopenharmony_ci    }
132020a203aSopenharmony_ci    return CopyOrMoveFile(env, info, true);
133020a203aSopenharmony_ci}
134020a203aSopenharmony_ci
135020a203aSopenharmony_cistatic napi_value Remove(napi_env env, napi_callback_info info)
136020a203aSopenharmony_ci{
137020a203aSopenharmony_ci    if (!HiviewNapiUtil::IsSystemAppCall()) {
138020a203aSopenharmony_ci        HiviewNapiUtil::ThrowSystemAppPermissionError(env);
139020a203aSopenharmony_ci        return nullptr;
140020a203aSopenharmony_ci    }
141020a203aSopenharmony_ci    constexpr size_t removeParamNum = 2;
142020a203aSopenharmony_ci    size_t paramNum = removeParamNum;
143020a203aSopenharmony_ci    napi_value params[removeParamNum] = { 0 };
144020a203aSopenharmony_ci    napi_value result = nullptr;
145020a203aSopenharmony_ci    napi_get_undefined(env, &result);
146020a203aSopenharmony_ci    NAPI_CALL(env, napi_get_cb_info(env, info, &paramNum, params, nullptr, nullptr));
147020a203aSopenharmony_ci    if (paramNum < removeParamNum) {
148020a203aSopenharmony_ci        HIVIEW_LOGE("num of params is invalid: %{public}zu", paramNum);
149020a203aSopenharmony_ci        return result;
150020a203aSopenharmony_ci    }
151020a203aSopenharmony_ci    std::string logType;
152020a203aSopenharmony_ci    std::string logName;
153020a203aSopenharmony_ci    if (!HiviewNapiUtil::ParseStringValue(env, "logType", params[LOG_TYPE_INDEX], logType)
154020a203aSopenharmony_ci        || !HiviewNapiUtil::ParseStringValue(env, "logName", params[LOG_NAME_INDEX], logName)) {
155020a203aSopenharmony_ci        return result;
156020a203aSopenharmony_ci    }
157020a203aSopenharmony_ci    HIVIEW_LOGI("type: %{public}s, name: %{public}s", logType.c_str(), StringUtil::HideSnInfo(logName).c_str());
158020a203aSopenharmony_ci    int32_t retCode = HiviewServiceAgent::GetInstance().Remove(logType, logName);
159020a203aSopenharmony_ci    if (retCode != 0) {
160020a203aSopenharmony_ci        HIVIEW_LOGI("retCode: %{public}u.", retCode);
161020a203aSopenharmony_ci        HiviewNapiUtil::ThrowErrorByCode(env, retCode);
162020a203aSopenharmony_ci    }
163020a203aSopenharmony_ci    return result;
164020a203aSopenharmony_ci}
165020a203aSopenharmony_ci
166020a203aSopenharmony_ciEXTERN_C_START
167020a203aSopenharmony_cistatic napi_value Init(napi_env env, napi_value exports)
168020a203aSopenharmony_ci{
169020a203aSopenharmony_ci    napi_property_descriptor desc[] = {
170020a203aSopenharmony_ci        DECLARE_NAPI_FUNCTION("list", List),
171020a203aSopenharmony_ci        DECLARE_NAPI_FUNCTION("copy", Copy),
172020a203aSopenharmony_ci        DECLARE_NAPI_FUNCTION("move", Move),
173020a203aSopenharmony_ci        DECLARE_NAPI_FUNCTION("remove", Remove)
174020a203aSopenharmony_ci    };
175020a203aSopenharmony_ci    NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(napi_property_descriptor), desc));
176020a203aSopenharmony_ci    return exports;
177020a203aSopenharmony_ci}
178020a203aSopenharmony_ciEXTERN_C_END
179020a203aSopenharmony_ci
180020a203aSopenharmony_cistatic napi_module _module = {
181020a203aSopenharmony_ci    .nm_version = 1,
182020a203aSopenharmony_ci    .nm_flags = 0,
183020a203aSopenharmony_ci    .nm_filename = nullptr,
184020a203aSopenharmony_ci    .nm_register_func = Init,
185020a203aSopenharmony_ci    .nm_modname = "logLibrary",
186020a203aSopenharmony_ci    .nm_priv = ((void *) 0),
187020a203aSopenharmony_ci    .reserved = {0}
188020a203aSopenharmony_ci};
189020a203aSopenharmony_ci
190020a203aSopenharmony_ciextern "C" __attribute__((constructor)) void RegisterModule(void)
191020a203aSopenharmony_ci{
192020a203aSopenharmony_ci    napi_module_register(&_module);
193020a203aSopenharmony_ci}
194020a203aSopenharmony_ci} // namespace HiviewDFX
195020a203aSopenharmony_ci} // namespace OHOS