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 #ifndef OS_ACCOUNT_SERVICES_ACCOUNTMGR_INCLUDE_OHOS_ACCOUNT_DATA_DEAL_H
17 #define OS_ACCOUNT_SERVICES_ACCOUNTMGR_INCLUDE_OHOS_ACCOUNT_DATA_DEAL_H
18 
19 #include <string>
20 #include <mutex>
21 #include <nlohmann/json.hpp>
22 #include "account_error_no.h"
23 #include "account_file_operator.h"
24 #include "account_file_watcher_manager.h"
25 #include "account_info.h"
26 
27 namespace OHOS {
28 namespace AccountSA {
29 using json = nlohmann::json;
30 
31 class OhosAccountDataDeal {
32 public:
33     explicit OhosAccountDataDeal(const std::string &configFileDir);
34     ErrCode Init(std::int32_t userId);
35 
36     ErrCode AccountInfoFromJson(AccountInfo &accountInfo, int32_t userId);
37     ErrCode AccountInfoToJson(const AccountInfo &accountInfo);
~OhosAccountDataDeal()38     ~OhosAccountDataDeal() {}
39 
40 private:
41     bool DealWithFileModifyEvent(const std::string &fileName, const int32_t id);
42     void DealWithFileDeleteEvent(const std::string &fileName, const int32_t id);
43     bool initOk_;
44     std::string configFileDir_;
45     std::mutex mutex_;
46     void BuildJsonFileFromScratch(int32_t userId);
47     ErrCode SaveAccountInfo(const AccountInfo &accountInfo);
48     ErrCode GetAccountInfo(AccountInfo &accountInfo, const int32_t userId);
49     ErrCode ParseJsonFromFile(const std::string &filePath, nlohmann::json &jsonData, int32_t userId);
50     ErrCode GetAccountInfoFromJson(const nlohmann::json &jsonData, AccountInfo &accountInfo, const int32_t userId);
51     ErrCode GenerateAccountInfoDigestStr(
52         const std::string &userInfoPath, const std::string &accountInfoStr, std::string &digestStr);
53     void AddFileWatcher(const int32_t id);
54 
55     std::mutex accountInfoFileLock_;
56     AccountFileWatcherMgr &accountFileWatcherMgr_;
57     std::shared_ptr<AccountFileOperator> accountFileOperator_;
58     CheckNotifyEventCallbackFunc checkCallbackFunc_;
59 };
60 }  // namespace AccountSA
61 }  // namespace OHOS
62 
63 #endif  // OS_ACCOUNT_SERVICES_ACCOUNTMGR_INCLUDE_OHOS_ACCOUNT_DATA_DEAL_H
64