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