• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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          * Get the singleton instance.
76          * */
77         static FrontendApiServer &Get();
78 
~FrontendApiServer()79         ~FrontendApiServer() {}
80 
81     private:
82         /** constructor, initialize apiMap_ and apiMapReverse_*/
83         FrontendApiServer();
84         /** handle result value/error code of old api call*/
85         void ApiMapPost(const std::string &oldApiName, ApiReplyInfo &out) const;
86         /** convert old api call to new api call*/
87         std::string ApiMapPre(ApiCallInfo &inModifier) const;
88         /** Command apiCall pre-processors before it's dispatched to target handler.*/
89         std::map<std::string, ApiInvokeHandler> commonPreprocessors_;
90         /** Registered api handlers.*/
91         std::map<std::string, ApiInvokeHandler> handlers_;
92         /** mapping classes of old API to classes of new API*/
93         std::map<std::string, std::string> old2NewApiMap_;
94         /** mapping classes of new API to classes of old API*/
95         std::map<std::string, std::string> new2OldApiMap_;
96     };
97 
98     /** Function serving external api-invocation request.*/
99     void ApiTransact(const ApiCallInfo& in, ApiReplyInfo& out);
100 }
101 
102 #endif