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 "commonevent/data_change_event.h" 26 #include "commonevent/set_searchable_event.h" 27 #include "concurrent_map.h" 28 #include "feature/static_acts.h" 29 #include "metadata/secret_key_meta_data.h" 30 #include "metadata/store_meta_data.h" 31 #include "rdb_notifier_proxy.h" 32 #include "rdb_watcher.h" 33 #include "rdb_query.h" 34 #include "store/auto_cache.h" 35 #include "store/general_store.h" 36 #include "store_observer.h" 37 #include "visibility.h" 38 #include "store/general_value.h" 39 #include "snapshot/bind_event.h" 40 41 namespace OHOS::DistributedRdb { 42 class RdbServiceImpl : public RdbServiceStub { 43 public: 44 using StoreMetaData = OHOS::DistributedData::StoreMetaData; 45 using SecretKeyMetaData = DistributedData::SecretKeyMetaData; 46 using DetailAsync = DistributedData::GeneralStore::DetailAsync; 47 using Handler = std::function<void(int, std::map<std::string, std::vector<std::string>> &)>; 48 using StoreInfo = DistributedData::StoreInfo; 49 RdbServiceImpl(); 50 virtual ~RdbServiceImpl(); 51 52 /* IPC interface */ 53 std::string ObtainDistributedTableName(const std::string& device, const std::string& table) override; 54 55 int32_t InitNotifier(const RdbSyncerParam ¶m, sptr<IRemoteObject> notifier) override; 56 57 int32_t SetDistributedTables(const RdbSyncerParam ¶m, const std::vector<std::string> &tables, 58 const std::vector<Reference> &references, bool isRebuild, int32_t type = DISTRIBUTED_DEVICE) override; 59 60 std::pair<int32_t, std::shared_ptr<ResultSet>> RemoteQuery(const RdbSyncerParam& param, const std::string& device, 61 const std::string& sql, const std::vector<std::string>& selectionArgs) override; 62 63 int32_t Sync(const RdbSyncerParam ¶m, const Option &option, const PredicatesMemo &predicates, 64 const AsyncDetail &async) override; 65 66 int32_t Subscribe(const RdbSyncerParam ¶m, const SubscribeOption &option, RdbStoreObserver *observer) override; 67 68 int32_t UnSubscribe(const RdbSyncerParam ¶m, const SubscribeOption &option, 69 RdbStoreObserver *observer) override; 70 71 int32_t RegisterAutoSyncCallback(const RdbSyncerParam& param, 72 std::shared_ptr<DetailProgressObserver> observer) override; 73 74 int32_t UnregisterAutoSyncCallback(const RdbSyncerParam& param, 75 std::shared_ptr<DetailProgressObserver> observer) override; 76 77 int32_t ResolveAutoLaunch(const std::string &identifier, DistributedDB::AutoLaunchParam ¶m) override; 78 79 int32_t OnAppExit(pid_t uid, pid_t pid, uint32_t tokenId, const std::string &bundleName) 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, 91 const RdbNotifyConfig &rdbNotifyConfig) override; 92 int32_t SetSearchable(const RdbSyncerParam& param, bool isSearchable) override; 93 int32_t Disable(const RdbSyncerParam& param) override; 94 int32_t Enable(const RdbSyncerParam& param) override; 95 96 int32_t BeforeOpen(RdbSyncerParam ¶m) override; 97 98 int32_t AfterOpen(const RdbSyncerParam ¶m) override; 99 100 int32_t GetPassword(const RdbSyncerParam ¶m, std::vector<uint8_t> &password) override; 101 102 std::pair<int32_t, uint32_t> LockCloudContainer(const RdbSyncerParam ¶m) override; 103 104 int32_t UnlockCloudContainer(const RdbSyncerParam ¶m) override; 105 106 int32_t GetDebugInfo(const RdbSyncerParam ¶m, std::map<std::string, RdbDebugInfo> &debugInfo) override; 107 108 private: 109 using Watchers = DistributedData::AutoCache::Watchers; 110 using StaticActs = DistributedData::StaticActs; 111 using DBStatus = DistributedDB::DBStatus; 112 using SyncResult = std::pair<std::vector<std::string>, std::map<std::string, DBStatus>>; 113 struct SyncAgent { 114 SyncAgent() = default; 115 explicit SyncAgent(const std::string &bundleName); 116 int32_t count_ = 0; 117 std::map<std::string, int> callBackStores_; 118 std::string bundleName_; 119 sptr<RdbNotifierProxy> notifier_ = nullptr; 120 std::shared_ptr<RdbWatcher> watcher_ = nullptr; 121 void SetNotifier(sptr<RdbNotifierProxy> notifier); 122 void SetWatcher(std::shared_ptr<RdbWatcher> watcher); 123 }; 124 using SyncAgents = std::map<int32_t, SyncAgent>; 125 126 class RdbStatic : public StaticActs { 127 public: ~RdbStatic()128 ~RdbStatic() override {}; 129 int32_t OnAppUninstall(const std::string &bundleName, int32_t user, int32_t index) override; 130 int32_t OnAppUpdate(const std::string &bundleName, int32_t user, int32_t index) override; 131 int32_t OnClearAppStorage(const std::string &bundleName, int32_t user, int32_t index, int32_t tokenId) override; 132 private: 133 static constexpr inline int32_t INVALID_TOKENID = 0; 134 int32_t CloseStore(const std::string &bundleName, int32_t user, int32_t index, 135 int32_t tokenId = INVALID_TOKENID) const; 136 }; 137 138 class Factory { 139 public: 140 Factory(); 141 ~Factory(); 142 private: 143 std::shared_ptr<RdbServiceImpl> product_; 144 std::shared_ptr<RdbStatic> staticActs_; 145 }; 146 147 static constexpr inline uint32_t WAIT_TIME = 30 * 1000; 148 static constexpr inline uint32_t SHARE_WAIT_TIME = 60; // seconds 149 150 void RegisterRdbServiceInfo(); 151 152 void RegisterHandler(); 153 154 void DumpRdbServiceInfo(int fd, std::map<std::string, std::vector<std::string>> ¶ms); 155 156 void DoCloudSync(const RdbSyncerParam ¶m, const Option &option, const PredicatesMemo &predicates, 157 const AsyncDetail &async); 158 159 void DoCompensateSync(const DistributedData::BindEvent& event); 160 161 int DoSync(const RdbSyncerParam ¶m, const Option &option, const PredicatesMemo &predicates, 162 const AsyncDetail &async); 163 164 Watchers GetWatchers(uint32_t tokenId, const std::string &storeName); 165 166 DetailAsync GetCallbacks(uint32_t tokenId, const std::string &storeName); 167 168 bool CheckAccess(const std::string& bundleName, const std::string& storeName); 169 170 std::shared_ptr<DistributedData::GeneralStore> GetStore(const RdbSyncerParam& param); 171 172 void OnAsyncComplete(uint32_t tokenId, pid_t pid, uint32_t seqNum, Details &&result); 173 174 StoreMetaData GetStoreMetaData(const RdbSyncerParam ¶m); 175 176 int32_t SetSecretKey(const RdbSyncerParam ¶m, const StoreMetaData &meta); 177 178 int32_t Upgrade(const RdbSyncerParam ¶m, const StoreMetaData &old); 179 180 std::pair<int32_t, std::shared_ptr<DistributedData::Cursor>> AllocResource( 181 StoreInfo& storeInfo, std::shared_ptr<RdbQuery> rdbQuery); 182 183 static Details HandleGenDetails(const DistributedData::GenDetails &details); 184 185 static std::string TransferStringToHex(const std::string& origStr); 186 187 static std::string RemoveSuffix(const std::string& name); 188 189 static std::pair<int32_t, int32_t> GetInstIndexAndUser(uint32_t tokenId, const std::string &bundleName); 190 191 static bool GetDBPassword(const StoreMetaData &metaData, DistributedDB::CipherPassword &password); 192 193 void GetCloudSchema(const RdbSyncerParam ¶m); 194 195 void SetReturnParam(StoreMetaData &metadata, RdbSyncerParam ¶m); 196 197 bool IsNeedMetaSync(const StoreMetaData &meta, const std::vector<std::string> &uuids); 198 199 SyncResult ProcessResult(const std::map<std::string, int32_t> &results); 200 201 StoreInfo GetStoreInfo(const RdbSyncerParam ¶m); 202 203 int32_t SaveDebugInfo(const StoreMetaData &metaData, const RdbSyncerParam ¶m); 204 205 int32_t PostSearchEvent(int32_t evtId, const RdbSyncerParam& param, 206 DistributedData::SetSearchableEvent::EventInfo &eventInfo); 207 208 bool IsPostImmediately(const int32_t callingPid, const RdbNotifyConfig &rdbNotifyConfig, StoreInfo &storeInfo, 209 DistributedData::DataChangeEvent::EventInfo &eventInfo, const std::string &storeName); 210 211 static Factory factory_; 212 ConcurrentMap<uint32_t, SyncAgents> syncAgents_; 213 std::shared_ptr<ExecutorPool> executors_; 214 ConcurrentMap<int32_t, std::map<std::string, ExecutorPool::TaskId>> heartbeatTaskIds_; 215 }; 216 } // namespace OHOS::DistributedRdb 217 #endif