• 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 <unistd.h>
20 
21 #include "base/json/json_util.h"
22 #include "base/log/log.h"
23 #include "core/common/ace_application_info.h"
24 
25 namespace OHOS::Ace {
26 
27 namespace {
28 
29 using StartServer = bool (*)(const std::string& packageName);
30 using SendMessage = void (*)(const std::string& message);
31 using StopServer = void (*)(const std::string& packageName);
32 using StoreMessage = void (*)(int32_t instanceId, const std::string& message);
33 using RemoveMessage = void (*)(int32_t instanceId);
34 using WaitForDebugger = bool (*)();
35 
36 } // namespace
37 
ConnectServerManager()38 ConnectServerManager::ConnectServerManager(): handlerConnectServerSo_(nullptr)
39 {
40     isDebugVersion_ = AceApplicationInfo::GetInstance().IsDebugVersion();
41     if (!isDebugVersion_) {
42         return;
43     }
44     packageName_ = AceApplicationInfo::GetInstance().GetPackageName();
45     LoadConnectServerSo();
46     StartConnectServer();
47 }
48 
~ConnectServerManager()49 ConnectServerManager::~ConnectServerManager()
50 {
51     if (!isDebugVersion_) {
52         return;
53     }
54     StopConnectServer();
55     CloseConnectServerSo();
56 }
57 
Get()58 ConnectServerManager& ConnectServerManager::Get()
59 {
60     static ConnectServerManager connectServerManager;
61     return connectServerManager;
62 }
63 
LoadConnectServerSo()64 void ConnectServerManager::LoadConnectServerSo()
65 {
66     const std::string soDir = "libconnectserver_debugger.z.so";
67     handlerConnectServerSo_ = dlopen(soDir.c_str(), RTLD_LAZY);
68     if (handlerConnectServerSo_ == nullptr) {
69         LOGE("Cannot find %{public}s", soDir.c_str());
70     }
71 }
72 
CloseConnectServerSo()73 void ConnectServerManager::CloseConnectServerSo()
74 {
75     if (handlerConnectServerSo_ == nullptr) {
76         return;
77     }
78     dlclose(handlerConnectServerSo_);
79     handlerConnectServerSo_ = nullptr;
80 }
81 
StartConnectServer()82 void ConnectServerManager::StartConnectServer()
83 {
84     LOGI("Start connect server");
85     if (handlerConnectServerSo_ == nullptr) {
86         LOGE("handlerConnectServerSo_ is null");
87         return;
88     }
89     StartServer startServer = (StartServer)dlsym(handlerConnectServerSo_, "StartServer");
90     if (startServer == nullptr) {
91         LOGE("startServer = NULL, dlerror = %s", dlerror());
92         return;
93     }
94     startServer(packageName_);
95 }
96 
StopConnectServer()97 void ConnectServerManager::StopConnectServer()
98 {
99     LOGI("Stop connect server");
100     if (handlerConnectServerSo_ == nullptr) {
101         LOGE("handlerConnectServerSo_ is null");
102         return;
103     }
104     StopServer stopServer = (StopServer)dlsym(handlerConnectServerSo_, "StopServer");
105     if (stopServer == nullptr) {
106         LOGE("stopServer = NULL, dlerror = %s", dlerror());
107         return;
108     }
109     stopServer(packageName_);
110 }
111 
AddInstance(int32_t instanceId,const std::string & instanceName)112 void ConnectServerManager::AddInstance(int32_t instanceId, const std::string& instanceName)
113 {
114     if (!isDebugVersion_ || handlerConnectServerSo_ == nullptr) {
115         return;
116     }
117     LOGI("AddInstance %{public}d", instanceId);
118     {
119         std::lock_guard<std::mutex> lock(mutex_);
120         const auto result = instanceMap_.try_emplace(instanceId, instanceName);
121         if (!result.second) {
122             LOGW("Already have instance name of this instance id: %{public}d", instanceId);
123             return;
124         }
125     }
126     // Get the message including information of new instance, which will be send to IDE.
127     std::string message = GetInstanceMapMessage("addInstance", instanceId);
128 
129     WaitForDebugger waitForDebugger = (WaitForDebugger)dlsym(handlerConnectServerSo_, "WaitForDebugger");
130     if (waitForDebugger == nullptr) {
131         return;
132     }
133     if (!waitForDebugger()) { // waitForDebugger : waitForDebugger means the connection state of the connect server
134         AceApplicationInfo::GetInstance().SetNeedDebugBreakPoint(true);
135         SendMessage sendMessage = (SendMessage)dlsym(handlerConnectServerSo_, "SendMessage");
136         if (sendMessage != nullptr) {
137             sendMessage(message); // if connected, message will be sent immediately.
138         }
139     } else { // if not connected, message will be stored and sent later when "connected" coming.
140         StoreMessage storeMessage = (StoreMessage)dlsym(handlerConnectServerSo_, "StoreMessage");
141         if (storeMessage != nullptr) {
142             storeMessage(instanceId, message);
143         }
144     }
145 }
146 
RemoveInstance(int32_t instanceId)147 void ConnectServerManager::RemoveInstance(int32_t instanceId)
148 {
149     if (!isDebugVersion_ || handlerConnectServerSo_ == nullptr) {
150         return;
151     }
152     LOGI("RemoveInstance %{public}d", instanceId);
153 
154     // Get the message including information of deleted instance, which will be send to IDE.
155     std::string message = GetInstanceMapMessage("destroyInstance", instanceId);
156     size_t numInstance = 0;
157     {
158         std::lock_guard<std::mutex> lock(mutex_);
159         numInstance = instanceMap_.erase(instanceId);
160     }
161     if (numInstance == 0) {
162         LOGW("Instance name not found with instance id: %{public}d", instanceId);
163     }
164 
165     WaitForDebugger waitForDebugger = (WaitForDebugger)dlsym(handlerConnectServerSo_, "WaitForDebugger");
166     if (waitForDebugger == nullptr) {
167         return;
168     }
169     if (!waitForDebugger()) {
170         SendMessage sendMessage = (SendMessage)dlsym(handlerConnectServerSo_, "SendMessage");
171         if (sendMessage != nullptr) {
172             sendMessage(message);
173         }
174     } else {
175         RemoveMessage removeMessage = (RemoveMessage)dlsym(handlerConnectServerSo_, "RemoveMessage");
176         if (removeMessage != nullptr) {
177             removeMessage(instanceId);
178         }
179     }
180 }
181 
GetInstanceMapMessage(const char * messageType,int32_t instanceId)182 std::string ConnectServerManager::GetInstanceMapMessage(const char* messageType, int32_t instanceId)
183 {
184     auto message = JsonUtil::Create(true);
185     message->Put("type", messageType);
186     message->Put("instanceId", instanceId);
187     message->Put("name", instanceMap_[instanceId].c_str());
188     return message->ToString();
189 }
190 
191 } // namespace OHOS::Ace