1 /* 2 * Copyright (c) 2022 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 DISTRIBUTEDDATASERVICE_RDB_SERVICE_H 17 #define DISTRIBUTEDDATASERVICE_RDB_SERVICE_H 18 19 #include "rdb_service_stub.h" 20 21 #include <map> 22 #include <mutex> 23 #include <string> 24 #include "cloud/cloud_event.h" 25 #include "concurrent_map.h" 26 #include "feature/static_acts.h" 27 #include "metadata/secret_key_meta_data.h" 28 #include "metadata/store_meta_data.h" 29 #include "rdb_notifier_proxy.h" 30 #include "rdb_watcher.h" 31 #include "rdb_query.h" 32 #include "store/auto_cache.h" 33 #include "store/general_store.h" 34 #include "store_observer.h" 35 #include "visibility.h" 36 #include "store/general_value.h" 37 #include "snapshot/bind_event.h" 38 39 namespace OHOS::DistributedRdb { 40 class API_EXPORT RdbServiceImpl : public RdbServiceStub { 41 public: 42 using StoreMetaData = OHOS::DistributedData::StoreMetaData; 43 using SecretKeyMetaData = DistributedData::SecretKeyMetaData; 44 using DetailAsync = DistributedData::GeneralStore::DetailAsync; 45 using Handler = std::function<void(int, std::map<std::string, std::vector<std::string>> &)>; 46 using StoreInfo = DistributedData::CloudEvent::StoreInfo; 47 RdbServiceImpl(); 48 virtual ~RdbServiceImpl(); 49 50 /* IPC interface */ 51 std::string ObtainDistributedTableName(const std::string& device, const std::string& table) override; 52 53 int32_t InitNotifier(const RdbSyncerParam ¶m, sptr<IRemoteObject> notifier) override; 54 55 int32_t SetDistributedTables(const RdbSyncerParam ¶m, const std::vector<std::string> &tables, 56 const std::vector<Reference> &references, int32_t type = DISTRIBUTED_DEVICE) override; 57 58 std::pair<int32_t, std::shared_ptr<ResultSet>> RemoteQuery(const RdbSyncerParam& param, const std::string& device, 59 const std::string& sql, const std::vector<std::string>& selectionArgs) override; 60 61 int32_t Sync(const RdbSyncerParam ¶m, const Option &option, const PredicatesMemo &predicates, 62 const AsyncDetail &async) override; 63 64 int32_t Subscribe(const RdbSyncerParam ¶m, const SubscribeOption &option, RdbStoreObserver *observer) override; 65 66 int32_t UnSubscribe(const RdbSyncerParam ¶m, const SubscribeOption &option, 67 RdbStoreObserver *observer) override; 68 69 int32_t RegisterAutoSyncCallback(const RdbSyncerParam& param, 70 std::shared_ptr<DetailProgressObserver> observer) override; 71 72 int32_t UnregisterAutoSyncCallback(const RdbSyncerParam& param, 73 std::shared_ptr<DetailProgressObserver> observer) override; 74 75 int32_t ResolveAutoLaunch(const std::string &identifier, DistributedDB::AutoLaunchParam ¶m) override; 76 77 int32_t OnAppExit(pid_t uid, pid_t pid, uint32_t tokenId, const std::string &bundleName) override; 78 79 int32_t GetSchema(const RdbSyncerParam ¶m) override; 80 81 int32_t Delete(const RdbSyncerParam ¶m) override; 82 83 std::pair<int32_t, std::shared_ptr<ResultSet>> QuerySharingResource(const RdbSyncerParam& param, 84 const PredicatesMemo& predicates, const std::vector<std::string>& columns) override; 85 86 int32_t OnBind(const BindInfo &bindInfo) override; 87 88 int32_t OnInitialize() override; 89 90 int32_t NotifyDataChange(const RdbSyncerParam ¶m, const RdbChangedData &rdbChangedData) override; 91 92 private: 93 using Watchers = DistributedData::AutoCache::Watchers; 94 using StaticActs = DistributedData::StaticActs; 95 struct SyncAgent { 96 pid_t pid_ = 0; 97 int32_t count_ = 0; 98 std::map<std::string, int> callBackStores_; 99 std::string bundleName_; 100 sptr<RdbNotifierProxy> notifier_ = nullptr; 101 std::shared_ptr<RdbWatcher> watcher_ = nullptr; 102 void ReInit(pid_t pid, const std::string &bundleName); 103 void SetNotifier(sptr<RdbNotifierProxy> notifier); 104 void SetWatcher(std::shared_ptr<RdbWatcher> watcher); 105 }; 106 107 class RdbStatic : public StaticActs { 108 public: ~RdbStatic()109 ~RdbStatic() override {}; 110 int32_t OnAppUninstall(const std::string &bundleName, int32_t user, int32_t index) override; 111 int32_t OnAppUpdate(const std::string &bundleName, int32_t user, int32_t index) override; 112 int32_t OnClearAppStorage(const std::string &bundleName, int32_t user, int32_t index, int32_t tokenId) override; 113 private: 114 static constexpr inline int32_t INVALID_TOKENID = 0; 115 int32_t CloseStore(const std::string &bundleName, int32_t user, int32_t index, 116 int32_t tokenId = INVALID_TOKENID) const; 117 }; 118 119 class Factory { 120 public: 121 Factory(); 122 ~Factory(); 123 private: 124 std::shared_ptr<RdbServiceImpl> product_; 125 std::shared_ptr<RdbStatic> staticActs_; 126 }; 127 128 static constexpr inline uint32_t WAIT_TIME = 30 * 1000; 129 static constexpr inline uint32_t SHARE_WAIT_TIME = 60; // seconds 130 131 void RegisterRdbServiceInfo(); 132 133 void RegisterHandler(); 134 135 void DumpRdbServiceInfo(int fd, std::map<std::string, std::vector<std::string>> ¶ms); 136 137 void DoCloudSync(const RdbSyncerParam ¶m, const Option &option, const PredicatesMemo &predicates, 138 const AsyncDetail &async); 139 140 void DoCompensateSync(const DistributedData::BindEvent& event); 141 142 int DoSync(const RdbSyncerParam ¶m, const Option &option, const PredicatesMemo &predicates, 143 const AsyncDetail &async); 144 145 Watchers GetWatchers(uint32_t tokenId, const std::string &storeName); 146 147 DetailAsync GetCallbacks(uint32_t tokenId, const std::string &storeName); 148 149 bool CheckAccess(const std::string& bundleName, const std::string& storeName); 150 151 std::shared_ptr<DistributedData::GeneralStore> GetStore(const RdbSyncerParam& param); 152 153 void OnAsyncComplete(uint32_t tokenId, uint32_t seqNum, Details&& result); 154 155 int32_t CreateMetaData(const RdbSyncerParam ¶m, StoreMetaData &old); 156 157 StoreMetaData GetStoreMetaData(const RdbSyncerParam ¶m); 158 159 int32_t SetSecretKey(const RdbSyncerParam ¶m, const StoreMetaData &meta); 160 161 int32_t Upgrade(const RdbSyncerParam ¶m, const StoreMetaData &old); 162 163 std::pair<int32_t, std::shared_ptr<DistributedData::Cursor>> AllocResource( 164 StoreInfo& storeInfo, std::shared_ptr<RdbQuery> rdbQuery); 165 166 static Details HandleGenDetails(const DistributedData::GenDetails &details); 167 168 static std::string TransferStringToHex(const std::string& origStr); 169 170 static std::string RemoveSuffix(const std::string& name); 171 172 static std::pair<int32_t, int32_t> GetInstIndexAndUser(uint32_t tokenId, const std::string &bundleName); 173 174 static bool GetPassword(const StoreMetaData &metaData, DistributedDB::CipherPassword &password); 175 176 static Factory factory_; 177 ConcurrentMap<uint32_t, SyncAgent> syncAgents_; 178 std::shared_ptr<ExecutorPool> executors_; 179 }; 180 } // namespace OHOS::DistributedRdb 181 #endif 182