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 INJECTION_EVENT_DISPATCH_H 17 #define INJECTION_EVENT_DISPATCH_H 18 19 #include "manage_inject_device.h" 20 #include "injection_tools_help_func.h" 21 22 namespace OHOS { 23 namespace MMI { 24 using InjectFunction = std::function<int32_t()>; 25 26 struct InjectFunctionMap { 27 std::string id; 28 InjectFunction fun; 29 }; 30 31 class InjectionEventDispatch { 32 public: 33 InjectionEventDispatch() = default; 34 ~InjectionEventDispatch() = default; 35 DISALLOW_COPY_AND_MOVE(InjectionEventDispatch); 36 37 void Init(); 38 void InitManageFunction(); 39 void Run(); 40 int32_t OnSendEvent(); 41 int32_t OnJson(); 42 int32_t OnHelp(); 43 int32_t ExecuteFunction(std::string funId); 44 std::string GetFunId() const; 45 bool VerifyArgvs(const int32_t &argc, const std::vector<std::string> &argv); RegisterInjectEvent(InjectFunctionMap & msg)46 bool RegisterInjectEvent(InjectFunctionMap &msg) 47 { 48 auto it = injectFuns_.find(msg.id); 49 if (it != injectFuns_.end()) { 50 return false; 51 } 52 injectFuns_[msg.id] = msg.fun; 53 return true; 54 } 55 GetFun(std::string id)56 InjectFunction* GetFun(std::string id) 57 { 58 auto it = injectFuns_.find(id); 59 if (it == injectFuns_.end()) { 60 return nullptr; 61 } 62 return &it->second; 63 } 64 private: 65 std::string funId_ { "" }; 66 int32_t argvNum_ { 0 }; 67 ManageInjectDevice manageInjectDevice_; 68 std::vector<std::string> injectArgvs_; 69 std::map<std::string, InjectFunction> injectFuns_; 70 bool CheckType(const std::string &inputType); 71 bool CheckCode(const std::string &inputCode); 72 bool CheckValue(const std::string &inputValue); 73 bool CheckEventValue(const std::string &inputType, const std::string &inputCode, 74 const std::string &inputValue); 75 }; 76 } // namespace MMI 77 } // namespace OHOS 78 #endif // INJECTION_EVENT_DISPATCH_H