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 #include "backuprule/backup_rule_manager.h"
16 namespace OHOS::DistributedData {
GetInstance()17 BackupRuleManager &BackupRuleManager::GetInstance()
18 {
19     static BackupRuleManager instance;
20     return instance;
21 }
22 
LoadBackupRules(const std::vector<std::string> & backupRules)23 void BackupRuleManager::LoadBackupRules(const std::vector<std::string> &backupRules)
24 {
25     for (const auto &backupRule : backupRules) {
26         auto it = getters_.Find(backupRule);
27         if (!it.first || it.second == nullptr) {
28             continue;
29         }
30         auto *backupRuler = it.second();
31         if (backupRuler == nullptr) {
32             continue;
33         }
34         backupRules_.push_back(backupRuler);
35     }
36 }
37 
RegisterPlugin(const std::string & backupRule,std::function<BackupRule * ()> getter)38 void BackupRuleManager::RegisterPlugin(const std::string &backupRule, std::function<BackupRule *()> getter)
39 {
40     getters_.ComputeIfAbsent(backupRule, [&getter](const auto &) mutable {
41         return std::move(getter);
42     });
43 }
44 
CanBackup()45 bool BackupRuleManager::CanBackup()
46 {
47     for (auto rule : backupRules_) {
48         if (rule == nullptr) {
49             continue;
50         }
51         if (!rule->CanBackup()) {
52             return false;
53         }
54     }
55     return true;
56 }
57 } // namespace OHOS::DistributedData
58