1 /*
2  * Copyright (c) 2023-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 #include "app_event_processor_proxy.h"
16 
17 #include <algorithm>
18 
19 #include "app_event_store.h"
20 #include "ffrt.h"
21 #include "hiappevent_base.h"
22 #include "hiappevent_userinfo.h"
23 #include "hilog/log.h"
24 
25 #undef LOG_DOMAIN
26 #define LOG_DOMAIN 0xD002D07
27 
28 #undef LOG_TAG
29 #define LOG_TAG "ProcessorProxy"
30 
31 namespace OHOS {
32 namespace HiviewDFX {
33 namespace HiAppEvent {
34 namespace {
CreateAppEventInfo(std::shared_ptr<AppEventPack> event)35 AppEventInfo CreateAppEventInfo(std::shared_ptr<AppEventPack> event)
36 {
37     AppEventInfo appEventInfo = {
38         .domain = event->GetDomain(),
39         .name = event->GetName(),
40         .eventType = event->GetType(),
41         .timestamp = event->GetTime(),
42         .params = event->GetParamStr(),
43     };
44     return appEventInfo;
45 }
46 }
47 
OnEvents(const std::vector<std::shared_ptr<AppEventPack>> & events)48 void AppEventProcessorProxy::OnEvents(const std::vector<std::shared_ptr<AppEventPack>>& events)
49 {
50     if (events.empty()) {
51         return;
52     }
53 
54     std::vector<UserId> userIds;
55     GetValidUserIds(userIds);
56     std::vector<UserProperty> userProperties;
57     GetValidUserProperties(userProperties);
58     int64_t observerSeq = GetSeq();
59 
60     // async
61     auto proxyPtr = shared_from_this();
62     ffrt::submit([proxyPtr, observerSeq, userIds, userProperties, events]() {
63         std::vector<AppEventInfo> eventInfos;
64         std::vector<int64_t> eventSeqs;
65         for (const auto& event : events) {
66             eventInfos.emplace_back(CreateAppEventInfo(event));
67             eventSeqs.emplace_back(event->GetSeq());
68         }
69         if (proxyPtr->processor_->OnReport(observerSeq, userIds, userProperties, eventInfos) == 0) {
70             if (!AppEventStore::GetInstance().DeleteData(observerSeq, eventSeqs)) {
71                 HILOG_ERROR(LOG_CORE, "failed to delete mapping data, seq=%{public}" PRId64 ", event num=%{public}zu",
72                     observerSeq, eventSeqs.size());
73             }
74         } else {
75             HILOG_DEBUG(LOG_CORE, "failed to report event, seq=%{public}" PRId64 ", event num=%{public}zu",
76                 observerSeq, eventSeqs.size());
77         }
78         }, {}, {}, ffrt::task_attr().name("appevent_report"));
79 }
80 
GetValidUserIds(std::vector<UserId> & userIds)81 void AppEventProcessorProxy::GetValidUserIds(std::vector<UserId>& userIds)
82 {
83     int64_t userIdVerForAll = HiAppEvent::UserInfo::GetInstance().GetUserIdVersion();
84     if (userIdVerForAll == userIdVersion_) {
85         userIds = userIds_;
86         return;
87     }
88     std::vector<UserId> allUserIds = HiAppEvent::UserInfo::GetInstance().GetUserIds();
89     std::for_each(allUserIds.begin(), allUserIds.end(), [&userIds, this](const auto& userId) {
90         if (reportConfig_.userIdNames.find(userId.name) != reportConfig_.userIdNames.end()
91             && processor_->ValidateUserId(userId) == 0) {
92             userIds.emplace_back(userId);
93         }
94     });
95     userIds_ = userIds;
96     userIdVersion_ = userIdVerForAll;
97 }
98 
GetValidUserProperties(std::vector<UserProperty> & userProperties)99 void AppEventProcessorProxy::GetValidUserProperties(std::vector<UserProperty>& userProperties)
100 {
101     int64_t userPropertyVerForAll = HiAppEvent::UserInfo::GetInstance().GetUserPropertyVersion();
102     if (userPropertyVerForAll == userPropertyVersion_) {
103         userProperties = userProperties_;
104         return;
105     }
106     std::vector<UserProperty> allUserProperties = HiAppEvent::UserInfo::GetInstance().GetUserProperties();
107     std::for_each(allUserProperties.begin(), allUserProperties.end(),
108         [&userProperties, this](const auto& userProperty) {
109             if (reportConfig_.userPropertyNames.find(userProperty.name) != reportConfig_.userPropertyNames.end()
110                 && processor_->ValidateUserProperty(userProperty) == 0) {
111                 userProperties.emplace_back(userProperty);
112             }
113         }
114     );
115     userProperties_ = userProperties;
116     userPropertyVersion_ = userPropertyVerForAll;
117 }
118 
VerifyEvent(std::shared_ptr<AppEventPack> event)119 bool AppEventProcessorProxy::VerifyEvent(std::shared_ptr<AppEventPack> event)
120 {
121     return AppEventObserver::VerifyEvent(event)
122         && (processor_->ValidateEvent(CreateAppEventInfo(event)) == 0);
123 }
124 } // namespace HiAppEvent
125 } // namespace HiviewDFX
126 } // namespace OHOS
127