• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 "common_event_sticky_manager.h"
17 #include "errors.h"
18 #include "event_log_wrapper.h"
19 
20 namespace OHOS {
21 namespace EventFwk {
22 constexpr int32_t LENGTH = 80;
23 
FindStickyEvents(const SubscribeInfoPtr & subscribeInfo,std::vector<CommonEventRecordPtr> & commonEventRecords)24 int CommonEventStickyManager::FindStickyEvents(
25     const SubscribeInfoPtr &subscribeInfo, std::vector<CommonEventRecordPtr> &commonEventRecords)
26 {
27     EVENT_LOGD("enter");
28 
29     if (subscribeInfo == nullptr) {
30         EVENT_LOGE("subscribeInfo is null");
31         return ERR_INVALID_VALUE;
32     }
33 
34     auto events = subscribeInfo->GetMatchingSkills().GetEvents();
35     if (events.size() == 0) {
36         EVENT_LOGW("No subscribed events");
37         return ERR_INVALID_VALUE;
38     }
39 
40     FindStickyEventsLocked(events, commonEventRecords);
41 
42     return ERR_OK;
43 }
44 
GetStickyCommonEvent(const std::string & event,CommonEventData & eventData)45 bool CommonEventStickyManager::GetStickyCommonEvent(const std::string &event, CommonEventData &eventData)
46 {
47     EVENT_LOGD("enter");
48 
49     if (event.empty()) {
50         EVENT_LOGE("Invalid event name");
51         return false;
52     }
53 
54     return GetStickyCommonEventLocked(event, eventData);
55 }
56 
UpdateStickyEvent(const CommonEventRecord & eventRecord)57 int CommonEventStickyManager::UpdateStickyEvent(const CommonEventRecord &eventRecord)
58 {
59     EVENT_LOGD("enter");
60 
61     auto commonEventRecordPtr = std::make_shared<CommonEventRecord>(eventRecord);
62     if (commonEventRecordPtr == nullptr) {
63         EVENT_LOGE("Failed to create CommonEventRecord");
64         return ERR_INVALID_VALUE;
65     }
66 
67     std::string event = commonEventRecordPtr->commonEventData->GetWant().GetAction();
68 
69     return UpdateStickyEventLocked(event, commonEventRecordPtr);
70 }
71 
DumpState(const std::string & event,const int32_t & userId,std::vector<std::string> & state)72 void CommonEventStickyManager::DumpState(
73     const std::string &event, const int32_t &userId, std::vector<std::string> &state)
74 {
75     EVENT_LOGD("enter");
76 
77     std::vector<CommonEventRecordPtr> records;
78 
79     std::lock_guard<std::mutex> lock(mutex_);
80 
81     GetStickyCommonEventRecords(event, userId, records);
82 
83     if (records.size() == 0) {
84         state.emplace_back("Sticky Events:\tNo information");
85         return;
86     }
87 
88     size_t num = 0;
89     for (auto record : records) {
90         num++;
91 
92         std::string no = std::to_string(num);
93         if (num == 1) {
94             no = "Sticky Events:\tTotal " + std::to_string(records.size()) + " information\nNO " + no + "\n";
95         } else {
96             no = "NO " + no + "\n";
97         }
98 
99         char systime[LENGTH];
100         strftime(systime, sizeof(char) * LENGTH, "%Y%m%d %I:%M %p", &record->recordTime);
101 
102         std::string recordTime = "\tTime: " + std::string(systime) + "\n";
103         std::string pid = "\tPID: " + std::to_string(record->eventRecordInfo.pid) + "\n";
104         std::string uid = "\tUID: " + std::to_string(record->eventRecordInfo.uid) + "\n";
105         std::string bundleName = "\tBundleName: " + record->eventRecordInfo.bundleName + "\n";
106 
107         std::string permission = "\tRequiredPermission: ";
108         std::string separator;
109         size_t permissionNum = 0;
110         for (auto permissionVec : record->publishInfo->GetSubscriberPermissions()) {
111             if (permissionNum == 0) {
112                 separator = "";
113             } else {
114                 separator = ", ";
115             }
116             permission = permission + separator + permissionVec;
117             permissionNum++;
118         }
119         permission = permission + "\n";
120 
121         std::string isSticky;
122         if (record->publishInfo->IsSticky()) {
123             isSticky = "\tIsSticky: true\n";
124         } else {
125             isSticky = "\tIsSticky: false\n";
126         }
127 
128         std::string isOrdered;
129         if (record->publishInfo->IsOrdered()) {
130             isOrdered = "\tIsOrdered: true\n";
131         } else {
132             isOrdered = "\tIsOrdered: false\n";
133         }
134         std::string isSystemEvent = record->isSystemEvent ? "true" : "false";
135         isSystemEvent = "\tIsSystemEvent: " + isSystemEvent + "\n";
136 
137         std::string action = "\t\tAction: " + record->commonEventData->GetWant().GetAction() + "\n";
138 
139         std::string entities = "\t\tEntity: ";
140         size_t entityNum = 0;
141         for (auto entitiesVec : record->commonEventData->GetWant().GetEntities()) {
142             if (entityNum == 0) {
143                 separator = "";
144             } else {
145                 separator = ", ";
146             }
147             entities = entities + separator + entitiesVec;
148             entityNum++;
149         }
150         entities = entities + "\n";
151 
152         std::string scheme = "\t\tScheme: " + record->commonEventData->GetWant().GetScheme() + "\n";
153         std::string uri = "\t\tUri: " + record->commonEventData->GetWant().GetUriString() + "\n";
154         std::string flags = "\t\tFlags: " + std::to_string(record->commonEventData->GetWant().GetFlags()) + "\n";
155         std::string type = "\t\tType: " + record->commonEventData->GetWant().GetType() + "\n";
156         std::string bundle = "\t\tBundleName: " + record->commonEventData->GetWant().GetBundle() + "\n";
157         std::string ability =
158             "\t\tAbilityName: " + record->commonEventData->GetWant().GetElement().GetAbilityName() + "\n";
159         std::string deviced = "\t\tDevicedID: " + record->commonEventData->GetWant().GetElement().GetDeviceID() + "\n";
160 
161         std::string want = "\tWant:\n" + action + entities + scheme + uri + flags + type + bundle + ability + deviced;
162         std::string code = "\tCode: " + std::to_string(record->commonEventData->GetCode()) + "\n";
163         std::string data = "\tData: " + record->commonEventData->GetData() + "\n";
164 
165         std::string dumpInfo = no + recordTime + pid + uid + bundleName + permission + isSticky + isOrdered +
166                                isSystemEvent + want + code + data;
167 
168         state.emplace_back(dumpInfo);
169     }
170 }
171 
FindStickyEventsLocked(const std::vector<std::string> & events,std::vector<CommonEventRecordPtr> & commonEventRecords)172 void CommonEventStickyManager::FindStickyEventsLocked(
173     const std::vector<std::string> &events, std::vector<CommonEventRecordPtr> &commonEventRecords)
174 {
175     std::lock_guard<std::mutex> lock(mutex_);
176 
177     for (auto event : events) {
178         auto it = commonEventRecords_.find(event);
179         if (it != commonEventRecords_.end()) {
180             commonEventRecords.emplace_back(it->second);
181         }
182     }
183 }
184 
GetStickyCommonEventLocked(const std::string & event,CommonEventData & eventData)185 bool CommonEventStickyManager::GetStickyCommonEventLocked(const std::string &event, CommonEventData &eventData)
186 {
187     std::lock_guard<std::mutex> lock(mutex_);
188 
189     auto it = commonEventRecords_.find(event);
190     if (it != commonEventRecords_.end()) {
191         eventData = *(it->second->commonEventData);
192         return true;
193     }
194 
195     return false;
196 }
197 
UpdateStickyEventLocked(const std::string & event,const CommonEventRecordPtr & record)198 int CommonEventStickyManager::UpdateStickyEventLocked(const std::string &event, const CommonEventRecordPtr &record)
199 {
200     if (event.empty()) {
201         EVENT_LOGE("Invalid event name");
202         return ERR_INVALID_VALUE;
203     }
204 
205     if (record == nullptr) {
206         EVENT_LOGE("Invalid common event record");
207         return ERR_INVALID_VALUE;
208     }
209 
210     std::lock_guard<std::mutex> lock(mutex_);
211 
212     commonEventRecords_[event] = record;
213 
214     return ERR_OK;
215 }
216 
GetStickyCommonEventRecords(const std::string & event,const int32_t & userId,std::vector<CommonEventRecordPtr> & records)217 void CommonEventStickyManager::GetStickyCommonEventRecords(
218     const std::string &event, const int32_t &userId, std::vector<CommonEventRecordPtr> &records)
219 {
220     if (event.empty()) {
221         for (auto record : commonEventRecords_) {
222             if ((userId == ALL_USER) || (record.second->userId == userId)) {
223                 records.emplace_back(record.second);
224             }
225         }
226     } else {
227         auto recordItem = commonEventRecords_.find(event);
228         if (recordItem == commonEventRecords_.end()) {
229             return;
230         }
231         if ((userId == ALL_USER) || (userId == recordItem->second->userId)) {
232             records.emplace_back(recordItem->second);
233         }
234     }
235 }
236 
RemoveStickyCommonEvent(const std::string & event,uint32_t callerUid)237 int32_t CommonEventStickyManager::RemoveStickyCommonEvent(const std::string &event, uint32_t callerUid)
238 {
239     std::lock_guard<std::mutex> lock(mutex_);
240 
241     auto it = commonEventRecords_.find(event);
242     if (it != commonEventRecords_.end()) {
243         if (it->second->eventRecordInfo.uid == callerUid) {
244             commonEventRecords_.erase(it);
245         }
246     }
247     return ERR_OK;
248 }
249 }  // namespace EventFwk
250 }  // namespace OHOS