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 KVSTORE_DATASERVICE_H 17 #define KVSTORE_DATASERVICE_H 18 19 #include <map> 20 #include <mutex> 21 #include <set> 22 23 #include "account_delegate.h" 24 #include "backup_handler.h" 25 #include "constant.h" 26 #include "device_change_listener_impl.h" 27 #include "ikvstore_data_service.h" 28 #include "kvstore_device_listener.h" 29 #include "kvstore_impl.h" 30 #include "kvstore_user_manager.h" 31 #include "reporter.h" 32 #include "security/security.h" 33 #include "single_kvstore_impl.h" 34 #include "system_ability.h" 35 #include "types.h" 36 37 namespace OHOS::DistributedRdb { 38 class IRdbService; 39 class RdbServiceImpl; 40 } 41 42 namespace OHOS::DistributedKv { 43 class KvStoreAccountObserver; 44 class KvStoreDataService 45 : public SystemAbility 46 , public KvStoreDataServiceStub { 47 DECLARE_SYSTEM_ABILITY(KvStoreDataService); 48 49 public: 50 // record kvstore meta version for compatible, should update when modify kvstore meta structure. 51 static constexpr uint32_t STORE_VERSION = 0x03000001; 52 53 explicit KvStoreDataService(bool runOnCreate = false); 54 explicit KvStoreDataService(int32_t systemAbilityId, bool runOnCreate = false); 55 virtual ~KvStoreDataService(); 56 57 Status GetKvStore(const Options &options, const AppId &appId, const StoreId &storeId, 58 std::function<void(sptr<IKvStoreImpl>)> callback) override; 59 60 Status GetSingleKvStore(const Options &options, const AppId &appId, const StoreId &storeId, 61 std::function<void(sptr<ISingleKvStore>)> callback) override; 62 63 void GetAllKvStoreId(const AppId &appId, std::function<void(Status, std::vector<StoreId> &)> callback) override; 64 65 Status CloseKvStore(const AppId &appId, const StoreId &storeId) override; 66 67 Status CloseAllKvStore(const AppId &appId) override; 68 69 Status DeleteKvStore(const AppId &appId, const StoreId &storeId) override; 70 71 Status DeleteAllKvStore(const AppId &appId) override; 72 73 Status RegisterClientDeathObserver(const AppId &appId, sptr<IRemoteObject> observer) override; 74 75 Status GetLocalDevice(DeviceInfo &device) override; 76 Status GetDeviceList(std::vector<DeviceInfo> &deviceInfoList, DeviceFilterStrategy strategy) override; 77 Status StartWatchDeviceChange(sptr<IDeviceStatusChangeListener> observer, DeviceFilterStrategy strategy) override; 78 Status StopWatchDeviceChange(sptr<IDeviceStatusChangeListener> observer) override; 79 sptr<DistributedRdb::IRdbService> GetRdbService() override; 80 81 void OnDump() override; 82 83 int Dump(int fd, const std::vector<std::u16string> &args) override; 84 85 void OnStart() override; 86 87 void OnStop() override; 88 89 Status DeleteKvStoreOnly(const std::string &bundleName, pid_t uid, const std::string &storeId); 90 91 void AccountEventChanged(const AccountEventInfo &eventInfo); 92 93 void SetCompatibleIdentify(const AppDistributedKv::DeviceInfo &info) const; 94 95 bool CheckBackupFileExist(const std::string &userId, const std::string &bundleName, 96 const std::string &storeId, int pathType); 97 98 struct KvStoreParam { 99 std::string bundleName; 100 std::string storeId; 101 std::string trueAppId; 102 std::string userId; 103 pid_t uid; 104 Status status = Status::SUCCESS; 105 }; 106 struct SecretKeyPara { 107 std::vector<uint8_t> metaKey; 108 std::vector<uint8_t> secretKey; 109 std::vector<uint8_t> metaSecretKey; 110 std::string secretKeyFile; 111 Status alreadyCreated = Status::SUCCESS; 112 bool outdated = false; 113 }; 114 115 private: 116 class KvStoreClientDeathObserverImpl { 117 public: 118 KvStoreClientDeathObserverImpl(const AppId &appId, pid_t uid, 119 KvStoreDataService &service, sptr<IRemoteObject> observer); 120 121 virtual ~KvStoreClientDeathObserverImpl(); 122 123 private: 124 class KvStoreDeathRecipient : public IRemoteObject::DeathRecipient { 125 public: 126 explicit KvStoreDeathRecipient(KvStoreClientDeathObserverImpl &kvStoreClientDeathObserverImpl); 127 virtual ~KvStoreDeathRecipient(); 128 void OnRemoteDied(const wptr<IRemoteObject> &remote) override; 129 130 private: 131 KvStoreClientDeathObserverImpl &kvStoreClientDeathObserverImpl_; 132 }; 133 void NotifyClientDie(); 134 AppId appId_; 135 pid_t uid_; 136 KvStoreDataService &dataService_; 137 sptr<IRemoteObject> observerProxy_; 138 sptr<KvStoreDeathRecipient> deathRecipient_; 139 }; 140 141 void AddPermission() const; 142 143 void Initialize(); 144 145 void StartService(); 146 147 void InitSecurityAdapter(); 148 149 Status DeleteKvStore(const std::string &bundleName, const StoreId &storeId); 150 151 template<class T> 152 Status RecoverKvStore(const Options &options, const std::string &bundleName, const std::string &storeId, 153 const std::vector<uint8_t> &secretKey, sptr<T> &kvStore); 154 Status GetSecretKey(const Options &options, const KvStoreParam &KvParas, SecretKeyPara &secretKeyParas); 155 156 Status RecoverSecretKey(const Status &alreadyCreated, bool &outdated, const std::vector<uint8_t> &metaSecretKey, 157 std::vector<uint8_t> &secretKey, const std::string &secretKeyFile); 158 159 Status UpdateMetaData(const Options &options, const KvStoreParam &kvParas, 160 const std::vector<uint8_t> &metaKey, KvStoreUserManager &kvStoreUserManager); 161 162 void OnStoreMetaChanged(const std::vector<uint8_t> &key, const std::vector<uint8_t> &value, CHANGE_FLAG flag); 163 164 Status GetKvStoreFailDo(const Options &options, const KvStoreParam &kvParas, SecretKeyPara &secKeyParas, 165 KvStoreUserManager &kvUserManager, sptr<KvStoreImpl> &kvStore); 166 167 Status GetSingleKvStoreFailDo(const Options &options, const KvStoreParam &kvParas, SecretKeyPara &secKeyParas, 168 KvStoreUserManager &kvUserManager, sptr<SingleKvStoreImpl> &kvStore); 169 170 Status AppExit(const AppId &appId, pid_t uid); 171 172 bool CheckPermissions(const std::string &userId, const std::string &appId, const std::string &storeId, 173 const std::string &deviceId, uint8_t flag) const; 174 bool ResolveAutoLaunchParamByIdentifier(const std::string &identifier, DistributedDB::AutoLaunchParam ¶m); 175 static void ResolveAutoLaunchCompatible(const MetaData &meta, const std::string &identifier); 176 bool CheckSyncActivation(const std::string &userId, const std::string &appId, const std::string &storeId); 177 178 bool CheckOptions(const Options &options, const std::vector<uint8_t> &metaKey) const; 179 void CreateRdbService(); 180 bool IsStoreOpened(const std::string &userId, const std::string &appId, const std::string &storeId); 181 static Status FillStoreParam( 182 const Options &options, const AppId &appId, const StoreId &storeId, KvStoreParam ¶m); 183 184 static constexpr int TEN_SEC = 10; 185 186 std::mutex accountMutex_; 187 std::map<std::string, KvStoreUserManager> deviceAccountMap_; 188 std::mutex clientDeathObserverMutex_; 189 std::map<std::string, KvStoreClientDeathObserverImpl> clientDeathObserverMap_; 190 std::shared_ptr<KvStoreAccountObserver> accountEventObserver_; 191 std::unique_ptr<BackupHandler> backup_; 192 std::map<IRemoteObject *, sptr<IDeviceStatusChangeListener>> deviceListeners_; 193 std::mutex deviceListenerMutex_; 194 std::shared_ptr<DeviceChangeListenerImpl> deviceListener_; 195 196 std::shared_ptr<Security> security_; 197 sptr<DistributedRdb::RdbServiceImpl> rdbService_; 198 std::shared_ptr<KvStoreDeviceListener> deviceInnerListener_; 199 }; 200 201 class DbMetaCallbackDelegateMgr : public DbMetaCallbackDelegate { 202 public: 203 using Option = DistributedDB::KvStoreNbDelegate::Option; ~DbMetaCallbackDelegateMgr()204 virtual ~DbMetaCallbackDelegateMgr() {} 205 DbMetaCallbackDelegateMgr(DistributedDB::KvStoreDelegateManager * delegate)206 explicit DbMetaCallbackDelegateMgr(DistributedDB::KvStoreDelegateManager *delegate) 207 : delegate_(delegate) {} 208 bool GetKvStoreDiskSize(const std::string &storeId, uint64_t &size) override; 209 void GetKvStoreKeys(std::vector<StoreInfo> &dbStats) override; IsDestruct()210 bool IsDestruct() 211 { 212 return delegate_ == nullptr; 213 } 214 215 private: Split(const std::string & str,const std::string & delimiter,std::vector<std::string> & out)216 void Split(const std::string &str, const std::string &delimiter, std::vector<std::string> &out) 217 { 218 size_t start; 219 size_t end = 0; 220 while ((start = str.find_first_not_of(delimiter, end)) != std::string::npos) { 221 end = str.find(delimiter, start); 222 if (end == std::string::npos) { 223 end = str.size(); 224 } 225 out.push_back(str.substr(start, end - start)); 226 } 227 } 228 229 DistributedDB::KvStoreDelegateManager *delegate_ {}; 230 static const inline int USER_ID = 0; 231 static const inline int APP_ID = 1; 232 static const inline int STORE_ID = 2; 233 static const inline int VECTOR_SIZE = 2; 234 }; 235 } 236 #endif // KVSTORE_DATASERVICE_H 237