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