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 "concurrent_map.h" 25 #include "metadata/secret_key_meta_data.h" 26 #include "metadata/store_meta_data.h" 27 #include "rdb_notifier_proxy.h" 28 #include "rdb_watcher.h" 29 #include "store/auto_cache.h" 30 #include "store/general_store.h" 31 #include "store_observer.h" 32 #include "visibility.h" 33 #include "store/general_value.h" 34 namespace OHOS::DistributedRdb { 35 class API_EXPORT RdbServiceImpl : public RdbServiceStub { 36 public: 37 using StoreMetaData = OHOS::DistributedData::StoreMetaData; 38 using SecretKeyMetaData = DistributedData::SecretKeyMetaData; 39 RdbServiceImpl(); 40 41 void OnClientDied(pid_t pid); 42 43 /* IPC interface */ 44 std::string ObtainDistributedTableName(const std::string& device, const std::string& table) override; 45 46 int32_t InitNotifier(const RdbSyncerParam ¶m, sptr<IRemoteObject> notifier) override; 47 48 int32_t SetDistributedTables(const RdbSyncerParam ¶m, const std::vector<std::string> &tables, 49 int32_t type = DISTRIBUTED_DEVICE) override; 50 51 int32_t RemoteQuery(const RdbSyncerParam& param, const std::string& device, const std::string& sql, 52 const std::vector<std::string>& selectionArgs, sptr<IRemoteObject>& resultSet) override; 53 54 int32_t Sync(const RdbSyncerParam ¶m, const Option &option, const PredicatesMemo &predicates, 55 const AsyncDetail &async) override; 56 57 int32_t Subscribe(const RdbSyncerParam ¶m, const SubscribeOption &option, RdbStoreObserver *observer) override; 58 59 int32_t UnSubscribe(const RdbSyncerParam ¶m, const SubscribeOption &option, 60 RdbStoreObserver *observer) override; 61 62 int32_t ResolveAutoLaunch(const std::string &identifier, DistributedDB::AutoLaunchParam ¶m) override; 63 64 int32_t OnInitialize() override; 65 66 int32_t OnAppExit(pid_t uid, pid_t pid, uint32_t tokenId, const std::string &bundleName) override; 67 68 int32_t OnAppUninstall(const std::string &bundleName, int32_t user, int32_t index) override; 69 70 int32_t OnAppUpdate(const std::string &bundleName, int32_t user, int32_t index) override; 71 72 int32_t GetSchema(const RdbSyncerParam ¶m) override; 73 74 int32_t Delete(const RdbSyncerParam ¶m) override; 75 76 int32_t OnBind(const BindInfo &bindInfo) override; 77 78 private: 79 using Watchers = DistributedData::AutoCache::Watchers; 80 struct SyncAgent { 81 pid_t pid_ = 0; 82 int32_t count_ = 0; 83 std::string bundleName_; 84 sptr<RdbNotifierProxy> notifier_ = nullptr; 85 std::shared_ptr<RdbWatcher> watcher_ = nullptr; 86 void ReInit(pid_t pid, const std::string &bundleName); 87 void SetNotifier(sptr<RdbNotifierProxy> notifier); 88 void SetWatcher(std::shared_ptr<RdbWatcher> watcher); 89 }; 90 91 class Factory { 92 public: 93 Factory(); 94 ~Factory(); 95 private: 96 std::shared_ptr<RdbServiceImpl> product_; 97 }; 98 99 static constexpr inline uint32_t WAIT_TIME = 30 * 1000; 100 101 void DoCloudSync(const RdbSyncerParam ¶m, const Option &option, const PredicatesMemo &predicates, 102 const AsyncDetail &async); 103 104 int DoSync(const RdbSyncerParam ¶m, const Option &option, const PredicatesMemo &predicates, 105 const AsyncDetail &async); 106 107 Watchers GetWatchers(uint32_t tokenId, const std::string &storeName); 108 109 bool CheckAccess(const std::string& bundleName, const std::string& storeName); 110 111 std::shared_ptr<DistributedData::GeneralStore> GetStore(const RdbSyncerParam& param); 112 113 void OnAsyncComplete(uint32_t tokenId, uint32_t seqNum, Details&& result); 114 115 int32_t CreateMetaData(const RdbSyncerParam ¶m, StoreMetaData &old); 116 117 StoreMetaData GetStoreMetaData(const RdbSyncerParam ¶m); 118 119 int32_t SetSecretKey(const RdbSyncerParam ¶m, const StoreMetaData &meta); 120 121 int32_t Upgrade(const RdbSyncerParam ¶m, const StoreMetaData &old); 122 123 static Details HandleGenDetails(const DistributedData::GenDetails &details); 124 125 static std::string TransferStringToHex(const std::string& origStr); 126 127 static std::string RemoveSuffix(const std::string& name); 128 129 static std::pair<int32_t, int32_t> GetInstIndexAndUser(uint32_t tokenId, const std::string &bundleName); 130 131 static bool GetPassword(const StoreMetaData &metaData, DistributedDB::CipherPassword &password); 132 133 int32_t CloseStore(const std::string &bundleName, int32_t user, int32_t index) const; 134 135 static Factory factory_; 136 ConcurrentMap<uint32_t, SyncAgent> syncAgents_; 137 std::shared_ptr<ExecutorPool> executors_; 138 }; 139 } // namespace OHOS::DistributedRdb 140 #endif 141