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_USER_MANAGER_H 17 #define KV_STORE_USER_MANAGER_H 18 19 #include <map> 20 #include <mutex> 21 #include "kvstore_app_manager.h" 22 #include "kvstore_impl.h" 23 #include "types.h" 24 25 namespace OHOS { 26 namespace DistributedKv { 27 class KvStoreUserManager { 28 public: 29 explicit KvStoreUserManager(const std::string &userId); 30 31 virtual ~KvStoreUserManager(); 32 33 template<typename T> GetKvStore(const Options & options,const std::string & bundleName,const std::string & storeId,pid_t uid,const std::vector<uint8_t> & cipherKey,sptr<T> & kvStore)34 Status GetKvStore(const Options &options, const std::string &bundleName, const std::string &storeId, pid_t uid, 35 const std::vector<uint8_t> &cipherKey, sptr<T> &kvStore) 36 { 37 std::lock_guard<std::mutex> lg(appMutex_); 38 auto it = appMap_.find(bundleName); 39 if (it == appMap_.end()) { 40 auto result = appMap_.emplace( 41 std::piecewise_construct, std::forward_as_tuple(bundleName), std::forward_as_tuple(bundleName, uid)); 42 if (result.second) { 43 it = result.first; 44 } 45 } 46 if (it == appMap_.end()) { 47 kvStore = nullptr; 48 return Status::ERROR; 49 } 50 return (it->second).GetKvStore(options, bundleName, storeId, cipherKey, kvStore); 51 } 52 53 Status CloseKvStore(const std::string &appId, const std::string &storeId); 54 55 Status CloseAllKvStore(const std::string &appId); 56 57 void CloseAllKvStore(); 58 59 Status DeleteKvStore(const std::string &bundleName, pid_t uid, const std::string &storeId); 60 61 void DeleteAllKvStore(); 62 63 Status MigrateAllKvStore(const std::string &harmonyAccountId); 64 65 std::string GetDbDir(const std::string &bundleName, const Options &options); 66 67 void Dump(int fd) const; 68 69 bool IsStoreOpened(const std::string &appId, const std::string &storeId); 70 void SetCompatibleIdentify(const std::string &deviceId) const; 71 72 private: 73 std::mutex appMutex_; 74 std::map<std::string, KvStoreAppManager> appMap_; 75 std::string userId_; 76 }; 77 } // namespace DistributedKv 78 } // namespace OHOS 79 80 #endif // KV_STORE_USER_MANAGER_H 81