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 OHOS_ABILITY_RUNTIME_DATAOBS_MGR_INNER_EXT_H 17 #define OHOS_ABILITY_RUNTIME_DATAOBS_MGR_INNER_EXT_H 18 19 #include <atomic> 20 #include <list> 21 #include <string> 22 #include <memory> 23 #include <map> 24 #include <mutex> 25 #include "cpp/mutex.h" 26 27 #include "data_ability_observer_interface.h" 28 #include "dataobs_mgr_errors.h" 29 #include "dataobs_mgr_inner_common.h" 30 #include "iremote_object.h" 31 #include "refbase.h" 32 #include "uri.h" 33 34 namespace OHOS { 35 namespace AAFwk { 36 class DataObsMgrInnerExt : public std::enable_shared_from_this<DataObsMgrInnerExt> { 37 public: 38 39 DataObsMgrInnerExt(); 40 virtual ~DataObsMgrInnerExt(); 41 42 Status HandleRegisterObserver(Uri &uri, sptr<IDataAbilityObserver> dataObserver, ObserverInfo &info, 43 bool isDescendants = false); 44 Status HandleUnregisterObserver(Uri &uri, sptr<IDataAbilityObserver> dataObserver); 45 Status HandleUnregisterObserver(sptr<IDataAbilityObserver> dataObserver); 46 Status HandleNotifyChange(const ChangeInfo &changeInfo, int32_t userId); 47 void OnCallBackDied(const wptr<IRemoteObject> &remote); 48 49 private: 50 struct DeathRecipientRef { DeathRecipientRefDeathRecipientRef51 DeathRecipientRef(sptr<IRemoteObject::DeathRecipient> deathRec) : deathRecipient(deathRec), ref(1) {} 52 sptr<IRemoteObject::DeathRecipient> deathRecipient; 53 std::atomic<uint32_t> ref; 54 }; 55 56 struct Entry { EntryEntry57 Entry(sptr<IDataAbilityObserver> obs, int32_t userId, uint32_t tokenId, 58 std::shared_ptr<DeathRecipientRef> deathRef, bool isDes) 59 : observer(obs), userId(userId), tokenId(tokenId), deathRecipientRef(deathRef), isDescendants(isDes) 60 { 61 } 62 sptr<IDataAbilityObserver> observer; 63 int32_t userId; 64 uint32_t tokenId = 0; 65 std::shared_ptr<DeathRecipientRef> deathRecipientRef; 66 bool isDescendants; 67 std::string permission; 68 }; 69 70 struct ObsNotifyInfo { ObsNotifyInfoObsNotifyInfo71 ObsNotifyInfo() 72 { 73 tokenId = 0; 74 permission = ""; 75 uriList = std::list<Uri>(); 76 } 77 uint32_t tokenId; 78 std::string permission; 79 std::list<Uri> uriList; 80 }; 81 82 using ObsMap = std::map<sptr<IDataAbilityObserver>, ObsNotifyInfo>; 83 using EntryList = std::list<Entry>; 84 85 class Node { 86 public: 87 Node(const std::string &name); 88 void GetObs(const std::vector<std::string> &path, uint32_t index, Uri &uri, int32_t userId, ObsMap &obsMap); 89 bool AddObserver(const std::vector<std::string> &path, uint32_t index, const Entry &entry); 90 bool RemoveObserver(const std::vector<std::string> &path, uint32_t index, 91 sptr<IDataAbilityObserver> dataObserver); 92 inline bool RemoveObserver(sptr<IDataAbilityObserver> dataObserver); 93 bool RemoveObserver(sptr<IRemoteObject> dataObserver); 94 bool IsLimit(const Entry &entry); 95 96 private: 97 std::string name_; 98 EntryList entrys_; 99 std::map<std::string, std::shared_ptr<Node>> childrens_; 100 }; 101 102 std::shared_ptr<DeathRecipientRef> AddObsDeathRecipient(const sptr<IRemoteObject> &dataObserver); 103 void RemoveObsDeathRecipient(const sptr<IRemoteObject> &dataObserver, bool isForce = false); 104 105 static constexpr uint32_t OBS_NUM_MAX = 50; 106 static constexpr uint32_t OBS_ALL_NUM_MAX = OBS_NUM_MAX * OBS_NUM_MAX; 107 108 ffrt::mutex nodeMutex_; 109 std::shared_ptr<Node> root_; 110 std::map<sptr<IRemoteObject>, std::shared_ptr<DeathRecipientRef>> obsRecipientRefs; 111 }; 112 } // namespace AAFwk 113 } // namespace OHOS 114 #endif // OHOS_ABILITY_RUNTIME_DATAOBS_MGR_INNER_H 115