• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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().AddProcessor(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().RemoveObserver(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 (!IsValidUserIdValue(std::string(value))) {
163         return ERR_CODE_PARAM_INVALID;
164     }
165     if (UserInfo::GetInstance().SetUserId(name, value) != 0) {
166         LOGE("failed to set userId");
167         return ERR_CODE_PARAM_INVALID;
168     }
169     return SUCCESS_CODE;
170 }
171 
GetUserId(const std::string & name)172 std::tuple<int, std::string> HiAppEventImpl::GetUserId(const std::string& name)
173 {
174     std::string strUserId;
175     if (UserInfo::GetInstance().GetUserId(name, strUserId) != 0) {
176         LOGE("failed to get userId");
177         return {ERR_CODE_PARAM_INVALID, nullptr};
178     }
179     return {SUCCESS_CODE, strUserId};
180 }
181 
SetUserProperty(const std::string & name,const std::string & value)182 int HiAppEventImpl::SetUserProperty(const std::string& name, const std::string& value)
183 {
184     if (value.empty()) {
185         if (UserInfo::GetInstance().RemoveUserProperty(name) != 0) {
186             LOGE("failed to set user propertyd");
187             return ERR_CODE_PARAM_INVALID;
188         }
189         return SUCCESS_CODE;
190     }
191     if (UserInfo::GetInstance().SetUserProperty(name, value) != 0) {
192         LOGE("failed to set user property");
193         return ERR_CODE_PARAM_INVALID;
194     }
195     return SUCCESS_CODE;
196 }
197 
GetUserProperty(const std::string & name)198 std::tuple<int, std::string> HiAppEventImpl::GetUserProperty(const std::string& name)
199 {
200     std::string strUserProperty;
201     if (UserInfo::GetInstance().GetUserProperty(name, strUserProperty) != 0) {
202         LOGE("failed to get user property");
203         return {ERR_CODE_PARAM_INVALID, nullptr};
204     }
205     return {SUCCESS_CODE, strUserProperty};
206 }
207 
ClearData()208 void HiAppEventImpl::ClearData()
209 {
210     std::string dir = HiAppEventConfig::GetInstance().GetStorageDir();
211     HiAppEventClean::ClearData(dir);
212 }
213 
addWatcher(const std::string & name,const std::vector<AppEventFilter> & filters,const TriggerCondition & cond,void (* callbackOnTriggerRef)(int,int,int64_t),void (* callbackOnReceiveRef)(char *,CArrRetAppEventGroup))214 std::tuple<int, int64_t> HiAppEventImpl::addWatcher(const std::string& name,
215                                                     const std::vector<AppEventFilter>& filters,
216                                                     const TriggerCondition& cond,
217                                                     void (*callbackOnTriggerRef)(int, int, int64_t),
218                                                     void (*callbackOnReceiveRef)(char*, CArrRetAppEventGroup))
219 {
220     auto watcherPtr = std::make_shared<AppEventWatcherImpl>(name, filters, cond);
221     if (callbackOnTriggerRef != (void*)-1) {
222         watcherPtr->InitTrigger(callbackOnTriggerRef);
223     }
224     if (callbackOnReceiveRef != (void*)-1) {
225         watcherPtr->InitReceiver(callbackOnReceiveRef);
226     }
227     int64_t observerSeq = AppEventObserverMgr::GetInstance().AddWatcher(watcherPtr);
228     if (observerSeq <= 0) {
229         LOGE("invalid observer sequence");
230         return {ERR_CODE_PARAM_INVALID, -1};
231     }
232     auto holder = OHOS::FFI::FFIData::Create<AppEventPackageHolderImpl>(name, -1);
233     if (holder == nullptr) {
234         return {ERR_PARAM, -1};
235     }
236     watcherPtr->InitHolder(holder);
237     return {SUCCESS_CODE, holder->GetID()};
238 }
239 
removeWatcher(const std::string & name)240 void HiAppEventImpl::removeWatcher(const std::string& name)
241 {
242     AppEventObserverMgr::GetInstance().RemoveObserver(name);
243 }
244 
Load(const std::string & moduleName)245 int HiAppEventImpl::Load(const std::string& moduleName)
246 {
247     return AppEventObserverMgr::GetInstance().Load(moduleName);
248 }
249 } // HiAppEvent
250 } // CJSystemapi
251 } // OHOS