• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2022-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 PANDA_TOOLING_INSPECTOR_CONNECTION_SERVER_H
17 #define PANDA_TOOLING_INSPECTOR_CONNECTION_SERVER_H
18 
19 #include <functional>
20 
21 #include "json_serialization/serializable.h"
22 #include "libpandabase/utils/expected.h"
23 #include "libpandabase/utils/json_builder.h"
24 
25 #include "connection/event_loop.h"
26 #include "json_serialization/jrpc_error.h"
27 
28 namespace ark {
29 class JsonObject;
30 class JsonObjectBuilder;
31 }  // namespace ark
32 
33 namespace ark::tooling::inspector {
34 class Server : public virtual EventLoop {  // NOLINT(fuchsia-virtual-inheritance)
35 public:
36     using MethodResponse = Expected<std::unique_ptr<JsonSerializable>, JRPCError>;
37     using Handler = std::function<MethodResponse(const std::string &, const JsonObject &params)>;
38 
39 public:
40     virtual void OnValidate(std::function<void()> &&handler) = 0;
41     virtual void OnOpen(std::function<void()> &&handler) = 0;
42     virtual void OnFail(std::function<void()> &&handler) = 0;
43 
44     virtual void Call(const std::string &sessionId, const char *method,
45                       std::function<void(JsonObjectBuilder &)> &&params) = 0;
46 
Call(const char * method,std::function<void (JsonObjectBuilder &)> && params)47     void Call(const char *method, std::function<void(JsonObjectBuilder &)> &&params)
48     {
49         Call({}, method, std::move(params));
50     }
51 
Call(const std::string & sessionId,const char * method)52     void Call(const std::string &sessionId, const char *method)
53     {
54         Call(sessionId, method, [](JsonObjectBuilder & /* builder */) {});
55     }
56 
Call(const char * method)57     void Call(const char *method)
58     {
59         Call({}, method, [](JsonObjectBuilder & /* builder */) {});
60     }
61 
62     virtual void OnCall(const char *method, Handler &&handler) = 0;
63 };
64 }  // namespace ark::tooling::inspector
65 
66 #endif  // PANDA_TOOLING_INSPECTOR_CONNECTION_SERVER_H
67