• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2022-2024 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 #include "connection/endpoint_base.h"
17 
18 #include <optional>
19 
20 #include "utils/json_builder.h"
21 #include "utils/json_parser.h"
22 #include "utils/logger.h"
23 
24 namespace ark::tooling::inspector {
OnCall(const char * method,EndpointBase::MethodHandler && handler)25 void EndpointBase::OnCall(const char *method, EndpointBase::MethodHandler &&handler)
26 {
27     os::memory::LockHolder lock(methodHandlersMutex_);
28     methodHandlers_[method] = std::move(handler);
29 }
30 
HandleMessage(const std::string & message)31 void EndpointBase::HandleMessage(const std::string &message)
32 {
33     JsonObject request(message);
34     if (!request.IsValid()) {
35         LOG(INFO, DEBUGGER) << "Invalid request: " << message;
36         return;
37     }
38 
39     LOG(INFO, DEBUGGER) << "Received " << message;
40 
41     auto sessionId = request.GetValue<JsonObject::StringT>("sessionId");
42     auto id = request.GetValue<JsonObject::NumT>("id");
43     auto method = request.GetValue<JsonObject::StringT>("method");
44     auto result = request.GetValue<JsonObject::JsonObjPointer>("result");
45 
46     if (method != nullptr && result == nullptr) {
47         os::memory::LockHolder lock(methodHandlersMutex_);
48 
49         auto optId = id != nullptr ? std::make_optional(*id) : std::nullopt;
50         if (auto handler = methodHandlers_.find(*method); handler != methodHandlers_.end()) {
51             auto *params = request.GetValue<JsonObject::JsonObjPointer>("params");
52 
53             JsonObject empty;
54 
55             handler->second(sessionId != nullptr ? *sessionId : std::string(), optId,
56                             params != nullptr ? **params : empty);
57         } else {
58             LOG(WARNING, DEBUGGER) << "Unsupported method: " << message;
59             HandleUnsupportedMethod(optId, *method);
60         }
61     } else if (result != nullptr && method == nullptr) {
62         if (id == nullptr) {
63             LOG(INFO, DEBUGGER) << "Response object with no \"id\": " << message;
64             return;
65         }
66 
67         if (auto handler = resultHandlers_.extract(*id)) {
68             handler.mapped()(**result);
69         }
70     } else {
71         LOG(INFO, DEBUGGER) << "Expected either 'method' or 'result' parameter";
72     }
73 }
74 
Call(const std::string & sessionId,std::optional<Id> id,const char * method,std::function<void (JsonObjectBuilder &)> && params)75 void EndpointBase::Call(const std::string &sessionId, std::optional<Id> id, const char *method,
76                         std::function<void(JsonObjectBuilder &)> &&params)
77 {
78     Send([&sessionId, id, method, &params](JsonObjectBuilder &call) {
79         if (id) {
80             call.AddProperty("id", *id);
81         }
82 
83         call.AddProperty("method", method);
84         call.AddProperty("params", std::move(params));
85 
86         if (!sessionId.empty()) {
87             call.AddProperty("sessionId", sessionId);
88         }
89     });
90 }
91 
HandleUnsupportedMethod(std::optional<double> optId,const std::string & method)92 void EndpointBase::HandleUnsupportedMethod(std::optional<double> optId, const std::string &method)
93 {
94     Send([&optId, &method](JsonObjectBuilder &builder) {
95         if (optId) {
96             builder.AddProperty("id", *optId);
97         }
98         builder.AddProperty("error", [&method](JsonObjectBuilder &errorBuilder) {
99             errorBuilder.AddProperty("code", static_cast<int>(InspectorErrorCode::METHOD_NOT_FOUND));
100             errorBuilder.AddProperty("message", "Unsupported method: " + method);
101         });
102     });
103 }
104 }  // namespace ark::tooling::inspector
105