• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2024 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 #ifdef ENABLE_FILE_WATCHER
25 #include "account_file_watcher_manager.h"
26 #endif // ENABLE_FILE_WATCHER
27 #include "account_info.h"
28 
29 namespace OHOS {
30 namespace AccountSA {
31 using json = nlohmann::json;
32 
33 class OhosAccountDataDeal {
34 public:
35     explicit OhosAccountDataDeal(const std::string &configFileDir);
36     ErrCode Init(std::int32_t userId);
37 
38     ErrCode AccountInfoFromJson(AccountInfo &accountInfo, int32_t userId);
39     ErrCode AccountInfoToJson(const AccountInfo &accountInfo);
~OhosAccountDataDeal()40     ~OhosAccountDataDeal() {}
41 
42 private:
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 #ifdef ENABLE_FILE_WATCHER
52     bool DealWithFileModifyEvent(const std::string &fileName, const int32_t id);
53     void DealWithFileDeleteEvent(const std::string &fileName, const int32_t id);
54     ErrCode GenerateAccountInfoDigestStr(
55         const std::string &userInfoPath, const std::string &accountInfoStr, std::string &digestStr);
56     void AddFileWatcher(const int32_t id);
57 #endif // ENABLE_FILE_WATCHER
58 
59     std::mutex accountInfoFileLock_;
60 #ifdef ENABLE_FILE_WATCHER
61     AccountFileWatcherMgr &accountFileWatcherMgr_;
62     CheckNotifyEventCallbackFunc checkCallbackFunc_;
63 #endif // ENABLE_FILE_WATCHER
64     std::shared_ptr<AccountFileOperator> accountFileOperator_;
65 };
66 }  // namespace AccountSA
67 }  // namespace OHOS
68 
69 #endif  // OS_ACCOUNT_SERVICES_ACCOUNTMGR_INCLUDE_OHOS_ACCOUNT_DATA_DEAL_H
70