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