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 RDB_SUBSCRIBER_MANAGER_H
17 #define RDB_SUBSCRIBER_MANAGER_H
18 
19 #include <memory>
20 
21 #include "callbacks_manager.h"
22 #include "data_proxy_observer.h"
23 #include "data_proxy_observer_stub.h"
24 #include "datashare_template.h"
25 #include "idatashare.h"
26 #include "iremote_stub.h"
27 #include "data_share_service_proxy.h"
28 
29 namespace OHOS {
30 namespace DataShare {
31 struct RdbObserverMapKey {
32     std::string uri_;
33     std::string clearUri_;
34     TemplateId templateId_;
RdbObserverMapKeyRdbObserverMapKey35     RdbObserverMapKey(const std::string &uri, const TemplateId &templateId) : uri_(uri), templateId_(templateId)
36     {
37         auto pos = uri_.find_first_of('?');
38         if (pos != std::string::npos) {
39             clearUri_ = uri_.substr(0, pos);
40         } else {
41             clearUri_ = uri_;
42         }
43     }
44     bool operator==(const RdbObserverMapKey &node) const
45     {
46         return clearUri_ == node.clearUri_ && templateId_ == node.templateId_;
47     }
48     bool operator!=(const RdbObserverMapKey &node) const
49     {
50         return !(node == *this);
51     }
52     bool operator<(const RdbObserverMapKey &node) const
53     {
54         if (clearUri_ != node.clearUri_) {
55             return clearUri_ < node.clearUri_;
56         }
57         return templateId_ < node.templateId_;
58     }
stringRdbObserverMapKey59     operator std::string() const
60     {
61         return uri_;
62     }
63 };
64 
65 class RdbObserver {
66 public:
67     RdbObserver(const RdbCallback &callback);
68     void OnChange(const RdbChangeNode &changeNode);
69     bool operator==(const RdbObserver &rhs) const;
70     bool operator!=(const RdbObserver &rhs) const;
71 
72 private:
73     RdbCallback callback_;
74 };
75 
76 class RdbSubscriberManager : public CallbacksManager<RdbObserverMapKey, RdbObserver> {
77 public:
78     using Key = RdbObserverMapKey;
79     using Observer = RdbObserver;
80     using BaseCallbacks = CallbacksManager<RdbObserverMapKey, RdbObserver>;
81     static RdbSubscriberManager &GetInstance();
82 
83     std::vector<OperationResult> AddObservers(void *subscriber, std::shared_ptr<DataShareServiceProxy> proxy,
84         const std::vector<std::string> &uris, const TemplateId &templateId, const RdbCallback &callback);
85     std::vector<OperationResult> DelObservers(void *subscriber, std::shared_ptr<DataShareServiceProxy> proxy,
86         const std::vector<std::string> &uris, const TemplateId &templateId);
87     std::vector<OperationResult> DelObservers(void *subscriber, std::shared_ptr<DataShareServiceProxy> proxy);
88     std::vector<OperationResult> EnableObservers(void *subscriber, std::shared_ptr<DataShareServiceProxy> proxy,
89         const std::vector<std::string> &uris, const TemplateId &templateId);
90     std::vector<OperationResult> DisableObservers(void *subscriber, std::shared_ptr<DataShareServiceProxy> proxy,
91         const std::vector<std::string> &uris, const TemplateId &templateId);
92     void RecoverObservers(std::shared_ptr<DataShareServiceProxy> proxy);
93     void Emit(const RdbChangeNode &changeNode);
94 
95 private:
96     void Emit(const std::vector<Key> &keys, const std::shared_ptr<Observer> &observer);
97     void EmitOnEnable(std::map<Key, std::vector<ObserverNodeOnEnabled>> &obsMap);
98     RdbSubscriberManager();
99     bool Init();
100     void Destroy();
101     sptr<RdbObserverStub> serviceCallback_;
102     std::map<Key, RdbChangeNode> lastChangeNodeMap_;
103 };
104 } // namespace DataShare
105 } // namespace OHOS
106 #endif // RDB_SUBSCRIBER_MANAGER_H
107