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 #include "extension_config_mgr.h"
17
18 #include <fstream>
19 #include <nlohmann/json.hpp>
20
21 #include "app_module_checker.h"
22 #include "hilog_tag_wrapper.h"
23
24 namespace OHOS::AbilityRuntime {
25 namespace {
26 constexpr char EXTENSION_BLOCKLIST_FILE_PATH[] = "/system/etc/extension_blocklist_config.json";
27 }
28
Init()29 void ExtensionConfigMgr::Init()
30 {
31 // clear cached data
32 blocklistConfig_.clear();
33 extensionBlocklist_.clear();
34
35 // read blocklist from extension_blocklist_config.json
36 std::ifstream inFile;
37 inFile.open(EXTENSION_BLOCKLIST_FILE_PATH, std::ios::in);
38 if (!inFile.is_open()) {
39 TAG_LOGE(AAFwkTag::EXT, "read extension config error");
40 return;
41 }
42 nlohmann::json extensionConfig;
43 inFile >> extensionConfig;
44 if (extensionConfig.is_discarded()) {
45 TAG_LOGE(AAFwkTag::EXT, "extension config json discarded error");
46 inFile.close();
47 return;
48 }
49 if (!extensionConfig.contains(ExtensionConfigItem::ITEM_NAME_BLOCKLIST)) {
50 TAG_LOGE(AAFwkTag::EXT, "extension config file have no blocklist node");
51 inFile.close();
52 return;
53 }
54 auto blackList = extensionConfig.at(ExtensionConfigItem::ITEM_NAME_BLOCKLIST);
55 std::unordered_set<std::string> currentBlockList;
56 for (const auto& item : blackList.items()) {
57 if (!blackList[item.key()].is_array()) {
58 continue;
59 }
60 for (const auto& value : blackList[item.key()]) {
61 currentBlockList.emplace(value.get<std::string>());
62 }
63 blocklistConfig_.emplace(item.key(), std::move(currentBlockList));
64 currentBlockList.clear();
65 }
66 inFile.close();
67 }
68
AddBlockListItem(const std::string & name,int32_t type)69 void ExtensionConfigMgr::AddBlockListItem(const std::string& name, int32_t type)
70 {
71 TAG_LOGD(AAFwkTag::EXT, "name: %{public}s, type: %{public}d", name.c_str(), type);
72 auto iter = blocklistConfig_.find(name);
73 if (iter == blocklistConfig_.end()) {
74 TAG_LOGD(AAFwkTag::EXT, "Extension name: %{public}s not exist", name.c_str());
75 return;
76 }
77 extensionBlocklist_.emplace(type, iter->second);
78 }
79
UpdateRuntimeModuleChecker(const std::unique_ptr<AbilityRuntime::Runtime> & runtime)80 void ExtensionConfigMgr::UpdateRuntimeModuleChecker(const std::unique_ptr<AbilityRuntime::Runtime> &runtime)
81 {
82 if (!runtime) {
83 TAG_LOGE(AAFwkTag::EXT, "null runtime");
84 return;
85 }
86 TAG_LOGD(AAFwkTag::EXT, "extensionType_: %{public}d", extensionType_);
87 auto moduleChecker = std::make_shared<AppModuleChecker>(extensionType_, std::move(extensionBlocklist_));
88 runtime->SetModuleLoadChecker(moduleChecker);
89 }
90 }