1 /*
2  * Copyright (c) 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_content_proxy.h"
17 
18 #include "ipc_skeleton.h"
19 
20 #include "adapter/ohos/entrance/ui_session/include/ui_service_hilog.h"
21 
22 namespace OHOS::Ace {
23 
GetInspectorTree(const std::function<void(std::string, int32_t, bool)>& eventCallback)24 int32_t UIContentServiceProxy::GetInspectorTree(const std::function<void(std::string, int32_t, bool)>& eventCallback)
25 {
26     MessageParcel data;
27     MessageParcel reply;
28     MessageOption option;
29     if (!data.WriteInterfaceToken(GetDescriptor())) {
30         LOGW("GetInspectorTree write interface token failed");
31         return FAILED;
32     }
33     if (report_ == nullptr) {
34         LOGW("reportStub is nullptr");
35         return FAILED;
36     }
37     report_->RegisterGetInspectorTreeCallback(eventCallback);
38     if (Remote()->SendRequest(UI_CONTENT_SERVICE_GET_TREE, data, reply, option) != ERR_NONE) {
39         LOGW("GetInspectorTree send request failed");
40         return REPLY_ERROR;
41     }
42     return NO_ERROR;
43 }
44 
Connect(const EventCallback& eventCallback)45 int32_t UIContentServiceProxy::Connect(const EventCallback& eventCallback)
46 {
47     MessageParcel data;
48     MessageParcel reply;
49     MessageOption option;
50     if (!data.WriteInterfaceToken(GetDescriptor())) {
51         LOGW("connect write interface token failed");
52         return FAILED;
53     }
54     report_ = new (std::nothrow) UiReportStub();
55     processId_ = IPCSkeleton::GetCallingRealPid();
56     isConnected = true;
57     if (report_ == nullptr) {
58         LOGW("connect failed,create reportStub failed");
59         return FAILED;
60     }
61     report_->RegisterGetBaseInfoCallback(eventCallback);
62     if (!data.WriteRemoteObject(report_)) {
63         LOGW("write reportStub failed");
64         return FAILED;
65     }
66     if (!data.WriteInt32(processId_)) {
67         LOGW("write processId failed");
68         return FAILED;
69     }
70     if (Remote()->SendRequest(UI_CONTENT_CONNECT, data, reply, option) != ERR_NONE) {
71         LOGW("connect send request failed");
72         return REPLY_ERROR;
73     }
74     return NO_ERROR;
75 }
76 
RegisterClickEventCallback(const EventCallback& eventCallback)77 int32_t UIContentServiceProxy::RegisterClickEventCallback(const EventCallback& eventCallback)
78 {
79     MessageParcel data;
80     MessageParcel reply;
81     MessageOption option;
82     if (!data.WriteInterfaceToken(GetDescriptor())) {
83         LOGW("RegisterClickEventCallback write interface token failed");
84         return FAILED;
85     }
86     if (report_ == nullptr) {
87         LOGW("reportStub is nullptr");
88         return FAILED;
89     }
90     report_->RegisterClickEventCallback(eventCallback);
91     if (Remote()->SendRequest(REGISTER_CLICK_EVENT, data, reply, option) != ERR_NONE) {
92         LOGW("RegisterClickEventCallback send request failed");
93         return REPLY_ERROR;
94     }
95     return NO_ERROR;
96 }
97 
RegisterSearchEventCallback(const EventCallback& eventCallback)98 int32_t UIContentServiceProxy::RegisterSearchEventCallback(const EventCallback& eventCallback)
99 {
100     MessageParcel data;
101     MessageParcel reply;
102     MessageOption option;
103     if (!data.WriteInterfaceToken(GetDescriptor())) {
104         LOGW("RegisterSearchEventCallback write interface token failed");
105         return FAILED;
106     }
107     if (report_ == nullptr) {
108         LOGW("reportStub is nullptr");
109         return FAILED;
110     }
111     report_->RegisterSearchEventCallback(eventCallback);
112     if (Remote()->SendRequest(REGISTER_SEARCH_EVENT, data, reply, option) != ERR_NONE) {
113         LOGW("RegisterSearchEventCallback send request failed");
114         return REPLY_ERROR;
115     }
116     return NO_ERROR;
117 }
118 
RegisterRouterChangeEventCallback(const EventCallback& eventCallback)119 int32_t UIContentServiceProxy::RegisterRouterChangeEventCallback(const EventCallback& eventCallback)
120 {
121     MessageParcel data;
122     MessageParcel reply;
123     MessageOption option;
124     if (!data.WriteInterfaceToken(GetDescriptor())) {
125         LOGW("RegisterRouterChangeEventCallback write interface token failed");
126         return FAILED;
127     }
128     if (report_ == nullptr) {
129         LOGW("reportStub is nullptr");
130         return FAILED;
131     }
132     report_->RegisterRouterChangeEventCallback(eventCallback);
133     if (Remote()->SendRequest(REGISTER_ROUTER_CHANGE_EVENT, data, reply, option) != ERR_NONE) {
134         LOGW("RegisterRouterChangeEventCallback send request failed");
135         return REPLY_ERROR;
136     }
137     return NO_ERROR;
138 }
139 
RegisterComponentChangeEventCallback(const EventCallback& eventCallback)140 int32_t UIContentServiceProxy::RegisterComponentChangeEventCallback(const EventCallback& eventCallback)
141 {
142     MessageParcel data;
143     MessageParcel reply;
144     MessageOption option;
145     if (!data.WriteInterfaceToken(GetDescriptor())) {
146         LOGW("RegisterComponentChangeEventCallback write interface token failed");
147         return FAILED;
148     }
149     if (report_ == nullptr) {
150         LOGW("reportStub is nullptr");
151         return FAILED;
152     }
153     report_->RegisterComponentChangeEventCallback(eventCallback);
154     if (Remote()->SendRequest(REGISTER_COMPONENT_EVENT, data, reply, option) != ERR_NONE) {
155         LOGW("RegisterComponentChangeEventCallback send request failed");
156         return REPLY_ERROR;
157     }
158     return NO_ERROR;
159 }
160 
RegisterWebUnfocusEventCallback( const std::function<void(int64_t accessibilityId, const std::string& data)>& eventCallback)161 int32_t UIContentServiceProxy::RegisterWebUnfocusEventCallback(
162     const std::function<void(int64_t accessibilityId, const std::string& data)>& eventCallback)
163 {
164     MessageParcel data;
165     MessageParcel reply;
166     MessageOption option;
167     if (!data.WriteInterfaceToken(GetDescriptor())) {
168         LOGW("RegisterWebUnfocusEventCallback write interface token failed");
169         return FAILED;
170     }
171     if (report_ == nullptr) {
172         LOGW("reportStub is nullptr,connect is not execute");
173     }
174     report_->RegisterWebUnfocusEventCallback(eventCallback);
175     if (Remote()->SendRequest(REGISTER_WEB_UNFOCUS_EVENT, data, reply, option) != ERR_NONE) {
176         LOGW("RegisterWebUnfocusEventCallback send request failed");
177         return REPLY_ERROR;
178     }
179     return NO_ERROR;
180 }
181 
UnregisterClickEventCallback()182 int32_t UIContentServiceProxy::UnregisterClickEventCallback()
183 {
184     MessageParcel data;
185     MessageParcel reply;
186     MessageOption option;
187     if (!data.WriteInterfaceToken(GetDescriptor())) {
188         LOGW("UnregisterClickEventCallback write interface token failed");
189         return FAILED;
190     }
191     if (report_ == nullptr) {
192         LOGW("reportStub is nullptr");
193         return FAILED;
194     }
195     report_->UnregisterClickEventCallback();
196     if (Remote()->SendRequest(UNREGISTER_CLICK_EVENT, data, reply, option) != ERR_NONE) {
197         LOGW("UnregisterClickEventCallback send request failed");
198         return REPLY_ERROR;
199     }
200     return NO_ERROR;
201 }
202 
UnregisterSearchEventCallback()203 int32_t UIContentServiceProxy::UnregisterSearchEventCallback()
204 {
205     MessageParcel data;
206     MessageParcel reply;
207     MessageOption option;
208     if (!data.WriteInterfaceToken(GetDescriptor())) {
209         LOGW("UnregisterSearchEventCallback write interface token failed");
210         return FAILED;
211     }
212     if (report_ == nullptr) {
213         LOGW("reportStub is nullptr");
214         return FAILED;
215     }
216     report_->UnregisterSearchEventCallback();
217     if (Remote()->SendRequest(UNREGISTER_SEARCH_EVENT, data, reply, option) != ERR_NONE) {
218         LOGW("UnregisterSearchEventCallback send request failed");
219         return REPLY_ERROR;
220     }
221     return NO_ERROR;
222 }
223 
UnregisterRouterChangeEventCallback()224 int32_t UIContentServiceProxy::UnregisterRouterChangeEventCallback()
225 {
226     MessageParcel data;
227     MessageParcel reply;
228     MessageOption option;
229     if (!data.WriteInterfaceToken(GetDescriptor())) {
230         LOGW("UnregisterRouterChangeEventCallback write interface token failed");
231         return FAILED;
232     }
233     if (report_ == nullptr) {
234         LOGW("reportStub is nullptr");
235         return FAILED;
236     }
237     report_->UnregisterRouterChangeEventCallback();
238     if (Remote()->SendRequest(UNREGISTER_ROUTER_CHANGE_EVENT, data, reply, option) != ERR_NONE) {
239         LOGW("UnregisterRouterChangeEventCallback send request failed");
240         return REPLY_ERROR;
241     }
242     return NO_ERROR;
243 }
244 
UnregisterComponentChangeEventCallback()245 int32_t UIContentServiceProxy::UnregisterComponentChangeEventCallback()
246 {
247     MessageParcel data;
248     MessageParcel reply;
249     MessageOption option;
250     if (!data.WriteInterfaceToken(GetDescriptor())) {
251         LOGW("UnregisterComponentChangeEventCallback write interface token failed");
252         return FAILED;
253     }
254     if (report_ == nullptr) {
255         LOGW("reportStub is nullptr");
256         return FAILED;
257     }
258     report_->UnregisterComponentChangeEventCallback();
259     if (Remote()->SendRequest(UNREGISTER_COMPONENT_EVENT, data, reply, option) != ERR_NONE) {
260         LOGW("UnregisterComponentChangeEventCallback send request failed");
261         return REPLY_ERROR;
262     }
263     return NO_ERROR;
264 }
265 
UnregisterWebUnfocusEventCallback()266 int32_t UIContentServiceProxy::UnregisterWebUnfocusEventCallback()
267 {
268     MessageParcel data;
269     MessageParcel reply;
270     MessageOption option;
271     if (!data.WriteInterfaceToken(GetDescriptor())) {
272         LOGW("UnregisterComponentChangeEventCallback write interface token failed");
273         return FAILED;
274     }
275     if (report_ == nullptr) {
276         LOGW("reportStub is nullptr,connect is not execute");
277     }
278     report_->UnregisterComponentChangeEventCallback();
279     if (Remote()->SendRequest(UNREGISTER_COMPONENT_EVENT, data, reply, option) != ERR_NONE) {
280         LOGW("UnregisterComponentChangeEventCallback send request failed");
281         return REPLY_ERROR;
282     }
283     return NO_ERROR;
284 }
285 
IsConnect()286 bool UIContentServiceProxy::IsConnect()
287 {
288     return isConnected;
289 }
290 } // namespace OHOS::Ace
291