1 /*
2 * Copyright (c) 2022 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 "plugin_switch.h"
17 #include "res_sched_log.h"
18
19 using namespace std;
20
21 namespace OHOS {
22 namespace ResourceSchedule {
23 namespace {
24 constexpr auto XML_TAG_PLUGIN_LIST = "pluginlist";
25 constexpr auto XML_TAG_PLUGIN = "plugin";
26 constexpr auto XML_ATTR_LIB_PATH = "libpath";
27 constexpr auto XML_ATTR_SWITCH = "switch";
28 }
29
IsInvalidNode(const xmlNode & currNode)30 bool PluginSwitch::IsInvalidNode(const xmlNode& currNode)
31 {
32 if (!currNode.name || currNode.type == XML_COMMENT_NODE) {
33 return true;
34 }
35 return false;
36 }
37
LoadFromConfigFile(const string & configFile)38 bool PluginSwitch::LoadFromConfigFile(const string& configFile)
39 {
40 // skip the empty string, else you will get empty node
41 xmlDocPtr xmlDocPtr = xmlReadFile(configFile.c_str(), nullptr, XML_PARSE_NOBLANKS);
42 if (!xmlDocPtr) {
43 RESSCHED_LOGE("PluginSwitch::LoadFromConfigFile xmlReadFile error!");
44 return false;
45 }
46 xmlNodePtr rootNodePtr = xmlDocGetRootElement(xmlDocPtr);
47 if (!rootNodePtr || !rootNodePtr->name ||
48 xmlStrcmp(rootNodePtr->name, reinterpret_cast<const xmlChar*>(XML_TAG_PLUGIN_LIST)) != 0) {
49 RESSCHED_LOGE("PluginSwitch::LoadFromConfigFile root element tag wrong!");
50 xmlFreeDoc(xmlDocPtr);
51 return false;
52 }
53
54 std::list<PluginInfo> pluginInfoList;
55 xmlNodePtr currNodePtr = rootNodePtr->xmlChildrenNode;
56 for (; currNodePtr; currNodePtr = currNodePtr->next) {
57 if (IsInvalidNode(*currNodePtr)) {
58 continue;
59 }
60
61 xmlChar *attrValue = nullptr;
62 if (xmlStrcmp(currNodePtr->name, reinterpret_cast<const xmlChar*>(XML_TAG_PLUGIN)) != 0) {
63 RESSCHED_LOGW("PluginSwitch::LoadFromConfigFile plugin (%{public}s) config wrong!", currNodePtr->name);
64 xmlFreeDoc(xmlDocPtr);
65 return false;
66 }
67 PluginInfo info;
68 attrValue = xmlGetProp(currNodePtr, reinterpret_cast<const xmlChar*>(XML_ATTR_LIB_PATH));
69 if (!attrValue) {
70 RESSCHED_LOGW("PluginSwitch::LoadFromConfigFile libPath null!");
71 continue;
72 }
73 info.libPath = reinterpret_cast<const char*>(attrValue);
74 xmlFree(attrValue);
75
76 attrValue = xmlGetProp(currNodePtr, reinterpret_cast<const xmlChar*>(XML_ATTR_SWITCH));
77 if (attrValue) {
78 std::string value = reinterpret_cast<const char*>(attrValue);
79 if (value == "1") {
80 info.switchOn = true;
81 }
82 xmlFree(attrValue);
83 }
84 pluginInfoList.emplace_back(info);
85 }
86 xmlFreeDoc(xmlDocPtr);
87 pluginInfoList_ = std::move(pluginInfoList);
88 return true;
89 }
90
GetPluginSwitch()91 std::list<PluginInfo> PluginSwitch::GetPluginSwitch()
92 {
93 return pluginInfoList_;
94 }
95 } // namespace ResourceSchedule
96 } // namespace OHOS