1 /*
2  * Copyright (c) 2021-2022 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 "interfaces/inner_api/ui_session/ui_session_manager.h"
17 
18 #include "adapter/ohos/entrance/ui_session/include/ui_service_hilog.h"
19 namespace OHOS::Ace {
20 std::mutex UiSessionManager::mutex_;
21 constexpr int32_t ONCE_IPC_SEND_DATA_MAX_SIZE = 4096;
GetInstance()22 UiSessionManager& UiSessionManager::GetInstance()
23 {
24     static UiSessionManager instance_;
25     return instance_;
26 }
27 
ReportClickEvent(const std::string & data)28 void UiSessionManager::ReportClickEvent(const std::string& data)
29 {
30     for (auto pair : reportObjectMap_) {
31         auto reportService = iface_cast<ReportService>(pair.second);
32         if (reportService != nullptr) {
33             reportService->ReportClickEvent(data);
34         } else {
35             LOGW("report click event failed,process id:%{public}d", pair.first);
36         }
37     }
38 }
39 
ReportSearchEvent(const std::string & data)40 void UiSessionManager::ReportSearchEvent(const std::string& data)
41 {
42     for (auto pair : reportObjectMap_) {
43         auto reportService = iface_cast<ReportService>(pair.second);
44         if (reportService != nullptr) {
45             reportService->ReportSearchEvent(data);
46         } else {
47             LOGW("report search event failed,process id:%{public}d", pair.first);
48         }
49     }
50 }
51 
ReportRouterChangeEvent(const std::string & data)52 void UiSessionManager::ReportRouterChangeEvent(const std::string& data)
53 {
54     for (auto pair : reportObjectMap_) {
55         auto reportService = iface_cast<ReportService>(pair.second);
56         if (reportService != nullptr) {
57             reportService->ReportRouterChangeEvent(data);
58         } else {
59             LOGW("report switch event failed,process id:%{public}d", pair.first);
60         }
61     }
62 }
63 
ReportComponentChangeEvent(const std::string & key,const std::string & value)64 void UiSessionManager::ReportComponentChangeEvent(const std::string& key, const std::string& value)
65 {
66     for (auto pair : reportObjectMap_) {
67         auto reportService = iface_cast<ReportService>(pair.second);
68         if (reportService != nullptr && GetComponentChangeEventRegistered()) {
69             auto data = InspectorJsonUtil::Create();
70             data->Put(key.data(), value.data());
71             reportService->ReportComponentChangeEvent(data->ToString());
72         } else {
73             LOGW("report component event failed,process id:%{public}d", pair.first);
74         }
75     }
76 }
77 
ReportWebUnfocusEvent(int64_t accessibilityId,const std::string & data)78 void UiSessionManager::ReportWebUnfocusEvent(int64_t accessibilityId, const std::string& data)
79 {
80     auto jsonValue = InspectorJsonUtil::Create(true);
81     jsonValue->Put("id", accessibilityId);
82     jsonValue->Put("$type", "web");
83     jsonValue->Put("text", data.c_str());
84     for (auto pair : reportObjectMap_) {
85         auto reportService = iface_cast<ReportService>(pair.second);
86         if (reportService != nullptr) {
87             reportService->ReportWebUnfocusEvent(accessibilityId, jsonValue->ToString());
88         } else {
89             LOGW("report web unfocus event failed,process id:%{public}d", pair.first);
90         }
91     }
92 }
93 
SaveReportStub(sptr<IRemoteObject> reportStub,int32_t processId)94 void UiSessionManager::SaveReportStub(sptr<IRemoteObject> reportStub, int32_t processId)
95 {
96     reportObjectMap_[processId] = reportStub;
97 }
98 
SetClickEventRegistered(bool status)99 void UiSessionManager::SetClickEventRegistered(bool status)
100 {
101     std::lock_guard<std::mutex> lock(mutex_);
102     if (status) {
103         clickEventRegisterProcesses_++;
104     } else {
105         clickEventRegisterProcesses_--;
106     }
107 }
108 
SetSearchEventRegistered(bool status)109 void UiSessionManager::SetSearchEventRegistered(bool status)
110 {
111     std::lock_guard<std::mutex> lock(mutex_);
112     if (status) {
113         searchEventRegisterProcesses_++;
114     } else {
115         searchEventRegisterProcesses_--;
116     }
117 }
118 
SetRouterChangeEventRegistered(bool status)119 void UiSessionManager::SetRouterChangeEventRegistered(bool status)
120 {
121     std::lock_guard<std::mutex> lock(mutex_);
122     if (status) {
123         routerChangeEventRegisterProcesses_++;
124     } else {
125         routerChangeEventRegisterProcesses_--;
126     }
127 }
128 
SetComponentChangeEventRegistered(bool status)129 void UiSessionManager::SetComponentChangeEventRegistered(bool status)
130 {
131     std::lock_guard<std::mutex> lock(mutex_);
132     if (status) {
133         componentChangeEventRegisterProcesses_++;
134     } else {
135         componentChangeEventRegisterProcesses_--;
136     }
137 }
138 
GetClickEventRegistered()139 bool UiSessionManager::GetClickEventRegistered()
140 {
141     return clickEventRegisterProcesses_ > 0 ? true : false;
142 }
143 
GetSearchEventRegistered()144 bool UiSessionManager::GetSearchEventRegistered()
145 {
146     return searchEventRegisterProcesses_ > 0 ? true : false;
147 }
148 
GetRouterChangeEventRegistered()149 bool UiSessionManager::GetRouterChangeEventRegistered()
150 {
151     return routerChangeEventRegisterProcesses_ > 0 ? true : false;
152 }
153 
GetComponentChangeEventRegistered()154 bool UiSessionManager::GetComponentChangeEventRegistered()
155 {
156     return componentChangeEventRegisterProcesses_ > 0 ? true : false;
157 }
158 
GetInspectorTree()159 void UiSessionManager::GetInspectorTree()
160 {
161     jsonValue_ = InspectorJsonUtil::Create(true);
162     webTaskNums = 0;
163     WebTaskNumsChange(1);
164     inspectorFunction_();
165 }
166 
SaveInspectorTreeFunction(InspectorFunction && function)167 void UiSessionManager::SaveInspectorTreeFunction(InspectorFunction&& function)
168 {
169     inspectorFunction_ = std::move(function);
170 }
171 
AddValueForTree(int32_t id,const std::string & value)172 void UiSessionManager::AddValueForTree(int32_t id, const std::string& value)
173 {
174     std::string key = std::to_string(id);
175     if (jsonValue_->Contains(key)) {
176         jsonValue_->Replace(key.c_str(), value.c_str());
177     } else {
178         jsonValue_->Put(key.c_str(), value.c_str());
179     }
180 }
181 
WebTaskNumsChange(int32_t num)182 void UiSessionManager::WebTaskNumsChange(int32_t num)
183 {
184     webTaskNums += num;
185     if (webTaskNums == 0) {
186         std::string data = jsonValue_->ToString();
187         ReportInspectorTreeValue(data);
188     }
189 }
190 
ReportInspectorTreeValue(const std::string & data)191 void UiSessionManager::ReportInspectorTreeValue(const std::string& data)
192 {
193     for (auto pair : reportObjectMap_) {
194         auto reportService = iface_cast<ReportService>(pair.second);
195         if (reportService != nullptr) {
196             size_t partSize = data.size() / ONCE_IPC_SEND_DATA_MAX_SIZE;
197             for (size_t i = 0; i <= partSize; i++) {
198                 if (i != partSize) {
199                     reportService->ReportInspectorTreeValue(
200                         data.substr(i * ONCE_IPC_SEND_DATA_MAX_SIZE, ONCE_IPC_SEND_DATA_MAX_SIZE), i + 1, false);
201                 } else {
202                     reportService->ReportInspectorTreeValue(data.substr(i * ONCE_IPC_SEND_DATA_MAX_SIZE), i + 1, true);
203                 }
204             }
205         } else {
206             LOGW("report component event failed,process id:%{public}d", pair.first);
207         }
208     }
209 }
210 
NotifyAllWebPattern(bool isRegister)211 void UiSessionManager::NotifyAllWebPattern(bool isRegister)
212 {
213     webFocusEventRegistered = isRegister;
214     notifyWebFunction_(isRegister);
215 }
216 
SaveRegisterForWebFunction(NotifyAllWebFunction && function)217 void UiSessionManager::SaveRegisterForWebFunction(NotifyAllWebFunction&& function)
218 {
219     notifyWebFunction_ = std::move(function);
220 }
221 
GetWebFocusRegistered()222 bool UiSessionManager::GetWebFocusRegistered()
223 {
224     return webFocusEventRegistered;
225 }
226 
OnRouterChange(const std::string & path,const std::string & event)227 void UiSessionManager::OnRouterChange(const std::string& path, const std::string& event)
228 {
229     if (GetRouterChangeEventRegistered()) {
230         auto value = InspectorJsonUtil::Create(true);
231         value->Put("path", path.c_str());
232         value->Put("event", event.c_str());
233         ReportRouterChangeEvent(value->ToString());
234     }
235 }
236 
SaveBaseInfo(const std::string & info)237 void UiSessionManager::SaveBaseInfo(const std::string& info)
238 {
239     baseInfo_ = info;
240 }
241 
SendBaseInfo(int32_t processId)242 void UiSessionManager::SendBaseInfo(int32_t processId)
243 {
244     auto reportService = iface_cast<ReportService>(reportObjectMap_[processId]);
245     if (reportService != nullptr) {
246         reportService->SendBaseInfo(baseInfo_);
247     }
248 }
249 } // namespace OHOS::Ace
250