1 /*
2 * Copyright (c) 2024 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 <mutex>
17 #include <string>
18
19 #include "appevent_watcher_impl.h"
20 #include "app_event_observer_mgr.h"
21 #include "cj_ffi/cj_common_ffi.h"
22 #include "error.h"
23 #include "file_util.h"
24 #include "hiappevent_clean.h"
25 #include "hiappevent_config.h"
26 #include "hiappevent_impl.h"
27 #include "hiappevent_userinfo.h"
28 #include "hiappevent_verify.h"
29 #include "hiappevent_write.h"
30 #include "log.h"
31 #include "time_util.h"
32
33 using namespace OHOS::HiviewDFX;
34 using namespace OHOS::HiviewDFX::HiAppEvent;
35
36 namespace OHOS {
37 namespace CJSystemapi {
38 namespace HiAppEvent {
39 std::mutex g_mutex;
Configure(bool disable, const std::string& maxStorage)40 int HiAppEventImpl::Configure(bool disable, const std::string& maxStorage)
41 {
42 std::string disableStr = disable == true ? "true" : "false";
43 bool disableRes = HiAppEventConfig::GetInstance().SetConfigurationItem("disable", disableStr);
44 if (!disableRes) {
45 LOGE("HiAppEvent failed to configure disable HiAppEvent");
46 return ERR_INVALID_MAX_STORAGE;
47 }
48 bool maxStorageRes = HiAppEventConfig::GetInstance().SetConfigurationItem("max_storage", maxStorage);
49 if (!maxStorageRes) {
50 LOGE("HiAppEvent failed to configure maxStorage HiAppEvent");
51 return ERR_INVALID_MAX_STORAGE;
52 }
53 return SUCCESS_CODE;
54 }
55
GetStorageDirPath()56 std::string GetStorageDirPath()
57 {
58 return HiAppEventConfig::GetInstance().GetStorageDir();
59 }
60
GetMaxStorageSize()61 uint64_t GetMaxStorageSize()
62 {
63 return HiAppEventConfig::GetInstance().GetMaxStorageSize();
64 }
65
GetStorageFileName()66 std::string GetStorageFileName()
67 {
68 return "app_event_" + TimeUtil::GetDate() + ".log";
69 }
70
CheckStorageSpace(const std::string& dir)71 void CheckStorageSpace(const std::string& dir)
72 {
73 auto maxSize = GetMaxStorageSize();
74 if (!HiAppEventClean::IsStorageSpaceFull(dir, maxSize)) {
75 return;
76 }
77 LOGI("hiappevent dir space is full, start to clean");
78 HiAppEventClean::ReleaseSomeStorageSpace(dir, maxSize);
79 }
80
WriteEventToFile(const std::string& filePath, const std::string& event)81 bool WriteEventToFile(const std::string& filePath, const std::string& event)
82 {
83 return FileUtil::SaveStringToFile(filePath, event);
84 }
85
HiWriteEvent(std::shared_ptr<AppEventPack> appEventPack)86 void HiWriteEvent(std::shared_ptr<AppEventPack> appEventPack)
87 {
88 if (HiAppEventConfig::GetInstance().GetDisable()) {
89 LOGE("the HiAppEvent function is disabled.");
90 return;
91 }
92 if (appEventPack == nullptr) {
93 LOGE("appEventPack is null.");
94 return;
95 }
96 std::string dirPath = GetStorageDirPath();
97 if (dirPath.empty()) {
98 LOGE("dirPath is null, stop writing the event.");
99 return;
100 }
101 std::string event = appEventPack->GetEventStr();
102 {
103 std::lock_guard<std::mutex> lockGuard(g_mutex);
104 if (!FileUtil::IsFileExists(dirPath) && !FileUtil::ForceCreateDirectory(dirPath)) {
105 LOGE("failed to create hiappevent dir, errno=%{public}d.", errno);
106 return;
107 }
108 CheckStorageSpace(dirPath);
109 std::string filePath = FileUtil::GetFilePathByDir(dirPath, GetStorageFileName());
110 if (WriteEventToFile(filePath, event)) {
111 std::vector<std::shared_ptr<AppEventPack>> events;
112 events.emplace_back(appEventPack);
113 AppEventObserverMgr::GetInstance().HandleEvents(events);
114 return;
115 }
116 LOGE("failed to write event to log file, errno=%{public}d.", errno);
117 }
118 }
119
Write(std::shared_ptr<HiviewDFX::AppEventPack> appEventPack)120 int HiAppEventImpl::Write(std::shared_ptr<HiviewDFX::AppEventPack> appEventPack)
121 {
122 if (auto ret = VerifyAppEvent(appEventPack); ret != 0) {
123 LOGE("HiAppEvent failed to write HiAppEvent %{public}d", ret);
124 return ret;
125 }
126 HiWriteEvent(appEventPack);
127 return SUCCESS_CODE;
128 }
129
AddProcessor(const ReportConfig& conf)130 int64_t HiAppEventImpl::AddProcessor(const ReportConfig& conf)
131 {
132 int64_t processorId = AppEventObserverMgr::GetInstance().RegisterObserver(conf.name, conf);
133 if (processorId <= 0) {
134 LOGE("failed to add processor=%{public}s, register processor error", conf.name.c_str());
135 return processorId;
136 }
137 return processorId;
138 }
139
RemoveProcessor(int64_t processorId)140 int HiAppEventImpl::RemoveProcessor(int64_t processorId)
141 {
142 if (processorId <= 0) {
143 LOGE("failed to remove processor id=%{public}" PRIi64 "", processorId);
144 return SUCCESS_CODE;
145 }
146 if (AppEventObserverMgr::GetInstance().UnregisterObserver(processorId) != 0) {
147 LOGE("failed to remove processor id=%{public}" PRIi64"", processorId);
148 return ERR_CODE_PARAM_INVALID;
149 }
150 return SUCCESS_CODE;
151 }
152
SetUserId(const std::string& name, const std::string& value)153 int HiAppEventImpl::SetUserId(const std::string& name, const std::string& value)
154 {
155 if (value.empty()) {
156 if (UserInfo::GetInstance().RemoveUserId(name) != 0) {
157 LOGE("failed to remove userId");
158 return ERR_CODE_PARAM_INVALID;
159 }
160 return SUCCESS_CODE;
161 }
162 if (UserInfo::GetInstance().SetUserId(name, value) != 0) {
163 LOGE("failed to set userId");
164 return ERR_CODE_PARAM_INVALID;
165 }
166 return SUCCESS_CODE;
167 }
168
GetUserId(const std::string& name)169 std::tuple<int, std::string> HiAppEventImpl::GetUserId(const std::string& name)
170 {
171 std::string strUserId;
172 if (UserInfo::GetInstance().GetUserId(name, strUserId) != 0) {
173 LOGE("failed to get userId");
174 return {ERR_CODE_PARAM_INVALID, nullptr};
175 }
176 return {SUCCESS_CODE, strUserId};
177 }
178
SetUserProperty(const std::string& name, const std::string& value)179 int HiAppEventImpl::SetUserProperty(const std::string& name, const std::string& value)
180 {
181 if (value.empty()) {
182 if (UserInfo::GetInstance().RemoveUserProperty(name) != 0) {
183 LOGE("failed to set user propertyd");
184 return ERR_CODE_PARAM_INVALID;
185 }
186 return SUCCESS_CODE;
187 }
188 if (UserInfo::GetInstance().SetUserProperty(name, value) != 0) {
189 LOGE("failed to set user property");
190 return ERR_CODE_PARAM_INVALID;
191 }
192 return SUCCESS_CODE;
193 }
194
GetUserProperty(const std::string& name)195 std::tuple<int, std::string> HiAppEventImpl::GetUserProperty(const std::string& name)
196 {
197 std::string strUserProperty;
198 if (UserInfo::GetInstance().GetUserProperty(name, strUserProperty) != 0) {
199 LOGE("failed to get user property");
200 return {ERR_CODE_PARAM_INVALID, nullptr};
201 }
202 return {SUCCESS_CODE, strUserProperty};
203 }
204
ClearData()205 void HiAppEventImpl::ClearData()
206 {
207 std::string dir = HiAppEventConfig::GetInstance().GetStorageDir();
208 HiAppEventClean::ClearData(dir);
209 }
210
addWatcher(const std::string& name, const std::vector<AppEventFilter>& filters, const TriggerCondition& cond, void (*callbackOnTriggerRef)(int, int, int64_t), void (*callbackOnReceiveRef)(char*, CArrRetAppEventGroup))211 std::tuple<int, int64_t> HiAppEventImpl::addWatcher(const std::string& name,
212 const std::vector<AppEventFilter>& filters,
213 const TriggerCondition& cond,
214 void (*callbackOnTriggerRef)(int, int, int64_t),
215 void (*callbackOnReceiveRef)(char*, CArrRetAppEventGroup))
216 {
217 auto watcherPtr = std::make_shared<AppEventWatcherImpl>(name, filters, cond);
218 if (callbackOnTriggerRef != (void*)-1) {
219 watcherPtr->InitTrigger(callbackOnTriggerRef);
220 }
221 if (callbackOnReceiveRef != (void*)-1) {
222 watcherPtr->InitReceiver(callbackOnReceiveRef);
223 }
224 int64_t observerSeq = AppEventObserverMgr::GetInstance().RegisterObserver(watcherPtr);
225 if (observerSeq <= 0) {
226 LOGE("invalid observer sequence");
227 return {ERR_CODE_PARAM_INVALID, -1};
228 }
229 auto holder = OHOS::FFI::FFIData::Create<AppEventPackageHolderImpl>(name, -1);
230 if (holder == nullptr) {
231 return {ERR_PARAM, -1};
232 }
233 watcherPtr->InitHolder(holder);
234 return {SUCCESS_CODE, holder->GetID()};
235 }
236
removeWatcher(const std::string& name)237 void HiAppEventImpl::removeWatcher(const std::string& name)
238 {
239 AppEventObserverMgr::GetInstance().UnregisterObserver(name);
240 }
241
Load(const std::string& moduleName)242 int HiAppEventImpl::Load(const std::string& moduleName)
243 {
244 return AppEventObserverMgr::GetInstance().Load(moduleName);
245 }
246 } // HiAppEvent
247 } // CJSystemapi
248 } // OHOS