1 /* 2 * Copyright (c) 2025 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 OHOS_ABILITY_RUNTIME_HIDDEN_START_OBSERVER_MANAGER_H 17 #define OHOS_ABILITY_RUNTIME_HIDDEN_START_OBSERVER_MANAGER_H 18 19 #include <set> 20 #include <map> 21 #include "ffrt.h" 22 #include "ihidden_start_observer.h" 23 #include "singleton.h" 24 25 namespace OHOS { 26 namespace AAFwk { 27 using HiddenStartObserverSet = std::set<sptr<IHiddenStartObserver>>; 28 29 /** 30 * @class HiddenStartObserverRecipient 31 * HiddenStartObserverRecipient notices IRemoteBroker died. 32 */ 33 class HiddenStartObserverRecipient : public IRemoteObject::DeathRecipient { 34 public: 35 using RemoteDiedHandler = std::function<void(const wptr<IRemoteObject> &)>; 36 explicit HiddenStartObserverRecipient(RemoteDiedHandler handler); 37 virtual ~HiddenStartObserverRecipient(); 38 virtual void OnRemoteDied(const wptr<IRemoteObject> &remote); 39 40 private: 41 RemoteDiedHandler handler_; 42 }; 43 44 class HiddenStartObserverManager { 45 public: 46 static HiddenStartObserverManager &GetInstance(); 47 int32_t RegisterObserver(const sptr<IHiddenStartObserver> &observer); 48 int32_t UnregisterObserver(const sptr<IHiddenStartObserver> &observer); 49 bool IsHiddenStart(int32_t uid); 50 void OnObserverDied(const wptr<IRemoteObject> &remote); 51 void AddObserverDeathRecipient(const sptr<IRemoteBroker> &observer); 52 void RemoveObserverDeathRecipient(const sptr<IRemoteBroker> &observer); 53 HiddenStartObserverSet GetObserversCopy(); 54 55 private: 56 bool ObserverExist(const sptr<IRemoteBroker> &observer); 57 HiddenStartObserverManager(); 58 ~HiddenStartObserverManager(); 59 60 private: 61 ffrt::mutex observerLock_; 62 std::map<sptr<IRemoteObject>, sptr<IRemoteObject::DeathRecipient>> recipientMap_; 63 HiddenStartObserverSet observers_; 64 ffrt::mutex recipientMapMutex_; 65 DISALLOW_COPY_AND_MOVE(HiddenStartObserverManager); 66 }; 67 } // namespace AAFwk 68 } // namespace OHOS 69 #endif // OHOS_ABILITY_RUNTIME_HIDDEN_START_OBSERVER_MANAGER_H