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_napi_adapter.h" 17020a203aSopenharmony_ci 18020a203aSopenharmony_ci#include "hiview_err_code.h" 19020a203aSopenharmony_ci#include "hiview_napi_util.h" 20020a203aSopenharmony_ci#include "hiview_service_agent.h" 21020a203aSopenharmony_ci 22020a203aSopenharmony_cinamespace OHOS { 23020a203aSopenharmony_cinamespace HiviewDFX { 24020a203aSopenharmony_cinamespace { 25020a203aSopenharmony_ciconstexpr size_t ERR_INDEX = 0; 26020a203aSopenharmony_ciconstexpr size_t VAL_INDEX = 1; 27020a203aSopenharmony_ciconstexpr size_t RET_SIZE = 2; 28020a203aSopenharmony_ci} 29020a203aSopenharmony_ci 30020a203aSopenharmony_civoid HiviewNapiAdapter::Copy(napi_env env, HiviewFileParams* params) 31020a203aSopenharmony_ci{ 32020a203aSopenharmony_ci napi_value resource = nullptr; 33020a203aSopenharmony_ci HiviewNapiUtil::CreateStringValue(env, "CopyFileProcess", resource); 34020a203aSopenharmony_ci napi_create_async_work(env, nullptr, resource, CopyFileExecution, 35020a203aSopenharmony_ci FileOperationCompleteCallback, reinterpret_cast<void*>(params), ¶ms->asyncWork); 36020a203aSopenharmony_ci napi_queue_async_work_with_qos(env, params->asyncWork, napi_qos_default); 37020a203aSopenharmony_ci} 38020a203aSopenharmony_ci 39020a203aSopenharmony_civoid HiviewNapiAdapter::CopyFileExecution(napi_env env, void* data) 40020a203aSopenharmony_ci{ 41020a203aSopenharmony_ci HiviewFileParams* params = reinterpret_cast<HiviewFileParams*>(data); 42020a203aSopenharmony_ci params->result = HiviewServiceAgent::GetInstance().Copy(params->logType, params->logName, params->destDir); 43020a203aSopenharmony_ci} 44020a203aSopenharmony_ci 45020a203aSopenharmony_civoid HiviewNapiAdapter::Move(napi_env env, HiviewFileParams* params) 46020a203aSopenharmony_ci{ 47020a203aSopenharmony_ci napi_value resource = nullptr; 48020a203aSopenharmony_ci HiviewNapiUtil::CreateStringValue(env, "MoveFileProcess", resource); 49020a203aSopenharmony_ci napi_create_async_work(env, nullptr, resource, MoveFileExecution, 50020a203aSopenharmony_ci FileOperationCompleteCallback, reinterpret_cast<void*>(params), ¶ms->asyncWork); 51020a203aSopenharmony_ci napi_queue_async_work_with_qos(env, params->asyncWork, napi_qos_default); 52020a203aSopenharmony_ci} 53020a203aSopenharmony_ci 54020a203aSopenharmony_civoid HiviewNapiAdapter::MoveFileExecution(napi_env env, void* data) 55020a203aSopenharmony_ci{ 56020a203aSopenharmony_ci HiviewFileParams* params = reinterpret_cast<HiviewFileParams*>(data); 57020a203aSopenharmony_ci params->result = HiviewServiceAgent::GetInstance().Move(params->logType, params->logName, params->destDir); 58020a203aSopenharmony_ci} 59020a203aSopenharmony_ci 60020a203aSopenharmony_civoid HiviewNapiAdapter::FileOperationCompleteCallback(napi_env env, napi_status status, void* data) 61020a203aSopenharmony_ci{ 62020a203aSopenharmony_ci HiviewFileParams* params = reinterpret_cast<HiviewFileParams*>(data); 63020a203aSopenharmony_ci napi_value results[RET_SIZE] = {0}; 64020a203aSopenharmony_ci auto isSuccess = (params->result == 0); 65020a203aSopenharmony_ci if (isSuccess) { 66020a203aSopenharmony_ci HiviewNapiUtil::CreateUndefined(env, results[ERR_INDEX]); 67020a203aSopenharmony_ci HiviewNapiUtil::CreateInt32Value(env, params->result, results[VAL_INDEX]); 68020a203aSopenharmony_ci } else { 69020a203aSopenharmony_ci HiviewNapiUtil::CreateUndefined(env, results[VAL_INDEX]); 70020a203aSopenharmony_ci HiviewNapiUtil::CreateErrorByRet(env, params->result, results[ERR_INDEX]); 71020a203aSopenharmony_ci } 72020a203aSopenharmony_ci if (params->deferred != nullptr) { 73020a203aSopenharmony_ci isSuccess ? napi_resolve_deferred(env, params->deferred, results[VAL_INDEX]) : 74020a203aSopenharmony_ci napi_reject_deferred(env, params->deferred, results[ERR_INDEX]); 75020a203aSopenharmony_ci } else { 76020a203aSopenharmony_ci napi_value callback = nullptr; 77020a203aSopenharmony_ci napi_get_reference_value(env, params->callback, &callback); 78020a203aSopenharmony_ci napi_value retValue = nullptr; 79020a203aSopenharmony_ci napi_call_function(env, nullptr, callback, RET_SIZE, results, &retValue); 80020a203aSopenharmony_ci napi_delete_reference(env, params->callback); 81020a203aSopenharmony_ci } 82020a203aSopenharmony_ci napi_delete_async_work(env, params->asyncWork); 83020a203aSopenharmony_ci delete params; 84020a203aSopenharmony_ci} 85020a203aSopenharmony_ci} // namespace HiviewDFX 86020a203aSopenharmony_ci} // namespace OHOS 87