1 /*
2  * Copyright (c) 2023 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 DISTRIBUTEDDATAMGR_PROFILE_CONFIG_H
17 #define DISTRIBUTEDDATAMGR_PROFILE_CONFIG_H
18 
19 #include <string>
20 #include <map>
21 #include <mutex>
22 #include <vector>
23 
24 #include "bundle_info.h"
25 #include "resource_manager.h"
26 #include "serializable/serializable.h"
27 
28 namespace OHOS::DataShare {
29 using namespace OHOS::Global::Resource;
30 struct Config final : public DistributedData::Serializable {
31     std::string uri = "*";
32     int crossUserMode = 0;
33     std::string writePermission = "";
34     std::string readPermission = "";
35     bool Marshal(json &node) const override;
36     bool Unmarshal(const json &node) override;
37 };
38 
39 struct LaunchInfo final : public DistributedData::Serializable {
40     std::string storeId = "";
41     std::vector<std::string> tableNames;
42     bool Marshal(json &node) const override;
43     bool Unmarshal(const json &node) override;
44 };
45 
46 struct ProfileInfo : public DistributedData::Serializable {
47     std::vector<Config> tableConfig;
48     bool isSilentProxyEnable = true;
49     std::string storeName;
50     std::string tableName;
51     std::string scope = "module";
52     std::string type = "rdb";
53     std::string backup;
54     std::string extUri;
55     std::vector<LaunchInfo> launchInfos;
56     bool storeMetaDataFromUri = false;
57     bool launchForCleanData = false;
58     bool Marshal(json &node) const override;
59     bool Unmarshal(const json &node) override;
60 };
61 
62 enum AccessCrossMode : uint8_t {
63     USER_UNDEFINED,
64     USER_SHARED,
65     USER_SINGLE,
66     USER_MAX,
67 };
68 
69 class DataShareProfileConfig {
70 public:
71     constexpr static int8_t TABLE_MATCH_PRIORITY = 3;
72     constexpr static int8_t STORE_MATCH_PRIORITY = 2;
73     constexpr static int8_t COMMON_MATCH_PRIORITY = 1;
74     constexpr static int8_t UNDEFINED_PRIORITY = -1;
75 
76     static bool GetProfileInfo(const std::string &calledBundleName, int32_t currentUserId,
77         std::map<std::string, ProfileInfo> &profileInfos);
78     static std::pair<int, ProfileInfo> GetDataProperties(const std::vector<AppExecFwk::Metadata> &metadata,
79         const std::string &resPath, const std::string &hapPath, const std::string &name);
80     static AccessCrossMode GetAccessCrossMode(const ProfileInfo &profileInfo,
81         const std::string &tableUri, const std::string &storeUri);
82 private:
83     static std::shared_ptr<ResourceManager> InitResMgr(const std::string &resourcePath);
84     static std::string GetProfileInfoByMetadata(const std::vector<AppExecFwk::Metadata> &metadata,
85         const std::string &resourcePath, const std::string &hapPath, const std::string &name);
86     static std::string GetResFromResMgr(const std::string &resName, ResourceManager &resMgr,
87         const std::string &hapPath);
88     static std::string ReadProfile(const std::string &resPath);
89     static bool IsFileExisted(const std::string &filePath);
90     static std::mutex infosMutex_;
91     static void SetCrossUserMode(uint8_t priority, uint8_t crossMode,
92         std::pair<AccessCrossMode, int8_t> &mode);
93     static constexpr const char *DATA_SHARE_EXTENSION_META = "ohos.extension.dataShare";
94 };
95 } // namespace OHOS::DataShare
96 #endif // DISTRIBUTEDDATAMGR_PROFILE_CONFIG_H
97