• 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 #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     auto it = getters_.Find(backupRule);
41     if (it.first) {
42         return;
43     }
44     getters_.Compute(backupRule, [&getter](const auto &key, auto &value) {
45         value = move(getter);
46         return true;
47     });
48 }
49 
CanBackup()50 bool BackupRuleManager::CanBackup()
51 {
52     for (auto rule : backupRules_) {
53         if (rule == nullptr) {
54             continue;
55         }
56         if (!rule->CanBackup()) {
57             return false;
58         }
59     }
60     return true;
61 }
62 } // namespace OHOS::DistributedData
63