1 /*
2 * Copyright (c) 2023 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 "hiview_napi_adapter.h"
17
18 #include "hiview_napi_err_code.h"
19 #include "hiview_napi_util.h"
20 #include "hiview_service_agent.h"
21
22 namespace OHOS {
23 namespace HiviewDFX {
24 namespace {
25 constexpr size_t ERR_INDEX = 0;
26 constexpr size_t VAL_INDEX = 1;
27 constexpr size_t RET_SIZE = 2;
28 }
29
Copy(napi_env env,HiviewFileParams * params)30 void HiviewNapiAdapter::Copy(napi_env env, HiviewFileParams* params)
31 {
32 napi_value resource = nullptr;
33 HiviewNapiUtil::CreateStringValue(env, "CopyFileProcess", resource);
34 napi_create_async_work(env, nullptr, resource, CopyFileExecution,
35 FileOperationCompleteCallback, reinterpret_cast<void*>(params), ¶ms->asyncWork);
36 napi_queue_async_work_with_qos(env, params->asyncWork, napi_qos_default);
37 }
38
CopyFileExecution(napi_env env,void * data)39 void HiviewNapiAdapter::CopyFileExecution(napi_env env, void* data)
40 {
41 HiviewFileParams* params = reinterpret_cast<HiviewFileParams*>(data);
42 params->result = HiviewServiceAgent::Copy(params->logType, params->logName, params->destDir);
43 }
44
Move(napi_env env,HiviewFileParams * params)45 void HiviewNapiAdapter::Move(napi_env env, HiviewFileParams* params)
46 {
47 napi_value resource = nullptr;
48 HiviewNapiUtil::CreateStringValue(env, "MoveFileProcess", resource);
49 napi_create_async_work(env, nullptr, resource, MoveFileExecution,
50 FileOperationCompleteCallback, reinterpret_cast<void*>(params), ¶ms->asyncWork);
51 napi_queue_async_work_with_qos(env, params->asyncWork, napi_qos_default);
52 }
53
MoveFileExecution(napi_env env,void * data)54 void HiviewNapiAdapter::MoveFileExecution(napi_env env, void* data)
55 {
56 HiviewFileParams* params = reinterpret_cast<HiviewFileParams*>(data);
57 params->result = HiviewServiceAgent::Move(params->logType, params->logName, params->destDir);
58 }
59
FileOperationCompleteCallback(napi_env env,napi_status status,void * data)60 void HiviewNapiAdapter::FileOperationCompleteCallback(napi_env env, napi_status status, void* data)
61 {
62 HiviewFileParams* params = reinterpret_cast<HiviewFileParams*>(data);
63 napi_value results[RET_SIZE] = {0};
64 auto isSuccess = (params->result == 0);
65 if (isSuccess) {
66 HiviewNapiUtil::CreateUndefined(env, results[ERR_INDEX]);
67 HiviewNapiUtil::CreateInt32Value(env, params->result, results[VAL_INDEX]);
68 } else {
69 HiviewNapiUtil::CreateUndefined(env, results[VAL_INDEX]);
70 HiviewNapiUtil::CreateErrorByRet(env, params->result, results[ERR_INDEX]);
71 }
72 if (params->deferred != nullptr) {
73 isSuccess ? napi_resolve_deferred(env, params->deferred, results[VAL_INDEX]) :
74 napi_reject_deferred(env, params->deferred, results[ERR_INDEX]);
75 } else {
76 napi_value callback = nullptr;
77 napi_get_reference_value(env, params->callback, &callback);
78 napi_value retValue = nullptr;
79 napi_call_function(env, nullptr, callback, RET_SIZE, results, &retValue);
80 napi_delete_reference(env, params->callback);
81 }
82 napi_delete_async_work(env, params->asyncWork);
83 delete params;
84 }
85 } // namespace HiviewDFX
86 } // namespace OHOS
87