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