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