1 /*
2  * Copyright (c) 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 FOUNDATION_RESOURCESCHEDULE_BACKGROUND_TASK_MGR_DATA_STORAGE_HELPER_H
17 #define FOUNDATION_RESOURCESCHEDULE_BACKGROUND_TASK_MGR_DATA_STORAGE_HELPER_H
18 
19 #include "singleton.h"
20 #include "nlohmann/json.hpp"
21 
22 #include "bg_efficiency_resources_mgr.h"
23 #include "bg_continuous_task_mgr.h"
24 
25 namespace OHOS {
26 namespace BackgroundTaskMgr {
27 class DataStorageHelper : public DelayedSingleton<BgContinuousTaskMgr> {
28 using ResourceRecordMap = std::unordered_map<int32_t, std::shared_ptr<ResourceApplicationRecord>>;
29 public:
30     ErrCode RefreshTaskRecord(const std::unordered_map<std::string, std::shared_ptr<ContinuousTaskRecord>> &allRecord);
31     ErrCode RestoreTaskRecord(std::unordered_map<std::string, std::shared_ptr<ContinuousTaskRecord>> &allRecord);
32     ErrCode RefreshResourceRecord(const ResourceRecordMap &appRecord, const ResourceRecordMap &processRecord);
33     ErrCode RestoreResourceRecord(ResourceRecordMap &appRecord, ResourceRecordMap &processRecord);
34     bool ParseFastSuspendDozeTime(const std::string &FilePath, int &time);
35     std::string GetConfigFileAbsolutePath(const std::string &relativePath);
36     int32_t ParseJsonValueFromFile(nlohmann::json &value, const std::string &filePath);
37 
38 private:
39     int32_t SaveJsonValueToFile(const std::string &value, const std::string &filePath);
40     bool CreateNodeFile(const std::string &filePath);
41     bool ConvertFullPath(const std::string &partialPath, std::string &fullPath);
42     void ConvertMapToString(const ResourceRecordMap &appRecord,
43         const ResourceRecordMap &processRecord, std::string &recordString);
44     void ConvertMapToJson(const ResourceRecordMap &appRecord, nlohmann::json &root);
45     void DivideJsonToMap(nlohmann::json &root,
46         ResourceRecordMap &appRecord, ResourceRecordMap &processRecord);
47     void ConvertJsonToMap(const nlohmann::json &value, ResourceRecordMap &recordMap);
48     ErrCode ConvertStringToJson(const std::string &recordString,
49         nlohmann::json &appRecord, nlohmann::json &processRecord);
50     DECLARE_DELAYED_SINGLETON(DataStorageHelper);
51 };
52 }  // namespace BackgroundTaskMgr
53 }  // namespace OHOS
54 #endif  // FOUNDATION_RESOURCESCHEDULE_BACKGROUND_TASK_MGR_DATA_STORAGE_HELPER_H