• 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_ENDPOINT_BASE_H
17 #define PANDA_TOOLING_INSPECTOR_CONNECTION_ENDPOINT_BASE_H
18 
19 #include <functional>
20 #include <optional>
21 #include <string>
22 #include <type_traits>
23 #include <unordered_map>
24 #include <utility>
25 
26 #include "utils/json_builder.h"
27 #include "utils/logger.h"
28 
29 #include "json_serialization/jrpc_error.h"
30 
31 namespace ark {
32 class JsonObject;
33 }  // namespace ark
34 
35 namespace ark::tooling::inspector {
36 enum class InspectorErrorCode {
37     PARSE_ERROR = -32700,
38     INTERNAL_ERROR = -32603,
39     INVALID_PARAMS = -32602,
40     METHOD_NOT_FOUND = -32601,
41     INVALID_REQUEST = -32600,
42     SESSION_NOT_FOUND = -32001,
43     SERVER_ERROR = -32000,
44 };
45 
46 // Base class implementation of JSON-RPC endpoint handling the Inspector protocol.
47 class EndpointBase {
48 public:
49     using Id = double;
50 
51 private:
52     using MethodHandler = std::function<void(const std::string &, std::optional<Id>, const JsonObject &)>;
53     using ResultHandler = std::function<void(const JsonObject &)>;
54 
55 public:
56     void OnCall(const char *method, MethodHandler &&handler);
57 
58 protected:
59     void HandleMessage(const std::string &message);
60 
OnResult(Id id,ResultHandler && handler)61     void OnResult(Id id, ResultHandler &&handler)
62     {
63         resultHandlers_[id] = std::move(handler);
64     }
65 
66     void Call(
67         const std::string &sessionId, std::optional<Id> id, const char *method,
68         std::function<void(JsonObjectBuilder &)> &&params = [](JsonObjectBuilder & /* builder */) {});
69 
70     template <typename Result>
Reply(const std::string & sessionId,Id id,Result && result)71     void Reply(const std::string &sessionId, Id id, Result &&result)
72     {
73         Send([&sessionId, id, &result](JsonObjectBuilder &reply) {
74             reply.AddProperty("id", id);
75             reply.AddProperty("result", std::forward<Result>(result));
76 
77             if (!sessionId.empty()) {
78                 reply.AddProperty("sessionId", sessionId);
79             }
80         });
81     }
82 
ReplyError(const std::string & sessionId,Id id,JRPCError && error)83     void ReplyError(const std::string &sessionId, Id id, JRPCError &&error)
84     {
85         Send([&sessionId, id, &error](JsonObjectBuilder &reply) {
86             reply.AddProperty("id", id);
87             reply.AddProperty("error", std::move(error));
88 
89             if (!sessionId.empty()) {
90                 reply.AddProperty("sessionId", sessionId);
91             }
92         });
93     }
94 
95 private:
96     /// Send JSON message.
97     virtual void SendMessage(const std::string &message) = 0;
98 
99     /// Send JSON message created with the provided build function.
100     template <typename BuildFunction>
Send(BuildFunction && build)101     void Send(BuildFunction &&build)
102     {
103         JsonObjectBuilder builder;
104         build(builder);
105         auto message = std::move(builder).Build();
106         LOG(INFO, DEBUGGER) << "Sending " << message;
107         SendMessage(message);
108     }
109 
110     void HandleUnsupportedMethod(std::optional<double> optId, const std::string &method);
111 
112     os::memory::Mutex methodHandlersMutex_;
113     std::unordered_map<std::string, MethodHandler> methodHandlers_ GUARDED_BY(methodHandlersMutex_);
114     std::unordered_map<Id, ResultHandler> resultHandlers_;
115 };
116 }  // namespace ark::tooling::inspector
117 
118 #endif  // PANDA_TOOLING_INSPECTOR_CONNECTION_ENDPOINT_BASE_H
119