/* * Copyright (c) 2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef OHOS_ABILITY_RUNTIME_DATAOBS_MGR_INNER_EXT_H #define OHOS_ABILITY_RUNTIME_DATAOBS_MGR_INNER_EXT_H #include #include #include #include #include #include #include "cpp/mutex.h" #include "data_ability_observer_interface.h" #include "dataobs_mgr_errors.h" #include "iremote_object.h" #include "refbase.h" namespace OHOS { namespace AAFwk { class DataObsMgrInnerExt : public std::enable_shared_from_this { public: DataObsMgrInnerExt(); virtual ~DataObsMgrInnerExt(); Status HandleRegisterObserver(Uri &uri, sptr dataObserver, bool isDescendants = false); Status HandleUnregisterObserver(Uri &uri, sptr dataObserver); Status HandleUnregisterObserver(sptr dataObserver); Status HandleNotifyChange(const ChangeInfo &changeInfo); void OnCallBackDied(const wptr &remote); private: struct DeathRecipientRef { DeathRecipientRef(sptr deathRec) : deathRecipient(deathRec), ref(1) {} sptr deathRecipient; std::atomic ref; }; struct Entry { Entry(sptr obs, std::shared_ptr deathRef, bool isDes) : observer(obs), deathRecipientRef(deathRef), isDescendants(isDes) { } sptr observer; std::shared_ptr deathRecipientRef; bool isDescendants; }; using ObsMap = std::map, std::list>; using EntryList = std::list; class Node { public: Node(const std::string &name); void GetObs(const std::vector &path, uint32_t index, Uri &uri, ObsMap &obsMap); bool AddObserver(const std::vector &path, uint32_t index, const Entry &entry); bool RemoveObserver(const std::vector &path, uint32_t index, sptr dataObserver); inline bool RemoveObserver(sptr dataObserver); bool RemoveObserver(sptr dataObserver); private: std::string name_; EntryList entrys_; std::map> childrens_; }; std::shared_ptr AddObsDeathRecipient(const sptr &dataObserver); void RemoveObsDeathRecipient(const sptr &dataObserver, bool isForce = false); static constexpr uint32_t OBS_NUM_MAX = 50; ffrt::mutex nodeMutex_; std::shared_ptr root_; std::map, std::shared_ptr> obsRecipientRefs; }; } // namespace AAFwk } // namespace OHOS #endif // OHOS_ABILITY_RUNTIME_DATAOBS_MGR_INNER_H