• 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 "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 
HandleHeadersVal(const std::map<std::string,std::string> & response_headers)44     void HandleHeadersVal(const std::map<std::string, std::string>& response_headers) override
45     {
46         if (nwebResponse_ == nullptr) {
47             return;
48         }
49         nwebResponse_->PutResponseHeaders(response_headers);
50     }
51 
HandleEncoding(std::string & encoding)52     void HandleEncoding(std::string& encoding) override
53     {
54         if (nwebResponse_ == nullptr) {
55             return;
56         }
57         nwebResponse_->PutResponseEncoding(encoding);
58     }
59 
HandleMimeType(std::string & mimeType)60     void HandleMimeType(std::string& mimeType) override
61     {
62         if (nwebResponse_ == nullptr) {
63             return;
64         }
65         nwebResponse_->PutResponseMimeType(mimeType);
66     }
67 
HandleStatusCodeAndReason(int32_t statusCode,std::string & reason)68     void HandleStatusCodeAndReason(int32_t statusCode, std::string& reason) override
69     {
70         if (nwebResponse_ == nullptr) {
71             return;
72         }
73         nwebResponse_->PutResponseStateAndStatuscode(statusCode, reason);
74     }
75 
HandleResponseStatus(bool isReady)76     void HandleResponseStatus(bool isReady) override
77     {
78         if (nwebResponse_ == nullptr) {
79             return;
80         }
81         nwebResponse_->PutResponseDataStatus(isReady);
82     }
83 
84 private:
85     std::shared_ptr<OHOS::NWeb::NWebUrlResourceResponse> nwebResponse_;
86 };
87 
OnJsCommonDialog(const WebClientImpl * webClientImpl,DialogEventType dialogEventType,std::shared_ptr<NWeb::NWebJSDialogResult> result,const std::string & url,const std::string & message,const std::string & value="")88 bool OnJsCommonDialog(
89     const WebClientImpl* webClientImpl,
90     DialogEventType dialogEventType,
91     std::shared_ptr<NWeb::NWebJSDialogResult> result,
92     const std::string &url,
93     const std::string &message,
94     const std::string &value = "")
95 {
96     bool jsResult = false;
97     auto param = std::make_shared<WebDialogEvent>(url, message, value, dialogEventType,
98         AceType::MakeRefPtr<ResultOhos>(result));
99     auto task = Container::CurrentTaskExecutor();
100     if (task == nullptr) {
101         LOGW("can't get task executor");
102         return false;
103     }
104     task->PostSyncTask([&webClientImpl, dialogEventType, &param, &jsResult] {
105         if (webClientImpl == nullptr) {
106             return;
107         }
108         auto delegate = webClientImpl->GetWebDelegate();
109         if (delegate) {
110             jsResult = delegate->OnCommonDialog(param, dialogEventType);
111         }
112         },
113         OHOS::Ace::TaskExecutor::TaskType::JS);
114     LOGI("OnJsCommonDialog result:%{public}d", jsResult);
115     return jsResult;
116 }
117 
OnDownloadStart(const std::string & url,const std::string & userAgent,const std::string & contentDisposition,const std::string & mimetype,long contentLength)118 void DownloadListenerImpl::OnDownloadStart(const std::string& url, const std::string& userAgent,
119     const std::string& contentDisposition, const std::string& mimetype, long contentLength)
120 {
121     ContainerScope scope(instanceId_);
122     LOGI("OnDownloadStart.");
123     auto delegate = webDelegate_.Upgrade();
124     if (!delegate) {
125         return;
126     }
127     delegate->OnDownloadStart(url, userAgent, contentDisposition, mimetype, contentLength);
128 }
129 
OnFindResultReceived(const int activeMatchOrdinal,const int numberOfMatches,const bool isDoneCounting)130 void FindListenerImpl::OnFindResultReceived(
131     const int activeMatchOrdinal, const int numberOfMatches, const bool isDoneCounting)
132 {
133     ContainerScope scope(instanceId_);
134     auto delegate = webDelegate_.Upgrade();
135     if (!delegate) {
136         return;
137     }
138     delegate->OnSearchResultReceive(activeMatchOrdinal, numberOfMatches, isDoneCounting);
139 }
140 
OnPageLoadEnd(int httpStatusCode,const std::string & url)141 void WebClientImpl::OnPageLoadEnd(int httpStatusCode, const std::string& url)
142 {
143     ContainerScope scope(instanceId_);
144     auto delegate = webDelegate_.Upgrade();
145     if (!delegate) {
146         return;
147     }
148     delegate->OnPageFinished(url);
149 }
150 
OnFocus()151 void WebClientImpl::OnFocus()
152 {
153     ContainerScope scope(instanceId_);
154     auto delegate = webDelegate_.Upgrade();
155     if (!delegate) {
156         return;
157     }
158     delegate->OnRequestFocus();
159     delegate->RequestFocus();
160 }
161 
OnConsoleLog(const OHOS::NWeb::NWebConsoleLog & message)162 bool WebClientImpl::OnConsoleLog(const OHOS::NWeb::NWebConsoleLog& message)
163 {
164     ContainerScope scope(instanceId_);
165     bool jsMessage = false;
166     auto task = Container::CurrentTaskExecutor();
167     if (!task) {
168         return false;
169     }
170     task->PostSyncTask([webClient = this, &message, &jsMessage] {
171         if (!webClient) {
172             return;
173         }
174         auto delegate = webClient->webDelegate_.Upgrade();
175         if (delegate) {
176             jsMessage = delegate->OnConsoleLog(std::make_shared<OHOS::NWeb::NWebConsoleLog>(message));
177         }
178         },
179         OHOS::Ace::TaskExecutor::TaskType::JS);
180 
181     return jsMessage;
182 }
183 
OnPageLoadBegin(const std::string & url)184 void WebClientImpl::OnPageLoadBegin(const std::string& url)
185 {
186     ContainerScope scope(instanceId_);
187     auto delegate = webDelegate_.Upgrade();
188     if (!delegate) {
189         return;
190     }
191     delegate->OnPageStarted(url);
192 }
193 
OnLoadingProgress(int newProgress)194 void WebClientImpl::OnLoadingProgress(int newProgress)
195 {
196     ContainerScope scope(instanceId_);
197     auto delegate = webDelegate_.Upgrade();
198     if (!delegate) {
199         return;
200     }
201     delegate->OnProgressChanged(newProgress);
202 }
203 
OnPageTitle(const std::string & title)204 void WebClientImpl::OnPageTitle(const std::string &title)
205 {
206     ContainerScope scope(instanceId_);
207     auto delegate = webDelegate_.Upgrade();
208     if (!delegate) {
209         return;
210     }
211     delegate->OnReceivedTitle(title);
212 }
213 
OnFullScreenExit()214 void WebClientImpl::OnFullScreenExit()
215 {
216     ContainerScope scope(instanceId_);
217     auto delegate = webDelegate_.Upgrade();
218     CHECK_NULL_VOID(delegate);
219     delegate->OnFullScreenExit();
220 }
221 
OnFullScreenEnter(std::shared_ptr<NWeb::NWebFullScreenExitHandler> handler)222 void WebClientImpl::OnFullScreenEnter(std::shared_ptr<NWeb::NWebFullScreenExitHandler> handler)
223 {
224     ContainerScope scope(instanceId_);
225     auto delegate = webDelegate_.Upgrade();
226     CHECK_NULL_VOID(delegate);
227     CHECK_NULL_VOID(handler);
228     delegate->OnFullScreenEnter(handler);
229 }
230 
OnGeolocationHide()231 void WebClientImpl::OnGeolocationHide()
232 {
233     ContainerScope scope(instanceId_);
234     auto delegate = webDelegate_.Upgrade();
235     if (!delegate) {
236         return;
237     }
238     delegate->OnGeolocationPermissionsHidePrompt();
239 }
240 
OnGeolocationShow(const std::string & origin,std::shared_ptr<OHOS::NWeb::NWebGeolocationCallbackInterface> callback)241 void WebClientImpl::OnGeolocationShow(const std::string& origin,
242     std::shared_ptr<OHOS::NWeb::NWebGeolocationCallbackInterface> callback)
243 {
244     ContainerScope scope(instanceId_);
245     auto delegate = webDelegate_.Upgrade();
246     if (!delegate) {
247         return;
248     }
249     delegate->OnGeolocationPermissionsShowPrompt(origin, callback);
250 }
251 
SetNWeb(std::shared_ptr<OHOS::NWeb::NWeb> nweb)252 void WebClientImpl::SetNWeb(std::shared_ptr<OHOS::NWeb::NWeb> nweb)
253 {
254     webviewWeak_ = nweb;
255 }
256 
OnProxyDied()257 void WebClientImpl::OnProxyDied()
258 {
259     auto delegate = webDelegate_.Upgrade();
260     if (!delegate) {
261         return;
262     }
263 }
264 
OnResourceLoadError(std::shared_ptr<NWeb::NWebUrlResourceRequest> request,std::shared_ptr<NWeb::NWebUrlResourceError> error)265 void WebClientImpl::OnResourceLoadError(
266     std::shared_ptr<NWeb::NWebUrlResourceRequest> request, std::shared_ptr<NWeb::NWebUrlResourceError> error)
267 {
268     ContainerScope scope(instanceId_);
269     auto delegate = webDelegate_.Upgrade();
270     if (!delegate) {
271         return;
272     }
273     delegate->OnErrorReceive(request, error);
274 }
275 
OnHttpError(std::shared_ptr<OHOS::NWeb::NWebUrlResourceRequest> request,std::shared_ptr<OHOS::NWeb::NWebUrlResourceResponse> response)276 void WebClientImpl::OnHttpError(std::shared_ptr<OHOS::NWeb::NWebUrlResourceRequest> request,
277     std::shared_ptr<OHOS::NWeb::NWebUrlResourceResponse> response)
278 {
279     ContainerScope scope(instanceId_);
280     auto delegate = webDelegate_.Upgrade();
281     if (!delegate) {
282         return;
283     }
284     delegate->OnHttpErrorReceive(request, response);
285 }
286 
OnMessage(const std::string & param)287 void WebClientImpl::OnMessage(const std::string& param)
288 {
289     ContainerScope scope(instanceId_);
290     auto delegate = webDelegate_.Upgrade();
291     if (!delegate) {
292         return;
293     }
294     delegate->OnMessage(param);
295 }
296 
OnRouterPush(const std::string & param)297 void WebClientImpl::OnRouterPush(const std::string& param)
298 {
299     ContainerScope scope(instanceId_);
300     auto delegate = webDelegate_.Upgrade();
301     if (!delegate) {
302         return;
303     }
304     delegate->OnRouterPush(param);
305 }
306 
OnHandleInterceptUrlLoading(const std::string & url)307 bool WebClientImpl::OnHandleInterceptUrlLoading(const std::string& url)
308 {
309     ContainerScope scope(instanceId_);
310     auto delegate = webDelegate_.Upgrade();
311     if (!delegate) {
312         return false;
313     }
314     return delegate->OnHandleInterceptUrlLoading(url);
315 }
316 
OnHandleInterceptRequest(std::shared_ptr<OHOS::NWeb::NWebUrlResourceRequest> request)317 std::shared_ptr<OHOS::NWeb::NWebUrlResourceResponse> WebClientImpl::OnHandleInterceptRequest(
318     std::shared_ptr<OHOS::NWeb::NWebUrlResourceRequest> request)
319 {
320     ContainerScope scope(instanceId_);
321 
322     LOGI("OnHandleInterceptRequest url %{private}s", request->Url().c_str());
323     auto delegate = webDelegate_.Upgrade();
324     if (!delegate) {
325         return nullptr;
326     }
327     if (delegate->IsEmptyOnInterceptRequest() == true) {
328         LOGI("OnHandleInterceptRequest is empty");
329         return nullptr;
330     }
331 
332     auto webRequest = AceType::MakeRefPtr<WebRequest>(request->RequestHeaders(),
333         request->Method(), request->Url(), request->FromGesture(),
334         request->IsAboutMainFrame(), request->IsRequestRedirect());
335     auto param = std::make_shared<OnInterceptRequestEvent>(webRequest);
336 
337     RefPtr<WebResponse> webResponse = nullptr;
338     auto task = Container::CurrentTaskExecutor();
339     if (task == nullptr) {
340         LOGE("can't get task executor");
341         return nullptr;
342     }
343     task->PostSyncTask([&delegate, &webResponse, &param] {
344             webResponse = delegate->OnInterceptRequest(param);
345         }, OHOS::Ace::TaskExecutor::TaskType::JS);
346 
347     if (webResponse == nullptr) {
348         LOGI("webResponse is null");
349         return nullptr;
350     }
351     std::string data = webResponse->GetData();
352     LOGI("intercept Encoding %{public}s",  webResponse->GetEncoding().c_str());
353     LOGI("intercept GetMimeType %{public}s",  webResponse->GetMimeType().c_str());
354     LOGI("intercept GetStatusCode %{public}d",  webResponse->GetStatusCode());
355     LOGI("intercept GetReason %{public}s",  webResponse->GetReason().c_str());
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     if (webResponse->IsFileHandle() == true) {
360         nwebResponse->PutResponseFileHandle(webResponse->GetFileHandle());
361     }
362     if (webResponse->GetResponseStatus() == false) {
363         LOGI("intercept response async Handle");
364         std::shared_ptr<NWebResponseAsyncHandle> asyncHandle = std::make_shared<NWebResponseAsyncHandle>(nwebResponse);
365         webResponse->SetAsyncHandle(asyncHandle);
366         nwebResponse->PutResponseDataStatus(false);
367     }
368     return nwebResponse;
369 }
370 
OnAlertDialogByJS(const std::string & url,const std::string & message,std::shared_ptr<NWeb::NWebJSDialogResult> result)371 bool WebClientImpl::OnAlertDialogByJS(
372     const std::string &url, const std::string &message, std::shared_ptr<NWeb::NWebJSDialogResult> result)
373 {
374     LOGI("OnAlertDialogByJS");
375     ContainerScope scope(instanceId_);
376     return OnJsCommonDialog(this, DialogEventType::DIALOG_EVENT_ALERT, result, url, message);
377 }
378 
OnBeforeUnloadByJS(const std::string & url,const std::string & message,std::shared_ptr<NWeb::NWebJSDialogResult> result)379 bool WebClientImpl::OnBeforeUnloadByJS(
380     const std::string &url, const std::string &message, std::shared_ptr<NWeb::NWebJSDialogResult> result)
381 {
382     LOGI("OnBeforeUnloadByJS");
383     ContainerScope scope(instanceId_);
384     return OnJsCommonDialog(this, DialogEventType::DIALOG_EVENT_BEFORE_UNLOAD, result, url, message);
385 }
386 
OnConfirmDialogByJS(const std::string & url,const std::string & message,std::shared_ptr<NWeb::NWebJSDialogResult> result)387 bool WebClientImpl::OnConfirmDialogByJS(
388     const std::string &url, const std::string &message, std::shared_ptr<NWeb::NWebJSDialogResult> result)
389 {
390     LOGI("OnConfirmDialogByJS");
391     ContainerScope scope(instanceId_);
392     return OnJsCommonDialog(this, DialogEventType::DIALOG_EVENT_CONFIRM, result, url, message);
393 }
394 
OnPromptDialogByJS(const std::string & url,const std::string & message,const std::string & defaultValue,std::shared_ptr<NWeb::NWebJSDialogResult> result)395 bool WebClientImpl::OnPromptDialogByJS(const std::string &url, const std::string &message,
396     const std::string &defaultValue, std::shared_ptr<NWeb::NWebJSDialogResult> result)
397 {
398     LOGI("OnPromptDialogByJS: %{public}s", defaultValue.c_str());
399     ContainerScope scope(instanceId_);
400     return OnJsCommonDialog(this, DialogEventType::DIALOG_EVENT_PROMPT, result, url, message, defaultValue);
401 }
402 
OnRenderExited(OHOS::NWeb::RenderExitReason reason)403 void WebClientImpl::OnRenderExited(OHOS::NWeb::RenderExitReason reason)
404 {
405     ContainerScope scope(instanceId_);
406     auto delegate = webDelegate_.Upgrade();
407     if (!delegate) {
408         return;
409     }
410     delegate->OnRenderExited(reason);
411 }
412 
OnRefreshAccessedHistory(const std::string & url,bool isReload)413 void WebClientImpl::OnRefreshAccessedHistory(const std::string& url, bool isReload)
414 {
415     ContainerScope scope(instanceId_);
416     auto delegate = webDelegate_.Upgrade();
417     if (!delegate) {
418         return;
419     }
420     delegate->OnRefreshAccessedHistory(url, isReload);
421 }
422 
OnFileSelectorShow(std::shared_ptr<NWeb::FileSelectorCallback> callback,std::shared_ptr<NWeb::NWebFileSelectorParams> params)423 bool WebClientImpl::OnFileSelectorShow(
424     std::shared_ptr<NWeb::FileSelectorCallback> callback,
425     std::shared_ptr<NWeb::NWebFileSelectorParams> params)
426 {
427     ContainerScope scope(instanceId_);
428     bool jsResult = false;
429     auto param = std::make_shared<FileSelectorEvent>(AceType::MakeRefPtr<FileSelectorParamOhos>(params),
430         AceType::MakeRefPtr<FileSelectorResultOhos>(callback));
431     auto task = Container::CurrentTaskExecutor();
432     if (task == nullptr) {
433         LOGW("can't get task executor");
434         return false;
435     }
436     task->PostSyncTask([webClient = this, &param, &jsResult] {
437         if (webClient == nullptr) {
438             return;
439         }
440         auto delegate = webClient->GetWebDelegate();
441         if (delegate) {
442             jsResult = delegate->OnFileSelectorShow(param);
443         }
444         },
445         OHOS::Ace::TaskExecutor::TaskType::JS);
446     LOGI("OnFileSelectorShow result:%{public}d", jsResult);
447     return jsResult;
448 }
449 
OnResource(const std::string & url)450 void WebClientImpl::OnResource(const std::string& url)
451 {
452     ContainerScope scope(instanceId_);
453     auto delegate = webDelegate_.Upgrade();
454     if (!delegate) {
455         return;
456     }
457     delegate->OnResourceLoad(url);
458 }
459 
OnScaleChanged(float oldScaleFactor,float newScaleFactor)460 void WebClientImpl::OnScaleChanged(float oldScaleFactor, float newScaleFactor)
461 {
462     ContainerScope scope(instanceId_);
463     auto delegate = webDelegate_.Upgrade();
464     if (!delegate) {
465         return;
466     }
467     delegate->OnScaleChange(oldScaleFactor, newScaleFactor);
468 }
469 
OnScroll(double xOffset,double yOffset)470 void WebClientImpl::OnScroll(double xOffset, double yOffset)
471 {
472     ContainerScope scope(instanceId_);
473     auto delegate = webDelegate_.Upgrade();
474     if (!delegate) {
475         return;
476     }
477     delegate->OnScroll(xOffset, yOffset);
478 }
479 
OnHttpAuthRequestByJS(std::shared_ptr<NWeb::NWebJSHttpAuthResult> result,const std::string & host,const std::string & realm)480 bool WebClientImpl::OnHttpAuthRequestByJS(std::shared_ptr<NWeb::NWebJSHttpAuthResult> result, const std::string &host,
481     const std::string &realm)
482 {
483     LOGI("OnHttpAuthRequestByJS");
484     ContainerScope scope(instanceId_);
485 
486     bool jsResult = false;
487     auto param = std::make_shared<WebHttpAuthEvent>(AceType::MakeRefPtr<AuthResultOhos>(result), host, realm);
488     auto task = Container::CurrentTaskExecutor();
489     if (task == nullptr) {
490         LOGW("can't get task executor");
491         return false;
492     }
493     task->PostSyncTask([webClient = this, &param, &jsResult] {
494             if (!webClient) {
495                 return;
496             }
497             auto delegate = webClient->webDelegate_.Upgrade();
498             if (delegate) {
499                 jsResult = delegate->OnHttpAuthRequest(param);
500             }
501         }, OHOS::Ace::TaskExecutor::TaskType::JS);
502 
503     LOGI("OnHttpAuthRequestByJS result:%{public}d", jsResult);
504     return jsResult;
505 }
506 
OnSslErrorRequestByJS(std::shared_ptr<NWeb::NWebJSSslErrorResult> result,OHOS::NWeb::SslError error)507 bool WebClientImpl::OnSslErrorRequestByJS(std::shared_ptr<NWeb::NWebJSSslErrorResult> result,
508     OHOS::NWeb::SslError error)
509 {
510     LOGI("OnSslErrorRequestByJS");
511     ContainerScope scope(instanceId_);
512 
513     bool jsResult = false;
514     auto param = std::make_shared<WebSslErrorEvent>(AceType::MakeRefPtr<SslErrorResultOhos>(result), static_cast<int32_t>(error));
515     auto task = Container::CurrentTaskExecutor();
516     if (task == nullptr) {
517         LOGW("can't get task executor");
518         return false;
519     }
520     task->PostSyncTask([webClient = this, &param, &jsResult] {
521             if (!webClient) {
522                 return;
523             }
524             auto delegate = webClient->webDelegate_.Upgrade();
525             if (delegate) {
526                 jsResult = delegate->OnSslErrorRequest(param);
527             }
528         }, OHOS::Ace::TaskExecutor::TaskType::JS);
529 
530     LOGI("OnSslErrorRequestByJS result:%{public}d", jsResult);
531     return jsResult;
532 }
533 
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)534 bool WebClientImpl::OnSslSelectCertRequestByJS(
535     std::shared_ptr<NWeb::NWebJSSslSelectCertResult> result,
536     const std::string& host,
537     int port,
538     const std::vector<std::string>& keyTypes,
539     const std::vector<std::string>& issuers)
540 {
541     ContainerScope scope(instanceId_);
542 
543     bool jsResult = false;
544     auto param = std::make_shared<WebSslSelectCertEvent>(AceType::MakeRefPtr<SslSelectCertResultOhos>(result),
545         host, port, keyTypes, issuers);
546     auto task = Container::CurrentTaskExecutor();
547     if (task == nullptr) {
548         LOGW("can't get task executor");
549         return false;
550     }
551 
552     task->PostSyncTask([webClient = this, &param, &jsResult] {
553             if (!webClient) {
554                 return;
555             }
556             auto delegate = webClient->webDelegate_.Upgrade();
557             if (delegate) {
558                 jsResult = delegate->OnSslSelectCertRequest(param);
559             }
560         }, OHOS::Ace::TaskExecutor::TaskType::JS);
561 
562     return jsResult;
563 }
564 
OnPermissionRequest(std::shared_ptr<NWeb::NWebAccessRequest> request)565 void WebClientImpl::OnPermissionRequest(std::shared_ptr<NWeb::NWebAccessRequest> request)
566 {
567     LOGI("OnPermissionRequest");
568     ContainerScope scope(instanceId_);
569     auto delegate = webDelegate_.Upgrade();
570     CHECK_NULL_VOID(delegate);
571     delegate->OnPermissionRequestPrompt(request);
572 }
573 
RunContextMenu(std::shared_ptr<NWeb::NWebContextMenuParams> params,std::shared_ptr<NWeb::NWebContextMenuCallback> callback)574 bool WebClientImpl::RunContextMenu(
575     std::shared_ptr<NWeb::NWebContextMenuParams> params,
576     std::shared_ptr<NWeb::NWebContextMenuCallback> callback)
577 {
578     ContainerScope scope(instanceId_);
579     bool jsResult = false;
580     auto param = std::make_shared<ContextMenuEvent>(AceType::MakeRefPtr<ContextMenuParamOhos>(params),
581         AceType::MakeRefPtr<ContextMenuResultOhos>(callback));
582     auto task = Container::CurrentTaskExecutor();
583     if (task == nullptr) {
584         LOGW("can't get task executor");
585         return false;
586     }
587     task->PostSyncTask([webClient = this, &param, &jsResult] {
588         if (webClient == nullptr) {
589             return;
590         }
591         auto delegate = webClient->GetWebDelegate();
592         if (delegate) {
593             jsResult = delegate->OnContextMenuShow(param);
594         }
595         },
596         OHOS::Ace::TaskExecutor::TaskType::JS);
597     LOGI("OnContextMenuEventShow result:%{public}d", jsResult);
598     return jsResult;
599 }
600 
RunQuickMenu(std::shared_ptr<NWeb::NWebQuickMenuParams> params,std::shared_ptr<NWeb::NWebQuickMenuCallback> callback)601 bool WebClientImpl::RunQuickMenu(std::shared_ptr<NWeb::NWebQuickMenuParams> params,
602                                  std::shared_ptr<NWeb::NWebQuickMenuCallback> callback)
603 {
604     if (!params || !callback) {
605         return false;
606     }
607     ContainerScope scope(instanceId_);
608     auto task = Container::CurrentTaskExecutor();
609     if (task == nullptr) {
610         LOGW("can't get task executor");
611         return false;
612     }
613     auto delegate = webDelegate_.Upgrade();
614     if (!delegate) {
615         return false;
616     }
617     return delegate->RunQuickMenu(params, callback);
618 }
619 
OnQuickMenuDismissed()620 void WebClientImpl::OnQuickMenuDismissed()
621 {
622     ContainerScope scope(instanceId_);
623     auto task = Container::CurrentTaskExecutor();
624     if (task == nullptr) {
625         LOGW("can't get task executor");
626         return;
627     }
628     auto delegate = webDelegate_.Upgrade();
629     if (!delegate) {
630         return;
631     }
632     delegate->OnQuickMenuDismissed();
633 }
634 
OnTouchSelectionChanged(std::shared_ptr<OHOS::NWeb::NWebTouchHandleState> insertHandle,std::shared_ptr<OHOS::NWeb::NWebTouchHandleState> startSelectionHandle,std::shared_ptr<OHOS::NWeb::NWebTouchHandleState> endSelectionHandle)635 void WebClientImpl::OnTouchSelectionChanged(
636     std::shared_ptr<OHOS::NWeb::NWebTouchHandleState> insertHandle,
637     std::shared_ptr<OHOS::NWeb::NWebTouchHandleState> startSelectionHandle,
638     std::shared_ptr<OHOS::NWeb::NWebTouchHandleState> endSelectionHandle)
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->OnTouchSelectionChanged(
651         insertHandle, startSelectionHandle, endSelectionHandle);
652 }
653 
OnDragAndDropData(const void * data,size_t len,const NWeb::ImageOptions & opt)654 bool WebClientImpl::OnDragAndDropData(const void* data, size_t len, const NWeb::ImageOptions& opt)
655 {
656     ContainerScope scope(instanceId_);
657     auto delegate = webDelegate_.Upgrade();
658     if (!delegate) {
659         return false;
660     }
661     return delegate->OnDragAndDropData(data, len, opt.width, opt.height);
662 }
663 
OnWindowNewByJS(const std::string & targetUrl,bool isAlert,bool isUserTrigger,std::shared_ptr<NWeb::NWebControllerHandler> handler)664 void WebClientImpl::OnWindowNewByJS(
665     const std::string& targetUrl,
666     bool isAlert,
667     bool isUserTrigger,
668     std::shared_ptr<NWeb::NWebControllerHandler> handler)
669 {
670     LOGI("OnWindowNewByJS");
671     ContainerScope scope(instanceId_);
672     auto delegate = webDelegate_.Upgrade();
673     CHECK_NULL_VOID(delegate);
674     delegate->OnWindowNew(targetUrl, isAlert, isUserTrigger, handler);
675 }
676 
OnWindowExitByJS()677 void WebClientImpl::OnWindowExitByJS()
678 {
679     LOGI("OnWindowExitByJS");
680     ContainerScope scope(instanceId_);
681     auto delegate = webDelegate_.Upgrade();
682     CHECK_NULL_VOID(delegate);
683     delegate->OnWindowExit();
684 }
685 
OnPageVisible(const std::string & url)686 void WebClientImpl::OnPageVisible(const std::string& url)
687 {
688     LOGI("OnPageVisible");
689     ContainerScope scope(instanceId_);
690     auto delegate = webDelegate_.Upgrade();
691     CHECK_NULL_VOID(delegate);
692     delegate->OnPageVisible(url);
693 }
694 
OnDataResubmission(std::shared_ptr<NWeb::NWebDataResubmissionCallback> handler)695 void WebClientImpl::OnDataResubmission(std::shared_ptr<NWeb::NWebDataResubmissionCallback> handler)
696 {
697     LOGI("OnDataResubmission");
698     ContainerScope scope(instanceId_);
699     auto delegate = webDelegate_.Upgrade();
700     CHECK_NULL_VOID(delegate);
701     CHECK_NULL_VOID(handler);
702     delegate->OnDataResubmitted(handler);
703 }
704 
OnPageIcon(const void * data,size_t width,size_t height,NWeb::ImageColorType colorType,NWeb::ImageAlphaType alphaType)705 void WebClientImpl::OnPageIcon(const void* data,
706                                size_t width,
707                                size_t height,
708                                NWeb::ImageColorType colorType,
709                                NWeb::ImageAlphaType alphaType)
710 {
711     LOGI("OnPageIcon");
712     ContainerScope scope(instanceId_);
713     auto delegate = webDelegate_.Upgrade();
714     CHECK_NULL_VOID(delegate);
715     delegate->OnFaviconReceived(data, width, height, colorType, alphaType);
716 }
717 
OnDesktopIconUrl(const std::string & icon_url,bool precomposed)718 void WebClientImpl::OnDesktopIconUrl(const std::string& icon_url, bool precomposed)
719 {
720     LOGI("OnDesktopIconUrl");
721     ContainerScope scope(instanceId_);
722     auto delegate = webDelegate_.Upgrade();
723     CHECK_NULL_VOID(delegate);
724     delegate->OnTouchIconUrl(icon_url, precomposed);
725 }
726 
OnCursorChange(const NWeb::CursorType & type,const NWeb::NWebCursorInfo & info)727 bool WebClientImpl::OnCursorChange(const NWeb::CursorType& type, const NWeb::NWebCursorInfo& info)
728 {
729     LOGI("OnCursorChange");
730     ContainerScope scope(instanceId_);
731     auto delegate = webDelegate_.Upgrade();
732     CHECK_NULL_RETURN(delegate, false);
733     return delegate->OnCursorChange(type, info);
734 }
735 
OnSelectPopupMenu(std::shared_ptr<OHOS::NWeb::NWebSelectPopupMenuParam> params,std::shared_ptr<OHOS::NWeb::NWebSelectPopupMenuCallback> callback)736 void WebClientImpl::OnSelectPopupMenu(
737     std::shared_ptr<OHOS::NWeb::NWebSelectPopupMenuParam> params,
738     std::shared_ptr<OHOS::NWeb::NWebSelectPopupMenuCallback> callback)
739 {
740     LOGI("OnSelectPopupMenu");
741     ContainerScope scope(instanceId_);
742     auto delegate = webDelegate_.Upgrade();
743     CHECK_NULL_VOID(delegate);
744     delegate->OnSelectPopupMenu(params, callback);
745 }
746 
ReleaseSurface()747 void ReleaseSurfaceImpl::ReleaseSurface()
748 {
749     ContainerScope scope(instanceId_);
750     if (!surfaceDelegate_) {
751         return;
752     }
753     surfaceDelegate_->ReleaseSurface();
754 }
755 } // namespace OHOS::Ace
756