• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 "loglibrary_ani.h"
17 #include "loglibrary_ani_util.h"
18 #include "hiview_service_agent.h"
19 
20 using namespace OHOS::HiviewDFX;
21 namespace {
22 DEFINE_LOG_LABEL(0xD002D10, "LogLibraryAni");
23 }
24 
List(ani_env * env,ani_string logType)25 ani_ref LogLibraryAni::List(ani_env *env, ani_string logType)
26 {
27     ani_ref undefined = LogLibraryAniUtil::GetAniUndefined(env);
28     if (!LogLibraryAniUtil::IsSystemAppCall()) {
29         LogLibraryAniUtil::ThrowAniError(env, HiviewNapiErrCode::ERR_NON_SYS_APP_PERMISSION,
30             "Permission denied, non-system app called system api.");
31         return undefined;
32     }
33     std::string logTypeTemp = LogLibraryAniUtil::ParseStringValue(env, logType);
34     std::vector<HiviewFileInfo> fileInfos;
35     int32_t retCode = HiviewServiceAgent::GetInstance().List(logTypeTemp, fileInfos);
36     HIVIEW_LOGI("retCode: %{public}u.", retCode);
37     if (retCode == 0) {
38         return LogLibraryAniUtil::ListResult(env, fileInfos);
39     } else {
40         LogLibraryAniUtil::ThrowAniError(env, LogLibraryAniUtil::GetErrorDetailByRet(retCode).first,
41             LogLibraryAniUtil::GetErrorDetailByRet(retCode).second);
42         return undefined;
43     }
44 }
45 
Copy(ani_env * env,ani_string logType,ani_string logName,ani_string dest)46 ani_object LogLibraryAni::Copy(ani_env *env, ani_string logType, ani_string logName, ani_string dest)
47 {
48     if (!LogLibraryAniUtil::IsSystemAppCall()) {
49         return LogLibraryAniUtil::CopyOrMoveResult(env,
50             LogLibraryAniUtil::GetErrorDetailByRet(HiviewNapiErrCode::ERR_NON_SYS_APP_PERMISSION));
51     }
52     return LogLibraryAniUtil::CopyOrMoveFile(env, logType, logName, dest, false);
53 }
54 
Move(ani_env * env,ani_string logType,ani_string logName,ani_string dest)55 ani_object LogLibraryAni::Move(ani_env *env, ani_string logType, ani_string logName, ani_string dest)
56 {
57     if (!LogLibraryAniUtil::IsSystemAppCall()) {
58         return LogLibraryAniUtil::CopyOrMoveResult(env,
59             LogLibraryAniUtil::GetErrorDetailByRet(HiviewNapiErrCode::ERR_NON_SYS_APP_PERMISSION));
60     }
61     return LogLibraryAniUtil::CopyOrMoveFile(env, logType, logName, dest, true);
62 }
63 
Remove(ani_env * env,ani_string logType,ani_string logName)64 void LogLibraryAni::Remove(ani_env *env, ani_string logType, ani_string logName)
65 {
66     if (!LogLibraryAniUtil::IsSystemAppCall()) {
67         LogLibraryAniUtil::ThrowAniError(env, HiviewNapiErrCode::ERR_NON_SYS_APP_PERMISSION,
68             "Permission denied, non-system app called system api.");
69         return;
70     }
71 
72     std::string logTypeTemp = LogLibraryAniUtil::ParseStringValue(env, logType);
73     std::string logNameTemp = LogLibraryAniUtil::ParseStringValue(env, logName);
74     HIVIEW_LOGI("type: %{public}s, name: %{public}s", logTypeTemp.c_str(),
75         StringUtil::HideSnInfo(logNameTemp).c_str());
76     int32_t retCode = HiviewServiceAgent::GetInstance().Remove(logTypeTemp, logNameTemp);
77     if (retCode != 0) {
78         HIVIEW_LOGI("retCode: %{public}u.", retCode);
79         LogLibraryAniUtil::ThrowAniError(env, LogLibraryAniUtil::GetErrorDetailByRet(retCode).first,
80             LogLibraryAniUtil::GetErrorDetailByRet(retCode).second);
81     }
82 }
83 
ANI_Constructor(ani_vm * vm,uint32_t * result)84 ANI_EXPORT ani_status ANI_Constructor(ani_vm *vm, uint32_t *result)
85 {
86     ani_env *env = nullptr;
87     if (ANI_OK != vm->GetEnv(ANI_VERSION_1, &env)) {
88         HILOG_ERROR(LOG_CORE, "Unsupported ANI_VERSION_1");
89         return ANI_ERROR;
90     }
91 
92     ani_namespace ns {};
93     if (ANI_OK != env->FindNamespace(CLASS_NAME_LOGLIBRARY, &ns)) {
94         HILOG_ERROR(LOG_CORE, "FindNamespace %{public}s failed", CLASS_NAME_LOGLIBRARY);
95         return ANI_ERROR;
96     }
97 
98     std::array methods = {
99         ani_native_function {"list", nullptr, reinterpret_cast<void *>(LogLibraryAni::List)},
100         ani_native_function {"copySync", nullptr, reinterpret_cast<void *>(LogLibraryAni::Copy)},
101         ani_native_function {"moveSync", nullptr, reinterpret_cast<void *>(LogLibraryAni::Move)},
102         ani_native_function {"remove", nullptr, reinterpret_cast<void *>(LogLibraryAni::Remove)},
103     };
104 
105     if (ANI_OK != env->Namespace_BindNativeFunctions(ns, methods.data(), methods.size())) {
106         HILOG_ERROR(LOG_CORE, "Namespace %{public}s BindNativeFunctions failed", CLASS_NAME_LOGLIBRARY);
107         return ANI_ERROR;
108     };
109 
110     *result = ANI_VERSION_1;
111     return ANI_OK;
112 }
113