• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 "core/common/connect_server_manager.h"
17 
18 #include <dlfcn.h>
19 #include <string>
20 #include <unistd.h>
21 
22 #include "base/json/json_util.h"
23 #include "base/log/log.h"
24 #include "base/utils/utils.h"
25 #include "core/common/ace_application_info.h"
26 #include "core/common/ace_engine.h"
27 #include "core/common/ace_page.h"
28 #include "core/common/container.h"
29 #include "core/common/layout_inspector.h"
30 #include "core/event/ace_event_handler.h"
31 
32 
33 namespace OHOS::Ace {
34 
35 namespace {
36 
37 using StartServer = bool (*)(const std::string& packageName);
38 using SendMessage = void (*)(const std::string& message);
39 using SendLayoutMessage = void (*)(const std::string& message);
40 using StopServer = void (*)(const std::string& packageName);
41 using StoreMessage = void (*)(int32_t instanceId, const std::string& message);
42 using StoreInspectorInfo = void (*)(const std::string& jsonTreeStr, const std::string& jsonSnapshotStr);
43 using RemoveMessage = void (*)(int32_t instanceId);
44 using WaitForDebugger = bool (*)();
45 using SetSwitchCallBack = void (*)(const std::function<void(bool)>& setStatus,
46     const std::function<void(int32_t)>& createLayoutInfo, int32_t instanceId);
47 
48 SendMessage g_sendMessage = nullptr;
49 SendLayoutMessage g_sendLayoutMessage = nullptr;
50 RemoveMessage g_removeMessage = nullptr;
51 StoreInspectorInfo g_storeInspectorInfo = nullptr;
52 StoreMessage g_storeMessage = nullptr;
53 SetSwitchCallBack g_setSwitchCallBack = nullptr;
54 WaitForDebugger g_waitForDebugger = nullptr;
55 
56 
57 } // namespace
58 
ConnectServerManager()59 ConnectServerManager::ConnectServerManager(): handlerConnectServerSo_(nullptr)
60 {
61     isDebugVersion_ = AceApplicationInfo::GetInstance().IsDebugVersion();
62     if (!isDebugVersion_) {
63         return;
64     }
65     packageName_ = AceApplicationInfo::GetInstance().GetPackageName();
66     InitConnectServer();
67 }
68 
~ConnectServerManager()69 ConnectServerManager::~ConnectServerManager()
70 {
71     if (!isDebugVersion_) {
72         return;
73     }
74     StopConnectServer();
75     CloseConnectServerSo();
76 }
77 
Get()78 ConnectServerManager& ConnectServerManager::Get()
79 {
80     static ConnectServerManager connectServerManager;
81     return connectServerManager;
82 }
83 
InitFunc()84 bool ConnectServerManager::InitFunc()
85 {
86     g_sendMessage = reinterpret_cast<SendMessage>(dlsym(handlerConnectServerSo_, "SendMessage"));
87     g_storeMessage = reinterpret_cast<StoreMessage>(dlsym(handlerConnectServerSo_, "StoreMessage"));
88     g_removeMessage = reinterpret_cast<RemoveMessage>(dlsym(handlerConnectServerSo_, "RemoveMessage"));
89     g_setSwitchCallBack = reinterpret_cast<SetSwitchCallBack>(dlsym(handlerConnectServerSo_, "SetSwitchCallBack"));
90     g_sendLayoutMessage = reinterpret_cast<SendLayoutMessage>(dlsym(handlerConnectServerSo_, "SendLayoutMessage"));
91     g_storeInspectorInfo = reinterpret_cast<StoreInspectorInfo>(dlsym(handlerConnectServerSo_, "StoreInspectorInfo"));
92     g_waitForDebugger = reinterpret_cast<WaitForDebugger>(dlsym(handlerConnectServerSo_, "WaitForDebugger"));
93     if (g_sendMessage == nullptr || g_storeMessage == nullptr || g_removeMessage == nullptr) {
94         CloseConnectServerSo();
95         return false;
96     }
97 
98     if (g_storeInspectorInfo == nullptr || g_setSwitchCallBack == nullptr || g_waitForDebugger == nullptr ||
99         g_sendLayoutMessage == nullptr) {
100         CloseConnectServerSo();
101         return false;
102     }
103     return true;
104 }
105 
InitConnectServer()106 void ConnectServerManager::InitConnectServer()
107 {
108     const std::string soDir = "libconnectserver_debugger.z.so";
109     handlerConnectServerSo_ = dlopen(soDir.c_str(), RTLD_LAZY);
110     if (handlerConnectServerSo_ == nullptr) {
111         LOGE("Cannot find %{public}s", soDir.c_str());
112         return;
113     }
114     StartServer startServer = reinterpret_cast<StartServer>(dlsym(handlerConnectServerSo_, "StartServer"));
115     if (startServer == nullptr || !InitFunc()) {
116         LOGE("startServer = NULL, dlerror = %s", dlerror());
117         return;
118     }
119     startServer(packageName_);
120 }
121 
CloseConnectServerSo()122 void ConnectServerManager::CloseConnectServerSo()
123 {
124     CHECK_NULL_VOID_NOLOG(handlerConnectServerSo_);
125     dlclose(handlerConnectServerSo_);
126     handlerConnectServerSo_ = nullptr;
127 }
128 
129 // When use multi-instances project, debug mode should be set to support debug
SetDebugMode()130 void ConnectServerManager::SetDebugMode()
131 {
132     if (!isDebugVersion_ || handlerConnectServerSo_ == nullptr) {
133         return;
134     }
135 
136     if (!g_waitForDebugger()) { // waitForDebugger : waitForDebugger means the connection state of the connect server
137         AceApplicationInfo::GetInstance().SetNeedDebugBreakPoint(true);
138     }
139 }
140 
StopConnectServer()141 void ConnectServerManager::StopConnectServer()
142 {
143     LOGD("Stop connect server");
144     CHECK_NULL_VOID(handlerConnectServerSo_);
145     StopServer stopServer = reinterpret_cast<StopServer>(dlsym(handlerConnectServerSo_, "StopServer"));
146     if (stopServer == nullptr) {
147         LOGE("stopServer = NULL, dlerror = %s", dlerror());
148         return;
149     }
150     stopServer(packageName_);
151 }
152 
AddInstance(int32_t instanceId,const std::string & instanceName)153 void ConnectServerManager::AddInstance(int32_t instanceId, const std::string& instanceName)
154 {
155     if (!isDebugVersion_ || handlerConnectServerSo_ == nullptr) {
156         return;
157     }
158     LOGD("AddInstance %{public}d", instanceId);
159     {
160         std::lock_guard<std::mutex> lock(mutex_);
161         const auto result = instanceMap_.try_emplace(instanceId, instanceName);
162         if (!result.second) {
163             LOGD("Already have instance name of this instance id: %{public}d", instanceId);
164             return;
165         }
166     }
167     // Get the message including information of new instance, which will be send to IDE.
168     std::string message = GetInstanceMapMessage("addInstance", instanceId);
169 
170     if (!g_waitForDebugger()) { // g_waitForDebugger : the res means the connection state of the connect server
171         g_sendMessage(message); // if connected, message will be sent immediately.
172     } else { // if not connected, message will be stored and sent later when "connected" coming.
173         g_storeMessage(instanceId, message);
174     }
175     CHECK_NULL_VOID(createLayoutInfo_);
176     g_setSwitchCallBack([this](bool status) { setStatus_(status); },
177         [this](int32_t containerId) { createLayoutInfo_(containerId); }, instanceId);
178 }
179 
SendInspector(const std::string & jsonTreeStr,const std::string & jsonSnapshotStr)180 void ConnectServerManager::SendInspector(const std::string& jsonTreeStr, const std::string& jsonSnapshotStr)
181 {
182     LOGI("ConnectServerManager SendInspector Start");
183     g_sendLayoutMessage(jsonTreeStr);
184     g_sendLayoutMessage(jsonSnapshotStr);
185     g_storeInspectorInfo(jsonTreeStr, jsonSnapshotStr);
186 }
187 
RemoveInstance(int32_t instanceId)188 void ConnectServerManager::RemoveInstance(int32_t instanceId)
189 {
190     if (!isDebugVersion_ || handlerConnectServerSo_ == nullptr) {
191         return;
192     }
193     LOGD("RemoveInstance %{public}d", instanceId);
194 
195     // Get the message including information of deleted instance, which will be send to IDE.
196     std::string message = GetInstanceMapMessage("destroyInstance", instanceId);
197     size_t numInstance = 0;
198     {
199         std::lock_guard<std::mutex> lock(mutex_);
200         numInstance = instanceMap_.erase(instanceId);
201     }
202     if (numInstance == 0) {
203         LOGW("Instance name not found with instance id: %{public}d", instanceId);
204     }
205 
206     if (!g_waitForDebugger()) {
207         g_sendMessage(message);
208     } else {
209         g_removeMessage(instanceId);
210     }
211 }
212 
GetInstanceMapMessage(const char * messageType,int32_t instanceId)213 std::string ConnectServerManager::GetInstanceMapMessage(const char* messageType, int32_t instanceId)
214 {
215     std::lock_guard<std::mutex> lock(mutex_);
216     auto message = JsonUtil::Create(true);
217     message->Put("type", messageType);
218     message->Put("instanceId", instanceId);
219     message->Put("name", instanceMap_[instanceId].c_str());
220     message->Put("tid", gettid());
221     return message->ToString();
222 }
223 
SetLayoutInspectorCallback(const std::function<void (int32_t)> & createLayoutInfo,const std::function<void (bool)> & setStatus)224 void ConnectServerManager::SetLayoutInspectorCallback(
225     const std::function<void(int32_t)>& createLayoutInfo, const std::function<void(bool)>& setStatus)
226 {
227     createLayoutInfo_ = createLayoutInfo;
228     setStatus_ = setStatus;
229 }
230 
GetLayoutInspectorCallback()231 std::function<void(int32_t)> ConnectServerManager::GetLayoutInspectorCallback()
232 {
233     return createLayoutInfo_;
234 }
235 } // namespace OHOS::Ace
236