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 "iremote_object.h"
30 #include "refbase.h"
31 
32 namespace OHOS {
33 namespace AAFwk {
34 class DataObsMgrInnerExt : public std::enable_shared_from_this<DataObsMgrInnerExt> {
35 public:
36 
37     DataObsMgrInnerExt();
38     virtual ~DataObsMgrInnerExt();
39 
40     Status HandleRegisterObserver(Uri &uri, sptr<IDataAbilityObserver> dataObserver, bool isDescendants = false);
41     Status HandleUnregisterObserver(Uri &uri, sptr<IDataAbilityObserver> dataObserver);
42     Status HandleUnregisterObserver(sptr<IDataAbilityObserver> dataObserver);
43     Status HandleNotifyChange(const ChangeInfo &changeInfo);
44     void OnCallBackDied(const wptr<IRemoteObject> &remote);
45 
46 private:
47     struct DeathRecipientRef {
DeathRecipientRefDeathRecipientRef48         DeathRecipientRef(sptr<IRemoteObject::DeathRecipient> deathRec) : deathRecipient(deathRec), ref(1) {}
49         sptr<IRemoteObject::DeathRecipient> deathRecipient;
50         std::atomic<uint32_t> ref;
51     };
52 
53     struct Entry {
EntryEntry54         Entry(sptr<IDataAbilityObserver> obs, std::shared_ptr<DeathRecipientRef> deathRef, bool isDes)
55             : observer(obs), deathRecipientRef(deathRef), isDescendants(isDes)
56         {
57         }
58         sptr<IDataAbilityObserver> observer;
59         std::shared_ptr<DeathRecipientRef> deathRecipientRef;
60         bool isDescendants;
61     };
62 
63     using ObsMap = std::map<sptr<IDataAbilityObserver>, std::list<Uri>>;
64     using EntryList = std::list<Entry>;
65 
66     class Node {
67     public:
68         Node(const std::string &name);
69         void GetObs(const std::vector<std::string> &path, uint32_t index, Uri &uri, ObsMap &obsMap);
70         bool AddObserver(const std::vector<std::string> &path, uint32_t index, const Entry &entry);
71         bool RemoveObserver(const std::vector<std::string> &path, uint32_t index,
72             sptr<IDataAbilityObserver> dataObserver);
73         inline bool RemoveObserver(sptr<IDataAbilityObserver> dataObserver);
74         bool RemoveObserver(sptr<IRemoteObject> dataObserver);
75 
76     private:
77         std::string name_;
78         EntryList entrys_;
79         std::map<std::string, std::shared_ptr<Node>> childrens_;
80     };
81 
82     std::shared_ptr<DeathRecipientRef> AddObsDeathRecipient(const sptr<IRemoteObject> &dataObserver);
83     void RemoveObsDeathRecipient(const sptr<IRemoteObject> &dataObserver, bool isForce = false);
84 
85     static constexpr uint32_t OBS_NUM_MAX = 50;
86 
87     ffrt::mutex nodeMutex_;
88     std::shared_ptr<Node> root_;
89     std::map<sptr<IRemoteObject>, std::shared_ptr<DeathRecipientRef>> obsRecipientRefs;
90 };
91 }  // namespace AAFwk
92 }  // namespace OHOS
93 #endif  // OHOS_ABILITY_RUNTIME_DATAOBS_MGR_INNER_H
94