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 OHOS_DISTRIBUTED_DATA_SERVICES_FRAMEWORK_CHECKER_CHECKER_MANAGER_H
17 #define OHOS_DISTRIBUTED_DATA_SERVICES_FRAMEWORK_CHECKER_CHECKER_MANAGER_H
18 #include <vector>
19 #include "visibility.h"
20 #include "concurrent_map.h"
21 namespace OHOS {
22 namespace DistributedData {
23 class CheckerManager {
24 public:
25     struct Trust {
26         std::string bundleName;
27         std::string appId;
28         std::string packageName;
29         std::string base64Key;
30         std::string checker;
31     };
32     using Distrust = Trust;
33     using Switches = Trust;
34     struct StoreInfo {
35         pid_t uid;
36         uint32_t tokenId;
37         std::string bundleName;
38         std::string storeId;
39     };
40     class Checker {
41     public:
42         virtual void Initialize() = 0;
43         virtual bool SetTrustInfo(const Trust &trust) = 0;
44         virtual bool SetDistrustInfo(const Distrust &distrust) = 0;
45         virtual bool SetSwitchesInfo(const Switches &switches) = 0;
46         virtual std::string GetAppId(const StoreInfo &info) = 0;
47         virtual bool IsValid(const StoreInfo &info) = 0;
48         virtual bool IsDistrust(const StoreInfo &info) = 0;
49         virtual bool IsSwitches(const StoreInfo &info) = 0;
50         virtual bool AddDynamicStore(const StoreInfo& storeInfo) = 0;
51         virtual bool AddStaticStore(const StoreInfo& storeInfo) = 0;
52         virtual std::vector<StoreInfo> GetDynamicStores() = 0;
53         virtual std::vector<StoreInfo> GetStaticStores() = 0;
54         virtual bool IsDynamic(const StoreInfo &info) = 0;
55         virtual bool IsStatic(const StoreInfo &info) = 0;
56     protected:
57         API_EXPORT ~Checker() = default;
58     };
59     API_EXPORT static CheckerManager &GetInstance();
60     API_EXPORT void RegisterPlugin(const std::string &checker, std::function<Checker *()> getter);
61     API_EXPORT std::string GetAppId(const StoreInfo &info);
62     API_EXPORT std::vector<StoreInfo> GetDynamicStores();
63     API_EXPORT std::vector<StoreInfo> GetStaticStores();
64     API_EXPORT bool IsDynamic(const StoreInfo &info);
65     API_EXPORT bool IsStatic(const StoreInfo &info);
66     API_EXPORT bool IsValid(const StoreInfo &info);
67     API_EXPORT bool IsDistrust(const StoreInfo &info);
68     API_EXPORT bool IsSwitches(const StoreInfo &info);
69     API_EXPORT void LoadCheckers(std::vector<std::string> &checkers);
70     API_EXPORT Checker *GetChecker(const std::string &checker);
71 private:
72     std::map<std::string, Checker *> checkers_;
73     ConcurrentMap<std::string, std::function<Checker *()>> getters_;
74 };
75 } // namespace DistributedData
76 } // namespace OHOS
77 #endif // OHOS_DISTRIBUTED_DATA_SERVICES_FRAMEWORK_CHECKER_CHECKER_MANAGER_H