• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
GetVisibleInspectorTree(const std::function<void (std::string,int32_t,bool)> & eventCallback)45 int32_t UIContentServiceProxy::GetVisibleInspectorTree(
46     const std::function<void(std::string, int32_t, bool)>& eventCallback)
47 {
48     MessageParcel data;
49     MessageParcel reply;
50     MessageOption option;
51     if (!data.WriteInterfaceToken(GetDescriptor())) {
52         LOGW("GetVisibleInspectorTree write interface token failed");
53         return FAILED;
54     }
55     if (report_ == nullptr) {
56         LOGW("reportStub is nullptr");
57         return FAILED;
58     }
59     report_->RegisterGetInspectorTreeCallback(eventCallback);
60     if (Remote()->SendRequest(GET_VISIBLE_TREE, data, reply, option) != ERR_NONE) {
61         LOGW("GetVisibleInspectorTree send request failed");
62         return REPLY_ERROR;
63     }
64     return NO_ERROR;
65 }
66 
Connect(const EventCallback & eventCallback)67 int32_t UIContentServiceProxy::Connect(const EventCallback& eventCallback)
68 {
69     MessageParcel data;
70     MessageParcel reply;
71     MessageOption option;
72     if (!data.WriteInterfaceToken(GetDescriptor())) {
73         LOGW("connect write interface token failed");
74         return FAILED;
75     }
76     report_ = new (std::nothrow) UiReportStub();
77     processId_ = IPCSkeleton::GetCallingRealPid();
78     isConnected = true;
79     if (report_ == nullptr) {
80         LOGW("connect failed,create reportStub failed");
81         return FAILED;
82     }
83     report_->RegisterGetBaseInfoCallback(eventCallback);
84     if (!data.WriteRemoteObject(report_)) {
85         LOGW("write reportStub failed");
86         return FAILED;
87     }
88     if (!data.WriteInt32(processId_)) {
89         LOGW("write processId failed");
90         return FAILED;
91     }
92     if (Remote()->SendRequest(UI_CONTENT_CONNECT, data, reply, option) != ERR_NONE) {
93         LOGW("connect send request failed");
94         return REPLY_ERROR;
95     }
96     return NO_ERROR;
97 }
98 
RegisterClickEventCallback(const EventCallback & eventCallback)99 int32_t UIContentServiceProxy::RegisterClickEventCallback(const EventCallback& eventCallback)
100 {
101     MessageParcel data;
102     MessageParcel reply;
103     MessageOption option;
104     if (!data.WriteInterfaceToken(GetDescriptor())) {
105         LOGW("RegisterClickEventCallback write interface token failed");
106         return FAILED;
107     }
108     if (report_ == nullptr) {
109         LOGW("reportStub is nullptr");
110         return FAILED;
111     }
112     report_->RegisterClickEventCallback(eventCallback);
113     if (Remote()->SendRequest(REGISTER_CLICK_EVENT, data, reply, option) != ERR_NONE) {
114         LOGW("RegisterClickEventCallback send request failed");
115         return REPLY_ERROR;
116     }
117     return NO_ERROR;
118 }
119 
RegisterSearchEventCallback(const EventCallback & eventCallback)120 int32_t UIContentServiceProxy::RegisterSearchEventCallback(const EventCallback& eventCallback)
121 {
122     MessageParcel data;
123     MessageParcel reply;
124     MessageOption option;
125     if (!data.WriteInterfaceToken(GetDescriptor())) {
126         LOGW("RegisterSearchEventCallback write interface token failed");
127         return FAILED;
128     }
129     if (report_ == nullptr) {
130         LOGW("reportStub is nullptr");
131         return FAILED;
132     }
133     report_->RegisterSearchEventCallback(eventCallback);
134     if (Remote()->SendRequest(REGISTER_SEARCH_EVENT, data, reply, option) != ERR_NONE) {
135         LOGW("RegisterSearchEventCallback send request failed");
136         return REPLY_ERROR;
137     }
138     return NO_ERROR;
139 }
140 
RegisterRouterChangeEventCallback(const EventCallback & eventCallback)141 int32_t UIContentServiceProxy::RegisterRouterChangeEventCallback(const EventCallback& eventCallback)
142 {
143     MessageParcel data;
144     MessageParcel reply;
145     MessageOption option;
146     if (!data.WriteInterfaceToken(GetDescriptor())) {
147         LOGW("RegisterRouterChangeEventCallback write interface token failed");
148         return FAILED;
149     }
150     if (report_ == nullptr) {
151         LOGW("reportStub is nullptr");
152         return FAILED;
153     }
154     report_->RegisterRouterChangeEventCallback(eventCallback);
155     if (Remote()->SendRequest(REGISTER_ROUTER_CHANGE_EVENT, data, reply, option) != ERR_NONE) {
156         LOGW("RegisterRouterChangeEventCallback send request failed");
157         return REPLY_ERROR;
158     }
159     return NO_ERROR;
160 }
161 
RegisterComponentChangeEventCallback(const EventCallback & eventCallback)162 int32_t UIContentServiceProxy::RegisterComponentChangeEventCallback(const EventCallback& eventCallback)
163 {
164     MessageParcel data;
165     MessageParcel reply;
166     MessageOption option;
167     if (!data.WriteInterfaceToken(GetDescriptor())) {
168         LOGW("RegisterComponentChangeEventCallback write interface token failed");
169         return FAILED;
170     }
171     if (report_ == nullptr) {
172         LOGW("reportStub is nullptr");
173         return FAILED;
174     }
175     report_->RegisterComponentChangeEventCallback(eventCallback);
176     if (Remote()->SendRequest(REGISTER_COMPONENT_EVENT, data, reply, option) != ERR_NONE) {
177         LOGW("RegisterComponentChangeEventCallback send request failed");
178         return REPLY_ERROR;
179     }
180     return NO_ERROR;
181 }
182 
RegisterWebUnfocusEventCallback(const std::function<void (int64_t accessibilityId,const std::string & data)> & eventCallback)183 int32_t UIContentServiceProxy::RegisterWebUnfocusEventCallback(
184     const std::function<void(int64_t accessibilityId, const std::string& data)>& eventCallback)
185 {
186     MessageParcel data;
187     MessageParcel reply;
188     MessageOption option;
189     if (!data.WriteInterfaceToken(GetDescriptor())) {
190         LOGW("RegisterWebUnfocusEventCallback write interface token failed");
191         return FAILED;
192     }
193     if (report_ == nullptr) {
194         LOGW("reportStub is nullptr,connect is not execute");
195         return FAILED;
196     }
197     report_->RegisterWebUnfocusEventCallback(eventCallback);
198     if (Remote()->SendRequest(REGISTER_WEB_UNFOCUS_EVENT, data, reply, option) != ERR_NONE) {
199         LOGW("RegisterWebUnfocusEventCallback send request failed");
200         return REPLY_ERROR;
201     }
202     return NO_ERROR;
203 }
204 
UnregisterClickEventCallback()205 int32_t UIContentServiceProxy::UnregisterClickEventCallback()
206 {
207     MessageParcel data;
208     MessageParcel reply;
209     MessageOption option;
210     if (!data.WriteInterfaceToken(GetDescriptor())) {
211         LOGW("UnregisterClickEventCallback write interface token failed");
212         return FAILED;
213     }
214     if (report_ == nullptr) {
215         LOGW("reportStub is nullptr");
216         return FAILED;
217     }
218     report_->UnregisterClickEventCallback();
219     if (Remote()->SendRequest(UNREGISTER_CLICK_EVENT, data, reply, option) != ERR_NONE) {
220         LOGW("UnregisterClickEventCallback send request failed");
221         return REPLY_ERROR;
222     }
223     return NO_ERROR;
224 }
225 
UnregisterSearchEventCallback()226 int32_t UIContentServiceProxy::UnregisterSearchEventCallback()
227 {
228     MessageParcel data;
229     MessageParcel reply;
230     MessageOption option;
231     if (!data.WriteInterfaceToken(GetDescriptor())) {
232         LOGW("UnregisterSearchEventCallback write interface token failed");
233         return FAILED;
234     }
235     if (report_ == nullptr) {
236         LOGW("reportStub is nullptr");
237         return FAILED;
238     }
239     report_->UnregisterSearchEventCallback();
240     if (Remote()->SendRequest(UNREGISTER_SEARCH_EVENT, data, reply, option) != ERR_NONE) {
241         LOGW("UnregisterSearchEventCallback send request failed");
242         return REPLY_ERROR;
243     }
244     return NO_ERROR;
245 }
246 
UnregisterRouterChangeEventCallback()247 int32_t UIContentServiceProxy::UnregisterRouterChangeEventCallback()
248 {
249     MessageParcel data;
250     MessageParcel reply;
251     MessageOption option;
252     if (!data.WriteInterfaceToken(GetDescriptor())) {
253         LOGW("UnregisterRouterChangeEventCallback write interface token failed");
254         return FAILED;
255     }
256     if (report_ == nullptr) {
257         LOGW("reportStub is nullptr");
258         return FAILED;
259     }
260     report_->UnregisterRouterChangeEventCallback();
261     if (Remote()->SendRequest(UNREGISTER_ROUTER_CHANGE_EVENT, data, reply, option) != ERR_NONE) {
262         LOGW("UnregisterRouterChangeEventCallback send request failed");
263         return REPLY_ERROR;
264     }
265     return NO_ERROR;
266 }
267 
UnregisterComponentChangeEventCallback()268 int32_t UIContentServiceProxy::UnregisterComponentChangeEventCallback()
269 {
270     MessageParcel data;
271     MessageParcel reply;
272     MessageOption option;
273     if (!data.WriteInterfaceToken(GetDescriptor())) {
274         LOGW("UnregisterComponentChangeEventCallback write interface token failed");
275         return FAILED;
276     }
277     if (report_ == nullptr) {
278         LOGW("reportStub is nullptr");
279         return FAILED;
280     }
281     report_->UnregisterComponentChangeEventCallback();
282     if (Remote()->SendRequest(UNREGISTER_COMPONENT_EVENT, data, reply, option) != ERR_NONE) {
283         LOGW("UnregisterComponentChangeEventCallback send request failed");
284         return REPLY_ERROR;
285     }
286     return NO_ERROR;
287 }
288 
UnregisterWebUnfocusEventCallback()289 int32_t UIContentServiceProxy::UnregisterWebUnfocusEventCallback()
290 {
291     MessageParcel data;
292     MessageParcel reply;
293     MessageOption option;
294     if (!data.WriteInterfaceToken(GetDescriptor())) {
295         LOGW("UnregisterComponentChangeEventCallback write interface token failed");
296         return FAILED;
297     }
298     if (report_ == nullptr) {
299         LOGW("reportStub is nullptr,connect is not execute");
300         return FAILED;
301     }
302     report_->UnregisterComponentChangeEventCallback();
303     if (Remote()->SendRequest(UNREGISTER_COMPONENT_EVENT, data, reply, option) != ERR_NONE) {
304         LOGW("UnregisterComponentChangeEventCallback send request failed");
305         return REPLY_ERROR;
306     }
307     return NO_ERROR;
308 }
309 
IsConnect()310 bool UIContentServiceProxy::IsConnect()
311 {
312     return isConnected;
313 }
314 
GetWebViewTranslateText(const std::string & data,const std::function<void (int32_t,std::string)> & eventCallback)315 int32_t UIContentServiceProxy::GetWebViewTranslateText(
316     const std::string& data, const std::function<void(int32_t, std::string)>& eventCallback)
317 {
318     MessageParcel value;
319     MessageParcel reply;
320     MessageOption option(MessageOption::TF_ASYNC);
321     if (!value.WriteInterfaceToken(GetDescriptor())) {
322         LOGW("GetWebViewTranslateText write interface token failed");
323         return FAILED;
324     }
325 
326     if (report_ == nullptr) {
327         LOGW("GetWebViewTranslateText is nullptr,connect is not execute");
328         return FAILED;
329     }
330     if (!value.WriteString(data)) {
331         LOGW("GetWebViewTranslateText write interface token failed");
332         return FAILED;
333     }
334     if (report_ == nullptr) {
335         LOGW("GetWebViewTranslateText is nullptr,connect is not execute");
336         return FAILED;
337     }
338     report_->RegisterGetTranslateTextCallback(eventCallback);
339 
340     if (Remote()->SendRequest(GET_WEB_TRANSLATE_TEXT, value, reply, option) != ERR_NONE) {
341         LOGW("GetWebViewTranslateText send request failed");
342         return REPLY_ERROR;
343     }
344     return NO_ERROR;
345 }
346 
ResetTranslateTextAll()347 int32_t UIContentServiceProxy::ResetTranslateTextAll()
348 {
349     MessageParcel data;
350     MessageParcel reply;
351     MessageOption option(MessageOption::TF_ASYNC);
352     if (!data.WriteInterfaceToken(GetDescriptor())) {
353         LOGW("ResetTranslateTextAll write interface token failed");
354         return FAILED;
355     }
356     if (report_ == nullptr) {
357         LOGW("ResetTranslateTextAll is nullptr,connect is not execute");
358         return FAILED;
359     }
360     if (Remote()->SendRequest(RESET_ALL_TEXT, data, reply, option) != ERR_NONE) {
361         LOGW("ResetTranslateTextAll send request failed");
362         return REPLY_ERROR;
363     }
364     return NO_ERROR;
365 }
366 
ResetTranslateText(int32_t nodeId)367 int32_t UIContentServiceProxy::ResetTranslateText(int32_t nodeId)
368 {
369     MessageParcel data;
370     MessageParcel reply;
371     MessageOption option(MessageOption::TF_ASYNC);
372     if (!data.WriteInterfaceToken(GetDescriptor())) {
373         LOGW("ResetTranslateText write interface token failed");
374         return FAILED;
375     }
376     if (report_ == nullptr) {
377         LOGW("ResetTranslateText is nullptr,connect is not execute");
378         return FAILED;
379     }
380     if (Remote()->SendRequest(RESET_TEXT_BY_ID, data, reply, option) != ERR_NONE) {
381         LOGW("ResetTranslateText send request failed");
382         return REPLY_ERROR;
383     }
384     return NO_ERROR;
385 }
386 
GetWebViewCurrentLanguage(const EventCallback & eventCallback)387 int32_t UIContentServiceProxy::GetWebViewCurrentLanguage(const EventCallback& eventCallback)
388 {
389     MessageParcel data;
390     MessageParcel reply;
391     MessageOption option(MessageOption::TF_ASYNC);
392     if (!data.WriteInterfaceToken(GetDescriptor())) {
393         LOGW("GetWebViewCurrentLanguage write interface token failed");
394         return FAILED;
395     }
396     if (!data.WriteInt32(processId_)) {
397         LOGW("write processId failed");
398         return FAILED;
399     }
400     if (report_ == nullptr) {
401         LOGW("GetWebViewCurrentLanguage is nullptr,connect is not execute");
402         return FAILED;
403     }
404     report_->RegisterGetWebViewCurrentLanguage(eventCallback);
405     if (Remote()->SendRequest(GET_WEB_VIEW_LANGUAGE, data, reply, option) != ERR_NONE) {
406         LOGW("GetWebViewCurrentLanguage send request failed");
407         return REPLY_ERROR;
408     }
409     return NO_ERROR;
410 }
411 
GetCurrentPageName(const std::function<void (std::string)> & finishCallback)412 int32_t UIContentServiceProxy::GetCurrentPageName(const std::function<void(std::string)>& finishCallback)
413 {
414     MessageParcel data;
415     MessageParcel reply;
416     MessageOption option(MessageOption::TF_ASYNC);
417     if (!data.WriteInterfaceToken(GetDescriptor())) {
418         LOGW("GetCurrentPageName write interface token failed");
419         return FAILED;
420     }
421     if (!data.WriteInt32(processId_)) {
422         LOGW("write processId failed");
423         return FAILED;
424     }
425     if (report_ == nullptr) {
426         LOGW("GetCurrentPageName is nullptr,connect is not execute");
427         return FAILED;
428     }
429     report_->RegisterGetCurrentPageName(finishCallback);
430     if (Remote()->SendRequest(GET_CURRENT_PAGE_NAME, data, reply, option) != ERR_NONE) {
431         LOGW("GetCurrentPageName send request failed");
432         return REPLY_ERROR;
433     }
434     return NO_ERROR;
435 }
436 
StartWebViewTranslate(const std::string & data,const std::function<void (int32_t,std::string)> & eventCallback)437 int32_t UIContentServiceProxy::StartWebViewTranslate(
438     const std::string& data, const std::function<void(int32_t, std::string)>& eventCallback)
439 {
440     MessageParcel value;
441     MessageParcel reply;
442     MessageOption option(MessageOption::TF_ASYNC);
443     if (!value.WriteInterfaceToken(GetDescriptor())) {
444         LOGW("StartWebViewTranslate write interface token failed");
445         return FAILED;
446     }
447 
448     if (report_ == nullptr) {
449         LOGW("StartWebViewTranslate is nullptr,connect is not execute");
450         return FAILED;
451     }
452     if (!value.WriteString(data)) {
453         LOGW("StartWebViewTranslate write interface token failed");
454         return FAILED;
455     }
456     if (!value.WriteInt32(processId_)) {
457         LOGW("write processId failed");
458         return FAILED;
459     }
460     report_->RegisterGetTranslateTextCallback(eventCallback);
461     if (Remote()->SendRequest(CONTINUE_GET_WEB_TEXT, value, reply, option) != ERR_NONE) {
462         LOGW("StartWebViewTranslate send request failed");
463         return REPLY_ERROR;
464     }
465     return NO_ERROR;
466 }
467 
SendTranslateResult(int32_t nodeId,std::vector<std::string> results,std::vector<int32_t> ids)468 int32_t UIContentServiceProxy::SendTranslateResult(
469     int32_t nodeId, std::vector<std::string> results, std::vector<int32_t> ids)
470 {
471     MessageParcel data;
472     MessageParcel reply;
473     MessageOption option(MessageOption::TF_ASYNC);
474     if (!data.WriteInterfaceToken(GetDescriptor())) {
475         LOGW("SendTranslateResult write interface token failed");
476         return FAILED;
477     }
478     if (!data.WriteInt32(nodeId)) {
479         LOGW("SendTranslateResult write interface token failed");
480         return FAILED;
481     }
482     if (!data.WriteStringVector(results)) {
483         LOGW("SendTranslateResult WriteStringVector  failed");
484         return FAILED;
485     }
486     if (!data.WriteInt32Vector(ids)) {
487         LOGW("SendTranslateResult WriteInt32Vector token failed");
488         return FAILED;
489     }
490     if (Remote()->SendRequest(SEND_TRANSLATE_RESULT, data, reply, option) != ERR_NONE) {
491         LOGW("SendTranslateResult send request failed");
492         return REPLY_ERROR;
493     }
494     return NO_ERROR;
495 }
496 
EndWebViewTranslate()497 int32_t UIContentServiceProxy::EndWebViewTranslate()
498 {
499     MessageParcel data;
500     MessageParcel reply;
501     MessageOption option(MessageOption::TF_ASYNC);
502     if (!data.WriteInterfaceToken(GetDescriptor())) {
503         LOGW("ResetTranslateTextAll write interface token failed");
504         return FAILED;
505     }
506     if (report_ == nullptr) {
507         LOGW("ResetTranslateTextAll is nullptr,connect is not execute");
508         return FAILED;
509     }
510     if (Remote()->SendRequest(END_WEB_TRANSLATE, data, reply, option) != ERR_NONE) {
511         LOGW("ResetTranslateTextAll send request failed");
512         return REPLY_ERROR;
513     }
514     return NO_ERROR;
515 }
516 
SendTranslateResult(int32_t nodeId,std::string result)517 int32_t UIContentServiceProxy::SendTranslateResult(int32_t nodeId, std::string result)
518 {
519     MessageParcel data;
520     MessageParcel reply;
521     MessageOption option(MessageOption::TF_ASYNC);
522     if (!data.WriteInterfaceToken(GetDescriptor())) {
523         LOGW("SendTranslateResult write interface token failed");
524         return FAILED;
525     }
526     if (!data.WriteInt32(nodeId)) {
527         LOGW("SendTranslateResult write interface token failed");
528         return FAILED;
529     }
530     if (!data.WriteString(result)) {
531         LOGW("SendTranslateResult WriteStringVector  failed");
532         return FAILED;
533     }
534 
535     if (Remote()->SendRequest(SEND_TRANSLATE_RESULT_STR, data, reply, option) != ERR_NONE) {
536         LOGW("SendTranslateResult send request failed");
537         return REPLY_ERROR;
538     }
539     return NO_ERROR;
540 }
541 
GetCurrentImagesShowing(const std::function<void (std::vector<std::pair<int32_t,std::shared_ptr<Media::PixelMap>>>)> & finishCallback)542 int32_t UIContentServiceProxy::GetCurrentImagesShowing(
543     const std::function<void(std::vector<std::pair<int32_t, std::shared_ptr<Media::PixelMap>>>)>& finishCallback)
544 {
545     MessageParcel data;
546     MessageParcel reply;
547     MessageOption option(MessageOption::TF_ASYNC);
548     if (!data.WriteInterfaceToken(GetDescriptor())) {
549         LOGW("GetCurrentImagesShowing write interface token failed");
550         return FAILED;
551     }
552     if (!data.WriteInt32(processId_)) {
553         LOGW("write processId failed");
554         return FAILED;
555     }
556     if (report_ == nullptr) {
557         LOGW("reportServiceStub is nullptr,connect is not execute");
558         return FAILED;
559     }
560     report_->RegisterGetShowingImageCallback(finishCallback);
561     if (Remote()->SendRequest(GET_CURRENT_SHOWING_IMAGE, data, reply, option) != ERR_NONE) {
562         LOGW("GetCurrentImagesShowing send request failed");
563         return REPLY_ERROR;
564     }
565     return NO_ERROR;
566 }
567 } // namespace OHOS::Ace
568