• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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_utils.h"
26 #include "frontend_api_defines.h"
27 #include "nlohmann/json.hpp"
28 
29 namespace OHOS::perftest {
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          * Register api invocation handler.
44          **/
45         void AddHandler(std::string_view apiId, ApiInvokeHandler handler);
46 
47         /**
48          * Check if any handler is registered for the target api.
49          */
50         bool HasHandlerFor(std::string_view apiId) const;
51 
52         /**
53          * Remove api invocation  handler.
54          **/
55         void RemoveHandler(std::string_view apiId);
56 
57         /**
58          * Add command pre-processor.
59          **/
60         void AddCommonPreprocessor(std::string_view name, ApiInvokeHandler processor);
61 
62          /**
63          * Remove command pre-processor.
64          **/
65         void RemoveCommonPreprocessor(std::string_view name);
66 
67         /**
68          * Handle api invocation request.
69          *
70          * */
71         void Call(const ApiCallInfo& in, ApiReplyInfo& out) const;
72 
73         /**
74          * Set handler to handle api callback from server.
75          *
76          * */
77         void SetCallbackHandler(ApiInvokeHandler handler);
78 
79         /**
80          * Handle api callback.
81          *
82          * */
83         void Callback(const ApiCallInfo& in, ApiReplyInfo& out) const;
84 
85         /**
86          * Get the singleton instance.
87          * */
88         static FrontendApiServer &Get();
89 
~FrontendApiServer()90         ~FrontendApiServer() {}
91 
92     private:
93         /** constructor, initialize apiMap_ and apiMapReverse_*/
FrontendApiServer()94         FrontendApiServer() {};
95         /** Command apiCall pre-processors before it's dispatched to target handler.*/
96         std::map<std::string, ApiInvokeHandler> commonPreprocessors_;
97         /** Registered api handlers.*/
98         std::map<std::string, ApiInvokeHandler> handlers_;
99         // function used for callback
100         ApiInvokeHandler callbackHandler_ = nullptr;
101     };
102 } // namespace OHOS::perftest
103 
104 #endif