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, uint32_t callerPid); 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, const std::string &bundleName, 65 std::shared_ptr<Context> context); 66 void Emit(const std::string &uri, std::shared_ptr<Context> context); 67 void Emit(const std::string &uri, int32_t userId, DistributedData::StoreMetaData &metaData); 68 void EmitByKey(const Key &key, int32_t userId, const std::string &rdbPath, int version); 69 std::vector<Key> GetKeysByUri(const std::string &uri); 70 void Clear(); 71 72 private: 73 struct ObserverNode { 74 ObserverNode(const sptr<IDataProxyRdbObserver> &observer, uint32_t firstCallerTokenId, 75 uint32_t callerTokenId = 0, uint32_t callerPid = 0); 76 sptr<IDataProxyRdbObserver> observer; 77 uint32_t firstCallerTokenId; 78 uint32_t callerTokenId; 79 uint32_t callerPid; 80 bool enabled = true; 81 bool isNotifyOnEnabled = false; 82 }; 83 84 RdbSubscriberManager() = default; 85 ConcurrentMap<Key, std::vector<ObserverNode>> rdbCache_; 86 int Notify(const Key &key, int32_t userId, const std::vector<ObserverNode> &val, const std::string &rdbDir, 87 int rdbVersion); 88 int GetEnableObserverCount(const Key &key); 89 void SetObserverNotifyOnEnabled(std::vector<ObserverNode> &nodes); 90 }; 91 } // namespace OHOS::DataShare 92 #endif // DATASHARESERVICE_RDB_SUBSCRIBER_MANAGER_H 93