1 /* 2 * Copyright (c) 2021 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 KV_STORE_DELEGATE_MANAGER_H 17 #define KV_STORE_DELEGATE_MANAGER_H 18 19 #include <string> 20 #include <functional> 21 #include <mutex> 22 #include <memory> 23 24 #include "auto_launch_export.h" 25 #include "iprocess_communicator.h" 26 #include "iprocess_system_api_adapter.h" 27 #include "kv_store_delegate.h" 28 #include "kv_store_nb_delegate.h" 29 #include "store_types.h" 30 31 namespace DistributedDB { 32 class KvStoreDelegateManager final { 33 public: 34 DB_API KvStoreDelegateManager(const std::string &appId, const std::string &userId, int32_t instanceId = 0); 35 DB_API KvStoreDelegateManager(const std::string &appId, const std::string &userId, const std::string &subUser, 36 int32_t instanceId = 0); 37 DB_API ~KvStoreDelegateManager(); 38 39 KvStoreDelegateManager(const KvStoreDelegateManager &) = delete; 40 KvStoreDelegateManager(KvStoreDelegateManager &&) = delete; 41 KvStoreDelegateManager &operator=(const KvStoreDelegateManager &) = delete; 42 KvStoreDelegateManager &operator=(KvStoreDelegateManager &&) = delete; 43 44 // Used to set global config of the KvStores, such dataDir, return OK if set config success. 45 DB_API DBStatus SetKvStoreConfig(const KvStoreConfig &kvStoreConfig); 46 47 // Used to open or create a KvStore. 48 // Return OK and a KvStoreDelegate* if there is no error. else return ERROR and nullptr; 49 DB_API void GetKvStore(const std::string &storeId, const KvStoreDelegate::Option &option, 50 const std::function<void(DBStatus, KvStoreDelegate *)> &callback); 51 52 // Used to open or create a KvStore(Natural store). 53 // Suggest: Not to use encrypted database in S3 SECE access controlled; 54 // Warning: Access controlled prevents access to files so cannot verify passwords, 55 // So that a cacheDb with incorrect passwd will be created or opened and lose these data. 56 DB_API void GetKvStore(const std::string &storeId, const KvStoreNbDelegate::Option &option, 57 const std::function<void(DBStatus, KvStoreNbDelegate *)> &callback); 58 59 // Close a KvStore, return OK if close success. 60 DB_API DBStatus CloseKvStore(KvStoreDelegate *kvStore); 61 62 DB_API DBStatus CloseKvStore(KvStoreNbDelegate *kvStore); 63 64 // Used to delete a KvStore, return OK if delete success. 65 DB_API DBStatus DeleteKvStore(const std::string &storeId); 66 67 // Get the database size. 68 DB_API DBStatus GetKvStoreDiskSize(const std::string &storeId, uint64_t &size); 69 70 // Used to set the process userid and appId 71 DB_API static DBStatus SetProcessLabel(const std::string &appId, const std::string &userId); 72 73 // Set process communicator. 74 DB_API static DBStatus SetProcessCommunicator(const std::shared_ptr<IProcessCommunicator> &inCommunicator); 75 76 DB_API static void SetKvStoreCorruptionHandler(const KvStoreCorruptionHandler &handler); 77 78 // Get database directory by storeId + appId + userId 79 DB_API static DBStatus GetDatabaseDir(const std::string &storeId, const std::string &appId, 80 const std::string &userId, std::string &directory); 81 82 // Get database directory by storeId 83 DB_API static DBStatus GetDatabaseDir(const std::string &storeId, std::string &directory); 84 85 DB_API static DBStatus SetPermissionCheckCallback(const PermissionCheckCallback &callback); 86 87 DB_API static DBStatus SetPermissionCheckCallback(const PermissionCheckCallbackV2 &callback); 88 89 DB_API static DBStatus EnableKvStoreAutoLaunch(const std::string &userId, const std::string &appId, 90 const std::string &storeId, const AutoLaunchOption &option, const AutoLaunchNotifier ¬ifier); 91 92 DB_API static DBStatus DisableKvStoreAutoLaunch(const std::string &userId, const std::string &appId, 93 const std::string &storeId); 94 95 // deprecated 96 DB_API static void SetAutoLaunchRequestCallback(const AutoLaunchRequestCallback &callback); 97 98 // deprecated 99 DB_API static std::string GetKvStoreIdentifier(const std::string &userId, const std::string &appId, 100 const std::string &storeId, bool syncDualTupleMode = false); 101 102 DB_API static DBStatus SetProcessSystemAPIAdapter(const std::shared_ptr<IProcessSystemApiAdapter> &adapter); 103 104 DB_API static bool IsProcessSystemApiAdapterValid(); 105 106 DB_API static void SetStoreStatusNotifier(const StoreStatusNotifier ¬ifier); 107 108 DB_API static DBStatus SetSyncActivationCheckCallback(const SyncActivationCheckCallback &callback); 109 110 DB_API static DBStatus NotifyUserChanged(); 111 private: 112 113 // Check if the dataDir is safe arg. 114 bool IsDataDirSafe(const std::string &dataDir, std::string &canonicalDir) const; 115 bool GetKvStoreParamCheck(const std::string &storeId, const KvStoreNbDelegate::Option &option, 116 const std::function<void(DBStatus, KvStoreNbDelegate *)> &callback) const; 117 DBStatus SetObserverNotifier(KvStoreNbDelegate *kvStore, const KvStoreNbDelegate::Option &option); 118 119 const std::string &GetKvStorePath() const; 120 static const std::string DEFAULT_PROCESS_APP_ID; 121 static std::mutex communicatorMutex_; 122 static std::shared_ptr<IProcessCommunicator> processCommunicator_; 123 static std::mutex multiUserMutex_; 124 125 KvStoreConfig kvStoreConfig_; 126 std::string appId_; 127 std::string userId_; 128 std::string subUser_; 129 int32_t instanceId_; 130 131 mutable std::mutex mutex_; 132 }; 133 } // namespace DistributedDB 134 135 #endif // KV_STORE_DELEGATE_MANAGER_H 136