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 #include "extension_config_mgr.h"
17
18 #include <fstream>
19 #include <nlohmann/json.hpp>
20
21 #include "hilog_wrapper.h"
22
23 namespace OHOS::AbilityRuntime {
24 namespace {
25 constexpr char EXTENSION_BLOCKLIST_FILE_PATH[] = "/system/etc/extension_blocklist_config.json";
26 constexpr char BACK_SLASH[] = "/";
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 HILOG_ERROR("read extension config error");
40 return;
41 }
42 nlohmann::json extensionConfig;
43 inFile >> extensionConfig;
44 if (extensionConfig.is_discarded()) {
45 HILOG_ERROR("extension config json discarded error");
46 inFile.close();
47 return;
48 }
49 if (!extensionConfig.contains(ExtensionConfigItem::ITEM_NAME_BLOCKLIST)) {
50 HILOG_ERROR("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
UpdateBundleExtensionInfo(NativeEngine & engine,AppExecFwk::BundleInfo & bundleInfo)69 void ExtensionConfigMgr::UpdateBundleExtensionInfo(NativeEngine& engine, AppExecFwk::BundleInfo& bundleInfo)
70 {
71 std::unordered_map<std::string, int32_t> extensionInfo;
72 for (const auto &info : bundleInfo.extensionInfos) {
73 std::string path = info.moduleName + BACK_SLASH + info.srcEntrance;
74 extensionInfo.emplace(path, static_cast<int32_t>(info.type));
75 }
76 engine.SetExtensionInfos(std::move(extensionInfo));
77 }
78
AddBlockListItem(const std::string & name,int32_t type)79 void ExtensionConfigMgr::AddBlockListItem(const std::string& name, int32_t type)
80 {
81 HILOG_INFO("AddBlockListItem name = %{public}s, type = %{public}d", name.c_str(), type);
82 auto iter = blocklistConfig_.find(name);
83 if (iter == blocklistConfig_.end()) {
84 HILOG_ERROR("Extension name = %{public}s, not exist in blocklist config", name.c_str());
85 return;
86 }
87 extensionBlocklist_.emplace(type, iter->second);
88 }
89
UpdateBlockListToEngine(NativeEngine & engine)90 void ExtensionConfigMgr::UpdateBlockListToEngine(NativeEngine& engine)
91 {
92 engine.SetModuleBlocklist(std::forward<decltype(extensionBlocklist_)>(extensionBlocklist_));
93 }
94 }