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 16 #ifndef SECURITY_GUARD_SECURITY_COLLECTOR_SUBSCRIBLER_MANAGER_H 17 #define SECURITY_GUARD_SECURITY_COLLECTOR_SUBSCRIBLER_MANAGER_H 18 19 #include <map> 20 #include <set> 21 #include <mutex> 22 #include <memory> 23 #include "singleton.h" 24 #include "timer.h" 25 26 #include "i_collector_fwk.h" 27 #include "security_collector_subscriber.h" 28 29 30 namespace OHOS::Security::SecurityCollector { 31 32 class SecurityCollectorSubscriberManager : public Singleton<SecurityCollectorSubscriberManager> { 33 public: 34 using UnsubscribeHandler = std::function<void (const sptr<IRemoteObject> &remote)>; 35 SecurityCollectorSubscriberManager(); 36 bool SubscribeCollector(const std::shared_ptr<SecurityCollectorSubscriber> &subscriber); 37 bool UnsubscribeCollector(const sptr<IRemoteObject> &remote); SetUnsubscribeHandler(UnsubscribeHandler handler)38 void SetUnsubscribeHandler(UnsubscribeHandler handler) { unsubscribeHandler_ = handler; } 39 40 private: 41 auto FindSecurityCollectorSubscribers(const sptr<IRemoteObject> &remote); 42 std::set<int64_t> FindEventIds(const sptr<IRemoteObject> &remote); 43 int32_t GetAppSubscribeCount(const std::string &appName); 44 int32_t GetAppSubscribeCount(const std::string &appName, int64_t eventId); CleanSubscriber(const sptr<IRemoteObject> & remote)45 void CleanSubscriber(const sptr<IRemoteObject> &remote) { unsubscribeHandler_(remote); } 46 void NotifySubscriber(const Event &event); 47 48 class CollectorListenner : public ICollectorFwk { 49 public: CollectorListenner(const std::shared_ptr<SecurityCollectorSubscriber> & subscriber)50 CollectorListenner(const std::shared_ptr<SecurityCollectorSubscriber> &subscriber) : subscriber_(subscriber) {} 51 std::string GetExtraInfo() override; 52 void OnNotify(const Event &event) override; 53 private: 54 std::shared_ptr<SecurityCollectorSubscriber> subscriber_; 55 }; 56 57 class CleanupTimer { 58 public: 59 CleanupTimer() = default; ~CleanupTimer()60 ~CleanupTimer() { Shutdown(); } StopCollector(const sptr<IRemoteObject> & remote)61 void StopCollector(const sptr<IRemoteObject> &remote) 62 { 63 // avoid dead lock 64 std::thread work([this, remote] () { 65 SecurityCollectorSubscriberManager::GetInstance().CleanSubscriber(remote); 66 }); 67 work.detach(); 68 } Start(const sptr<IRemoteObject> & remote,int64_t duration)69 void Start(const sptr<IRemoteObject> &remote, int64_t duration) 70 { 71 timer_.Setup(); 72 timerId_ = timer_.Register([this, remote] { this->StopCollector(remote); }, duration); 73 } Shutdown()74 void Shutdown() 75 { 76 if (timerId_ != 0) { 77 timer_.Unregister(timerId_); 78 } 79 timer_.Shutdown(); 80 timerId_ = 0; 81 } 82 private: 83 Utils::Timer timer_{"cleanup_collector"}; 84 uint32_t timerId_{}; 85 }; 86 87 UnsubscribeHandler unsubscribeHandler_{}; 88 std::mutex collectorMutex_{}; 89 std::map<int64_t, std::set<std::shared_ptr<SecurityCollectorSubscriber>>> eventToSubscribers_{}; 90 std::map<sptr<IRemoteObject>, std::shared_ptr<CleanupTimer>> timers_{}; 91 std::map<int64_t, std::shared_ptr<ICollectorFwk>> eventToListenner_; 92 }; 93 } 94 #endif // SECURITY_GUARD_SECURITY_COLLECTOR_SUBSCRIBLER_MANAGER_H