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 DATASHARESERVICE_RDB_SUBSCRIBER_MANAGER_H 17 #define DATASHARESERVICE_RDB_SUBSCRIBER_MANAGER_H 18 19 #include <list> 20 #include <string> 21 22 #include "concurrent_map.h" 23 #include "context.h" 24 #include "data_provider_config.h" 25 #include "data_proxy_observer.h" 26 #include "data_share_db_config.h" 27 #include "datashare_template.h" 28 #include "executor_pool.h" 29 30 namespace OHOS::DataShare { 31 struct Key { 32 Key(const std::string &uri, int64_t subscriberId, const std::string &bundleName); 33 bool operator==(const Key &rhs) const; 34 bool operator!=(const Key &rhs) const; 35 bool operator<(const Key &rhs) const; 36 bool operator>(const Key &rhs) const; 37 bool operator<=(const Key &rhs) const; 38 bool operator>=(const Key &rhs) const; 39 const std::string uri; 40 const int64_t subscriberId; 41 const std::string bundleName; 42 }; 43 class TemplateManager { 44 public: 45 static TemplateManager &GetInstance(); 46 int32_t Add(const Key &key, int32_t userId, const Template &tpl); 47 int32_t Delete(const Key &key, int32_t userId); 48 bool Get(const Key &key, int32_t userId, Template &tpl); 49 50 private: 51 TemplateManager(); 52 friend class RdbSubscriberManager; 53 }; 54 55 class RdbSubscriberManager { 56 public: 57 static RdbSubscriberManager &GetInstance(); 58 int Add(const Key &key, const sptr<IDataProxyRdbObserver> observer, std::shared_ptr<Context> context, 59 std::shared_ptr<ExecutorPool> executorPool); 60 int Delete(const Key &key, uint32_t firstCallerTokenId); 61 void Delete(uint32_t callerTokenId); 62 int Disable(const Key &key, uint32_t firstCallerTokenId); 63 int Enable(const Key &key, std::shared_ptr<Context> context); 64 void Emit(const std::string &uri, int64_t subscriberId, std::shared_ptr<Context> context); 65 void Emit(const std::string &uri, std::shared_ptr<Context> context); 66 void Emit(const std::string &uri, int32_t userId, DistributedData::StoreMetaData &metaData); 67 void EmitByKey(const Key &key, int32_t userId, const std::string &rdbPath, int version); 68 std::vector<Key> GetKeysByUri(const std::string &uri); 69 void Clear(); 70 71 private: 72 struct ObserverNode { 73 ObserverNode(const sptr<IDataProxyRdbObserver> &observer, uint32_t firstCallerTokenId, 74 uint32_t callerTokenId = 0); 75 sptr<IDataProxyRdbObserver> observer; 76 uint32_t firstCallerTokenId; 77 uint32_t callerTokenId; 78 bool enabled = true; 79 bool isNotifyOnEnabled = false; 80 }; 81 82 RdbSubscriberManager() = default; 83 ConcurrentMap<Key, std::vector<ObserverNode>> rdbCache_; 84 int Notify(const Key &key, int32_t userId, const std::vector<ObserverNode> &val, const std::string &rdbDir, 85 int rdbVersion); 86 int GetEnableObserverCount(const Key &key); 87 void SetObserverNotifyOnEnabled(std::vector<ObserverNode> &nodes); 88 }; 89 } // namespace OHOS::DataShare 90 #endif // DATASHARESERVICE_RDB_SUBSCRIBER_MANAGER_H 91