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 NAPI_RDB_SUBSCRIBER_MANAGER_H
17 #define NAPI_RDB_SUBSCRIBER_MANAGER_H
18 
19 #include <memory>
20 #include <uv.h>
21 
22 #include "napi_callbacks_manager.h"
23 #include "datashare_helper.h"
24 #include "napi/native_api.h"
25 #include "napi/native_common.h"
26 #include "napi/native_node_api.h"
27 #include "napi_observer.h"
28 
29 namespace OHOS {
30 namespace DataShare {
31 struct NapiRdbObserverMapKey {
32     std::string uri_;
33     TemplateId templateId_;
NapiRdbObserverMapKeyNapiRdbObserverMapKey34     NapiRdbObserverMapKey(const std::string &uri, const TemplateId &templateId) : uri_(uri), templateId_(templateId){};
35     bool operator==(const NapiRdbObserverMapKey &node) const
36     {
37         return uri_ == node.uri_ && templateId_ == node.templateId_;
38     }
39     bool operator!=(const NapiRdbObserverMapKey &node) const
40     {
41         return !(node == *this);
42     }
43     bool operator<(const NapiRdbObserverMapKey &node) const
44     {
45         if (uri_ != node.uri_) {
46             return uri_ < node.uri_;
47         }
48         return templateId_ < node.templateId_;
49     }
stringNapiRdbObserverMapKey50     operator std::string() const
51     {
52         return uri_;
53     }
54 };
55 
56 class NapiRdbSubscriberManager : public NapiCallbacksManager<NapiRdbObserverMapKey, NapiRdbObserver> {
57 public:
58     using Key = NapiRdbObserverMapKey;
59     using Observer = NapiRdbObserver;
60     using BaseCallbacks = NapiCallbacksManager<NapiRdbObserverMapKey, NapiRdbObserver>;
NapiRdbSubscriberManager(std::weak_ptr<DataShareHelper> dataShareHelper)61     explicit NapiRdbSubscriberManager(std::weak_ptr<DataShareHelper> dataShareHelper)
62         : dataShareHelper_(dataShareHelper){};
63     std::vector<OperationResult> AddObservers(napi_env env, napi_value callback, const std::vector<std::string> &uris,
64         const TemplateId &templateId);
65     std::vector<OperationResult> DelObservers(napi_env env, napi_value callback,
66         const std::vector<std::string> &uris, const TemplateId &templateId);
67     void Emit(const RdbChangeNode &changeNode);
68 
69 private:
70     void Emit(const std::vector<Key> &keys, const std::shared_ptr<Observer> &observer);
71     std::weak_ptr<DataShareHelper> dataShareHelper_;
72     std::map<Key, RdbChangeNode> lastChangeNodeMap_;
73 };
74 
75 struct NapiPublishedObserverMapKey {
76     std::string uri_;
77     int64_t subscriberId_;
NapiPublishedObserverMapKeyNapiPublishedObserverMapKey78     NapiPublishedObserverMapKey(const std::string &uri, int64_t subscriberId) : uri_(uri),
79         subscriberId_(subscriberId){};
80     bool operator==(const NapiPublishedObserverMapKey &node) const
81     {
82         return uri_ == node.uri_ && subscriberId_ == node.subscriberId_;
83     }
84     bool operator!=(const NapiPublishedObserverMapKey &node) const
85     {
86         return !(node == *this);
87     }
88     bool operator<(const NapiPublishedObserverMapKey &node) const
89     {
90         if (uri_ != node.uri_) {
91             return uri_ < node.uri_;
92         }
93         return subscriberId_ < node.subscriberId_;
94     }
stringNapiPublishedObserverMapKey95     operator std::string() const
96     {
97         return uri_;
98     }
99 };
100 
101 class NapiPublishedSubscriberManager : public NapiCallbacksManager<NapiPublishedObserverMapKey, NapiPublishedObserver> {
102 public:
103     using Key = NapiPublishedObserverMapKey;
104     using Observer = NapiPublishedObserver;
105     using BaseCallbacks = NapiCallbacksManager<NapiPublishedObserverMapKey, NapiPublishedObserver>;
NapiPublishedSubscriberManager(std::weak_ptr<DataShareHelper> dataShareHelper)106     explicit NapiPublishedSubscriberManager(std::weak_ptr<DataShareHelper> dataShareHelper)
107         : dataShareHelper_(dataShareHelper){};
108     std::vector<OperationResult> AddObservers(napi_env env, napi_value callback, const std::vector<std::string> &uris,
109         int64_t subscriberId);
110     std::vector<OperationResult> DelObservers(napi_env env, napi_value callback,
111         const std::vector<std::string> &uris, int64_t subscriberId);
112     void Emit(const PublishedDataChangeNode &changeNode);
113 
114 private:
115     void Emit(const std::vector<Key> &keys, const std::shared_ptr<Observer> &observer);
116     std::weak_ptr<DataShareHelper> dataShareHelper_;
117     std::map<Key, PublishedDataChangeNode> lastChangeNodeMap_;
118 };
119 } // namespace DataShare
120 } // namespace OHOS
121 #endif //NAPI_RDB_SUBSCRIBER_MANAGER_H
122