• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2023 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/components/web/resource/web_client_impl.h"
17 
18 #include "core/common/container.h"
19 #include "core/components/web/resource/web_delegate.h"
20 
21 namespace OHOS::Ace {
22 class NWebResponseAsyncHandle : public WebResponseAsyncHandle {
23     DECLARE_ACE_TYPE(NWebResponseAsyncHandle, WebResponseAsyncHandle);
24 public:
NWebResponseAsyncHandle(std::shared_ptr<OHOS::NWeb::NWebUrlResourceResponse> nwebResponse)25     explicit NWebResponseAsyncHandle(std::shared_ptr<OHOS::NWeb::NWebUrlResourceResponse> nwebResponse)
26         :nwebResponse_(nwebResponse) {}
27     ~NWebResponseAsyncHandle() = default;
HandleFileFd(int32_t fd)28     void HandleFileFd(int32_t fd) override
29     {
30         if (nwebResponse_ == nullptr) {
31             return;
32         }
33         nwebResponse_->PutResponseFileHandle(fd);
34     }
35 
HandleData(std::string & data)36     void HandleData(std::string& data) override
37     {
38         if (nwebResponse_ == nullptr) {
39             return;
40         }
41         nwebResponse_->PutResponseData(data);
42     }
43 
HandleResourceUrl(std::string & url)44     void HandleResourceUrl(std::string& url) override
45     {
46         CHECK_NULL_VOID(nwebResponse_);
47         nwebResponse_->PutResponseResourceUrl(url);
48     }
49 
HandleHeadersVal(const std::map<std::string,std::string> & response_headers)50     void HandleHeadersVal(const std::map<std::string, std::string>& response_headers) override
51     {
52         if (nwebResponse_ == nullptr) {
53             return;
54         }
55         nwebResponse_->PutResponseHeaders(response_headers);
56     }
57 
HandleEncoding(std::string & encoding)58     void HandleEncoding(std::string& encoding) override
59     {
60         if (nwebResponse_ == nullptr) {
61             return;
62         }
63         nwebResponse_->PutResponseEncoding(encoding);
64     }
65 
HandleMimeType(std::string & mimeType)66     void HandleMimeType(std::string& mimeType) override
67     {
68         if (nwebResponse_ == nullptr) {
69             return;
70         }
71         nwebResponse_->PutResponseMimeType(mimeType);
72     }
73 
HandleStatusCodeAndReason(int32_t statusCode,std::string & reason)74     void HandleStatusCodeAndReason(int32_t statusCode, std::string& reason) override
75     {
76         if (nwebResponse_ == nullptr) {
77             return;
78         }
79         nwebResponse_->PutResponseStateAndStatuscode(statusCode, reason);
80     }
81 
HandleResponseStatus(bool isReady)82     void HandleResponseStatus(bool isReady) override
83     {
84         if (nwebResponse_ == nullptr) {
85             return;
86         }
87         nwebResponse_->PutResponseDataStatus(isReady);
88     }
89 
90 private:
91     std::shared_ptr<OHOS::NWeb::NWebUrlResourceResponse> nwebResponse_;
92 };
93 
OnJsCommonDialog(const WebClientImpl * webClientImpl,DialogEventType dialogEventType,std::shared_ptr<NWeb::NWebJSDialogResult> result,const std::string & url,const std::string & message,const std::string & value="")94 bool OnJsCommonDialog(
95     const WebClientImpl* webClientImpl,
96     DialogEventType dialogEventType,
97     std::shared_ptr<NWeb::NWebJSDialogResult> result,
98     const std::string &url,
99     const std::string &message,
100     const std::string &value = "")
101 {
102     bool jsResult = false;
103     auto param = std::make_shared<WebDialogEvent>(url, message, value, dialogEventType,
104         AceType::MakeRefPtr<ResultOhos>(result));
105     auto task = Container::CurrentTaskExecutor();
106     if (task == nullptr) {
107         return false;
108     }
109     task->PostSyncTask([&webClientImpl, dialogEventType, &param, &jsResult] {
110         if (webClientImpl == nullptr) {
111             return;
112         }
113         auto delegate = webClientImpl->GetWebDelegate();
114         if (delegate) {
115             jsResult = delegate->OnCommonDialog(param, dialogEventType);
116         }
117         },
118         OHOS::Ace::TaskExecutor::TaskType::JS);
119     return jsResult;
120 }
121 
OnDownloadStart(const std::string & url,const std::string & userAgent,const std::string & contentDisposition,const std::string & mimetype,long contentLength)122 void DownloadListenerImpl::OnDownloadStart(const std::string& url, const std::string& userAgent,
123     const std::string& contentDisposition, const std::string& mimetype, long contentLength)
124 {
125     ContainerScope scope(instanceId_);
126     auto delegate = webDelegate_.Upgrade();
127     if (!delegate) {
128         return;
129     }
130     delegate->OnDownloadStart(url, userAgent, contentDisposition, mimetype, contentLength);
131 }
132 
OnAccessibilityEvent(int64_t accessibilityId,uint32_t eventType)133 void AccessibilityEventListenerImpl::OnAccessibilityEvent(int64_t accessibilityId, uint32_t eventType)
134 {
135     ContainerScope scope(instanceId_);
136     auto delegate = webDelegate_.Upgrade();
137     CHECK_NULL_VOID(delegate);
138     delegate->OnAccessibilityEvent(accessibilityId, static_cast<AccessibilityEventType>(eventType));
139 }
140 
OnFindResultReceived(const int activeMatchOrdinal,const int numberOfMatches,const bool isDoneCounting)141 void FindListenerImpl::OnFindResultReceived(
142     const int activeMatchOrdinal, const int numberOfMatches, const bool isDoneCounting)
143 {
144     ContainerScope scope(instanceId_);
145     auto delegate = webDelegate_.Upgrade();
146     if (!delegate) {
147         return;
148     }
149     delegate->OnSearchResultReceive(activeMatchOrdinal, numberOfMatches, isDoneCounting);
150 }
151 
OnPageLoadEnd(int httpStatusCode,const std::string & url)152 void WebClientImpl::OnPageLoadEnd(int httpStatusCode, const std::string& url)
153 {
154     ContainerScope scope(instanceId_);
155     auto delegate = webDelegate_.Upgrade();
156     if (!delegate) {
157         return;
158     }
159     delegate->OnPageFinished(url);
160 }
161 
OnFocus()162 bool WebClientImpl::OnFocus()
163 {
164     ContainerScope scope(instanceId_);
165     auto delegate = webDelegate_.Upgrade();
166     CHECK_NULL_RETURN(delegate, false);
167     bool isFocused = delegate->RequestFocus();
168     delegate->OnRequestFocus();
169     return isFocused;
170 }
171 
OnConsoleLog(const OHOS::NWeb::NWebConsoleLog & message)172 bool WebClientImpl::OnConsoleLog(const OHOS::NWeb::NWebConsoleLog& message)
173 {
174     ContainerScope scope(instanceId_);
175     bool jsMessage = false;
176     auto task = Container::CurrentTaskExecutor();
177     if (!task) {
178         return false;
179     }
180     task->PostSyncTask([webClient = this, &message, &jsMessage] {
181         if (!webClient) {
182             return;
183         }
184         auto delegate = webClient->webDelegate_.Upgrade();
185         if (delegate) {
186             jsMessage = delegate->OnConsoleLog(std::make_shared<OHOS::NWeb::NWebConsoleLog>(message));
187         }
188         },
189         OHOS::Ace::TaskExecutor::TaskType::JS);
190 
191     return jsMessage;
192 }
193 
OnPageLoadBegin(const std::string & url)194 void WebClientImpl::OnPageLoadBegin(const std::string& url)
195 {
196     ContainerScope scope(instanceId_);
197     auto delegate = webDelegate_.Upgrade();
198     if (!delegate) {
199         return;
200     }
201     delegate->OnPageStarted(url);
202 }
203 
OnLoadingProgress(int newProgress)204 void WebClientImpl::OnLoadingProgress(int newProgress)
205 {
206     ContainerScope scope(instanceId_);
207     auto delegate = webDelegate_.Upgrade();
208     if (!delegate) {
209         return;
210     }
211     delegate->OnProgressChanged(newProgress);
212 }
213 
OnPageTitle(const std::string & title)214 void WebClientImpl::OnPageTitle(const std::string &title)
215 {
216     ContainerScope scope(instanceId_);
217     auto delegate = webDelegate_.Upgrade();
218     if (!delegate) {
219         return;
220     }
221     delegate->OnReceivedTitle(title);
222 }
223 
OnFullScreenExit()224 void WebClientImpl::OnFullScreenExit()
225 {
226     ContainerScope scope(instanceId_);
227     auto delegate = webDelegate_.Upgrade();
228     CHECK_NULL_VOID(delegate);
229     delegate->OnFullScreenExit();
230 }
231 
OnFullScreenEnter(std::shared_ptr<NWeb::NWebFullScreenExitHandler> handler)232 void WebClientImpl::OnFullScreenEnter(std::shared_ptr<NWeb::NWebFullScreenExitHandler> handler)
233 {
234     ContainerScope scope(instanceId_);
235     auto delegate = webDelegate_.Upgrade();
236     CHECK_NULL_VOID(delegate);
237     CHECK_NULL_VOID(handler);
238     delegate->OnFullScreenEnter(handler);
239 }
240 
OnGeolocationHide()241 void WebClientImpl::OnGeolocationHide()
242 {
243     ContainerScope scope(instanceId_);
244     auto delegate = webDelegate_.Upgrade();
245     if (!delegate) {
246         return;
247     }
248     delegate->OnGeolocationPermissionsHidePrompt();
249 }
250 
OnGeolocationShow(const std::string & origin,std::shared_ptr<OHOS::NWeb::NWebGeolocationCallbackInterface> callback)251 void WebClientImpl::OnGeolocationShow(const std::string& origin,
252     std::shared_ptr<OHOS::NWeb::NWebGeolocationCallbackInterface> callback)
253 {
254     ContainerScope scope(instanceId_);
255     auto delegate = webDelegate_.Upgrade();
256     if (!delegate) {
257         return;
258     }
259     delegate->OnGeolocationPermissionsShowPrompt(origin, callback);
260 }
261 
SetNWeb(std::shared_ptr<OHOS::NWeb::NWeb> nweb)262 void WebClientImpl::SetNWeb(std::shared_ptr<OHOS::NWeb::NWeb> nweb)
263 {
264     webviewWeak_ = nweb;
265 }
266 
OnProxyDied()267 void WebClientImpl::OnProxyDied()
268 {
269     auto delegate = webDelegate_.Upgrade();
270     if (!delegate) {
271         return;
272     }
273 }
274 
OnResourceLoadError(std::shared_ptr<NWeb::NWebUrlResourceRequest> request,std::shared_ptr<NWeb::NWebUrlResourceError> error)275 void WebClientImpl::OnResourceLoadError(
276     std::shared_ptr<NWeb::NWebUrlResourceRequest> request, std::shared_ptr<NWeb::NWebUrlResourceError> error)
277 {
278     ContainerScope scope(instanceId_);
279     auto delegate = webDelegate_.Upgrade();
280     if (!delegate) {
281         return;
282     }
283     delegate->OnErrorReceive(request, error);
284 }
285 
OnHttpError(std::shared_ptr<OHOS::NWeb::NWebUrlResourceRequest> request,std::shared_ptr<OHOS::NWeb::NWebUrlResourceResponse> response)286 void WebClientImpl::OnHttpError(std::shared_ptr<OHOS::NWeb::NWebUrlResourceRequest> request,
287     std::shared_ptr<OHOS::NWeb::NWebUrlResourceResponse> response)
288 {
289     ContainerScope scope(instanceId_);
290     auto delegate = webDelegate_.Upgrade();
291     if (!delegate) {
292         return;
293     }
294     delegate->OnHttpErrorReceive(request, response);
295 }
296 
OnMessage(const std::string & param)297 void WebClientImpl::OnMessage(const std::string& param)
298 {
299     ContainerScope scope(instanceId_);
300     auto delegate = webDelegate_.Upgrade();
301     if (!delegate) {
302         return;
303     }
304     delegate->OnMessage(param);
305 }
306 
OnRouterPush(const std::string & param)307 void WebClientImpl::OnRouterPush(const std::string& param)
308 {
309     ContainerScope scope(instanceId_);
310     auto delegate = webDelegate_.Upgrade();
311     if (!delegate) {
312         return;
313     }
314     delegate->OnRouterPush(param);
315 }
316 
OnHandleInterceptUrlLoading(std::shared_ptr<OHOS::NWeb::NWebUrlResourceRequest> request)317 bool WebClientImpl::OnHandleInterceptUrlLoading(std::shared_ptr<OHOS::NWeb::NWebUrlResourceRequest> request)
318 {
319     ContainerScope scope(instanceId_);
320     auto delegate = webDelegate_.Upgrade();
321     if (!delegate) {
322         return false;
323     }
324 
325     bool result = delegate->OnHandleInterceptUrlLoading(request->Url());
326     if (!result) {
327         result = delegate->OnHandleInterceptLoading(request);
328     }
329     return result;
330 }
331 
OnHandleInterceptRequest(std::shared_ptr<OHOS::NWeb::NWebUrlResourceRequest> request)332 std::shared_ptr<OHOS::NWeb::NWebUrlResourceResponse> WebClientImpl::OnHandleInterceptRequest(
333     std::shared_ptr<OHOS::NWeb::NWebUrlResourceRequest> request)
334 {
335     ContainerScope scope(instanceId_);
336     auto delegate = webDelegate_.Upgrade();
337     if (!delegate || (delegate->IsEmptyOnInterceptRequest())) {
338         return nullptr;
339     }
340 
341     auto webRequest = AceType::MakeRefPtr<WebRequest>(request->RequestHeaders(), request->Method(), request->Url(),
342         request->FromGesture(), request->IsAboutMainFrame(), request->IsRequestRedirect());
343     auto param = std::make_shared<OnInterceptRequestEvent>(webRequest);
344     RefPtr<WebResponse> webResponse = nullptr;
345     auto task = Container::CurrentTaskExecutor();
346     if (task == nullptr) {
347         return nullptr;
348     }
349     task->PostSyncTask([&delegate, &webResponse, &param] {
350             webResponse = delegate->OnInterceptRequest(param);
351         }, OHOS::Ace::TaskExecutor::TaskType::JS);
352     if (webResponse == nullptr) {
353         return nullptr;
354     }
355     std::string data = webResponse->GetData();
356     std::shared_ptr<OHOS::NWeb::NWebUrlResourceResponse> nwebResponse =
357         std::make_shared<OHOS::NWeb::NWebUrlResourceResponse>(webResponse->GetMimeType(), webResponse->GetEncoding(),
358         webResponse->GetStatusCode(), webResponse->GetReason(), webResponse->GetHeaders(),  data);
359     switch (webResponse->GetDataType()) {
360         case WebResponseDataType::FILE_TYPE:
361             nwebResponse->PutResponseFileHandle(webResponse->GetFileHandle());
362             break;
363         case WebResponseDataType::RESOURCE_URL_TYPE:
364             nwebResponse->PutResponseResourceUrl(webResponse->GetResourceUrl());
365             break;
366         default:
367             nwebResponse->PutResponseData(data);
368             break;
369     }
370     if (webResponse->GetResponseStatus() == false) {
371         std::shared_ptr<NWebResponseAsyncHandle> asyncHandle = std::make_shared<NWebResponseAsyncHandle>(nwebResponse);
372         webResponse->SetAsyncHandle(asyncHandle);
373         nwebResponse->PutResponseDataStatus(false);
374     }
375     return nwebResponse;
376 }
377 
OnAlertDialogByJS(const std::string & url,const std::string & message,std::shared_ptr<NWeb::NWebJSDialogResult> result)378 bool WebClientImpl::OnAlertDialogByJS(
379     const std::string &url, const std::string &message, std::shared_ptr<NWeb::NWebJSDialogResult> result)
380 {
381     ContainerScope scope(instanceId_);
382     return OnJsCommonDialog(this, DialogEventType::DIALOG_EVENT_ALERT, result, url, message);
383 }
384 
OnBeforeUnloadByJS(const std::string & url,const std::string & message,std::shared_ptr<NWeb::NWebJSDialogResult> result)385 bool WebClientImpl::OnBeforeUnloadByJS(
386     const std::string &url, const std::string &message, std::shared_ptr<NWeb::NWebJSDialogResult> result)
387 {
388     ContainerScope scope(instanceId_);
389     return OnJsCommonDialog(this, DialogEventType::DIALOG_EVENT_BEFORE_UNLOAD, result, url, message);
390 }
391 
OnConfirmDialogByJS(const std::string & url,const std::string & message,std::shared_ptr<NWeb::NWebJSDialogResult> result)392 bool WebClientImpl::OnConfirmDialogByJS(
393     const std::string &url, const std::string &message, std::shared_ptr<NWeb::NWebJSDialogResult> result)
394 {
395     ContainerScope scope(instanceId_);
396     return OnJsCommonDialog(this, DialogEventType::DIALOG_EVENT_CONFIRM, result, url, message);
397 }
398 
OnPromptDialogByJS(const std::string & url,const std::string & message,const std::string & defaultValue,std::shared_ptr<NWeb::NWebJSDialogResult> result)399 bool WebClientImpl::OnPromptDialogByJS(const std::string &url, const std::string &message,
400     const std::string &defaultValue, std::shared_ptr<NWeb::NWebJSDialogResult> result)
401 {
402     ContainerScope scope(instanceId_);
403     return OnJsCommonDialog(this, DialogEventType::DIALOG_EVENT_PROMPT, result, url, message, defaultValue);
404 }
405 
OnRenderExited(OHOS::NWeb::RenderExitReason reason)406 void WebClientImpl::OnRenderExited(OHOS::NWeb::RenderExitReason reason)
407 {
408     ContainerScope scope(instanceId_);
409     auto delegate = webDelegate_.Upgrade();
410     if (!delegate) {
411         return;
412     }
413     delegate->OnRenderExited(reason);
414 }
415 
OnRefreshAccessedHistory(const std::string & url,bool isReload)416 void WebClientImpl::OnRefreshAccessedHistory(const std::string& url, bool isReload)
417 {
418     ContainerScope scope(instanceId_);
419     auto delegate = webDelegate_.Upgrade();
420     if (!delegate) {
421         return;
422     }
423     delegate->OnRefreshAccessedHistory(url, isReload);
424 }
425 
OnFileSelectorShow(std::shared_ptr<NWeb::FileSelectorCallback> callback,std::shared_ptr<NWeb::NWebFileSelectorParams> params)426 bool WebClientImpl::OnFileSelectorShow(
427     std::shared_ptr<NWeb::FileSelectorCallback> callback,
428     std::shared_ptr<NWeb::NWebFileSelectorParams> params)
429 {
430     ContainerScope scope(instanceId_);
431     bool jsResult = false;
432     auto param = std::make_shared<FileSelectorEvent>(AceType::MakeRefPtr<FileSelectorParamOhos>(params),
433         AceType::MakeRefPtr<FileSelectorResultOhos>(callback));
434     auto task = Container::CurrentTaskExecutor();
435     if (task == nullptr) {
436         return false;
437     }
438     task->PostSyncTask([webClient = this, &param, &jsResult] {
439         if (webClient == nullptr) {
440             return;
441         }
442         auto delegate = webClient->GetWebDelegate();
443         if (delegate) {
444             jsResult = delegate->OnFileSelectorShow(param);
445         }
446         },
447         OHOS::Ace::TaskExecutor::TaskType::JS);
448     return jsResult;
449 }
450 
OnResource(const std::string & url)451 void WebClientImpl::OnResource(const std::string& url)
452 {
453     ContainerScope scope(instanceId_);
454     auto task = Container::CurrentTaskExecutor();
455     if (task == nullptr) {
456         return;
457     }
458     std::weak_ptr<WebClientImpl> webClientWeak = shared_from_this();
459     task->PostTask([webClient = webClientWeak, url] {
460         auto webClientUpgrade = webClient.lock();
461         if (webClientUpgrade == nullptr) {
462             return;
463         }
464         auto delegate = webClientUpgrade->GetWebDelegate();
465         if (delegate) {
466             delegate->OnResourceLoad(url);
467         }
468         },
469         OHOS::Ace::TaskExecutor::TaskType::JS);
470 }
471 
OnScaleChanged(float oldScaleFactor,float newScaleFactor)472 void WebClientImpl::OnScaleChanged(float oldScaleFactor, float newScaleFactor)
473 {
474     ContainerScope scope(instanceId_);
475     auto delegate = webDelegate_.Upgrade();
476     if (!delegate) {
477         return;
478     }
479     delegate->OnScaleChange(oldScaleFactor, newScaleFactor);
480 }
481 
OnScroll(double xOffset,double yOffset)482 void WebClientImpl::OnScroll(double xOffset, double yOffset)
483 {
484     ContainerScope scope(instanceId_);
485     auto delegate = webDelegate_.Upgrade();
486     if (!delegate) {
487         return;
488     }
489     delegate->OnScroll(xOffset, yOffset);
490 }
491 
OnHttpAuthRequestByJS(std::shared_ptr<NWeb::NWebJSHttpAuthResult> result,const std::string & host,const std::string & realm)492 bool WebClientImpl::OnHttpAuthRequestByJS(std::shared_ptr<NWeb::NWebJSHttpAuthResult> result, const std::string &host,
493     const std::string &realm)
494 {
495     ContainerScope scope(instanceId_);
496 
497     bool jsResult = false;
498     auto param = std::make_shared<WebHttpAuthEvent>(AceType::MakeRefPtr<AuthResultOhos>(result), host, realm);
499     auto task = Container::CurrentTaskExecutor();
500     if (task == nullptr) {
501         return false;
502     }
503     task->PostSyncTask([webClient = this, &param, &jsResult] {
504             if (!webClient) {
505                 return;
506             }
507             auto delegate = webClient->webDelegate_.Upgrade();
508             if (delegate) {
509                 jsResult = delegate->OnHttpAuthRequest(param);
510             }
511         }, OHOS::Ace::TaskExecutor::TaskType::JS);
512     return jsResult;
513 }
514 
OnSslErrorRequestByJS(std::shared_ptr<NWeb::NWebJSSslErrorResult> result,OHOS::NWeb::SslError error)515 bool WebClientImpl::OnSslErrorRequestByJS(std::shared_ptr<NWeb::NWebJSSslErrorResult> result,
516     OHOS::NWeb::SslError error)
517 {
518     ContainerScope scope(instanceId_);
519 
520     bool jsResult = false;
521     auto param = std::make_shared<WebSslErrorEvent>(AceType::MakeRefPtr<SslErrorResultOhos>(result), static_cast<int32_t>(error));
522     auto task = Container::CurrentTaskExecutor();
523     if (task == nullptr) {
524         return false;
525     }
526     task->PostSyncTask([webClient = this, &param, &jsResult] {
527             if (!webClient) {
528                 return;
529             }
530             auto delegate = webClient->webDelegate_.Upgrade();
531             if (delegate) {
532                 jsResult = delegate->OnSslErrorRequest(param);
533             }
534         }, OHOS::Ace::TaskExecutor::TaskType::JS);
535     return jsResult;
536 }
537 
OnSslSelectCertRequestByJS(std::shared_ptr<NWeb::NWebJSSslSelectCertResult> result,const std::string & host,int port,const std::vector<std::string> & keyTypes,const std::vector<std::string> & issuers)538 bool WebClientImpl::OnSslSelectCertRequestByJS(
539     std::shared_ptr<NWeb::NWebJSSslSelectCertResult> result,
540     const std::string& host,
541     int port,
542     const std::vector<std::string>& keyTypes,
543     const std::vector<std::string>& issuers)
544 {
545     ContainerScope scope(instanceId_);
546 
547     bool jsResult = false;
548     auto param = std::make_shared<WebSslSelectCertEvent>(AceType::MakeRefPtr<SslSelectCertResultOhos>(result),
549         host, port, keyTypes, issuers);
550     auto task = Container::CurrentTaskExecutor();
551     if (task == nullptr) {
552         return false;
553     }
554 
555     task->PostSyncTask([webClient = this, &param, &jsResult] {
556             if (!webClient) {
557                 return;
558             }
559             auto delegate = webClient->webDelegate_.Upgrade();
560             if (delegate) {
561                 jsResult = delegate->OnSslSelectCertRequest(param);
562             }
563         }, OHOS::Ace::TaskExecutor::TaskType::JS);
564 
565     return jsResult;
566 }
567 
OnPermissionRequest(std::shared_ptr<NWeb::NWebAccessRequest> request)568 void WebClientImpl::OnPermissionRequest(std::shared_ptr<NWeb::NWebAccessRequest> request)
569 {
570     ContainerScope scope(instanceId_);
571     auto delegate = webDelegate_.Upgrade();
572     CHECK_NULL_VOID(delegate);
573     delegate->OnPermissionRequestPrompt(request);
574 }
575 
OnScreenCaptureRequest(std::shared_ptr<NWeb::NWebScreenCaptureAccessRequest> request)576 void WebClientImpl::OnScreenCaptureRequest(std::shared_ptr<NWeb::NWebScreenCaptureAccessRequest> request)
577 {
578     ContainerScope scope(instanceId_);
579     auto delegate = webDelegate_.Upgrade();
580     CHECK_NULL_VOID(delegate);
581     delegate->OnScreenCaptureRequest(request);
582 }
583 
RunContextMenu(std::shared_ptr<NWeb::NWebContextMenuParams> params,std::shared_ptr<NWeb::NWebContextMenuCallback> callback)584 bool WebClientImpl::RunContextMenu(
585     std::shared_ptr<NWeb::NWebContextMenuParams> params,
586     std::shared_ptr<NWeb::NWebContextMenuCallback> callback)
587 {
588     ContainerScope scope(instanceId_);
589     bool jsResult = false;
590     auto param = std::make_shared<ContextMenuEvent>(AceType::MakeRefPtr<ContextMenuParamOhos>(params),
591         AceType::MakeRefPtr<ContextMenuResultOhos>(callback));
592     auto task = Container::CurrentTaskExecutor();
593     if (task == nullptr) {
594         return false;
595     }
596     task->PostSyncTask([webClient = this, &param, &jsResult] {
597         if (webClient == nullptr) {
598             return;
599         }
600         auto delegate = webClient->GetWebDelegate();
601         if (delegate) {
602             jsResult = delegate->OnContextMenuShow(param);
603         }
604         },
605         OHOS::Ace::TaskExecutor::TaskType::JS);
606     return jsResult;
607 }
608 
RunQuickMenu(std::shared_ptr<NWeb::NWebQuickMenuParams> params,std::shared_ptr<NWeb::NWebQuickMenuCallback> callback)609 bool WebClientImpl::RunQuickMenu(std::shared_ptr<NWeb::NWebQuickMenuParams> params,
610                                  std::shared_ptr<NWeb::NWebQuickMenuCallback> callback)
611 {
612     if (!params || !callback) {
613         return false;
614     }
615     ContainerScope scope(instanceId_);
616     auto task = Container::CurrentTaskExecutor();
617     if (task == nullptr) {
618         return false;
619     }
620     auto delegate = webDelegate_.Upgrade();
621     if (!delegate) {
622         return false;
623     }
624     return delegate->RunQuickMenu(params, callback);
625 }
626 
OnQuickMenuDismissed()627 void WebClientImpl::OnQuickMenuDismissed()
628 {
629     ContainerScope scope(instanceId_);
630     auto task = Container::CurrentTaskExecutor();
631     if (task == nullptr) {
632         return;
633     }
634     auto delegate = webDelegate_.Upgrade();
635     if (!delegate) {
636         return;
637     }
638     delegate->OnQuickMenuDismissed();
639 }
640 
OnTouchSelectionChanged(std::shared_ptr<OHOS::NWeb::NWebTouchHandleState> insertHandle,std::shared_ptr<OHOS::NWeb::NWebTouchHandleState> startSelectionHandle,std::shared_ptr<OHOS::NWeb::NWebTouchHandleState> endSelectionHandle)641 void WebClientImpl::OnTouchSelectionChanged(
642     std::shared_ptr<OHOS::NWeb::NWebTouchHandleState> insertHandle,
643     std::shared_ptr<OHOS::NWeb::NWebTouchHandleState> startSelectionHandle,
644     std::shared_ptr<OHOS::NWeb::NWebTouchHandleState> endSelectionHandle)
645 {
646     ContainerScope scope(instanceId_);
647     auto task = Container::CurrentTaskExecutor();
648     if (task == nullptr) {
649         return;
650     }
651     auto delegate = webDelegate_.Upgrade();
652     if (!delegate) {
653         return;
654     }
655     delegate->OnTouchSelectionChanged(
656         insertHandle, startSelectionHandle, endSelectionHandle);
657 }
658 
OnDragAndDropData(const void * data,size_t len,const NWeb::ImageOptions & opt)659 bool WebClientImpl::OnDragAndDropData(const void* data, size_t len, const NWeb::ImageOptions& opt)
660 {
661     ContainerScope scope(instanceId_);
662     auto delegate = webDelegate_.Upgrade();
663     if (!delegate) {
664         return false;
665     }
666     return delegate->OnDragAndDropData(data, len, opt.width, opt.height);
667 }
668 
OnDragAndDropDataUdmf(std::shared_ptr<NWeb::NWebDragData> dragData)669 bool WebClientImpl::OnDragAndDropDataUdmf(std::shared_ptr<NWeb::NWebDragData> dragData)
670 {
671     ContainerScope scope(instanceId_);
672     auto delegate = webDelegate_.Upgrade();
673     if (!delegate) {
674         return false;
675     }
676     return delegate->OnDragAndDropDataUdmf(dragData);
677 }
678 
UpdateDragCursor(NWeb::NWebDragData::DragOperation op)679 void WebClientImpl::UpdateDragCursor(NWeb::NWebDragData::DragOperation op)
680 {
681     ContainerScope scope(instanceId_);
682     auto delegate = webDelegate_.Upgrade();
683     CHECK_NULL_VOID(delegate);
684     delegate->UpdateDragCursor(op);
685 }
686 
OnWindowNewByJS(const std::string & targetUrl,bool isAlert,bool isUserTrigger,std::shared_ptr<NWeb::NWebControllerHandler> handler)687 void WebClientImpl::OnWindowNewByJS(
688     const std::string& targetUrl,
689     bool isAlert,
690     bool isUserTrigger,
691     std::shared_ptr<NWeb::NWebControllerHandler> handler)
692 {
693     ContainerScope scope(instanceId_);
694     auto delegate = webDelegate_.Upgrade();
695     CHECK_NULL_VOID(delegate);
696     delegate->OnWindowNew(targetUrl, isAlert, isUserTrigger, handler);
697 }
698 
OnWindowExitByJS()699 void WebClientImpl::OnWindowExitByJS()
700 {
701     ContainerScope scope(instanceId_);
702     auto delegate = webDelegate_.Upgrade();
703     CHECK_NULL_VOID(delegate);
704     delegate->OnWindowExit();
705 }
706 
OnPageVisible(const std::string & url)707 void WebClientImpl::OnPageVisible(const std::string& url)
708 {
709     ContainerScope scope(instanceId_);
710     auto delegate = webDelegate_.Upgrade();
711     CHECK_NULL_VOID(delegate);
712     delegate->OnPageVisible(url);
713 }
714 
OnDataResubmission(std::shared_ptr<NWeb::NWebDataResubmissionCallback> handler)715 void WebClientImpl::OnDataResubmission(std::shared_ptr<NWeb::NWebDataResubmissionCallback> handler)
716 {
717     ContainerScope scope(instanceId_);
718     auto delegate = webDelegate_.Upgrade();
719     CHECK_NULL_VOID(delegate);
720     CHECK_NULL_VOID(handler);
721     delegate->OnDataResubmitted(handler);
722 }
723 
OnNavigationEntryCommitted(std::shared_ptr<NWeb::NWebLoadCommittedDetails> details)724 void WebClientImpl::OnNavigationEntryCommitted(
725     std::shared_ptr<NWeb::NWebLoadCommittedDetails> details)
726 {
727     ContainerScope scope(instanceId_);
728     auto delegate = webDelegate_.Upgrade();
729     CHECK_NULL_VOID(delegate);
730     CHECK_NULL_VOID(details);
731     delegate->OnNavigationEntryCommitted(details);
732 }
733 
OnPageIcon(const void * data,size_t width,size_t height,NWeb::ImageColorType colorType,NWeb::ImageAlphaType alphaType)734 void WebClientImpl::OnPageIcon(const void* data,
735                                size_t width,
736                                size_t height,
737                                NWeb::ImageColorType colorType,
738                                NWeb::ImageAlphaType alphaType)
739 {
740     ContainerScope scope(instanceId_);
741     auto delegate = webDelegate_.Upgrade();
742     CHECK_NULL_VOID(delegate);
743     delegate->OnFaviconReceived(data, width, height, colorType, alphaType);
744 }
745 
OnDesktopIconUrl(const std::string & icon_url,bool precomposed)746 void WebClientImpl::OnDesktopIconUrl(const std::string& icon_url, bool precomposed)
747 {
748     ContainerScope scope(instanceId_);
749     auto delegate = webDelegate_.Upgrade();
750     CHECK_NULL_VOID(delegate);
751     delegate->OnTouchIconUrl(icon_url, precomposed);
752 }
753 
OnCursorChange(const NWeb::CursorType & type,const NWeb::NWebCursorInfo & info)754 bool WebClientImpl::OnCursorChange(const NWeb::CursorType& type, const NWeb::NWebCursorInfo& info)
755 {
756     ContainerScope scope(instanceId_);
757     auto delegate = webDelegate_.Upgrade();
758     CHECK_NULL_RETURN(delegate, false);
759     return delegate->OnCursorChange(type, info);
760 }
761 
OnSelectPopupMenu(std::shared_ptr<OHOS::NWeb::NWebSelectPopupMenuParam> params,std::shared_ptr<OHOS::NWeb::NWebSelectPopupMenuCallback> callback)762 void WebClientImpl::OnSelectPopupMenu(
763     std::shared_ptr<OHOS::NWeb::NWebSelectPopupMenuParam> params,
764     std::shared_ptr<OHOS::NWeb::NWebSelectPopupMenuCallback> callback)
765 {
766     ContainerScope scope(instanceId_);
767     auto delegate = webDelegate_.Upgrade();
768     CHECK_NULL_VOID(delegate);
769     delegate->OnSelectPopupMenu(params, callback);
770 }
771 
ReleaseSurface()772 void ReleaseSurfaceImpl::ReleaseSurface()
773 {
774     ContainerScope scope(instanceId_);
775     if (!surfaceDelegate_) {
776         return;
777     }
778     surfaceDelegate_->ReleaseSurface();
779 }
780 
OnAudioStateChanged(bool playing)781 void WebClientImpl::OnAudioStateChanged(bool playing)
782 {
783     ContainerScope scope(instanceId_);
784     auto delegate = webDelegate_.Upgrade();
785     CHECK_NULL_VOID(delegate);
786     delegate->OnAudioStateChanged(playing);
787 }
788 
OnFirstContentfulPaint(int64_t navigationStartTick,int64_t firstContentfulPaintMs)789 void WebClientImpl::OnFirstContentfulPaint(int64_t navigationStartTick, int64_t firstContentfulPaintMs)
790 {
791     ContainerScope scope(instanceId_);
792     auto delegate = webDelegate_.Upgrade();
793     CHECK_NULL_VOID(delegate);
794     delegate->OnFirstContentfulPaint(navigationStartTick, firstContentfulPaintMs);
795 }
796 
OnSafeBrowsingCheckResult(int threat_type)797 void WebClientImpl::OnSafeBrowsingCheckResult(int threat_type)
798 {
799     ContainerScope scope(instanceId_);
800     auto delegate = webDelegate_.Upgrade();
801     CHECK_NULL_VOID(delegate);
802     delegate->OnSafeBrowsingCheckResult(threat_type);
803 }
804 
OnCompleteSwapWithNewSize()805 void WebClientImpl::OnCompleteSwapWithNewSize()
806 {
807     ContainerScope scope(instanceId_);
808     auto delegate = webDelegate_.Upgrade();
809     CHECK_NULL_VOID(delegate);
810     delegate->OnCompleteSwapWithNewSize();
811 }
812 
OnResizeNotWork()813 void WebClientImpl::OnResizeNotWork()
814 {
815     ContainerScope scope(instanceId_);
816     auto delegate = webDelegate_.Upgrade();
817     CHECK_NULL_VOID(delegate);
818     delegate->OnResizeNotWork();
819 }
820 
OnGetTouchHandleHotZone(NWeb::TouchHandleHotZone & hotZone)821 void WebClientImpl::OnGetTouchHandleHotZone(NWeb::TouchHandleHotZone& hotZone)
822 {
823     ContainerScope scope(instanceId_);
824     auto delegate = webDelegate_.Upgrade();
825     CHECK_NULL_VOID(delegate);
826     delegate->OnGetTouchHandleHotZone(hotZone);
827 }
828 
OnDateTimeChooserPopup(const NWeb::DateTimeChooser & chooser,const std::vector<NWeb::DateTimeSuggestion> & suggestions,std::shared_ptr<NWeb::NWebDateTimeChooserCallback> callback)829 void WebClientImpl::OnDateTimeChooserPopup(
830     const NWeb::DateTimeChooser& chooser,
831     const std::vector<NWeb::DateTimeSuggestion>& suggestions,
832     std::shared_ptr<NWeb::NWebDateTimeChooserCallback> callback)
833 {
834     ContainerScope scope(instanceId_);
835     auto delegate = webDelegate_.Upgrade();
836     CHECK_NULL_VOID(delegate);
837     delegate->OnDateTimeChooserPopup(chooser, suggestions, callback);
838 }
839 
OnDateTimeChooserClose()840 void WebClientImpl::OnDateTimeChooserClose()
841 {
842     ContainerScope scope(instanceId_);
843     auto delegate = webDelegate_.Upgrade();
844     CHECK_NULL_VOID(delegate);
845     delegate->OnDateTimeChooserClose();
846 }
847 
OnOverScroll(float xOffset,float yOffset)848 void WebClientImpl::OnOverScroll(float xOffset, float yOffset)
849 {
850     ContainerScope scope(instanceId_);
851     auto delegate = webDelegate_.Upgrade();
852     CHECK_NULL_VOID(delegate);
853     delegate->OnOverScroll(xOffset, yOffset);
854 }
855 
OnOverScrollFlingVelocity(float xVelocity,float yVelocity,bool isFling)856 void WebClientImpl::OnOverScrollFlingVelocity(float xVelocity, float yVelocity, bool isFling)
857 {
858     ContainerScope scope(instanceId_);
859     auto delegate = webDelegate_.Upgrade();
860     CHECK_NULL_VOID(delegate);
861     delegate->OnOverScrollFlingVelocity(xVelocity, yVelocity, isFling);
862 }
863 
OnOverScrollFlingEnd()864 void WebClientImpl::OnOverScrollFlingEnd() {}
865 
OnScrollState(bool scrollState)866 void WebClientImpl::OnScrollState(bool scrollState)
867 {
868     ContainerScope scope(instanceId_);
869     auto delegate = webDelegate_.Upgrade();
870     CHECK_NULL_VOID(delegate);
871     delegate->OnScrollState(scrollState);
872 }
OnNativeEmbedLifecycleChange(const NWeb::NativeEmbedDataInfo & dataInfo)873 void WebClientImpl::OnNativeEmbedLifecycleChange(const NWeb::NativeEmbedDataInfo& dataInfo)
874 {
875     ContainerScope scope(instanceId_);
876     auto delegate = webDelegate_.Upgrade();
877     CHECK_NULL_VOID(delegate);
878     delegate->OnNativeEmbedLifecycleChange(dataInfo);
879 }
OnNativeEmbedGestureEvent(const NWeb::NativeEmbedTouchEvent & event)880 void WebClientImpl::OnNativeEmbedGestureEvent(const NWeb::NativeEmbedTouchEvent& event)
881 {
882     ContainerScope scope(instanceId_);
883     auto delegate = webDelegate_.Upgrade();
884     CHECK_NULL_VOID(delegate);
885     delegate->OnNativeEmbedGestureEvent(event);
886 }
887 
OnRootLayerChanged(int width,int height)888 void WebClientImpl::OnRootLayerChanged(int width, int height)
889 {
890     ContainerScope scope(instanceId_);
891     auto delegate = webDelegate_.Upgrade();
892     CHECK_NULL_VOID(delegate);
893     delegate->OnRootLayerChanged(width, height);
894 }
895 
FilterScrollEvent(const float x,const float y,const float xVelocity,const float yVelocity)896 bool WebClientImpl::FilterScrollEvent(const float x, const float y, const float xVelocity, const float yVelocity)
897 {
898     ContainerScope scope(instanceId_);
899     auto delegate = webDelegate_.Upgrade();
900     CHECK_NULL_RETURN(delegate, false);
901     return delegate->FilterScrollEvent(x, y, xVelocity, yVelocity);
902 }
903 
904 } // namespace OHOS::Ace
905