• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 "webview_controller_impl.h"
17 
18 #include <cstdint>
19 #include <nweb_helper.h>
20 
21 #include "application_context.h"
22 #include "arkweb_scheme_handler.h"
23 #include "bundle_mgr_proxy.h"
24 #include "cj_common_ffi.h"
25 #include "ffi_remote_data.h"
26 #include "if_system_ability_manager.h"
27 #include "iservice_registry.h"
28 #include "native_arkweb_utils.h"
29 #include "native_interface_arkweb.h"
30 #include "nweb_precompile_callback.h"
31 #include "nweb_store_web_archive_callback.h"
32 #include "parameters.h"
33 #include "system_ability_definition.h"
34 #include "web_errors.h"
35 #include "web_native_media_player.h"
36 #include "webview_hasimage_callback.h"
37 #include "webview_javascript_execute_callback.h"
38 #include "webview_javascript_result_callback.h"
39 #include "webview_log.h"
40 #include "webview_utils.h"
41 #include "nweb_message_ext.h"
42 
43 namespace OHOS::Webview {
44     constexpr int MAX_CUSTOM_SCHEME_SIZE = 10;
45     constexpr int MAX_CUSTOM_SCHEME_NAME_LENGTH = 32;
46     std::unordered_map<int32_t, WebviewControllerImpl*> g_webview_controller_map;
47     std::string WebviewControllerImpl::customeSchemeCmdLine_ = "";
48     bool WebviewControllerImpl::existNweb_ = false;
49     bool WebviewControllerImpl::webDebuggingAccess_ = false;
50 
51     // WebMessagePortImpl
WebMessagePortImpl(int32_t nwebId,std::string port,bool isExtentionType)52     WebMessagePortImpl::WebMessagePortImpl(int32_t nwebId, std::string port, bool isExtentionType)
53         : nwebId_(nwebId), portHandle_(port), isExtentionType_(isExtentionType)
54     {}
55 
ClosePort()56     ErrCode WebMessagePortImpl::ClosePort()
57     {
58         auto nweb_ptr = NWeb::NWebHelper::Instance().GetNWeb(nwebId_);
59         if (!nweb_ptr) {
60             return NWebError::INIT_ERROR;
61         }
62 
63         nweb_ptr->ClosePort(portHandle_);
64         portHandle_.clear();
65         return NWebError::NO_ERROR;
66     }
67 
PostPortMessage(std::shared_ptr<NWeb::NWebMessage> data)68     ErrCode WebMessagePortImpl::PostPortMessage(std::shared_ptr<NWeb::NWebMessage> data)
69     {
70         auto nweb_ptr = NWeb::NWebHelper::Instance().GetNWeb(nwebId_);
71         if (!nweb_ptr) {
72             return NWebError::INIT_ERROR;
73         }
74 
75         if (portHandle_.empty()) {
76             WEBVIEWLOGE("can't post message, message port already closed");
77             return NWebError::CAN_NOT_POST_MESSAGE;
78         }
79         nweb_ptr->PostPortMessage(portHandle_, data);
80         return NWebError::NO_ERROR;
81     }
82 
SetPortMessageCallback(std::shared_ptr<NWeb::NWebMessageValueCallback> callback)83     ErrCode WebMessagePortImpl::SetPortMessageCallback(
84         std::shared_ptr<NWeb::NWebMessageValueCallback> callback)
85     {
86         auto nweb_ptr = NWeb::NWebHelper::Instance().GetNWeb(nwebId_);
87         if (!nweb_ptr) {
88             return NWebError::INIT_ERROR;
89         }
90 
91         if (portHandle_.empty()) {
92             WEBVIEWLOGE("can't register message port callback event, message port already closed");
93             return NWebError::CAN_NOT_REGISTER_MESSAGE_EVENT;
94         }
95         nweb_ptr->SetPortMessageCallback(portHandle_, callback);
96         return NWebError::NO_ERROR;
97     }
98 
GetPortHandle() const99     std::string WebMessagePortImpl::GetPortHandle() const
100     {
101         return portHandle_;
102     }
103 
OnReceiveValue(std::shared_ptr<NWeb::NWebMessage> result)104     void NWebMessageCallbackImpl::OnReceiveValue(std::shared_ptr<NWeb::NWebMessage> result)
105     {
106         WEBVIEWLOGD("message port received msg");
107         if (result == nullptr) {
108             return;
109         }
110         NWeb::NWebValue::Type type = result->GetType();
111         if (type == NWeb::NWebValue::Type::STRING) {
112             std::string msgStr = result->GetString();
113             char* message = MallocCString(msgStr);
114             RetWebMessage ret = {.messageStr = message, .messageArr = {.head = nullptr, .size = 0}};
115             callback_(ret);
116             free(message);
117         } else if (type == NWeb::NWebValue::Type::BINARY) {
118             std::vector<uint8_t> msgArr = result->GetBinary();
119             uint8_t* result = VectorToCArrUI8(msgArr);
120             if (result == nullptr) {
121                 return;
122             }
123             RetWebMessage ret = {.messageStr = nullptr, .messageArr = CArrUI8{result, msgArr.size()}};
124             callback_(ret);
125             free(result);
126         }
127     }
128 
OnReceiveValueV2(std::shared_ptr<NWeb::NWebHapValue> value)129     void NWebMessageCallbackImpl::OnReceiveValueV2(std::shared_ptr<NWeb::NWebHapValue> value)
130     {
131         std::shared_ptr<NWeb::NWebMessage> message = ConvertNwebHap2NwebMessage(value);
132         OnReceiveValue(message);
133     }
134 
OnReceiveValue(std::shared_ptr<NWeb::NWebMessage> result)135     void NWebWebMessageExtCallbackImpl::OnReceiveValue(std::shared_ptr<NWeb::NWebMessage> result)
136     {
137         WEBVIEWLOGD("message port received msg");
138         WebMessageExtImpl *webMessageExt = OHOS::FFI::FFIData::Create<WebMessageExtImpl>(result);
139         if (webMessageExt == nullptr) {
140             WEBVIEWLOGE("new WebMessageExt failed.");
141             return;
142         }
143         callback_(webMessageExt->GetID());
144     }
145 
OnReceiveValueV2(std::shared_ptr<NWeb::NWebHapValue> value)146     void NWebWebMessageExtCallbackImpl::OnReceiveValueV2(std::shared_ptr<NWeb::NWebHapValue> value)
147     {
148         WEBVIEWLOGD("message port received msg");
149         if (value == nullptr) {
150             return;
151         }
152         WebMessageExtImpl *webMessageExt = OHOS::FFI::FFIData::Create<WebMessageExtImpl>(value);
153         if (webMessageExt == nullptr) {
154             WEBVIEWLOGE("new WebMessageExt failed.");
155             return;
156         }
157         callback_(webMessageExt->GetID());
158     }
159 
WebviewControllerImpl(int32_t nwebId)160     WebviewControllerImpl::WebviewControllerImpl(int32_t nwebId) : nwebId_(nwebId)
161     {
162         if (IsInit()) {
163             std::unique_lock<std::mutex> lk(webMtx_);
164             g_webview_controller_map.emplace(nwebId, this);
165         }
166     }
167 
IsInit()168     bool WebviewControllerImpl::IsInit()
169     {
170         return NWeb::NWebHelper::Instance().GetNWeb(nwebId_) ? true : false;
171     }
172 
SetWebId(int32_t nwebId)173     void WebviewControllerImpl::SetWebId(int32_t nwebId)
174     {
175         nwebId_ = nwebId;
176         std::unique_lock<std::mutex> lk(webMtx_);
177         g_webview_controller_map.emplace(nwebId, this);
178 
179         if (webTag_.empty()) {
180             WEBVIEWLOGI("native webtag is empty, don't care because it's not a native instance");
181             return;
182         }
183 
184         auto nweb_ptr = OHOS::NWeb::NWebHelper::Instance().GetNWeb(nwebId);
185         if (nweb_ptr) {
186             OH_NativeArkWeb_BindWebTagToWebInstance(webTag_.c_str(), nweb_ptr);
187             NWeb::NWebHelper::Instance().SetWebTag(nwebId_, webTag_.c_str());
188         }
189     }
190 
InnerSetHapPath(const std::string & hapPath)191     void WebviewControllerImpl::InnerSetHapPath(const std::string &hapPath)
192     {
193         hapPath_ = hapPath;
194     }
195 
GetWebId() const196     int32_t WebviewControllerImpl::GetWebId() const
197     {
198         int32_t webId = -1;
199         auto nweb_ptr = NWeb::NWebHelper::Instance().GetNWeb(nwebId_);
200         if (nweb_ptr) {
201             webId = static_cast<int32_t>(nweb_ptr->GetWebId());
202         }
203         return webId;
204     }
205 
LoadUrl(std::string url)206     int32_t WebviewControllerImpl::LoadUrl(std::string url)
207     {
208         auto nweb_ptr = NWeb::NWebHelper::Instance().GetNWeb(nwebId_);
209         if (!nweb_ptr) {
210             return NWebError::INIT_ERROR;
211         }
212         return nweb_ptr->Load(url);
213     }
214 
LoadUrl(std::string url,std::map<std::string,std::string> httpHeaders)215     int32_t WebviewControllerImpl::LoadUrl(std::string url, std::map<std::string, std::string> httpHeaders)
216     {
217         auto nweb_ptr = NWeb::NWebHelper::Instance().GetNWeb(nwebId_);
218         if (!nweb_ptr) {
219             return NWebError::INIT_ERROR;
220         }
221         return nweb_ptr->Load(url, httpHeaders);
222     }
223 
LoadData(std::string data,std::string mimeType,std::string encoding,std::string baseUrl,std::string historyUrl)224     ErrCode WebviewControllerImpl::LoadData(std::string data, std::string mimeType, std::string encoding,
225         std::string baseUrl, std::string historyUrl)
226     {
227         auto nweb_ptr = NWeb::NWebHelper::Instance().GetNWeb(nwebId_);
228         if (!nweb_ptr) {
229             return NWebError::INIT_ERROR;
230         }
231         if (baseUrl.empty() && historyUrl.empty()) {
232             return nweb_ptr->LoadWithData(data, mimeType, encoding);
233         }
234         return nweb_ptr->LoadWithDataAndBaseUrl(baseUrl, data, mimeType, encoding, historyUrl);
235     }
236 
PreFetchPage(std::string url)237     int32_t WebviewControllerImpl::PreFetchPage(std::string url)
238     {
239         auto nweb_ptr = NWeb::NWebHelper::Instance().GetNWeb(nwebId_);
240         if (!nweb_ptr) {
241             return NWebError::INIT_ERROR;
242         }
243         std::map<std::string, std::string> httpHeaders;
244         nweb_ptr->PrefetchPage(url, httpHeaders);
245         return NWebError::NO_ERROR;
246     }
247 
PreFetchPage(std::string url,std::map<std::string,std::string> httpHeaders)248     int32_t WebviewControllerImpl::PreFetchPage(std::string url, std::map<std::string, std::string> httpHeaders)
249     {
250         auto nweb_ptr = NWeb::NWebHelper::Instance().GetNWeb(nwebId_);
251         if (!nweb_ptr) {
252             return NWebError::INIT_ERROR;
253         }
254         nweb_ptr->PrefetchPage(url, httpHeaders);
255         return NWebError::NO_ERROR;
256     }
257 
Refresh()258     void WebviewControllerImpl::Refresh()
259     {
260         auto nweb_ptr = NWeb::NWebHelper::Instance().GetNWeb(nwebId_);
261         if (nweb_ptr) {
262             nweb_ptr->Reload();
263         }
264     }
265 
SetAudioMuted(bool mute)266     int32_t WebviewControllerImpl::SetAudioMuted(bool mute)
267     {
268         auto nweb_ptr = NWeb::NWebHelper::Instance().GetNWeb(nwebId_);
269         if (!nweb_ptr) {
270             return NWebError::INIT_ERROR;
271         }
272         nweb_ptr->SetAudioMuted(mute);
273         return NWebError::NO_ERROR;
274     }
275 
GetUserAgent()276     std::string WebviewControllerImpl::GetUserAgent()
277     {
278         auto nweb_ptr = NWeb::NWebHelper::Instance().GetNWeb(nwebId_);
279         if (!nweb_ptr) {
280             return "";
281         }
282         std::shared_ptr<OHOS::NWeb::NWebPreference> setting = nweb_ptr->GetPreference();
283         if (!setting) {
284             return "";
285         }
286         return setting->DefaultUserAgent();
287     }
288 
AccessForward()289     bool WebviewControllerImpl::AccessForward()
290     {
291         bool access = false;
292         auto nweb_ptr = NWeb::NWebHelper::Instance().GetNWeb(nwebId_);
293         if (nweb_ptr) {
294             access = nweb_ptr->IsNavigateForwardAllowed();
295         }
296         return access;
297     }
298 
AccessBackward()299     bool WebviewControllerImpl::AccessBackward()
300     {
301         bool access = false;
302         auto nweb_ptr = NWeb::NWebHelper::Instance().GetNWeb(nwebId_);
303         if (nweb_ptr) {
304             access = nweb_ptr->IsNavigatebackwardAllowed();
305         }
306         return access;
307     }
308 
SetCustomUserAgent(const std::string & userAgent)309     int32_t WebviewControllerImpl::SetCustomUserAgent(const std::string& userAgent)
310     {
311         auto nweb_ptr = NWeb::NWebHelper::Instance().GetNWeb(nwebId_);
312         if (!nweb_ptr) {
313             return NWebError::INIT_ERROR;
314         }
315         std::shared_ptr<OHOS::NWeb::NWebPreference> setting = nweb_ptr->GetPreference();
316         if (!setting) {
317             return NWebError::INIT_ERROR;
318         }
319         setting->PutUserAgent(userAgent);
320         return NWebError::NO_ERROR;
321     }
322 
GetCustomUserAgent() const323     std::string WebviewControllerImpl::GetCustomUserAgent() const
324     {
325         auto nweb_ptr = NWeb::NWebHelper::Instance().GetNWeb(nwebId_);
326         if (!nweb_ptr) {
327             return "";
328         }
329         std::shared_ptr<OHOS::NWeb::NWebPreference> setting = nweb_ptr->GetPreference();
330         if (!setting) {
331             return "";
332         }
333         return setting->UserAgent();
334     }
335 
RunJavaScript(std::string script,const std::function<void (RetDataCString)> & callbackRef)336     void WebviewControllerImpl::RunJavaScript(std::string script,
337         const std::function<void(RetDataCString)>& callbackRef)
338     {
339         RetDataCString ret = { .code = NWebError::INIT_ERROR, .data = nullptr };
340         auto nweb_ptr = NWeb::NWebHelper::Instance().GetNWeb(nwebId_);
341         if (!nweb_ptr) {
342             callbackRef(ret);
343             return;
344         }
345         auto callbackImpl = std::make_shared<WebviewJavaScriptExecuteCallback>(callbackRef);
346         nweb_ptr->ExecuteJavaScript(script, callbackImpl, false);
347     }
348 
RunJavaScriptExt(std::string script,const std::function<void (RetDataI64)> & callbackRef)349     void WebviewControllerImpl::RunJavaScriptExt(std::string script,
350         const std::function<void(RetDataI64)>& callbackRef)
351     {
352         RetDataI64 ret = { .code = NWebError::INIT_ERROR, .data = 0 };
353         auto nweb_ptr = NWeb::NWebHelper::Instance().GetNWeb(nwebId_);
354         if (!nweb_ptr) {
355             callbackRef(ret);
356             return;
357         }
358         auto callbackImpl = std::make_shared<WebviewJavaScriptExtExecuteCallback>(callbackRef);
359         nweb_ptr->ExecuteJavaScript(script, callbackImpl, true);
360     }
361 
GetUrl()362     std::string WebviewControllerImpl::GetUrl()
363     {
364         std::string url = "";
365         auto nweb_ptr = NWeb::NWebHelper::Instance().GetNWeb(nwebId_);
366         if (nweb_ptr) {
367             url = nweb_ptr->GetUrl();
368         }
369         return url;
370     }
371 
GetOriginalUrl()372     std::string WebviewControllerImpl::GetOriginalUrl()
373     {
374         std::string url = "";
375         auto nweb_ptr = NWeb::NWebHelper::Instance().GetNWeb(nwebId_);
376         if (nweb_ptr) {
377             url = nweb_ptr->GetOriginalUrl();
378         }
379         return url;
380     }
381 
ScrollPageUp(bool top)382     void WebviewControllerImpl::ScrollPageUp(bool top)
383     {
384         auto nweb_ptr = NWeb::NWebHelper::Instance().GetNWeb(nwebId_);
385         if (nweb_ptr) {
386             nweb_ptr->PageUp(top);
387         }
388         return;
389     }
390 
ScrollPageDown(bool bottom)391     void WebviewControllerImpl::ScrollPageDown(bool bottom)
392     {
393         auto nweb_ptr = NWeb::NWebHelper::Instance().GetNWeb(nwebId_);
394         if (nweb_ptr) {
395             nweb_ptr->PageDown(bottom);
396         }
397         return;
398     }
399 
ScrollTo(float x,float y)400     void WebviewControllerImpl::ScrollTo(float x, float y)
401     {
402         auto nweb_ptr = NWeb::NWebHelper::Instance().GetNWeb(nwebId_);
403         if (nweb_ptr) {
404             nweb_ptr->ScrollTo(x, y);
405         }
406         return;
407     }
408 
ScrollBy(float deltaX,float deltaY)409     void WebviewControllerImpl::ScrollBy(float deltaX, float deltaY)
410     {
411         auto nweb_ptr = NWeb::NWebHelper::Instance().GetNWeb(nwebId_);
412         if (nweb_ptr) {
413             nweb_ptr->ScrollBy(deltaX, deltaY);
414         }
415         return;
416     }
417 
ScrollToWithAnime(float x,float y,int32_t duration)418     void WebviewControllerImpl::ScrollToWithAnime(float x, float y, int32_t duration)
419     {
420         auto nweb_ptr = NWeb::NWebHelper::Instance().GetNWeb(nwebId_);
421         if (nweb_ptr) {
422             nweb_ptr->ScrollToWithAnime(x, y, duration);
423         }
424         return;
425     }
426 
ScrollByWithAnime(float deltaX,float deltaY,int32_t duration)427     void WebviewControllerImpl::ScrollByWithAnime(float deltaX, float deltaY, int32_t duration)
428     {
429         auto nweb_ptr = NWeb::NWebHelper::Instance().GetNWeb(nwebId_);
430         if (nweb_ptr) {
431             nweb_ptr->ScrollByWithAnime(deltaX, deltaY, duration);
432         }
433         return;
434     }
435 
Forward()436     void WebviewControllerImpl::Forward()
437     {
438         auto nweb_ptr = NWeb::NWebHelper::Instance().GetNWeb(nwebId_);
439         if (nweb_ptr) {
440             nweb_ptr->NavigateForward();
441         }
442         return;
443     }
444 
Backward()445     void WebviewControllerImpl::Backward()
446     {
447         auto nweb_ptr = NWeb::NWebHelper::Instance().GetNWeb(nwebId_);
448         if (nweb_ptr) {
449             nweb_ptr->NavigateBack();
450         }
451         return;
452     }
453 
BackOrForward(int32_t step)454     int32_t WebviewControllerImpl::BackOrForward(int32_t step)
455     {
456         auto nweb_ptr = NWeb::NWebHelper::Instance().GetNWeb(nwebId_);
457         if (!nweb_ptr) {
458             return NWebError::INIT_ERROR;
459         }
460         nweb_ptr->NavigateBackOrForward(step);
461         return NWebError::NO_ERROR;
462     }
463 
GetProgress()464     int32_t WebviewControllerImpl::GetProgress()
465     {
466         int32_t progress = 0;
467         auto nweb_ptr = NWeb::NWebHelper::Instance().GetNWeb(nwebId_);
468         if (nweb_ptr) {
469             progress = nweb_ptr->PageLoadProgress();
470         }
471         return progress;
472     }
473 
GetPageHeight()474     int32_t WebviewControllerImpl::GetPageHeight()
475     {
476         int32_t pageHeight = 0;
477         auto nweb_ptr = NWeb::NWebHelper::Instance().GetNWeb(nwebId_);
478         if (nweb_ptr) {
479             pageHeight = nweb_ptr->ContentHeight();
480         }
481         return pageHeight;
482     }
483 
GetTitle()484     std::string WebviewControllerImpl::GetTitle()
485     {
486         std::string title = "";
487         auto nweb_ptr = NWeb::NWebHelper::Instance().GetNWeb(nwebId_);
488         if (nweb_ptr) {
489             title = nweb_ptr->Title();
490         }
491         return title;
492     }
493 
Zoom(float factor)494     int32_t WebviewControllerImpl::Zoom(float factor)
495     {
496         auto nweb_ptr = NWeb::NWebHelper::Instance().GetNWeb(nwebId_);
497         if (!nweb_ptr) {
498             return NWebError::INIT_ERROR;
499         }
500         ErrCode result = NWebError::NO_ERROR;
501         result = nweb_ptr->Zoom(factor);
502 
503         return result;
504     }
505 
ZoomIn()506     int32_t WebviewControllerImpl::ZoomIn()
507     {
508         auto nweb_ptr = NWeb::NWebHelper::Instance().GetNWeb(nwebId_);
509         if (!nweb_ptr) {
510             return NWebError::INIT_ERROR;
511         }
512         ErrCode result = NWebError::NO_ERROR;
513         result = nweb_ptr->ZoomIn();
514 
515         return result;
516     }
517 
ZoomOut()518     int32_t WebviewControllerImpl::ZoomOut()
519     {
520         auto nweb_ptr = NWeb::NWebHelper::Instance().GetNWeb(nwebId_);
521         if (!nweb_ptr) {
522             return NWebError::INIT_ERROR;
523         }
524         ErrCode result = NWebError::NO_ERROR;
525         result = nweb_ptr->ZoomOut();
526 
527         return result;
528     }
529 
RequestFocus()530     int32_t WebviewControllerImpl::RequestFocus()
531     {
532         auto nweb_ptr = NWeb::NWebHelper::Instance().GetNWeb(nwebId_);
533         if (!nweb_ptr) {
534             return NWebError::INIT_ERROR;
535         }
536         nweb_ptr->OnFocus();
537         ErrCode result = NWebError::NO_ERROR;
538         return result;
539     }
540 
ClearHistory()541     void WebviewControllerImpl::ClearHistory()
542     {
543         auto nweb_ptr = NWeb::NWebHelper::Instance().GetNWeb(nwebId_);
544         if (nweb_ptr) {
545             nweb_ptr->DeleteNavigateHistory();
546         }
547     }
548 
AccessStep(int32_t step)549     bool WebviewControllerImpl::AccessStep(int32_t step)
550     {
551         bool access = false;
552         auto nweb_ptr = NWeb::NWebHelper::Instance().GetNWeb(nwebId_);
553         if (nweb_ptr) {
554             access = nweb_ptr->CanNavigateBackOrForward(step);
555         }
556         return access;
557     }
558 
OnActive()559     void WebviewControllerImpl::OnActive()
560     {
561         auto nweb_ptr = NWeb::NWebHelper::Instance().GetNWeb(nwebId_);
562         if (nweb_ptr) {
563             WEBVIEWLOGD("WebviewControllerImpl::OnActive start")
564             nweb_ptr->OnContinue();
565         }
566     }
567 
OnInactive()568     void WebviewControllerImpl::OnInactive()
569     {
570         auto nweb_ptr = NWeb::NWebHelper::Instance().GetNWeb(nwebId_);
571         if (nweb_ptr) {
572             WEBVIEWLOGD("WebviewControllerImpl::OnInactive start")
573             nweb_ptr->OnPause();
574         }
575     }
576 
GetHitTest()577     int WebviewControllerImpl::GetHitTest()
578     {
579         auto nweb_ptr = NWeb::NWebHelper::Instance().GetNWeb(nwebId_);
580         if (nweb_ptr) {
581             return ConverToWebHitTestType(nweb_ptr->GetHitTestResult()->GetType());
582         }
583         return static_cast<int>(WebHitTestType::UNKNOWN);
584     }
585 
GetHitTestValue()586     std::shared_ptr<NWeb::HitTestResult> WebviewControllerImpl::GetHitTestValue()
587     {
588         std::shared_ptr<NWeb::HitTestResult> nwebResult;
589         auto nweb_ptr = NWeb::NWebHelper::Instance().GetNWeb(nwebId_);
590         if (nweb_ptr) {
591             nwebResult = nweb_ptr->GetHitTestResult();
592             if (nwebResult) {
593                 nwebResult->SetType(ConverToWebHitTestType(nwebResult->GetType()));
594             }
595         }
596         return nwebResult;
597     }
598 
ConverToWebHitTestType(int hitType)599     int WebviewControllerImpl::ConverToWebHitTestType(int hitType)
600     {
601         WebHitTestType webHitType;
602         switch (hitType) {
603             case NWeb::HitTestResult::UNKNOWN_TYPE:
604                 webHitType = WebHitTestType::UNKNOWN;
605                 break;
606             case NWeb::HitTestResult::ANCHOR_TYPE:
607                 webHitType = WebHitTestType::HTTP;
608                 break;
609             case NWeb::HitTestResult::PHONE_TYPE:
610                 webHitType = WebHitTestType::PHONE;
611                 break;
612             case NWeb::HitTestResult::GEO_TYPE:
613                 webHitType = WebHitTestType::MAP;
614                 break;
615             case NWeb::HitTestResult::EMAIL_TYPE:
616                 webHitType = WebHitTestType::EMAIL;
617                 break;
618             case NWeb::HitTestResult::IMAGE_TYPE:
619                 webHitType = WebHitTestType::IMG;
620                 break;
621             case NWeb::HitTestResult::IMAGE_ANCHOR_TYPE:
622                 webHitType = WebHitTestType::HTTP_IMG;
623                 break;
624             case NWeb::HitTestResult::SRC_ANCHOR_TYPE:
625                 webHitType = WebHitTestType::HTTP;
626                 break;
627             case NWeb::HitTestResult::SRC_IMAGE_ANCHOR_TYPE:
628                 webHitType = WebHitTestType::HTTP_IMG;
629                 break;
630             case NWeb::HitTestResult::EDIT_TEXT_TYPE:
631                 webHitType = WebHitTestType::EDIT;
632                 break;
633             default:
634                 webHitType = WebHitTestType::UNKNOWN;
635                 break;
636         }
637         return static_cast<int>(webHitType);
638     }
639 
StoreWebArchiveCallback(std::string baseName,bool autoName,const std::function<void (RetDataCString)> & callbackRef)640     void WebviewControllerImpl::StoreWebArchiveCallback(std::string baseName, bool autoName,
641         const std::function<void(RetDataCString)>& callbackRef)
642     {
643         RetDataCString ret = { .code = NWebError::INIT_ERROR, .data = nullptr };
644         auto nweb_ptr = NWeb::NWebHelper::Instance().GetNWeb(nwebId_);
645         if (!nweb_ptr) {
646             callbackRef(ret);
647             return;
648         }
649         auto callbackImpl = std::make_shared<NWeb::NWebStoreWebArchiveCallback>();
650         callbackImpl->SetCallBack([cjCallback = callbackRef](std::string result) {
651             RetDataCString ret = { .code = NWebError::INVALID_RESOURCE, .data = nullptr };
652             if (result.empty()) {
653                 cjCallback(ret);
654                 return;
655             }
656             ret.code = NWebError::NO_ERROR;
657             ret.data = MallocCString(result);
658             if (ret.data == nullptr) {
659                 ret.code = NWebError::NEW_OOM;
660             }
661             cjCallback(ret);
662         });
663         nweb_ptr->StoreWebArchive(baseName, autoName, callbackImpl);
664         return;
665     }
666 
EnableSafeBrowsing(bool enable)667     void WebviewControllerImpl::EnableSafeBrowsing(bool enable)
668     {
669         auto nweb_ptr = NWeb::NWebHelper::Instance().GetNWeb(nwebId_);
670         if (nweb_ptr) {
671             nweb_ptr->EnableSafeBrowsing(enable);
672         }
673         return;
674     }
675 
IsSafeBrowsingEnabled()676     bool WebviewControllerImpl::IsSafeBrowsingEnabled()
677     {
678         bool is_safe_browsing_enabled = false;
679         auto nweb_ptr = NWeb::NWebHelper::Instance().GetNWeb(nwebId_);
680         if (nweb_ptr) {
681             is_safe_browsing_enabled = nweb_ptr->IsSafeBrowsingEnabled();
682         }
683         return is_safe_browsing_enabled;
684     }
685 
GetSecurityLevel()686     int WebviewControllerImpl::GetSecurityLevel()
687     {
688         auto nweb_ptr = NWeb::NWebHelper::Instance().GetNWeb(nwebId_);
689         if (!nweb_ptr) {
690             return static_cast<int>(SecurityLevel::NONE);
691         }
692 
693         int nwebSecurityLevel = nweb_ptr->GetSecurityLevel();
694         SecurityLevel securityLevel;
695         switch (nwebSecurityLevel) {
696             case static_cast<int>(CoreSecurityLevel::NONE):
697                 securityLevel = SecurityLevel::NONE;
698                 break;
699             case static_cast<int>(CoreSecurityLevel::SECURE):
700                 securityLevel = SecurityLevel::SECURE;
701                 break;
702             case static_cast<int>(CoreSecurityLevel::WARNING):
703                 securityLevel = SecurityLevel::WARNING;
704                 break;
705             case static_cast<int>(CoreSecurityLevel::DANGEROUS):
706                 securityLevel = SecurityLevel::DANGEROUS;
707                 break;
708             default:
709                 securityLevel = SecurityLevel::NONE;
710                 break;
711         }
712 
713         return static_cast<int>(securityLevel);
714     }
715 
IsIncognitoMode()716     bool WebviewControllerImpl::IsIncognitoMode()
717     {
718         bool incognitoMode = false;
719         auto nweb_ptr = NWeb::NWebHelper::Instance().GetNWeb(nwebId_);
720         if (nweb_ptr) {
721             incognitoMode = nweb_ptr->IsIncognitoMode();
722         }
723         return incognitoMode;
724     }
725 
RemoveCache(bool includeDiskFiles)726     void WebviewControllerImpl::RemoveCache(bool includeDiskFiles)
727     {
728         auto nweb_ptr = NWeb::NWebHelper::Instance().GetNWeb(nwebId_);
729         if (nweb_ptr) {
730             WEBVIEWLOGD("WebviewControllerImpl::RemoveCache start")
731             nweb_ptr->RemoveCache(includeDiskFiles);
732         }
733     }
734 
GetHistoryList()735     std::shared_ptr<OHOS::NWeb::NWebHistoryList> WebviewControllerImpl::GetHistoryList()
736     {
737         auto nweb_ptr = NWeb::NWebHelper::Instance().GetNWeb(nwebId_);
738         if (!nweb_ptr) {
739             return nullptr;
740         }
741         return nweb_ptr->GetHistoryList();
742     }
743 
GetFavicon(const void ** data,size_t & width,size_t & height,NWeb::ImageColorType & colorType,NWeb::ImageAlphaType & alphaType) const744     bool WebviewControllerImpl::GetFavicon(const void **data, size_t &width, size_t &height,
745         NWeb::ImageColorType &colorType, NWeb::ImageAlphaType &alphaType) const
746     {
747         bool isGetFavicon = false;
748         auto nweb_ptr = NWeb::NWebHelper::Instance().GetNWeb(nwebId_);
749         if (nweb_ptr) {
750             isGetFavicon = nweb_ptr->GetFavicon(data, width, height, colorType, alphaType);
751         }
752         return isGetFavicon;
753     }
754 
GetListSize()755     int32_t WebHistoryListImpl::GetListSize()
756     {
757         int32_t listSize = 0;
758 
759         if (!sptrHistoryList_) {
760             return listSize;
761         }
762         listSize = sptrHistoryList_->GetListSize();
763         return listSize;
764     }
765 
GetCurrentIndex()766     int32_t WebHistoryListImpl::GetCurrentIndex()
767     {
768         int32_t currentIndex = 0;
769 
770         if (!sptrHistoryList_) {
771             return currentIndex;
772         }
773         currentIndex = sptrHistoryList_->GetCurrentIndex();
774         return currentIndex;
775     }
776 
GetItem(int32_t index)777     std::shared_ptr<OHOS::NWeb::NWebHistoryItem> WebHistoryListImpl::GetItem(int32_t index)
778     {
779         if (!sptrHistoryList_) {
780             return nullptr;
781         }
782         return sptrHistoryList_->GetItem(index);
783     }
784 
SetNWebJavaScriptResultCallBack()785     void WebviewControllerImpl::SetNWebJavaScriptResultCallBack()
786     {
787         auto nweb_ptr = NWeb::NWebHelper::Instance().GetNWeb(nwebId_);
788         if (!nweb_ptr) {
789             return;
790         }
791         if (javaScriptResultCb_ && (javaScriptResultCb_->GetNWebId() == nwebId_)) {
792             return;
793         }
794 
795         javaScriptResultCb_ = std::make_shared<WebviewJavaScriptResultCallBackImpl>(nwebId_);
796         nweb_ptr->SetNWebJavaScriptResultCallBack(javaScriptResultCb_);
797     }
798 
RegisterJavaScriptProxy(const std::vector<std::function<char * (const char *)>> & cjFuncs,const std::string & objName,const std::vector<std::string> & methodList)799     void WebviewControllerImpl::RegisterJavaScriptProxy(const std::vector<std::function<char*(const char*)>>& cjFuncs,
800         const std::string& objName, const std::vector<std::string>& methodList)
801     {
802         auto nweb_ptr = NWeb::NWebHelper::Instance().GetNWeb(nwebId_);
803         if (!nweb_ptr) {
804             WEBVIEWLOGE("WebviewControllerImpl::RegisterJavaScriptProxy nweb_ptr is null");
805             return;
806         }
807         JavaScriptOb::ObjectID objId =
808             static_cast<JavaScriptOb::ObjectID>(JavaScriptOb::JavaScriptObjIdErrorCode::WEBCONTROLLERERROR);
809 
810         if (!javaScriptResultCb_) {
811             WEBVIEWLOGE("WebviewControllerImpl::RegisterJavaScriptProxy javaScriptResultCb_ is null");
812             return;
813         }
814 
815         if (methodList.empty()) {
816             WEBVIEWLOGE("WebviewControllerImpl::RegisterJavaScriptProxy methodList is empty");
817             return;
818         }
819 
820         objId = javaScriptResultCb_->RegisterJavaScriptProxy(cjFuncs, objName, methodList);
821 
822         nweb_ptr->RegisterArkJSfunction(objName, methodList, objId);
823     }
824 
RegisterJavaScriptProxyEx(const std::vector<std::function<char * (const char *)>> & cjFuncs,const std::string & objName,const std::vector<std::string> & methodList,char * permission)825     void WebviewControllerImpl::RegisterJavaScriptProxyEx(const std::vector<std::function<char*(const char*)>>& cjFuncs,
826         const std::string& objName, const std::vector<std::string>& methodList, char* permission)
827     {
828         auto nweb_ptr = NWeb::NWebHelper::Instance().GetNWeb(nwebId_);
829         if (!nweb_ptr) {
830             WEBVIEWLOGE("WebviewControllerImpl::RegisterJavaScriptProxy nweb_ptr is null");
831             return;
832         }
833         JavaScriptOb::ObjectID objId =
834             static_cast<JavaScriptOb::ObjectID>(JavaScriptOb::JavaScriptObjIdErrorCode::WEBCONTROLLERERROR);
835 
836         if (!javaScriptResultCb_) {
837             WEBVIEWLOGE("WebviewControllerImpl::RegisterJavaScriptProxy javaScriptResultCb_ is null");
838             return;
839         }
840 
841         if (methodList.empty()) {
842             WEBVIEWLOGE("WebviewControllerImpl::RegisterJavaScriptProxy methodList is empty");
843             return;
844         }
845 
846         objId = javaScriptResultCb_->RegisterJavaScriptProxy(cjFuncs, objName, methodList);
847 
848         nweb_ptr->RegisterArkJSfunction(objName, methodList, std::vector<std::string>(),
849             objId, permission);
850     }
851 
Stop()852     void WebviewControllerImpl::Stop()
853     {
854         auto nweb_ptr = NWeb::NWebHelper::Instance().GetNWeb(nwebId_);
855         if (nweb_ptr) {
856             return nweb_ptr->Stop();
857         }
858         return;
859     }
860 
SetBackForwardCacheOptions(int32_t size,int32_t timeToLive)861     void WebviewControllerImpl::SetBackForwardCacheOptions(int32_t size, int32_t timeToLive)
862     {
863         auto nweb_ptr = NWeb::NWebHelper::Instance().GetNWeb(nwebId_);
864         if (!nweb_ptr) {
865             WEBVIEWLOGE("WebviewControllerImpl::void SetBackForwardCacheOptions nweb_ptr is null");
866             return;
867         }
868         nweb_ptr->SetBackForwardCacheOptions(size, timeToLive);
869     }
870 
SlideScroll(float vx,float vy)871     void WebviewControllerImpl::SlideScroll(float vx, float vy)
872     {
873         auto nweb_ptr = NWeb::NWebHelper::Instance().GetNWeb(nwebId_);
874         if (nweb_ptr) {
875             return nweb_ptr->SlideScroll(vx, vy);
876         }
877         return;
878     }
879 
PutNetworkAvailable(bool enable)880     void WebviewControllerImpl::PutNetworkAvailable(bool enable)
881     {
882         auto nweb_ptr = NWeb::NWebHelper::Instance().GetNWeb(nwebId_);
883         if (nweb_ptr) {
884             return nweb_ptr->PutNetworkAvailable(enable);
885         }
886         return;
887     }
888 
ClearClientAuthenticationCache()889     void WebviewControllerImpl::ClearClientAuthenticationCache()
890     {
891         auto nweb_ptr = NWeb::NWebHelper::Instance().GetNWeb(nwebId_);
892         if (nweb_ptr) {
893             return nweb_ptr->ClearClientAuthenticationCache();
894         }
895         return;
896     }
897 
ClearSslCache()898     void WebviewControllerImpl::ClearSslCache()
899     {
900         auto nweb_ptr = NWeb::NWebHelper::Instance().GetNWeb(nwebId_);
901         if (nweb_ptr) {
902             return nweb_ptr->ClearSslCache();
903         }
904         return;
905     }
906 
SearchNext(bool forward)907     void WebviewControllerImpl::SearchNext(bool forward)
908     {
909         auto nweb_ptr = NWeb::NWebHelper::Instance().GetNWeb(nwebId_);
910         if (nweb_ptr) {
911             return nweb_ptr->FindNext(forward);
912         }
913         return;
914     }
915 
ClearMatches()916     void WebviewControllerImpl::ClearMatches()
917     {
918         auto nweb_ptr = NWeb::NWebHelper::Instance().GetNWeb(nwebId_);
919         if (nweb_ptr) {
920             return nweb_ptr->ClearMatches();
921         }
922         return;
923     }
924 
SearchAllAsync(std::string str)925     void WebviewControllerImpl::SearchAllAsync(std::string str)
926     {
927         auto nweb_ptr = NWeb::NWebHelper::Instance().GetNWeb(nwebId_);
928         if (nweb_ptr) {
929             return nweb_ptr->FindAllAsync(str);
930         }
931         return;
932     }
933 
DeleteJavaScriptRegister(const std::string & objName,const std::vector<std::string> & methodList)934     ErrCode WebviewControllerImpl::DeleteJavaScriptRegister(const std::string& objName,
935         const std::vector<std::string>& methodList)
936     {
937         auto nweb_ptr = NWeb::NWebHelper::Instance().GetNWeb(nwebId_);
938         if (nweb_ptr) {
939             nweb_ptr->UnregisterArkJSfunction(objName, methodList);
940         }
941         if (javaScriptResultCb_) {
942             bool ret = javaScriptResultCb_->DeleteJavaScriptRegister(objName);
943             if (!ret) {
944                 return NWebError::CANNOT_DEL_JAVA_SCRIPT_PROXY;
945             }
946         }
947         return NWebError::NO_ERROR;
948     }
949 
PostUrl(std::string & url,std::vector<char> & postData)950     int32_t WebviewControllerImpl::PostUrl(std::string& url, std::vector<char>& postData)
951     {
952         auto nweb_ptr = NWeb::NWebHelper::Instance().GetNWeb(nwebId_);
953         if (!nweb_ptr) {
954             return NWebError::INIT_ERROR;
955         }
956         return nweb_ptr->PostUrl(url, postData);
957     }
958 
CreateWebMessagePorts()959     std::vector<std::string> WebviewControllerImpl::CreateWebMessagePorts()
960     {
961         auto nweb_ptr = NWeb::NWebHelper::Instance().GetNWeb(nwebId_);
962         if (!nweb_ptr) {
963             std::vector<std::string> empty;
964             return empty;
965         }
966         return nweb_ptr->CreateWebMessagePorts();
967     }
968 
PostWebMessage(std::string & message,std::vector<std::string> & ports,std::string & targetUrl)969     ErrCode WebviewControllerImpl::PostWebMessage(std::string& message,
970         std::vector<std::string>& ports, std::string& targetUrl)
971     {
972         auto nweb_ptr = NWeb::NWebHelper::Instance().GetNWeb(nwebId_);
973         if (!nweb_ptr) {
974             return NWebError::INIT_ERROR;
975         }
976 
977         nweb_ptr->PostWebMessage(message, ports, targetUrl);
978         return NWebError::NO_ERROR;
979     }
980 
SerializeWebState()981     std::vector<uint8_t> WebviewControllerImpl::SerializeWebState()
982     {
983         auto nweb_ptr = NWeb::NWebHelper::Instance().GetNWeb(nwebId_);
984         if (nweb_ptr) {
985             return nweb_ptr->SerializeWebState();
986         }
987         std::vector<uint8_t> empty;
988         return empty;
989     }
990 
RestoreWebState(const std::vector<uint8_t> & state) const991     bool WebviewControllerImpl::RestoreWebState(const std::vector<uint8_t> &state) const
992     {
993         bool isRestored = false;
994         auto nweb_ptr = NWeb::NWebHelper::Instance().GetNWeb(nwebId_);
995         if (nweb_ptr) {
996             isRestored = nweb_ptr->RestoreWebState(state);
997         }
998         return isRestored;
999     }
1000 
GetCertChainDerData(std::vector<std::string> & certChainDerData) const1001     bool WebviewControllerImpl::GetCertChainDerData(std::vector<std::string> &certChainDerData) const
1002     {
1003         auto nweb_ptr = NWeb::NWebHelper::Instance().GetNWeb(nwebId_);
1004         if (!nweb_ptr) {
1005             WEBVIEWLOGE("GetCertChainDerData failed, nweb ptr is null");
1006             return false;
1007         }
1008 
1009         return nweb_ptr->GetCertChainDerData(certChainDerData, true);
1010     }
1011 
HasImagesCallback(const std::function<void (RetDataBool)> & callbackRef)1012     ErrCode WebviewControllerImpl::HasImagesCallback(const std::function<void(RetDataBool)>& callbackRef)
1013     {
1014         auto nweb_ptr = NWeb::NWebHelper::Instance().GetNWeb(nwebId_);
1015         if (!nweb_ptr) {
1016             return NWebError::INIT_ERROR;
1017         }
1018         if (callbackRef == nullptr) {
1019             return NWebError::PARAM_CHECK_ERROR;
1020         }
1021         auto callbackImpl = std::make_shared<WebviewHasImageCallback>(callbackRef);
1022         nweb_ptr->HasImages(callbackImpl);
1023         return NWebError::NO_ERROR;
1024     }
1025 
TerminateRenderProcess()1026     bool WebviewControllerImpl::TerminateRenderProcess()
1027     {
1028         auto nweb_ptr = NWeb::NWebHelper::Instance().GetNWeb(nwebId_);
1029         if (!nweb_ptr) {
1030             return false;
1031         }
1032         return nweb_ptr->TerminateRenderProcess();
1033     }
1034 
CloseAllMediaPresentations()1035     void WebviewControllerImpl::CloseAllMediaPresentations()
1036     {
1037         auto nweb_ptr = NWeb::NWebHelper::Instance().GetNWeb(nwebId_);
1038         if (!nweb_ptr) {
1039             return;
1040         }
1041         nweb_ptr->CloseAllMediaPresentations();
1042     }
1043 
PauseAllMedia()1044     void WebviewControllerImpl::PauseAllMedia()
1045     {
1046         auto nweb_ptr = NWeb::NWebHelper::Instance().GetNWeb(nwebId_);
1047         if (!nweb_ptr) {
1048             return;
1049         }
1050         nweb_ptr->PauseAllMedia();
1051     }
1052 
ResumeAllMedia()1053     void WebviewControllerImpl::ResumeAllMedia()
1054     {
1055         auto nweb_ptr = NWeb::NWebHelper::Instance().GetNWeb(nwebId_);
1056         if (!nweb_ptr) {
1057             return;
1058         }
1059         nweb_ptr->ResumeAllMedia();
1060     }
1061 
StopAllMedia()1062     void WebviewControllerImpl::StopAllMedia()
1063     {
1064         auto nweb_ptr = NWeb::NWebHelper::Instance().GetNWeb(nwebId_);
1065         if (!nweb_ptr) {
1066             return;
1067         }
1068         nweb_ptr->StopAllMedia();
1069     }
1070 
SetPrintBackground(bool enable)1071     void WebviewControllerImpl::SetPrintBackground(bool enable)
1072     {
1073         auto nweb_ptr = NWeb::NWebHelper::Instance().GetNWeb(nwebId_);
1074         if (!nweb_ptr) {
1075             return;
1076         }
1077         nweb_ptr->SetPrintBackground(enable);
1078     }
1079 
GetPrintBackground()1080     bool WebviewControllerImpl::GetPrintBackground()
1081     {
1082         auto nweb_ptr = NWeb::NWebHelper::Instance().GetNWeb(nwebId_);
1083         if (!nweb_ptr) {
1084             return false;
1085         }
1086         return nweb_ptr->GetPrintBackground();
1087     }
1088 
GetScrollable()1089     bool WebviewControllerImpl::GetScrollable()
1090     {
1091         auto nweb_ptr = NWeb::NWebHelper::Instance().GetNWeb(nwebId_);
1092         if (!nweb_ptr) {
1093             return true;
1094         }
1095         std::shared_ptr<OHOS::NWeb::NWebPreference> setting = nweb_ptr->GetPreference();
1096         if (!setting) {
1097             return true;
1098         }
1099         return setting->GetScrollable();
1100     }
1101 
SetScrollable(bool enable)1102     void WebviewControllerImpl::SetScrollable(bool enable)
1103     {
1104         auto nweb_ptr = NWeb::NWebHelper::Instance().GetNWeb(nwebId_);
1105         if (!nweb_ptr) {
1106             return;
1107         }
1108         std::shared_ptr<OHOS::NWeb::NWebPreference> setting = nweb_ptr->GetPreference();
1109         if (!setting) {
1110             return;
1111         }
1112         setting->SetScrollable(enable);
1113     }
1114 
SetScrollable(bool enable,int32_t scrollType)1115     void WebviewControllerImpl::SetScrollable(bool enable, int32_t scrollType)
1116     {
1117         auto nweb_ptr = NWeb::NWebHelper::Instance().GetNWeb(nwebId_);
1118         if (!nweb_ptr) {
1119             return;
1120         }
1121         std::shared_ptr<OHOS::NWeb::NWebPreference> setting = nweb_ptr->GetPreference();
1122         if (!setting) {
1123             return;
1124         }
1125         return setting->SetScrollable(enable, scrollType);
1126     }
1127 
ScrollByWithResult(float deltaX,float deltaY) const1128     bool WebviewControllerImpl::ScrollByWithResult(float deltaX, float deltaY) const
1129     {
1130         bool enabled = false;
1131         auto nweb_ptr = NWeb::NWebHelper::Instance().GetNWeb(nwebId_);
1132         if (nweb_ptr) {
1133             enabled = nweb_ptr->ScrollByWithResult(deltaX, deltaY);
1134         }
1135         return enabled;
1136     }
1137 
EnableAdsBlock(bool enable)1138     void WebviewControllerImpl::EnableAdsBlock(bool enable)
1139     {
1140         auto nweb_ptr = NWeb::NWebHelper::Instance().GetNWeb(nwebId_);
1141         if (!nweb_ptr) {
1142             return;
1143         }
1144         nweb_ptr->EnableAdsBlock(enable);
1145     }
1146 
IsAdsBlockEnabled()1147     bool WebviewControllerImpl::IsAdsBlockEnabled()
1148     {
1149         auto nweb_ptr = NWeb::NWebHelper::Instance().GetNWeb(nwebId_);
1150         if (!nweb_ptr) {
1151             return false;
1152         }
1153         return nweb_ptr->IsAdsBlockEnabled();
1154     }
1155 
IsAdsBlockEnabledForCurPage()1156     bool WebviewControllerImpl::IsAdsBlockEnabledForCurPage()
1157     {
1158         auto nweb_ptr = NWeb::NWebHelper::Instance().GetNWeb(nwebId_);
1159         if (!nweb_ptr) {
1160             return false;
1161         }
1162         return nweb_ptr->IsAdsBlockEnabledForCurPage();
1163     }
1164 
IsIntelligentTrackingPreventionEnabled()1165     bool WebviewControllerImpl::IsIntelligentTrackingPreventionEnabled()
1166     {
1167         auto nweb_ptr = NWeb::NWebHelper::Instance().GetNWeb(nwebId_);
1168         if (!nweb_ptr) {
1169             return false;
1170         }
1171         return nweb_ptr->IsIntelligentTrackingPreventionEnabled();
1172     }
1173 
EnableIntelligentTrackingPrevention(bool enable)1174     void WebviewControllerImpl::EnableIntelligentTrackingPrevention(bool enable)
1175     {
1176         auto nweb_ptr = NWeb::NWebHelper::Instance().GetNWeb(nwebId_);
1177         if (nweb_ptr) {
1178             nweb_ptr->EnableIntelligentTrackingPrevention(enable);
1179         }
1180         return;
1181     }
1182 
GetMediaPlaybackState()1183     int32_t WebviewControllerImpl::GetMediaPlaybackState()
1184     {
1185         auto nweb_ptr = NWeb::NWebHelper::Instance().GetNWeb(nwebId_);
1186         if (!nweb_ptr) {
1187             return static_cast<int32_t>(MediaPlaybackState::NONE);
1188         }
1189         return nweb_ptr->GetMediaPlaybackState();
1190     }
1191 
GetLastJavascriptProxyCallingFrameUrl()1192     std::string WebviewControllerImpl::GetLastJavascriptProxyCallingFrameUrl()
1193     {
1194         auto nweb_ptr = NWeb::NWebHelper::Instance().GetNWeb(nwebId_);
1195         if (!nweb_ptr) {
1196             return "";
1197         }
1198         return nweb_ptr->GetLastJavascriptProxyCallingFrameUrl();
1199     }
1200 
StartCamera()1201     void WebviewControllerImpl::StartCamera()
1202     {
1203         auto nweb_ptr = NWeb::NWebHelper::Instance().GetNWeb(nwebId_);
1204         if (!nweb_ptr) {
1205             return;
1206         }
1207         nweb_ptr->StartCamera();
1208     }
1209 
StopCamera()1210     void WebviewControllerImpl::StopCamera()
1211     {
1212         auto nweb_ptr = NWeb::NWebHelper::Instance().GetNWeb(nwebId_);
1213         if (!nweb_ptr) {
1214             return;
1215         }
1216         nweb_ptr->StopCamera();
1217     }
1218 
CloseCamera()1219     void WebviewControllerImpl::CloseCamera()
1220     {
1221         auto nweb_ptr = NWeb::NWebHelper::Instance().GetNWeb(nwebId_);
1222         if (!nweb_ptr) {
1223             return;
1224         }
1225         nweb_ptr->CloseCamera();
1226     }
1227 
GetSurfaceId()1228     std::string WebviewControllerImpl::GetSurfaceId()
1229     {
1230         auto nweb_ptr = NWeb::NWebHelper::Instance().GetNWeb(nwebId_);
1231         if (!nweb_ptr) {
1232             return "";
1233         }
1234         std::shared_ptr<OHOS::NWeb::NWebPreference> setting = nweb_ptr->GetPreference();
1235         if (!setting) {
1236             return "";
1237         }
1238         return setting->GetSurfaceId();
1239     }
1240 
InjectOfflineResources(const std::vector<std::string> & urlList,const std::vector<uint8_t> & resource,const std::map<std::string,std::string> & response_headers,const uint32_t type)1241     void WebviewControllerImpl::InjectOfflineResources(const std::vector<std::string>& urlList,
1242         const std::vector<uint8_t>& resource, const std::map<std::string, std::string>& response_headers,
1243         const uint32_t type)
1244     {
1245         auto nweb_ptr = NWeb::NWebHelper::Instance().GetNWeb(nwebId_);
1246         if (!nweb_ptr) {
1247             return;
1248         }
1249         if (urlList.size() == 0) {
1250             return;
1251         }
1252         std::string originUrl = urlList[0];
1253         if (urlList.size() == 1) {
1254             nweb_ptr->InjectOfflineResource(originUrl, originUrl, resource, response_headers, type);
1255             return;
1256         }
1257 
1258         for (size_t i = 1; i < urlList.size(); i++) {
1259             nweb_ptr->InjectOfflineResource(urlList[i], originUrl, resource, response_headers, type);
1260         }
1261     }
1262 
SetUrlTrustList(const std::string & urlTrustList,std::string & detailErrMsg)1263     int32_t WebviewControllerImpl::SetUrlTrustList(const std::string& urlTrustList, std::string& detailErrMsg)
1264     {
1265         auto nweb_ptr = NWeb::NWebHelper::Instance().GetNWeb(nwebId_);
1266         if (!nweb_ptr) {
1267             return NWebError::INIT_ERROR;
1268         }
1269 
1270         int ret = NWebError::NO_ERROR;
1271         switch (nweb_ptr->SetUrlTrustListWithErrMsg(urlTrustList, detailErrMsg)) {
1272             case static_cast<int>(UrlListSetResult::INIT_ERROR):
1273                 ret = NWebError::INIT_ERROR;
1274                 break;
1275             case static_cast<int>(UrlListSetResult::PARAM_ERROR):
1276                 ret = NWebError::PARAM_CHECK_ERROR;
1277                 break;
1278             case static_cast<int>(UrlListSetResult::SET_OK):
1279                 ret = NWebError::NO_ERROR;
1280                 break;
1281             default:
1282                 ret = NWebError::PARAM_CHECK_ERROR;
1283                 break;
1284         }
1285         return ret;
1286     }
1287 
SetPathAllowingUniversalAccess(const std::vector<std::string> & pathList,std::string & errorPath)1288     void WebviewControllerImpl::SetPathAllowingUniversalAccess(
1289         const std::vector<std::string>& pathList, std::string& errorPath)
1290     {
1291         auto nweb_ptr = NWeb::NWebHelper::Instance().GetNWeb(nwebId_);
1292         if (!nweb_ptr) {
1293             return;
1294         }
1295         if (moduleName_.empty()) {
1296             WEBVIEWLOGD("need to get module nme for path");
1297             if (!GetHapModuleInfo()) {
1298                 WEBVIEWLOGE("GetHapModuleInfo failed")
1299                 moduleName_.clear();
1300                 return;
1301             }
1302         }
1303         nweb_ptr->SetPathAllowingUniversalAccess(pathList, moduleName_, errorPath);
1304     }
1305 
GetHapModuleInfo()1306     bool WebviewControllerImpl::GetHapModuleInfo()
1307     {
1308         sptr<ISystemAbilityManager> systemAbilityManager =
1309             SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
1310         if (systemAbilityManager == nullptr) {
1311             WEBVIEWLOGE("get SystemAbilityManager failed");
1312             return false;
1313         }
1314         sptr<IRemoteObject> remoteObject = systemAbilityManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
1315         if (remoteObject == nullptr) {
1316             WEBVIEWLOGE("get Bundle Manager failed");
1317             return false;
1318         }
1319         auto bundleMgr = iface_cast<AppExecFwk::IBundleMgr>(remoteObject);
1320         if (bundleMgr == nullptr) {
1321             WEBVIEWLOGE("get Bundle Manager failed");
1322             return false;
1323         }
1324         AppExecFwk::BundleInfo bundleInfo;
1325         if (bundleMgr->GetBundleInfoForSelf(
1326             static_cast<int32_t>(AppExecFwk::GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_HAP_MODULE), bundleInfo) != 0) {
1327             WEBVIEWLOGE("get bundle info failed");
1328             return false;
1329         }
1330         moduleName_ = bundleInfo.moduleNames;
1331         return true;
1332     }
1333 
SetWebSchemeHandler(const char * scheme,WebSchemeHandlerImpl * handler)1334     bool WebviewControllerImpl::SetWebSchemeHandler(const char* scheme, WebSchemeHandlerImpl* handler)
1335     {
1336         if (!handler || !scheme) {
1337             WEBVIEWLOGE("WebviewControllerImpl::SetWebSchemeHandler schemeHandler or scheme is nullptr");
1338             return false;
1339         }
1340         ArkWeb_SchemeHandler* schemeHandler =
1341             const_cast<ArkWeb_SchemeHandler*>(WebSchemeHandlerImpl::GetArkWebSchemeHandler(handler));
1342         if (schemeHandler == nullptr) {
1343             WEBVIEWLOGE("schemeHandler is nullptr");
1344         }
1345         return OH_ArkWeb_SetSchemeHandler(scheme, webTag_.c_str(), schemeHandler);
1346     }
1347 
ClearWebSchemeHandler()1348     int32_t WebviewControllerImpl::ClearWebSchemeHandler()
1349     {
1350         return OH_ArkWeb_ClearSchemeHandlers(webTag_.c_str());
1351     }
1352 
SetWebServiceWorkerSchemeHandler(const char * scheme,WebSchemeHandlerImpl * handler)1353     bool WebviewControllerImpl::SetWebServiceWorkerSchemeHandler(const char* scheme, WebSchemeHandlerImpl* handler)
1354     {
1355         ArkWeb_SchemeHandler* schemeHandler =
1356             const_cast<ArkWeb_SchemeHandler*>(WebSchemeHandlerImpl::GetArkWebSchemeHandler(handler));
1357         return OH_ArkWebServiceWorker_SetSchemeHandler(scheme, schemeHandler);
1358     }
1359 
ClearWebServiceWorkerSchemeHandler()1360     int32_t WebviewControllerImpl::ClearWebServiceWorkerSchemeHandler()
1361     {
1362         return OH_ArkWebServiceWorker_ClearSchemeHandlers();
1363     }
1364 
OnCreateNativeMediaPlayer(std::function<int64_t (int64_t,CMediaInfo)> callback)1365     void WebviewControllerImpl::OnCreateNativeMediaPlayer(std::function<int64_t(int64_t, CMediaInfo)> callback)
1366     {
1367         auto nweb_ptr = NWeb::NWebHelper::Instance().GetNWeb(nwebId_);
1368         if (!nweb_ptr) {
1369             return;
1370         }
1371         auto callbackImpl = std::make_shared<NWebCreateNativeMediaPlayerCallbackImpl>(nwebId_, callback);
1372         nweb_ptr->OnCreateNativeMediaPlayer(callbackImpl);
1373     }
1374 
PrecompileJavaScript(std::string url,std::string script,std::shared_ptr<OHOS::NWeb::CacheOptions> cacheOptions)1375     int32_t WebviewControllerImpl::PrecompileJavaScript(std::string url, std::string script,
1376         std::shared_ptr<OHOS::NWeb::CacheOptions> cacheOptions)
1377     {
1378         auto nweb_ptr = NWeb::NWebHelper::Instance().GetNWeb(nwebId_);
1379         auto callbackImpl = std::make_shared<OHOS::NWeb::NWebPrecompileCallback>();
1380         if (!nweb_ptr) {
1381             return NWebError::INIT_ERROR;
1382         }
1383         if (url.empty() || script.empty()) {
1384             return NWebError::PARAM_CHECK_ERROR;
1385         }
1386         nweb_ptr->PrecompileJavaScript(url, script, cacheOptions, callbackImpl);
1387         return NWebError::NO_ERROR;
1388     }
1389 
WebPageSnapshot(const char * id,NWeb::PixelUnit type,int32_t width,int32_t height,const NWeb::WebSnapshotCallback callback)1390     int32_t WebviewControllerImpl::WebPageSnapshot(const char* id, NWeb::PixelUnit type,
1391         int32_t width, int32_t height, const NWeb::WebSnapshotCallback callback)
1392     {
1393         auto nweb_ptr = NWeb::NWebHelper::Instance().GetNWeb(nwebId_);
1394         if (!nweb_ptr) {
1395             return NWebError::INIT_ERROR;
1396         }
1397 
1398         bool init = nweb_ptr->WebPageSnapshot(id, type, width, height, std::move(callback));
1399         if (!init) {
1400             return NWebError::INIT_ERROR;
1401         }
1402         return NWebError::NO_ERROR;
1403     }
1404 
CheckSchemeName(const std::string & schemeName)1405     bool CheckSchemeName(const std::string& schemeName)
1406     {
1407         if (schemeName.empty() || schemeName.size() > MAX_CUSTOM_SCHEME_NAME_LENGTH) {
1408             WEBVIEWLOGE("Invalid scheme name length");
1409             return false;
1410         }
1411         for (auto it = schemeName.begin(); it != schemeName.end(); it++) {
1412             char chr = *it;
1413             if (!((chr >= 'a' && chr <= 'z') || (chr >= '0' && chr <= '9') ||
1414                 (chr == '.') || (chr == '+') || (chr == '-'))) {
1415                 WEBVIEWLOGE("invalid character %{public}c", chr);
1416                 return false;
1417             }
1418         }
1419         return true;
1420     }
1421 
SetCustomizeSchemeOption(Scheme & scheme)1422     void SetCustomizeSchemeOption(Scheme& scheme)
1423     {
1424         std::map<int, std::function<bool(const Scheme&)>> schemeProperties = {
1425             {0, [](const Scheme& scheme) { return scheme.isStandard; }},
1426             {1, [](const Scheme& scheme) { return scheme.isLocal; }},
1427             {2, [](const Scheme& scheme) { return scheme.isDisplayIsolated; }},
1428             {3, [](const Scheme& scheme) { return scheme.isSecure; }},
1429             {4, [](const Scheme& scheme) { return scheme.isSupportCORS; }},
1430             {5, [](const Scheme& scheme) { return scheme.isCspBypassing; }},
1431             {6, [](const Scheme& scheme) { return scheme.isSupportFetch; }},
1432             {7, [](const Scheme& scheme) { return scheme.isCodeCacheSupported; }}
1433         };
1434 
1435         for (const auto& property : schemeProperties) {
1436             if (property.second(scheme)) {
1437                 scheme.option += 1 << property.first;
1438             }
1439         }
1440     }
1441 
SetCustomizeScheme(CScheme cscheme,Scheme & scheme)1442     bool SetCustomizeScheme(CScheme cscheme, Scheme& scheme)
1443     {
1444         scheme.isSupportCORS = cscheme.isSupportCORS;
1445         scheme.isSupportFetch = cscheme.isSupportFetch;
1446         scheme.isStandard = cscheme.isStandard;
1447         scheme.isLocal = cscheme.isLocal;
1448         scheme.isDisplayIsolated = cscheme.isDisplayIsolated;
1449         scheme.isSecure = cscheme.isSecure;
1450         scheme.isCspBypassing = cscheme.isCspBypassing;
1451         scheme.isCodeCacheSupported = cscheme.isCodeCacheSupported;
1452         scheme.name = std::string(cscheme.name);
1453         if (!CheckSchemeName(scheme.name)) {
1454             return false;
1455         }
1456         SetCustomizeSchemeOption(scheme);
1457         return true;
1458     }
1459 
CustomizeSchemesArrayDataHandler(CArrScheme schemes)1460     int32_t WebviewControllerImpl::CustomizeSchemesArrayDataHandler(CArrScheme schemes)
1461     {
1462         int64_t arrayLength = schemes.size;
1463         if (arrayLength > MAX_CUSTOM_SCHEME_SIZE) {
1464             return NWebError::PARAM_CHECK_ERROR;
1465         }
1466         std::vector<Scheme> schemeVector;
1467         for (int64_t i = 0; i < arrayLength; ++i) {
1468             Scheme scheme;
1469             bool result = SetCustomizeScheme(schemes.head[i], scheme);
1470             if (!result) {
1471                 return NWebError::PARAM_CHECK_ERROR;
1472             }
1473             schemeVector.push_back(scheme);
1474         }
1475         int32_t registerResult;
1476         for (auto it = schemeVector.begin(); it != schemeVector.end(); ++it) {
1477             registerResult = OH_ArkWeb_RegisterCustomSchemes(it->name.c_str(), it->option);
1478             if (registerResult != NO_ERROR) {
1479                 return registerResult;
1480             }
1481         }
1482         return NWebError::NO_ERROR;
1483     }
1484 
GetLastHitTest()1485     std::shared_ptr<NWeb::HitTestResult> WebviewControllerImpl::GetLastHitTest()
1486     {
1487         std::shared_ptr<NWeb::HitTestResult> nwebResult;
1488         auto nweb_ptr = NWeb::NWebHelper::Instance().GetNWeb(nwebId_);
1489         if (nweb_ptr) {
1490             nwebResult = nweb_ptr->GetLastHitTestResult();
1491             if (nwebResult) {
1492                 nwebResult->SetType(ConverToWebHitTestType(nwebResult->GetType()));
1493             }
1494         }
1495         return nwebResult;
1496     }
1497 
CreateWebPrintDocumentAdapter(const std::string & jobName)1498     void* WebviewControllerImpl::CreateWebPrintDocumentAdapter(const std::string &jobName)
1499     {
1500         auto nweb_ptr = NWeb::NWebHelper::Instance().GetNWeb(nwebId_);
1501         if (!nweb_ptr) {
1502             return nullptr;
1503         }
1504         return nweb_ptr->CreateWebPrintDocumentAdapter(jobName);
1505     }
1506 
GetScrollOffset(float * offset_x,float * offset_y)1507     void WebviewControllerImpl::GetScrollOffset(float* offset_x, float* offset_y)
1508     {
1509         auto nweb_ptr = NWeb::NWebHelper::Instance().GetNWeb(nwebId_);
1510         if (nweb_ptr) {
1511             nweb_ptr->GetScrollOffset(offset_x, offset_y);
1512         }
1513     }
1514 
AvoidVisibleViewportBottom(int32_t avoidHeight)1515     int32_t WebviewControllerImpl::AvoidVisibleViewportBottom(int32_t avoidHeight)
1516     {
1517         auto nweb_ptr = NWeb::NWebHelper::Instance().GetNWeb(nwebId_);
1518         if (!nweb_ptr) {
1519             return NWebError::INIT_ERROR;
1520         }
1521         nweb_ptr->AvoidVisibleViewportBottom(avoidHeight);
1522         return NWebError::NO_ERROR;
1523     }
1524 
SetErrorPageEnabled(bool enable)1525     int32_t WebviewControllerImpl::SetErrorPageEnabled(bool enable)
1526     {
1527         auto nweb_ptr = NWeb::NWebHelper::Instance().GetNWeb(nwebId_);
1528         if (!nweb_ptr) {
1529             return NWebError::INIT_ERROR;
1530         }
1531         nweb_ptr->SetErrorPageEnabled(enable);
1532         return NWebError::NO_ERROR;
1533     }
1534 
GetErrorPageEnabled()1535     bool WebviewControllerImpl::GetErrorPageEnabled()
1536     {
1537         auto nweb_ptr = NWeb::NWebHelper::Instance().GetNWeb(nwebId_);
1538         if (!nweb_ptr) {
1539             return false;
1540         }
1541         return nweb_ptr->GetErrorPageEnabled();
1542     }
1543 }
1544