1 /*
2 * Copyright (c) 2021-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 #include "os_account_data_storage.h"
17
18 #include "account_error_no.h"
19 #include "account_log_wrapper.h"
20
21 namespace OHOS {
22 namespace AccountSA {
~OsAccountDataStorage()23 OsAccountDataStorage::~OsAccountDataStorage()
24 {
25 ACCOUNT_LOGD("enter");
26 }
27
OsAccountDataStorage(const std::string & appId,const std::string & storeId,const bool & autoSync)28 OsAccountDataStorage::OsAccountDataStorage(const std::string &appId, const std::string &storeId, const bool &autoSync)
29 : AccountDataStorage(appId, storeId, autoSync)
30 {
31 ACCOUNT_LOGD("enter");
32 }
33
SaveEntries(std::vector<OHOS::DistributedKv::Entry> allEntries,std::map<std::string,std::shared_ptr<IAccountInfo>> & infos)34 void OsAccountDataStorage::SaveEntries(
35 std::vector<OHOS::DistributedKv::Entry> allEntries, std::map<std::string, std::shared_ptr<IAccountInfo>> &infos)
36 {
37 ACCOUNT_LOGD("start, allEntries size is: %{public}zu", allEntries.size());
38 for (auto const &item : allEntries) {
39 OsAccountInfo osAccountInfo;
40 nlohmann::json jsonObject = nlohmann::json::parse(item.value.ToString(), nullptr, false);
41 if (jsonObject.is_discarded()) {
42 ACCOUNT_LOGE("error key: %{private}s", item.key.ToString().c_str());
43 // it's a bad json, delete it
44 {
45 std::lock_guard<std::mutex> lock(kvStorePtrMutex_);
46 kvStorePtr_->Delete(item.key);
47 }
48 continue;
49 }
50 osAccountInfo.FromJson(jsonObject);
51 infos.emplace(item.key.ToString(), std::make_shared<OsAccountInfo>(osAccountInfo));
52 }
53 ACCOUNT_LOGD("end");
54 }
55 } // namespace AccountSA
56 } // namespace OHOS