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 FRONTEND_API_HANDLER_H 17 #define FRONTEND_API_HANDLER_H 18 19 #include <memory> 20 #include <map> 21 #include <string> 22 #include <set> 23 #include <functional> 24 #include <list> 25 #include "common_utilities_hpp.h" 26 #include "frontend_api_defines.h" 27 #include "json.hpp" 28 29 namespace OHOS::uitest { 30 class BackendClass { 31 public: 32 virtual const FrontEndClassDef& GetFrontendClassDef() const = 0; 33 virtual ~BackendClass() = default; 34 }; 35 36 /**Prototype of function that handles ExternAPI invocation request.*/ 37 using ApiInvokeHandler = std::function<void(const ApiCallInfo& in, ApiReplyInfo& out)>; 38 39 /**Server that accepts and handles api invocation request.*/ 40 class FrontendApiServer { 41 public: 42 43 /** 44 * Register api invocation handler. 45 **/ 46 void AddHandler(std::string_view apiId, ApiInvokeHandler handler); 47 48 /** 49 * Check if any handler is registered for the target api. 50 */ 51 bool HasHandlerFor(std::string_view apiId) const; 52 53 /** 54 * Remove api invocation handler. 55 **/ 56 void RemoveHandler(std::string_view apiId); 57 58 /** 59 * Add command pre-processor. 60 **/ 61 void AddCommonPreprocessor(std::string_view name, ApiInvokeHandler processor); 62 63 /** 64 * Remove command pre-processor. 65 **/ 66 void RemoveCommonPreprocessor(std::string_view name); 67 68 /** 69 * Handle api invocation request. 70 * 71 * */ 72 void Call(const ApiCallInfo& in, ApiReplyInfo& out) const; 73 74 /** 75 * Set handler to handle api callback from server. 76 * 77 * */ 78 void SetCallbackHandler(ApiInvokeHandler handler); 79 80 /** 81 * Handle api callback. 82 * 83 * */ 84 void Callback(const ApiCallInfo& in, ApiReplyInfo& out) const; 85 86 /** 87 * Get the singleton instance. 88 * */ 89 static FrontendApiServer &Get(); 90 ~FrontendApiServer()91 ~FrontendApiServer() {} 92 93 private: 94 /** constructor, initialize apiMap_ and apiMapReverse_*/ 95 FrontendApiServer(); 96 /** handle result value/error code of old api call*/ 97 void ApiMapPost(const std::string &oldApiName, ApiReplyInfo &out) const; 98 /** convert old api call to new api call*/ 99 std::string ApiMapPre(ApiCallInfo &inModifier) const; 100 /** Command apiCall pre-processors before it's dispatched to target handler.*/ 101 std::map<std::string, ApiInvokeHandler> commonPreprocessors_; 102 /** Registered api handlers.*/ 103 std::map<std::string, ApiInvokeHandler> handlers_; 104 /** mapping classes of old API to classes of new API*/ 105 std::map<std::string, std::string> old2NewApiMap_; 106 /** mapping classes of new API to classes of old API*/ 107 std::map<std::string, std::string> new2OldApiMap_; 108 // function used for callback 109 ApiInvokeHandler callbackHandler_ = nullptr; 110 }; 111 } 112 113 #endif