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 "dataproxy_handle_common.h" 26 #include "resource_manager.h" 27 #include "serializable/serializable.h" 28 29 namespace OHOS::DataShare { 30 using namespace OHOS::Global::Resource; 31 struct Config final : public DistributedData::Serializable { 32 std::string uri = "*"; 33 int crossUserMode = 0; 34 std::string writePermission = ""; 35 std::string readPermission = ""; 36 bool Marshal(json &node) const override; 37 bool Unmarshal(const json &node) override; 38 }; 39 40 struct LaunchInfo final : public DistributedData::Serializable { 41 std::string storeId = ""; 42 std::vector<std::string> tableNames; 43 bool Marshal(json &node) const override; 44 bool Unmarshal(const json &node) override; 45 }; 46 47 // List of applications that can access shared data 48 struct AllowList final : public DistributedData::Serializable { 49 std::string appIdentifier; 50 bool onlyMain = false; 51 bool Marshal(json &node) const override; 52 bool Unmarshal(const json &node) override; 53 }; 54 55 struct ProfileInfo : public DistributedData::Serializable { 56 std::vector<Config> tableConfig; 57 bool isSilentProxyEnable = true; 58 std::string storeName; 59 std::string tableName; 60 std::string scope = "module"; 61 std::string type = "rdb"; 62 std::string backup; 63 std::string extUri; 64 std::vector<LaunchInfo> launchInfos; 65 std::vector<AllowList> allowLists; 66 bool storeMetaDataFromUri = false; 67 bool launchForCleanData = false; 68 bool Marshal(json &node) const override; 69 bool Unmarshal(const json &node) override; 70 }; 71 72 struct SerialDataShareProxyData : public DistributedData::Serializable { 73 SerialDataShareProxyData() = default; SerialDataShareProxyDataSerialDataShareProxyData74 SerialDataShareProxyData(const std::string &uri, const DataProxyValue &value, 75 const std::vector<std::string> &allowList) 76 : uri(uri), value(value), allowList(allowList) {} 77 virtual ~SerialDataShareProxyData() = default; 78 std::string uri; 79 DataProxyValue value; 80 std::vector<std::string> allowList; 81 bool Marshal(json &node) const override; 82 bool Unmarshal(const json &node) override; 83 }; 84 85 struct ProxyDataProfileInfo : public DistributedData::Serializable { 86 std::vector<SerialDataShareProxyData> dataShareProxyDatas; 87 bool Marshal(json &node) const override; 88 bool Unmarshal(const json &node) override; 89 }; 90 91 enum AccessCrossMode : uint8_t { 92 USER_UNDEFINED, 93 USER_SHARED, 94 USER_SINGLE, 95 USER_MAX, 96 }; 97 98 class DataShareProfileConfig { 99 public: 100 constexpr static int8_t TABLE_MATCH_PRIORITY = 3; 101 constexpr static int8_t STORE_MATCH_PRIORITY = 2; 102 constexpr static int8_t COMMON_MATCH_PRIORITY = 1; 103 constexpr static int8_t UNDEFINED_PRIORITY = -1; 104 105 static bool GetProfileInfo(const std::string &calledBundleName, int32_t currentUserId, 106 std::map<std::string, ProfileInfo> &profileInfos); 107 static std::pair<int, ProfileInfo> GetDataProperties(const std::vector<AppExecFwk::Metadata> &metadata, 108 const std::string &resPath, const std::string &hapPath, const std::string &name); 109 static std::pair<int, std::vector<SerialDataShareProxyData>> GetCrossAppSharedConfig(const std::string &resource, 110 const std::string &resPath, const std::string &hapPath); 111 static AccessCrossMode GetAccessCrossMode(const ProfileInfo &profileInfo, 112 const std::string &tableUri, const std::string &storeUri); 113 private: 114 static std::shared_ptr<ResourceManager> InitResMgr(const std::string &resourcePath); 115 static std::string GetProfileInfoByMetadata(const std::vector<AppExecFwk::Metadata> &metadata, 116 const std::string &resourcePath, const std::string &hapPath, const std::string &name); 117 static std::string GetResFromResMgr(const std::string &resName, ResourceManager &resMgr, 118 const std::string &hapPath); 119 static std::string ReadProfile(const std::string &resPath); 120 static bool IsFileExisted(const std::string &filePath); 121 static std::mutex infosMutex_; 122 static void SetCrossUserMode(uint8_t priority, uint8_t crossMode, 123 std::pair<AccessCrossMode, int8_t> &mode); 124 static constexpr const char *DATA_SHARE_EXTENSION_META = "ohos.extension.dataShare"; 125 }; 126 } // namespace OHOS::DataShare 127 #endif // DISTRIBUTEDDATAMGR_PROFILE_CONFIG_H 128