• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "webview_controller.h"
17 
18 #include "business_error.h"
19 #include "napi_parse_utils.h"
20 #include "nweb_log.h"
21 #include "nweb_store_web_archive_callback.h"
22 #include "webview_javascript_execute_callback.h"
23 #include "webview_javascript_result_callback.h"
24 #include "webview_hasimage_callback.h"
25 #include "web_errors.h"
26 
27 namespace {
28     constexpr int32_t PARAMZERO = 0;
29     constexpr int32_t PARAMONE = 1;
30     constexpr int32_t RESULT_COUNT = 2;
31 }
32 
33 namespace OHOS {
34 namespace NWeb {
35 using namespace NWebError;
36 std::string WebviewController::customeSchemeCmdLine_ = "";
37 bool WebviewController::existNweb_ = false;
38 bool WebviewController::webDebuggingAccess_ = false;
WebviewController(int32_t nwebId)39 WebviewController::WebviewController(int32_t nwebId) : nweb_(NWebHelper::Instance().GetNWeb(nwebId)) {}
40 
SetWebId(int32_t nwebId)41 void WebviewController::SetWebId(int32_t nwebId)
42 {
43     nweb_ = NWebHelper::Instance().GetNWeb(nwebId);
44 }
45 
IsInit()46 bool WebviewController::IsInit()
47 {
48     return nweb_.lock() ? true : false;
49 }
50 
AccessForward()51 bool WebviewController::AccessForward()
52 {
53     bool access = false;
54     auto nweb_ptr = nweb_.lock();
55     if (nweb_ptr) {
56         access = nweb_ptr->IsNavigateForwardAllowed();
57     }
58     return access;
59 }
60 
AccessBackward()61 bool WebviewController::AccessBackward()
62 {
63     bool access = false;
64     auto nweb_ptr = nweb_.lock();
65     if (nweb_ptr) {
66         access = nweb_ptr->IsNavigatebackwardAllowed();
67     }
68     return access;
69 }
70 
AccessStep(int32_t step)71 bool WebviewController::AccessStep(int32_t step)
72 {
73     bool access = false;
74     auto nweb_ptr = nweb_.lock();
75     if (nweb_ptr) {
76         access = nweb_ptr->CanNavigateBackOrForward(step);
77     }
78     return access;
79 }
80 
ClearHistory()81 void WebviewController::ClearHistory()
82 {
83     auto nweb_ptr = nweb_.lock();
84     if (nweb_ptr) {
85         nweb_ptr->DeleteNavigateHistory();
86     }
87 }
88 
Forward()89 void WebviewController::Forward()
90 {
91     auto nweb_ptr = nweb_.lock();
92     if (nweb_ptr) {
93         nweb_ptr->NavigateForward();
94     }
95 }
96 
Backward()97 void WebviewController::Backward()
98 {
99     auto nweb_ptr = nweb_.lock();
100     if (nweb_ptr) {
101         nweb_ptr->NavigateBack();
102     }
103 }
104 
OnActive()105 void WebviewController::OnActive()
106 {
107     auto nweb_ptr = nweb_.lock();
108     if (nweb_ptr) {
109         nweb_ptr->OnContinue();
110     }
111 }
112 
OnInactive()113 void WebviewController::OnInactive()
114 {
115     auto nweb_ptr = nweb_.lock();
116     if (nweb_ptr) {
117         nweb_ptr->OnPause();
118     }
119 }
120 
Refresh()121 void WebviewController::Refresh()
122 {
123     auto nweb_ptr = nweb_.lock();
124     if (nweb_ptr) {
125         nweb_ptr->Reload();
126     }
127 }
128 
ZoomIn()129 ErrCode WebviewController::ZoomIn()
130 {
131     auto nweb_ptr = nweb_.lock();
132     if (!nweb_ptr) {
133         return INIT_ERROR;
134     }
135     ErrCode result = NWebError::NO_ERROR;
136     result = nweb_ptr->ZoomIn();
137 
138     return result;
139 }
140 
ZoomOut()141 ErrCode WebviewController::ZoomOut()
142 {
143     auto nweb_ptr = nweb_.lock();
144     if (!nweb_ptr) {
145         return INIT_ERROR;
146     }
147     ErrCode result = NWebError::NO_ERROR;
148     result = nweb_ptr->ZoomOut();
149 
150     return result;
151 }
152 
GetWebId() const153 int32_t WebviewController::GetWebId() const
154 {
155     int32_t webId = -1;
156     auto nweb_ptr = nweb_.lock();
157     if (nweb_ptr) {
158         webId = static_cast<int32_t>(nweb_ptr->GetWebId());
159     }
160     return webId;
161 }
162 
GetUserAgent()163 std::string WebviewController::GetUserAgent()
164 {
165     auto nweb_ptr = nweb_.lock();
166     if (!nweb_ptr) {
167         return "";
168     }
169     std::shared_ptr<OHOS::NWeb::NWebPreference> setting = nweb_ptr->GetPreference();
170     if (!setting) {
171         return "";
172     }
173     return setting->DefaultUserAgent();
174 }
175 
GetTitle()176 std::string WebviewController::GetTitle()
177 {
178     std::string title = "";
179     auto nweb_ptr = nweb_.lock();
180     if (nweb_ptr) {
181         title = nweb_ptr->Title();
182     }
183     return title;
184 }
185 
GetPageHeight()186 int32_t WebviewController::GetPageHeight()
187 {
188     int32_t pageHeight = 0;
189     auto nweb_ptr = nweb_.lock();
190     if (nweb_ptr) {
191         pageHeight = nweb_ptr->ContentHeight();
192     }
193     return pageHeight;
194 }
195 
BackOrForward(int32_t step)196 ErrCode WebviewController::BackOrForward(int32_t step)
197 {
198     auto nweb_ptr = nweb_.lock();
199     if (!nweb_ptr) {
200         return INIT_ERROR;
201     }
202 
203     nweb_ptr->NavigateBackOrForward(step);
204     return NWebError::NO_ERROR;
205 }
206 
StoreWebArchiveCallback(const std::string & baseName,bool autoName,napi_env env,napi_ref jsCallback)207 void WebviewController::StoreWebArchiveCallback(const std::string &baseName, bool autoName, napi_env env,
208     napi_ref jsCallback)
209 {
210     auto nweb_ptr = nweb_.lock();
211     if (!nweb_ptr) {
212         napi_value setResult[RESULT_COUNT] = {0};
213         setResult[PARAMZERO] = BusinessError::CreateError(env, NWebError::INIT_ERROR);
214         napi_get_null(env, &setResult[PARAMONE]);
215 
216         napi_value args[RESULT_COUNT] = {setResult[PARAMZERO], setResult[PARAMONE]};
217         napi_value callback = nullptr;
218         napi_get_reference_value(env, jsCallback, &callback);
219         napi_value callbackResult = nullptr;
220         napi_call_function(env, nullptr, callback, RESULT_COUNT, args, &callbackResult);
221         napi_delete_reference(env, jsCallback);
222         return;
223     }
224 
225     if (jsCallback == nullptr) {
226         return;
227     }
228 
229     auto callbackImpl = std::make_shared<OHOS::NWeb::NWebStoreWebArchiveCallback>();
230     callbackImpl->SetCallBack([env, jCallback = std::move(jsCallback)](std::string result) {
231         if (!env) {
232             return;
233         }
234         napi_handle_scope scope = nullptr;
235         napi_open_handle_scope(env, &scope);
236         if (scope == nullptr) {
237             return;
238         }
239 
240         napi_value setResult[RESULT_COUNT] = {0};
241         if (result.empty()) {
242             setResult[PARAMZERO] = BusinessError::CreateError(env, NWebError::INVALID_RESOURCE);
243             napi_get_null(env, &setResult[PARAMONE]);
244         } else {
245             napi_get_undefined(env, &setResult[PARAMZERO]);
246             napi_create_string_utf8(env, result.c_str(), NAPI_AUTO_LENGTH, &setResult[PARAMONE]);
247         }
248         napi_value args[RESULT_COUNT] = {setResult[PARAMZERO], setResult[PARAMONE]};
249         napi_value callback = nullptr;
250         napi_get_reference_value(env, jCallback, &callback);
251         napi_value callbackResult = nullptr;
252         napi_call_function(env, nullptr, callback, RESULT_COUNT, args, &callbackResult);
253 
254         napi_delete_reference(env, jCallback);
255         napi_close_handle_scope(env, scope);
256     });
257     nweb_ptr->StoreWebArchive(baseName, autoName, callbackImpl);
258     return;
259 }
260 
StoreWebArchivePromise(const std::string & baseName,bool autoName,napi_env env,napi_deferred deferred)261 void WebviewController::StoreWebArchivePromise(const std::string &baseName, bool autoName, napi_env env,
262     napi_deferred deferred)
263 {
264     auto nweb_ptr = nweb_.lock();
265     if (!nweb_ptr) {
266         napi_value jsResult = nullptr;
267         jsResult = NWebError::BusinessError::CreateError(env, NWebError::INIT_ERROR);
268         napi_reject_deferred(env, deferred, jsResult);
269         return;
270     }
271 
272     if (deferred == nullptr) {
273         return;
274     }
275 
276     auto callbackImpl = std::make_shared<OHOS::NWeb::NWebStoreWebArchiveCallback>();
277     callbackImpl->SetCallBack([env, deferred](std::string result) {
278         if (!env) {
279             return;
280         }
281         napi_handle_scope scope = nullptr;
282         napi_open_handle_scope(env, &scope);
283         if (scope == nullptr) {
284             return;
285         }
286 
287         napi_value setResult[RESULT_COUNT] = {0};
288         setResult[PARAMZERO] = NWebError::BusinessError::CreateError(env, NWebError::INVALID_RESOURCE);
289         napi_create_string_utf8(env, result.c_str(), NAPI_AUTO_LENGTH, &setResult[PARAMONE]);
290         napi_value args[RESULT_COUNT] = {setResult[PARAMZERO], setResult[PARAMONE]};
291         if (!result.empty()) {
292             napi_resolve_deferred(env, deferred, args[PARAMONE]);
293         } else {
294             napi_reject_deferred(env, deferred, args[PARAMZERO]);
295         }
296         napi_close_handle_scope(env, scope);
297     });
298     nweb_ptr->StoreWebArchive(baseName, autoName, callbackImpl);
299     return;
300 }
301 
CreateWebMessagePorts(std::vector<std::string> & ports)302 ErrCode WebviewController::CreateWebMessagePorts(std::vector<std::string>& ports)
303 {
304     auto nweb_ptr = nweb_.lock();
305     if (!nweb_ptr) {
306         return INIT_ERROR;
307     }
308 
309     nweb_ptr->CreateWebMessagePorts(ports);
310     return NWebError::NO_ERROR;
311 }
312 
PostWebMessage(std::string & message,std::vector<std::string> & ports,std::string & targetUrl)313 ErrCode WebviewController::PostWebMessage(std::string& message, std::vector<std::string>& ports, std::string& targetUrl)
314 {
315     auto nweb_ptr = nweb_.lock();
316     if (!nweb_ptr) {
317         return INIT_ERROR;
318     }
319 
320     nweb_ptr->PostWebMessage(message, ports, targetUrl);
321     return NWebError::NO_ERROR;
322 }
323 
WebMessagePort(int32_t nwebId,std::string & port)324 WebMessagePort::WebMessagePort(int32_t nwebId, std::string& port)
325     : nweb_(NWebHelper::Instance().GetNWeb(nwebId)), portHandle_(port)
326 {}
327 
ClosePort()328 ErrCode WebMessagePort::ClosePort()
329 {
330     auto nweb_ptr = nweb_.lock();
331     if (!nweb_ptr) {
332         return INIT_ERROR;
333     }
334 
335     nweb_ptr->ClosePort(portHandle_);
336     portHandle_.clear();
337     return NWebError::NO_ERROR;
338 }
339 
PostPortMessage(std::shared_ptr<NWebMessage> data)340 ErrCode WebMessagePort::PostPortMessage(std::shared_ptr<NWebMessage> data)
341 {
342     auto nweb_ptr = nweb_.lock();
343     if (!nweb_ptr) {
344         return INIT_ERROR;
345     }
346 
347     if (portHandle_.empty()) {
348         WVLOG_E("can't post message, message port already closed");
349         return CAN_NOT_POST_MESSAGE;
350     }
351     nweb_ptr->PostPortMessage(portHandle_, data);
352     return NWebError::NO_ERROR;
353 }
354 
SetPortMessageCallback(std::shared_ptr<NWebValueCallback<std::shared_ptr<NWebMessage>>> callback)355 ErrCode WebMessagePort::SetPortMessageCallback(
356     std::shared_ptr<NWebValueCallback<std::shared_ptr<NWebMessage>>> callback)
357 {
358     auto nweb_ptr = nweb_.lock();
359     if (!nweb_ptr) {
360         return INIT_ERROR;
361     }
362 
363     if (portHandle_.empty()) {
364         WVLOG_E("can't register message port callback event, message port already closed");
365         return CAN_NOT_REGISTER_MESSAGE_EVENT;
366     }
367     nweb_ptr->SetPortMessageCallback(portHandle_, callback);
368     return NWebError::NO_ERROR;
369 }
370 
GetPortHandle() const371 std::string WebMessagePort::GetPortHandle() const
372 {
373     return portHandle_;
374 }
375 
GetHitTestValue()376 HitTestResult WebviewController::GetHitTestValue()
377 {
378     OHOS::NWeb::HitTestResult nwebResult;
379     auto nweb_ptr = nweb_.lock();
380     if (nweb_ptr) {
381         nwebResult = nweb_ptr->GetHitTestResult();
382         nwebResult.SetType(ConverToWebHitTestType(nwebResult.GetType()));
383     }
384     return nwebResult;
385 }
386 
RequestFocus()387 void WebviewController::RequestFocus()
388 {
389     auto nweb_ptr = nweb_.lock();
390     if (nweb_ptr) {
391         nweb_ptr->OnFocus();
392     }
393 }
394 
GetResourceMgr(std::string bundleName,std::string moduleName)395 std::shared_ptr<Global::Resource::ResourceManager> WebviewController::GetResourceMgr(std::string bundleName,
396     std::string moduleName)
397 {
398     std::shared_ptr<AbilityRuntime::ApplicationContext> context =
399         AbilityRuntime::ApplicationContext::GetApplicationContext();
400     if (!context) {
401         WVLOG_E("Failed to get application Context.");
402         return nullptr;
403     }
404     if (bundleName.empty() || moduleName.empty()) {
405         return context->GetResourceManager();
406     }
407     auto moduleContext = context->CreateModuleContext(bundleName, moduleName);
408     if (!moduleContext) {
409         WVLOG_E("Failed to get module Context.");
410         return nullptr;
411     }
412     return moduleContext->GetResourceManager();
413 }
414 
ParseUrl(napi_env env,napi_value urlObj,std::string & result)415 ErrCode WebviewController::ParseUrl(napi_env env, napi_value urlObj, std::string& result)
416 {
417     napi_valuetype valueType = napi_null;
418     napi_typeof(env, urlObj, &valueType);
419     if ((valueType != napi_object) && (valueType != napi_string)) {
420         WVLOG_E("Unable to parse url object.");
421         return NWebError::INVALID_URL;
422     }
423     if (valueType == napi_string) {
424         NapiParseUtils::ParseString(env, urlObj, result);
425         WVLOG_D("The parsed url is: %{public}s", result.c_str());
426         return NWebError::NO_ERROR;
427     }
428     napi_value type = nullptr;
429     napi_valuetype typeVlueType = napi_null;
430     napi_get_named_property(env, urlObj, "type", &type);
431     napi_typeof(env, type, &typeVlueType);
432     if (typeVlueType == napi_number) {
433         int32_t typeInteger;
434         NapiParseUtils::ParseInt32(env, type, typeInteger);
435         if (typeInteger == static_cast<int>(ResourceType::RAWFILE)) {
436             napi_value paraArray = nullptr;
437             napi_get_named_property(env, urlObj, "params", &paraArray);
438             bool isArray = false;
439             napi_is_array(env, paraArray, &isArray);
440             if (!isArray) {
441                 WVLOG_E("Unable to parse parameter array from url object.");
442                 return NWebError::INVALID_RESOURCE;
443             }
444             napi_value fileNameObj;
445             napi_value bundleNameObj;
446             napi_value moduleNameObj;
447             std::string fileName;
448             std::string bundleName;
449             std::string moduleName;
450             napi_get_element(env, paraArray, 0, &fileNameObj);
451             napi_get_named_property(env, urlObj, "bundleName", &bundleNameObj);
452             napi_get_named_property(env, urlObj, "moduleName", &moduleNameObj);
453             NapiParseUtils::ParseString(env, fileNameObj, fileName);
454             NapiParseUtils::ParseString(env, bundleNameObj, bundleName);
455             NapiParseUtils::ParseString(env, moduleNameObj, moduleName);
456             auto resourceManager = GetResourceMgr(bundleName, moduleName);
457             if (!resourceManager) {
458                 WVLOG_E("Get resourceManager failed.");
459                 return NWebError::INVALID_RESOURCE;
460             }
461 
462             // Adapt to the input like: "file:///index.html?a=1", before the new solution comes, copy from arkui.
463             auto it = std::find_if(fileName.begin(), fileName.end(), [](char c) {
464                 return (c == '#') || (c == '?');
465             });
466             std::string params;
467             std::string newFileName = fileName;
468             if (it != fileName.end()) {
469                 newFileName = std::string(fileName.begin(), it);
470                 params = std::string(it, fileName.end());
471             }
472             auto state = resourceManager->GetRawFilePathByName(newFileName, result);
473             if (state != Global::Resource::SUCCESS) {
474                 WVLOG_E("GetRawfile error, filename:%{public}s, error:%{public}u, newfilename = %{public}s",
475                     fileName.c_str(), state, newFileName.c_str());
476                 return NWebError::INVALID_RESOURCE;
477             }
478             result = "file:///" + result + params;
479             WVLOG_D("The parsed url is: %{public}s", result.c_str());
480             return NWebError::NO_ERROR;
481         }
482         WVLOG_E("The type parsed from url object is not RAWFILE.");
483         return NWebError::INVALID_URL;
484     }
485     WVLOG_E("Unable to parse type from url object.");
486     return NWebError::INVALID_URL;
487 }
488 
LoadUrl(std::string url)489 ErrCode WebviewController::LoadUrl(std::string url)
490 {
491     auto nweb_ptr = nweb_.lock();
492     if (!nweb_ptr) {
493         return INIT_ERROR;
494     }
495     return nweb_ptr->Load(url);
496 }
497 
LoadUrl(std::string url,std::map<std::string,std::string> httpHeaders)498 ErrCode WebviewController::LoadUrl(std::string url, std::map<std::string, std::string> httpHeaders)
499 {
500     auto nweb_ptr = nweb_.lock();
501     if (!nweb_ptr) {
502         return INIT_ERROR;
503     }
504     return nweb_ptr->Load(url, httpHeaders);
505 }
506 
LoadData(std::string data,std::string mimeType,std::string encoding,std::string baseUrl,std::string historyUrl)507 ErrCode WebviewController::LoadData(std::string data, std::string mimeType, std::string encoding,
508     std::string baseUrl, std::string historyUrl)
509 {
510     auto nweb_ptr = nweb_.lock();
511     if (!nweb_ptr) {
512         return INIT_ERROR;
513     }
514     if (baseUrl.empty() && historyUrl.empty()) {
515         return nweb_ptr->LoadWithData(data, mimeType, encoding);
516     }
517     return nweb_ptr->LoadWithDataAndBaseUrl(baseUrl, data, mimeType, encoding, historyUrl);
518 }
519 
ConverToWebHitTestType(int hitType)520 int WebviewController::ConverToWebHitTestType(int hitType)
521 {
522     WebHitTestType webHitType;
523     switch (hitType) {
524         case HitTestResult::UNKNOWN_TYPE:
525             webHitType = WebHitTestType::UNKNOWN;
526             break;
527         case HitTestResult::ANCHOR_TYPE:
528             webHitType = WebHitTestType::HTTP;
529             break;
530         case HitTestResult::PHONE_TYPE:
531             webHitType = WebHitTestType::PHONE;
532             break;
533         case HitTestResult::GEO_TYPE:
534             webHitType = WebHitTestType::MAP;
535             break;
536         case HitTestResult::EMAIL_TYPE:
537             webHitType = WebHitTestType::EMAIL;
538             break;
539         case HitTestResult::IMAGE_TYPE:
540             webHitType = WebHitTestType::IMG;
541             break;
542         case HitTestResult::IMAGE_ANCHOR_TYPE:
543             webHitType = WebHitTestType::HTTP_IMG;
544             break;
545         case HitTestResult::SRC_ANCHOR_TYPE:
546             webHitType = WebHitTestType::HTTP;
547             break;
548         case HitTestResult::SRC_IMAGE_ANCHOR_TYPE:
549             webHitType = WebHitTestType::HTTP_IMG;
550             break;
551         case HitTestResult::EDIT_TEXT_TYPE:
552             webHitType = WebHitTestType::EDIT;
553             break;
554         default:
555             webHitType = WebHitTestType::UNKNOWN;
556             break;
557     }
558     return static_cast<int>(webHitType);
559 }
560 
GetHitTest()561 int WebviewController::GetHitTest()
562 {
563     auto nweb_ptr = nweb_.lock();
564     if (nweb_ptr) {
565         return ConverToWebHitTestType(nweb_ptr->GetHitTestResult().GetType());
566     }
567     return static_cast<int>(WebHitTestType::UNKNOWN);
568 }
569 
570 
ClearMatches()571 void WebviewController::ClearMatches()
572 {
573     auto nweb_ptr = nweb_.lock();
574     if (nweb_ptr) {
575         nweb_ptr->ClearMatches();
576     }
577 }
578 
SearchNext(bool forward)579 void WebviewController::SearchNext(bool forward)
580 {
581     auto nweb_ptr = nweb_.lock();
582     if (nweb_ptr) {
583         nweb_ptr->FindNext(forward);
584     }
585 }
586 
SearchAllAsync(const std::string & searchString)587 void WebviewController::SearchAllAsync(const std::string& searchString)
588 {
589     auto nweb_ptr = nweb_.lock();
590     if (nweb_ptr) {
591         nweb_ptr->FindAllAsync(searchString);
592     }
593 }
594 
ClearSslCache()595 void WebviewController::ClearSslCache()
596 {
597     auto nweb_ptr = nweb_.lock();
598     if (nweb_ptr) {
599         nweb_ptr->ClearSslCache();
600     }
601 }
602 
ClearClientAuthenticationCache()603 void WebviewController::ClearClientAuthenticationCache()
604 {
605     auto nweb_ptr = nweb_.lock();
606     if (nweb_ptr) {
607         nweb_ptr->ClearClientAuthenticationCache();
608     }
609 }
610 
Stop()611 void WebviewController::Stop()
612 {
613     auto nweb_ptr = nweb_.lock();
614     if (nweb_ptr) {
615         nweb_ptr->Stop();
616     }
617 }
618 
Zoom(float factor)619 ErrCode WebviewController::Zoom(float factor)
620 {
621     auto nweb_ptr = nweb_.lock();
622     if (!nweb_ptr) {
623         return INIT_ERROR;
624     }
625     ErrCode result = NWebError::NO_ERROR;
626     result = nweb_ptr->Zoom(factor);
627 
628     return result;
629 }
630 
DeleteJavaScriptRegister(const std::string & objName,const std::vector<std::string> & methodList)631 ErrCode WebviewController::DeleteJavaScriptRegister(const std::string& objName,
632     const std::vector<std::string>& methodList)
633 {
634     auto nweb_ptr = nweb_.lock();
635     if (nweb_ptr) {
636         nweb_ptr->UnregisterArkJSfunction(objName, methodList);
637     }
638 
639     if (javaScriptResultCb_) {
640         bool ret = javaScriptResultCb_->DeleteJavaScriptRegister(objName);
641         if (!ret) {
642             return CANNOT_DEL_JAVA_SCRIPT_PROXY;
643         }
644     }
645 
646     return NWebError::NO_ERROR;
647 }
648 
SetNWebJavaScriptResultCallBack()649 void WebviewController::SetNWebJavaScriptResultCallBack()
650 {
651     auto nweb_ptr = nweb_.lock();
652     if (!nweb_ptr || javaScriptResultCb_) {
653         return;
654     }
655 
656     javaScriptResultCb_ = std::make_shared<WebviewJavaScriptResultCallBack>();
657     nweb_ptr->SetNWebJavaScriptResultCallBack(javaScriptResultCb_);
658 }
659 
RegisterJavaScriptProxy(napi_env env,napi_value obj,const std::string & objName,const std::vector<std::string> & methodList)660 void WebviewController::RegisterJavaScriptProxy(napi_env env, napi_value obj,
661     const std::string& objName, const std::vector<std::string>& methodList)
662 {
663     auto nweb_ptr = nweb_.lock();
664     if (!nweb_ptr) {
665         return;
666     }
667     if (javaScriptResultCb_) {
668         javaScriptResultCb_->RegisterJavaScriptProxy(env, obj, objName, methodList);
669     }
670     nweb_ptr->RegisterArkJSfunction(objName, methodList);
671 }
672 
RunJavaScriptCallback(const std::string & script,napi_env env,napi_ref jsCallback)673 void WebviewController::RunJavaScriptCallback(const std::string &script, napi_env env, napi_ref jsCallback)
674 {
675     auto nweb_ptr = nweb_.lock();
676     if (!nweb_ptr) {
677         napi_value setResult[RESULT_COUNT] = {0};
678         setResult[PARAMZERO] = BusinessError::CreateError(env, NWebError::INIT_ERROR);
679         napi_get_null(env, &setResult[PARAMONE]);
680 
681         napi_value args[RESULT_COUNT] = {setResult[PARAMZERO], setResult[PARAMONE]};
682         napi_value callback = nullptr;
683         napi_get_reference_value(env, jsCallback, &callback);
684         napi_value callbackResult = nullptr;
685         napi_call_function(env, nullptr, callback, RESULT_COUNT, args, &callbackResult);
686         napi_delete_reference(env, jsCallback);
687         return;
688     }
689 
690     if (jsCallback == nullptr) {
691         return;
692     }
693 
694     auto callbackImpl = std::make_shared<WebviewJavaScriptExecuteCallback>(env, jsCallback, nullptr);
695     nweb_ptr->ExecuteJavaScript(script, callbackImpl);
696 }
697 
RunJavaScriptPromise(const std::string & script,napi_env env,napi_deferred deferred)698 void WebviewController::RunJavaScriptPromise(const std::string &script, napi_env env,
699     napi_deferred deferred)
700 {
701     auto nweb_ptr = nweb_.lock();
702     if (!nweb_ptr) {
703         napi_value jsResult = nullptr;
704         jsResult = NWebError::BusinessError::CreateError(env, NWebError::INIT_ERROR);
705         napi_reject_deferred(env, deferred, jsResult);
706         return;
707     }
708 
709     if (deferred == nullptr) {
710         return;
711     }
712 
713     auto callbackImpl = std::make_shared<WebviewJavaScriptExecuteCallback>(env, nullptr, deferred);
714     nweb_ptr->ExecuteJavaScript(script, callbackImpl);
715 }
716 
GetUrl()717 std::string WebviewController::GetUrl()
718 {
719     std::string url = "";
720     auto nweb_ptr = nweb_.lock();
721     if (nweb_ptr) {
722         url = nweb_ptr->GetUrl();
723     }
724     return url;
725 }
726 
GetOriginalUrl()727 std::string WebviewController::GetOriginalUrl()
728 {
729     std::string url = "";
730     auto nweb_ptr = nweb_.lock();
731     if (nweb_ptr) {
732         url = nweb_ptr->GetOriginalUrl();
733     }
734     return url;
735 }
736 
PutNetworkAvailable(bool available)737 void WebviewController::PutNetworkAvailable(bool available)
738 {
739     auto nweb_ptr = nweb_.lock();
740     if (nweb_ptr) {
741         nweb_ptr->PutNetworkAvailable(available);
742     }
743 }
744 
HasImagesCallback(napi_env env,napi_ref jsCallback)745 ErrCode WebviewController::HasImagesCallback(napi_env env, napi_ref jsCallback)
746 {
747     auto nweb_ptr = nweb_.lock();
748     if (!nweb_ptr) {
749         napi_value setResult[RESULT_COUNT] = {0};
750         setResult[PARAMZERO] = BusinessError::CreateError(env, NWebError::INIT_ERROR);
751         napi_get_null(env, &setResult[PARAMONE]);
752 
753         napi_value args[RESULT_COUNT] = {setResult[PARAMZERO], setResult[PARAMONE]};
754         napi_value callback = nullptr;
755         napi_get_reference_value(env, jsCallback, &callback);
756         napi_value callbackResult = nullptr;
757         napi_call_function(env, nullptr, callback, RESULT_COUNT, args, &callbackResult);
758         napi_delete_reference(env, jsCallback);
759         return NWebError::INIT_ERROR;
760     }
761 
762     if (jsCallback == nullptr) {
763         return NWebError::PARAM_CHECK_ERROR;
764     }
765 
766     auto callbackImpl = std::make_shared<WebviewHasImageCallback>(env, jsCallback, nullptr);
767     nweb_ptr->HasImages(callbackImpl);
768     return NWebError::NO_ERROR;
769 }
770 
HasImagesPromise(napi_env env,napi_deferred deferred)771 ErrCode WebviewController::HasImagesPromise(napi_env env, napi_deferred deferred)
772 {
773     auto nweb_ptr = nweb_.lock();
774     if (!nweb_ptr) {
775         napi_value jsResult = nullptr;
776         jsResult = NWebError::BusinessError::CreateError(env, NWebError::INIT_ERROR);
777         napi_reject_deferred(env, deferred, jsResult);
778         return NWebError::INIT_ERROR;
779     }
780 
781     if (deferred == nullptr) {
782         return NWebError::PARAM_CHECK_ERROR;
783     }
784 
785     auto callbackImpl = std::make_shared<WebviewHasImageCallback>(env, nullptr, deferred);
786     nweb_ptr->HasImages(callbackImpl);
787     return NWebError::NO_ERROR;
788 }
789 
RemoveCache(bool include_disk_files)790 void WebviewController::RemoveCache(bool include_disk_files)
791 {
792     auto nweb_ptr = nweb_.lock();
793     if (nweb_ptr) {
794         nweb_ptr->RemoveCache(include_disk_files);
795     }
796 }
797 
GetHistoryList()798 std::shared_ptr<NWebHistoryList> WebviewController::GetHistoryList()
799 {
800     auto nweb_ptr = nweb_.lock();
801     if (!nweb_ptr) {
802         return nullptr;
803     }
804     return nweb_ptr->GetHistoryList();
805 }
806 
GetItem(int32_t index)807 std::shared_ptr<NWebHistoryItem> WebHistoryList::GetItem(int32_t index)
808 {
809     if (!sptrHistoryList_) {
810         return nullptr;
811     }
812     return sptrHistoryList_->GetItem(index);
813 }
814 
GetListSize()815 int32_t WebHistoryList::GetListSize()
816 {
817     int32_t listSize = 0;
818 
819     if (!sptrHistoryList_) {
820         return listSize;
821     }
822     listSize = sptrHistoryList_->GetListSize();
823     return listSize;
824 }
825 
GetFavicon(const void ** data,size_t & width,size_t & height,ImageColorType & colorType,ImageAlphaType & alphaType)826 bool WebviewController::GetFavicon(
827     const void **data, size_t &width, size_t &height, ImageColorType &colorType, ImageAlphaType &alphaType)
828 {
829     bool isGetFavicon = false;
830     auto nweb_ptr = nweb_.lock();
831     if (nweb_ptr) {
832         isGetFavicon = nweb_ptr->GetFavicon(data, width, height, colorType, alphaType);
833     }
834     return isGetFavicon;
835 }
836 
SerializeWebState()837 WebState WebviewController::SerializeWebState()
838 {
839     auto nweb_ptr = nweb_.lock();
840     if (nweb_ptr) {
841         return nweb_ptr->SerializeWebState();
842     }
843     return nullptr;
844 }
845 
RestoreWebState(WebState state)846 bool WebviewController::RestoreWebState(WebState state)
847 {
848     bool isRestored = false;
849     auto nweb_ptr = nweb_.lock();
850     if (nweb_ptr) {
851         isRestored = nweb_ptr->RestoreWebState(state);
852     }
853     return isRestored;
854 }
855 
ScrollPageDown(bool bottom)856 void WebviewController::ScrollPageDown(bool bottom)
857 {
858     auto nweb_ptr = nweb_.lock();
859     if (nweb_ptr) {
860         nweb_ptr->PageDown(bottom);
861     }
862     return;
863 }
864 
ScrollPageUp(bool top)865 void WebviewController::ScrollPageUp(bool top)
866 {
867     auto nweb_ptr = nweb_.lock();
868     if (nweb_ptr) {
869         nweb_ptr->PageUp(top);
870     }
871     return;
872 }
873 
ScrollTo(float x,float y)874 void WebviewController::ScrollTo(float x, float y)
875 {
876     auto nweb_ptr = nweb_.lock();
877     if (nweb_ptr) {
878         nweb_ptr->ScrollTo(x, y);
879     }
880     return;
881 }
882 
ScrollBy(float deltaX,float deltaY)883 void WebviewController::ScrollBy(float deltaX, float deltaY)
884 {
885     auto nweb_ptr = nweb_.lock();
886     if (nweb_ptr) {
887         nweb_ptr->ScrollBy(deltaX, deltaY);
888     }
889     return;
890 }
891 
SlideScroll(float vx,float vy)892 void WebviewController::SlideScroll(float vx, float vy)
893 {
894     auto nweb_ptr = nweb_.lock();
895     if (nweb_ptr) {
896         nweb_ptr->SlideScroll(vx, vy);
897     }
898     return;
899 }
900 } // namespace NWeb
901 } // namespace OHOS
902