• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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,
42         XML_PARSE_NOBLANKS | XML_PARSE_NOERROR | XML_PARSE_NOWARNING);
43     if (!xmlDocPtr) {
44         RESSCHED_LOGE("%{public}s, xmlReadFile error!", __func__);
45         return false;
46     }
47     xmlNodePtr rootNodePtr = xmlDocGetRootElement(xmlDocPtr);
48     if (!rootNodePtr || !rootNodePtr->name ||
49         xmlStrcmp(rootNodePtr->name, reinterpret_cast<const xmlChar*>(XML_TAG_PLUGIN_LIST)) != 0) {
50         RESSCHED_LOGE("%{public}s, root element tag wrong!", __func__);
51         xmlFreeDoc(xmlDocPtr);
52         return false;
53     }
54 
55     std::list<PluginInfo> pluginInfoList;
56     xmlNodePtr currNodePtr = rootNodePtr->xmlChildrenNode;
57     for (; currNodePtr; currNodePtr = currNodePtr->next) {
58         if (IsInvalidNode(*currNodePtr)) {
59             continue;
60         }
61 
62         xmlChar *attrValue = nullptr;
63         if (xmlStrcmp(currNodePtr->name, reinterpret_cast<const xmlChar*>(XML_TAG_PLUGIN)) != 0) {
64             RESSCHED_LOGW("%{public}s, plugin (%{public}s) config wrong!", __func__, currNodePtr->name);
65             xmlFreeDoc(xmlDocPtr);
66             return false;
67         }
68         PluginInfo info;
69         attrValue = xmlGetProp(currNodePtr, reinterpret_cast<const xmlChar*>(XML_ATTR_LIB_PATH));
70         if (!attrValue) {
71             RESSCHED_LOGW("%{public}s, libPath null!", __func__);
72             continue;
73         }
74         info.libPath = reinterpret_cast<const char*>(attrValue);
75         xmlFree(attrValue);
76 
77         attrValue = xmlGetProp(currNodePtr, reinterpret_cast<const xmlChar*>(XML_ATTR_SWITCH));
78         if (attrValue) {
79             std::string value = reinterpret_cast<const char*>(attrValue);
80             if (value == "1") {
81                 info.switchOn = true;
82             }
83             xmlFree(attrValue);
84         }
85         pluginInfoList.emplace_back(info);
86     }
87     xmlFreeDoc(xmlDocPtr);
88     pluginInfoList_ = std::move(pluginInfoList);
89     return true;
90 }
91 
GetPluginSwitch()92 std::list<PluginInfo> PluginSwitch::GetPluginSwitch()
93 {
94     return pluginInfoList_;
95 }
96 } // namespace ResourceSchedule
97 } // namespace OHOS
98