• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 "connect_inspector.h"
17 #include <mutex>
18 #include "log_wrapper.h"
19 
20 namespace OHOS::ArkCompiler::Toolchain {
21 std::mutex g_connectMutex;
22 std::unique_ptr<ConnectInspector> g_inspector = nullptr;
23 static constexpr char CONNECTED_MESSAGE[] = "connected";
24 static constexpr char OPEN_MESSAGE[] = "layoutOpen";
25 static constexpr char CLOSE_MESSAGE[] = "layoutClose";
26 static constexpr char REQUEST_MESSAGE[] = "tree";
27 
HandleDebugManager(void * const server)28 void* HandleDebugManager(void* const server)
29 {
30     if (server == nullptr) {
31         LOGE("HandleDebugManager server nullptr");
32         return nullptr;
33     }
34 #if defined(IOS_PLATFORM) || defined(MAC_PLATFORM)
35     pthread_setname_np("OS_DbgConThread");
36 #else
37     pthread_setname_np(pthread_self(), "OS_DbgConThread");
38 #endif
39 
40     static_cast<ConnectServer*>(server)->RunServer();
41     return nullptr;
42 }
43 
OnMessage(const std::string & message)44 void OnMessage(const std::string& message)
45 {
46     std::lock_guard<std::mutex> lock(g_connectMutex);
47     if (message.empty()) {
48         LOGE("message is empty");
49         return;
50     }
51 
52     LOGI("ConnectServer OnMessage: %{public}s", message.c_str());
53     if (g_inspector != nullptr && g_inspector->connectServer_ != nullptr) {
54         g_inspector->ideMsgQueue_.push(message);
55         if (message.find(CONNECTED_MESSAGE, 0) != std::string::npos) {
56             g_inspector->waitingForDebugger_ = false;
57             for (auto& info : g_inspector->infoBuffer_) {
58                 g_inspector->connectServer_->SendMessage(info.second);
59             }
60         }
61         if (message.find(OPEN_MESSAGE, 0) != std::string::npos) {
62             if (g_inspector->setSwitchStatus_ != nullptr) {
63                 LOGI("layoutOpen start");
64                 g_inspector->setSwitchStatus_(true);
65             }
66         }
67         if (message.find(CLOSE_MESSAGE, 0) != std::string::npos) {
68             if (g_inspector->setSwitchStatus_ != nullptr) {
69                 LOGI("layoutClose start");
70                 g_inspector->setSwitchStatus_(false);
71             }
72         }
73         if (message.find(REQUEST_MESSAGE, 0) != std::string::npos) {
74             if (g_inspector->createLayoutInfo_ != nullptr) {
75                 LOGI("tree start");
76                 g_inspector->createLayoutInfo_(g_inspector->instanceId_);
77             }
78         }
79     }
80 }
81 
SetSwitchCallBack(const std::function<void (bool)> & setSwitchStatus,const std::function<void (int32_t)> & createLayoutInfo,int32_t instanceId)82 void SetSwitchCallBack(const std::function<void(bool)>& setSwitchStatus,
83     const std::function<void(int32_t)>& createLayoutInfo, int32_t instanceId)
84 {
85     std::lock_guard<std::mutex> lock(g_connectMutex);
86     if (g_inspector != nullptr) {
87         g_inspector->setSwitchStatus_ = setSwitchStatus;
88         g_inspector->createLayoutInfo_ = createLayoutInfo;
89         g_inspector->instanceId_ = instanceId;
90     }
91 }
92 
ResetService()93 void ResetService()
94 {
95     if (g_inspector != nullptr && g_inspector->connectServer_ != nullptr) {
96         g_inspector->connectServer_->StopServer();
97         g_inspector->connectServer_.reset();
98     }
99 }
100 
StartServer(const std::string & componentName)101 void StartServer(const std::string& componentName)
102 {
103     g_inspector = std::make_unique<ConnectInspector>();
104     g_inspector->connectServer_ = std::make_unique<ConnectServer>(componentName,
105         std::bind(&OnMessage, std::placeholders::_1));
106 
107     pthread_t tid;
108     if (pthread_create(&tid, nullptr, &HandleDebugManager,
109         static_cast<void*>(g_inspector->connectServer_.get())) != 0) {
110         LOGE("pthread_create fail!");
111         ResetService();
112         return;
113     }
114 }
115 
StopServer(const std::string & componentName)116 void StopServer([[maybe_unused]] const std::string& componentName)
117 {
118     ResetService();
119 }
120 
StoreMessage(int32_t instanceId,const std::string & message)121 void StoreMessage(int32_t instanceId, const std::string& message)
122 {
123     std::lock_guard<std::mutex> lock(g_connectMutex);
124     if (g_inspector->infoBuffer_.count(instanceId) == 1) {
125         LOGE("The message with the current instance id has existed.");
126         return;
127     }
128     g_inspector->infoBuffer_[instanceId] = message;
129 }
130 
StoreInspectorInfo(const std::string & jsonTreeStr,const std::string & jsonSnapshotStr)131 void StoreInspectorInfo(const std::string& jsonTreeStr, const std::string& jsonSnapshotStr)
132 {
133     std::lock_guard<std::mutex> lock(g_connectMutex);
134     g_inspector->layoutInspectorInfo_.tree = jsonTreeStr;
135     g_inspector->layoutInspectorInfo_.snapShot = jsonSnapshotStr;
136 }
137 
RemoveMessage(int32_t instanceId)138 void RemoveMessage(int32_t instanceId)
139 {
140     std::lock_guard<std::mutex> lock(g_connectMutex);
141     if (g_inspector->infoBuffer_.count(instanceId) != 1) {
142         LOGE("The message with the current instance id does not exist.");
143         return;
144     }
145     g_inspector->infoBuffer_.erase(instanceId);
146 }
147 
SendLayoutMessage(const std::string & message)148 void SendLayoutMessage(const std::string& message)
149 {
150     LOGI("SendLayoutMessage start to send message");
151     if (g_inspector != nullptr && g_inspector->connectServer_ != nullptr) {
152         g_inspector->connectServer_->SendMessage(message);
153     }
154 }
155 
SendMessage(const std::string & message)156 void SendMessage(const std::string& message)
157 {
158     if (g_inspector != nullptr && g_inspector->connectServer_ != nullptr && !g_inspector->waitingForDebugger_) {
159         g_inspector->connectServer_->SendMessage(message);
160     }
161 }
162 
WaitForConnection()163 bool WaitForConnection()
164 {
165     if (g_inspector == nullptr) {
166         return true;
167     }
168     return g_inspector->waitingForDebugger_;
169 }
170 } // OHOS::ArkCompiler::Toolchain
171