1 /* 2 * Copyright (c) 2023-2024 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_ABILITY_RUNTIME_EXTENSION_CONFIG_H 17 #define OHOS_ABILITY_RUNTIME_EXTENSION_CONFIG_H 18 19 #include <map> 20 #include <mutex> 21 #include <nlohmann/json.hpp> 22 #include <optional> 23 #include <unordered_map> 24 #include <unordered_set> 25 26 #include "extension_ability_info.h" 27 #include "singleton.h" 28 29 namespace OHOS { 30 namespace AAFwk { 31 constexpr static int32_t DEFAULT_EXTENSION_AUTO_DISCONNECT_TIME = -1; 32 constexpr static bool EXTENSION_NETWORK_ENABLE_FLAG_DEFAULT = true; 33 constexpr static bool EXTENSION_SA_ENABLE_FLAG_DEFAULT = true; 34 constexpr static bool EXTENSION_THIRD_PARTY_APP_ENABLE_FLAG_DEFAULT = true; 35 constexpr static bool EXTENSION_START_SERVICE_ENABLE_FLAG_DEFAULT = true; 36 37 struct AbilityAccessItem { 38 std::optional<bool> thirdPartyAppAccessFlag = std::nullopt; 39 std::optional<bool> serviceAccessFlag = std::nullopt; 40 std::optional<bool> defaultAccessFlag = std::nullopt; 41 std::unordered_set<std::string> blockList; 42 std::unordered_set<std::string> allowList; 43 }; 44 45 struct ExtensionConfigItem { 46 bool networkEnableFlag = EXTENSION_NETWORK_ENABLE_FLAG_DEFAULT; 47 bool saEnableFlag = EXTENSION_SA_ENABLE_FLAG_DEFAULT; 48 bool thirdPartyAppEnableFlag = EXTENSION_THIRD_PARTY_APP_ENABLE_FLAG_DEFAULT; 49 bool serviceEnableFlag = EXTENSION_START_SERVICE_ENABLE_FLAG_DEFAULT; 50 int32_t extensionAutoDisconnectTime = DEFAULT_EXTENSION_AUTO_DISCONNECT_TIME; 51 std::unordered_set<std::string> serviceBlockedList; 52 AbilityAccessItem abilityAccess; 53 bool hasAbilityAccess = false; 54 bool screenUnlockIntercept = false; 55 bool screenUnlockInterceptExcludeSystemApp = false; 56 }; 57 58 class ExtensionConfig : public DelayedSingleton<ExtensionConfig> { 59 public: 60 explicit ExtensionConfig() = default; 61 virtual ~ExtensionConfig() = default; 62 void LoadExtensionConfiguration(); 63 int32_t GetExtensionAutoDisconnectTime(const std::string &extensionTypeName); 64 bool IsExtensionStartThirdPartyAppEnable(const std::string &extensionTypeName); 65 bool IsExtensionStartServiceEnable(const std::string &extensionTypeName, const std::string &targetUri); 66 bool HasAbilityAccess(const std::string &extensionTypeName); 67 bool HasThridPartyAppAccessFlag(const std::string &extensionTypeName); 68 bool HasServiceAccessFlag(const std::string &extensionTypeName); 69 bool HasDefaultAccessFlag(const std::string &extensionTypeName); 70 bool IsExtensionStartThirdPartyAppEnableNew(const std::string &extensionTypeName, const std::string &targetUri); 71 bool IsExtensionStartServiceEnableNew(const std::string &extensionTypeName, const std::string &targetUri); 72 bool IsExtensionStartDefaultEnable(const std::string &extensionTypeName, const std::string &targetUri); 73 bool IsExtensionNetworkEnable(const std::string &extensionTypeName); 74 bool IsExtensionSAEnable(const std::string &extensionTypeName); 75 bool IsScreenUnlockIntercept(const std::string &extensionTypeName, bool isSystemApp); 76 private: 77 void LoadExtensionConfig(const nlohmann::json &object); 78 bool ReadFileInfoJson(const std::string &filePath, nlohmann::json &jsonBuf); 79 80 std::string GetExtensionConfigPath() const; 81 void LoadExtensionAutoDisconnectTime(const nlohmann::json &object, const std::string &extensionTypeName); 82 void LoadExtensionThirdPartyAppBlockedList(const nlohmann::json &object, std::string extensionTypeName); 83 void LoadExtensionServiceBlockedList(const nlohmann::json &object, std::string extensionTypeNameobject); 84 void LoadExtensionNetworkEnable(const nlohmann::json &object, const std::string &extensionTypeName); 85 void LoadExtensionSAEnable(const nlohmann::json &object, const std::string &extensionTypeName); 86 bool LoadExtensionAbilityAccess(const nlohmann::json &object, const std::string &extensionTypeName); 87 void LoadExtensionAllowOrBlockedList(const nlohmann::json &object, const std::string &key, 88 std::unordered_set<std::string> &list); 89 void LoadScreenUnlockIntercept(const nlohmann::json &object, const std::string &extensionTypeName); 90 91 std::optional<bool> GetSingleAccessFlag(const std::string &extensionTypeName, 92 std::function<std::optional<bool>(const AbilityAccessItem&)> getAccessFlag); 93 bool IsExtensionAbilityAccessEnable(const std::string &extensionTypeName, const std::string &targetUri, 94 std::function<std::optional<bool>(const AbilityAccessItem&)> getAccessFlag); 95 bool FindTargetUriInList(const AppExecFwk::ElementName &targetElementName, std::unordered_set<std::string> &list); 96 std::string FormatAccessFlag(const std::optional<bool> &flag); 97 bool CheckExtensionUriValid(const std::string &uri); 98 99 std::unordered_map<std::string, ExtensionConfigItem> configMap_; 100 std::mutex configMapMutex_; 101 }; 102 } // OHOS 103 } // AAFwk 104 105 #endif // OHOS_ABILITY_RUNTIME_EXTENSION_CONFIG_H