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