• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 ECMASCRIPT_OHOS_ENABLE_AOT_LIST_HELPER_H
17 #define ECMASCRIPT_OHOS_ENABLE_AOT_LIST_HELPER_H
18 
19 #include <fstream>
20 #include <memory>
21 #include <set>
22 #include <string>
23 #include <vector>
24 
25 #include "ecmascript/log_wrapper.h"
26 #include "ecmascript/platform/file.h"
27 #include "macros.h"
28 namespace panda::ecmascript::ohos {
29 class EnableAotListHelper {
30 public:
GetInstance()31     static std::shared_ptr<EnableAotListHelper> GetInstance()
32     {
33         static auto helper = std::make_shared<EnableAotListHelper>(ENABLE_LIST_NAME, DISABLE_LIST_NAME);
34         return helper;
35     }
36 
EnableAotListHelper(const std::string & enableListName,const std::string & disableListName)37     explicit EnableAotListHelper(const std::string &enableListName, const std::string &disableListName)
38     {
39         ReadEnableAotList(enableListName);
40         ReadEnableAotList(disableListName);
41     }
42 
43     EnableAotListHelper() = default;
44     ~EnableAotListHelper() = default;
45 
IsEnableList(const std::string & candidate)46     bool IsEnableList(const std::string &candidate)
47     {
48         return enableList_.find(candidate) != enableList_.end();
49     }
50 
IsEnableList(const std::string & bundleName,const std::string & moduleName)51     bool IsEnableList(const std::string &bundleName, const std::string &moduleName)
52     {
53         if (IsEnableList(bundleName)) {
54             return true;
55         }
56         return IsEnableList(bundleName + ":" + moduleName);
57     }
58 
IsDisableBlackList(const std::string & candidate)59     bool IsDisableBlackList(const std::string &candidate)
60     {
61         return disableList_.find(candidate) != disableList_.end();
62     }
63 
IsDisableBlackList(const std::string & bundleName,const std::string & moduleName)64     bool IsDisableBlackList(const std::string &bundleName, const std::string &moduleName)
65     {
66         if (IsDisableBlackList(bundleName)) {
67             return true;
68         }
69         return IsDisableBlackList(bundleName + ":" + moduleName);
70     }
71 
AddEnableListEntry(const std::string & entry)72     void AddEnableListEntry(const std::string &entry)
73     {
74         enableList_.insert(entry);
75     }
76 
AddDisableListEntry(const std::string & entry)77     void AddDisableListEntry(const std::string &entry)
78     {
79         disableList_.insert(entry);
80     }
81 
Clear()82     void Clear()
83     {
84         disableList_.clear();
85         enableList_.clear();
86     }
87 
88 private:
89     NO_COPY_SEMANTIC(EnableAotListHelper);
90     NO_MOVE_SEMANTIC(EnableAotListHelper);
Trim(std::string & data)91     static void Trim(std::string &data)
92     {
93         if (data.empty()) {
94             return;
95         }
96         data.erase(0, data.find_first_not_of(' '));
97         data.erase(data.find_last_not_of(' ') + 1);
98     }
99 
ReadEnableAotList(const std::string & aotListName)100     void ReadEnableAotList(const std::string &aotListName)
101     {
102         if (!panda::ecmascript::FileExist(aotListName.c_str())) {
103             LOG_ECMA(INFO) << "bundle enable list not exist and will pass by all. file: " << aotListName;
104             return;
105         }
106 
107         std::ifstream inputFile(aotListName);
108 
109         if (!inputFile.is_open()) {
110             LOG_ECMA(ERROR) << "bundle enable list open failed! file: " << aotListName << ", errno: " << errno;
111             return;
112         }
113 
114         std::string line;
115         while (getline(inputFile, line)) {
116             auto appName = line;
117             Trim(appName);
118             // skip empty line
119             if (appName.empty()) {
120                 continue;
121             }
122             // skip comment line
123             if (appName.at(0) == '#') {
124                 continue;
125             }
126             if (aotListName == ENABLE_LIST_NAME) {
127                 AddEnableListEntry(appName);
128             }
129             if (aotListName == DISABLE_LIST_NAME) {
130                 AddDisableListEntry(appName);
131             }
132         }
133     }
134     std::set<std::string> enableList_ {};
135     std::set<std::string> disableList_ {};
136     static const std::string ENABLE_LIST_NAME;
137     static const std::string DISABLE_LIST_NAME;
138 };
139 }  // namespace panda::ecmascript::ohos
140 #endif  // ECMASCRIPT_OHOS_ENABLE_AOT_LIST_HELPER_H
141 
142