• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "app_event_processor_proxy.h"
16 
17 #include <algorithm>
18 
19 #include "hiappevent_base.h"
20 #include "hiappevent_userinfo.h"
21 
22 namespace OHOS {
23 namespace HiviewDFX {
24 namespace HiAppEvent {
25 namespace {
CreateAppEventInfo(std::shared_ptr<AppEventPack> event)26 AppEventInfo CreateAppEventInfo(std::shared_ptr<AppEventPack> event)
27 {
28     AppEventInfo appEventInfo = {
29         .domain = event->GetDomain(),
30         .name = event->GetName(),
31         .eventType = event->GetType(),
32         .timestamp = event->GetTime(),
33         .params = event->GetParamStr(),
34     };
35     return appEventInfo;
36 }
37 }
38 
OnEvents(const std::vector<std::shared_ptr<AppEventPack>> & events)39 void AppEventProcessorProxy::OnEvents(const std::vector<std::shared_ptr<AppEventPack>>& events)
40 {
41     if (events.empty()) {
42         return;
43     }
44 
45     std::vector<UserId> userIds;
46     GetValidUserIds(userIds);
47     std::vector<UserProperty> userProperties;
48     GetValidUserProperties(userProperties);
49     std::vector<AppEventInfo> eventInfos(events.size());
50     std::transform(events.begin(), events.end(), eventInfos.begin(), [](auto event) {
51         return CreateAppEventInfo(event);
52     });
53     (void)processor_->OnReport(GetSeq(), userIds, userProperties, eventInfos);
54 }
55 
GetValidUserIds(std::vector<UserId> & userIds)56 void AppEventProcessorProxy::GetValidUserIds(std::vector<UserId>& userIds)
57 {
58     int64_t userIdVerForAll = HiAppEvent::UserInfo::GetInstance().GetUserIdVersion();
59     if (userIdVerForAll == userIdVersion_) {
60         userIds = userIds_;
61         return;
62     }
63     std::vector<UserId> allUserIds = HiAppEvent::UserInfo::GetInstance().GetUserIds();
64     std::for_each(allUserIds.begin(), allUserIds.end(), [&userIds, this](const auto& userId) {
65         if (reportConfig_.userIdNames.find(userId.name) != reportConfig_.userIdNames.end()
66             && processor_->ValidateUserId(userId) == 0) {
67             userIds.emplace_back(userId);
68         }
69     });
70     userIds_ = userIds;
71     userIdVersion_ = userIdVerForAll;
72 }
73 
GetValidUserProperties(std::vector<UserProperty> & userProperties)74 void AppEventProcessorProxy::GetValidUserProperties(std::vector<UserProperty>& userProperties)
75 {
76     int64_t userPropertyVerForAll = HiAppEvent::UserInfo::GetInstance().GetUserPropertyVersion();
77     if (userPropertyVerForAll == userPropertyVersion_) {
78         userProperties = userProperties_;
79         return;
80     }
81     std::vector<UserProperty> allUserProperties = HiAppEvent::UserInfo::GetInstance().GetUserProperties();
82     std::for_each(allUserProperties.begin(), allUserProperties.end(),
83         [&userProperties, this](const auto& userProperty) {
84             if (reportConfig_.userPropertyNames.find(userProperty.name) != reportConfig_.userPropertyNames.end()
85                 && processor_->ValidateUserProperty(userProperty) == 0) {
86                 userProperties.emplace_back(userProperty);
87             }
88         }
89     );
90     userProperties_ = userProperties;
91     userPropertyVersion_ = userPropertyVerForAll;
92 }
93 
VerifyEvent(std::shared_ptr<AppEventPack> event)94 bool AppEventProcessorProxy::VerifyEvent(std::shared_ptr<AppEventPack> event)
95 {
96     return AppEventObserver::VerifyEvent(event)
97         && (processor_->ValidateEvent(CreateAppEventInfo(event)) == 0);
98 }
99 } // namespace HiAppEvent
100 } // namespace HiviewDFX
101 } // namespace OHOS
102