1 /* 2 * Copyright (c) 2021-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 WHITE_LIST_UTIL_H 17 #define WHITE_LIST_UTIL_H 18 19 #include <mutex> 20 #include <unordered_set> 21 #include <fstream> 22 #include <sstream> 23 24 #include "constants_dinput.h" 25 26 namespace OHOS { 27 namespace DistributedHardware { 28 namespace DistributedInput { 29 using TYPE_KEY_CODE_VEC = std::vector<int32_t>; 30 using TYPE_COMBINATION_KEY_VEC = std::vector<TYPE_KEY_CODE_VEC>; 31 using TYPE_WHITE_LIST_VEC = std::vector<TYPE_COMBINATION_KEY_VEC>; 32 using TYPE_DEVICE_WHITE_LIST_MAP = std::map<std::string, TYPE_WHITE_LIST_VEC>; 33 34 struct WhiteListItemHash { 35 std::string hash; 36 37 // the keys num 38 int32_t len; 39 WhiteListItemHashWhiteListItemHash40 WhiteListItemHash() : hash(""), len(0) {} 41 WhiteListItemHashWhiteListItemHash42 WhiteListItemHash(std::string &hash, int32_t len) : hash(hash), len(len) {} 43 }; 44 45 class WhiteListUtil { 46 public: 47 static WhiteListUtil &GetInstance(void); 48 int32_t SyncWhiteList(const std::string &deviceId, const TYPE_WHITE_LIST_VEC &vecWhiteList); 49 int32_t ClearWhiteList(const std::string &deviceId); 50 int32_t ClearWhiteList(void); 51 int32_t GetWhiteList(const std::string &deviceId, TYPE_WHITE_LIST_VEC &vecWhiteList); 52 bool GetWhiteListCfgFile(std::ifstream &ifs); 53 /* 54 * check is event in white list of deviceId 55 * 56 * Return: 57 * true: event in white list of this device 58 * false: event not in white list of this device, or white list of this device not exist 59 */ 60 bool IsNeedFilterOut(const std::string &deviceId, const BusinessEvent &event); 61 private: 62 int32_t Init(); 63 WhiteListUtil(); 64 ~WhiteListUtil(); 65 WhiteListUtil(const WhiteListUtil &other) = delete; 66 const WhiteListUtil &operator=(const WhiteListUtil &other) = delete; 67 void ReadLineDataStepOne(std::string &column, TYPE_KEY_CODE_VEC &vecKeyCode, 68 TYPE_COMBINATION_KEY_VEC &vecCombinationKey) const; 69 void GetCombKeysHash(TYPE_COMBINATION_KEY_VEC combKeys, std::unordered_set<std::string> &targetSet); 70 void GetAllComb(TYPE_COMBINATION_KEY_VEC vecs, WhiteListItemHash hash, 71 int32_t targetLen, std::unordered_set<std::string> &hashSets); 72 std::string GetBusinessEventHash(const BusinessEvent &event); 73 bool IsValidLine(const std::string &line) const; 74 bool CheckIsNumber(const std::string &str) const; 75 void SplitCombinationKey(std::string &line, TYPE_KEY_CODE_VEC &vecKeyCode, 76 TYPE_COMBINATION_KEY_VEC &vecCombinationKey) const; 77 private: 78 TYPE_DEVICE_WHITE_LIST_MAP mapDeviceWhiteList_; 79 std::map<std::string, std::unordered_set<std::string>> combKeysHashMap_; 80 std::mutex mutex_; 81 }; 82 } // namespace DistributedInput 83 } // namespace DistributedHardware 84 } // namespace OHOS 85 86 #endif // WHITE_LIST_UTIL_H 87