• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
35 private:
36     int32_t SaveJsonValueToFile(const std::string &value, const std::string &filePath);
37     int32_t ParseJsonValueFromFile(nlohmann::json &value, const std::string &filePath);
38     bool CreateNodeFile(const std::string &filePath);
39     bool ConvertFullPath(const std::string &partialPath, std::string &fullPath);
40     void ConvertMapToString(const ResourceRecordMap &appRecord,
41         const ResourceRecordMap &processRecord, std::string &recordString);
42     void ConvertMapToJson(const ResourceRecordMap &appRecord, nlohmann::json &root);
43     void DivideJsonToMap(nlohmann::json &root,
44         ResourceRecordMap &appRecord, ResourceRecordMap &processRecord);
45     void ConvertJsonToMap(const nlohmann::json &value, ResourceRecordMap &recordMap);
46     ErrCode ConvertStringToJson(const std::string &recordString,
47         nlohmann::json &appRecord, nlohmann::json &processRecord);
48     DECLARE_DELAYED_SINGLETON(DataStorageHelper);
49 };
50 }  // namespace BackgroundTaskMgr
51 }  // namespace OHOS
52 #endif  // FOUNDATION_RESOURCESCHEDULE_BACKGROUND_TASK_MGR_DATA_STORAGE_HELPER_H