1 /*
2  * Copyright (c) 2022-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 "nweb_helper.h"
17 
18 #include <cstdint>
19 #include <refbase.h>
20 #include <surface.h>
21 #include <thread>
22 
23 #include "app_mgr_client.h"
24 #include "application_context.h"
25 #include "ark_web_nweb_webview_bridge_helper.h"
26 #include "config_policy_utils.h"
27 #include "locale_config.h"
28 #include "nweb_adapter_helper.h"
29 #include "nweb_c_api.h"
30 #include "nweb_enhance_surface_adapter.h"
31 #include "nweb_hisysevent.h"
32 #include "nweb_log.h"
33 #include "nweb_surface_adapter.h"
34 #include "parameter.h"
35 #include "parameters.h"
36 
37 namespace {
38 static bool g_isFirstTimeStartUp = false;
39 
40 const int32_t NS_TO_S = 1000000000;
41 const uint32_t NWEB_SURFACE_MAX_WIDTH = 7680;
42 const uint32_t NWEB_SURFACE_MAX_HEIGHT = 7680;
43 
44 // Run DO macro for every function defined in the API.
45 #define FOR_EACH_API_FN(DO)                           \
46     DO(WebDownloadManager_PutDownloadCallback);       \
47     DO(WebDownloader_ResumeDownloadStatic);           \
48     DO(WebDownloader_StartDownload);                  \
49     DO(WebDownloader_CreateDownloadDelegateCallback); \
50     DO(WebDownloader_SetDownloadBeforeStart);         \
51     DO(WebDownloader_SetDownloadDidUpdate);           \
52     DO(WebDownload_Continue);                         \
53     DO(WebDownload_CancelBeforeDownload);             \
54     DO(WebDownload_PauseBeforeDownload);              \
55     DO(WebDownload_ResumeBeforeDownload);             \
56     DO(WebDownload_Cancel);                           \
57     DO(WebDownload_Pause);                            \
58     DO(WebDownload_Resume);                           \
59     DO(WebDownload_GetItemState);                     \
60     DO(WebDownload_GetItemStateByGuid);               \
61     DO(WebDownloadItem_Guid);                         \
62     DO(WebDownloadItem_GetDownloadItemId);            \
63     DO(WebDownloadItem_GetState);                     \
64     DO(WebDownloadItem_CurrentSpeed);                 \
65     DO(WebDownloadItem_PercentComplete);              \
66     DO(WebDownloadItem_TotalBytes);                   \
67     DO(WebDownloadItem_ReceivedBytes);                \
68     DO(WebDownloadItem_FullPath);                     \
69     DO(WebDownloadItem_Url);                          \
70     DO(WebDownloadItem_OriginalUrl);                  \
71     DO(WebDownloadItem_SuggestedFileName);            \
72     DO(WebDownloadItem_ContentDisposition);           \
73     DO(WebDownloadItem_ETag);                         \
74     DO(WebDownloadItem_MimeType);                     \
75     DO(WebDownloadItem_NWebId);                       \
76     DO(WebDownloadItem_IsPaused);                     \
77     DO(WebDownloadItem_Method);                       \
78     DO(WebDownloadItem_LastErrorCode);                \
79     DO(WebDownloadItem_ReceivedSlices);               \
80     DO(WebDownloadItem_LastModified);                 \
81     DO(WebDownloadItem_CreateWebDownloadItem);        \
82     DO(WebDownloadItem_Destroy);                      \
83     DO(WebDownloadItem_SetUrl);                       \
84     DO(WebDownloadItem_SetFullPath);                  \
85     DO(WebDownloadItem_SetETag);                      \
86     DO(WebDownloadItem_SetLastModified);              \
87     DO(WebDownloadItem_SetMimeType);                  \
88     DO(WebDownloadItem_SetReceivedBytes);             \
89     DO(WebDownloadItem_SetTotalBytes);                \
90     DO(WebDownloadItem_SetReceivedSlices);            \
91     DO(WebDownloadItem_SetGuid);                      \
92     DO(DestroyBeforeDownloadCallbackWrapper);         \
93     DO(DestroyDownloadItemCallbackWrapper)
94 
95 struct NWebCApi {
96     // Generate a function pointer field for every NWeb C API function.
97 #define GEN_FN_PTR(fn) decltype(&(fn)) impl_##fn = nullptr
98     FOR_EACH_API_FN(GEN_FN_PTR);
99 #undef GEN_FN_PTR
100 };
101 
102 template<typename Fn>
LoadFunction(const char * functionName,Fn * fnOut)103 void LoadFunction(const char* functionName, Fn* fnOut)
104 {
105     void* fn = OHOS::NWeb::NWebHelper::Instance().LoadFuncSymbol(functionName);
106     if (!fn) {
107         WVLOG_E("%{public}s not found.", functionName);
108         return;
109     }
110     *fnOut = reinterpret_cast<Fn>(fn);
111 }
112 
113 NWebCApi* g_nwebCApi = nullptr;
114 
LoadNWebCApi(NWebCApi * api)115 void LoadNWebCApi(NWebCApi* api)
116 {
117     // Initialize each NWebExApi function pointer field from the DLL
118 #define LOAD_FN_PTR(fn) LoadFunction(#fn, &api->impl_##fn)
119     FOR_EACH_API_FN(LOAD_FN_PTR);
120 #undef LOAD_FN_PTR
121 }
122 
LoadNWebSDK()123 bool LoadNWebSDK()
124 {
125     if (g_nwebCApi) {
126         WVLOG_I("LoadNWebSDK had loaded.");
127         return true;
128     }
129 
130     auto* nwebCApi = new NWebCApi();
131     if (nwebCApi == nullptr) {
132         WVLOG_E("nwebCApi is nullptr.");
133         return false;
134     }
135     LoadNWebCApi(nwebCApi);
136     g_nwebCApi = nwebCApi;
137     return true;
138 }
139 #undef FOR_EACH_API_FN
140 } // namespace
141 
WebDownloadManager_PutDownloadCallback(WebDownloadDelegateCallback * callback)142 extern "C" void WebDownloadManager_PutDownloadCallback(WebDownloadDelegateCallback* callback)
143 {
144     if (!g_nwebCApi->impl_WebDownloadManager_PutDownloadCallback) {
145         WVLOG_E("WebDownloadManager_PutDownloadCallback not found.");
146         return;
147     }
148     g_nwebCApi->impl_WebDownloadManager_PutDownloadCallback(callback);
149 }
150 
WebDownloader_SetDownloadBeforeStart(WebDownloadDelegateCallback * callback,OnDownloadBeforeStart fun)151 extern "C" void WebDownloader_SetDownloadBeforeStart(WebDownloadDelegateCallback* callback, OnDownloadBeforeStart fun)
152 {
153     if (!g_nwebCApi->impl_WebDownloader_SetDownloadBeforeStart) {
154         WVLOG_E("WebDownloader_SetDownloadBeforeStart not found.");
155         return;
156     }
157     g_nwebCApi->impl_WebDownloader_SetDownloadBeforeStart(callback, fun);
158 }
159 
WebDownloader_SetDownloadDidUpdate(WebDownloadDelegateCallback * callback,OnDownloadDidUpdate fun)160 extern "C" void WebDownloader_SetDownloadDidUpdate(WebDownloadDelegateCallback* callback, OnDownloadDidUpdate fun)
161 {
162     if (!g_nwebCApi->impl_WebDownloader_SetDownloadDidUpdate) {
163         WVLOG_E("WebDownloader_SetDownloadDidUpdate not found");
164         return;
165     }
166     g_nwebCApi->impl_WebDownloader_SetDownloadDidUpdate(callback, fun);
167 }
168 
WebDownloader_ResumeDownloadStatic(const NWebDownloadItem * downloadItem)169 extern "C" void WebDownloader_ResumeDownloadStatic(const NWebDownloadItem* downloadItem)
170 {
171     if (!g_nwebCApi->impl_WebDownloader_ResumeDownloadStatic) {
172         WVLOG_E("WebDownloader_ResumeDownloadStatic not found.");
173         return;
174     }
175     g_nwebCApi->impl_WebDownloader_ResumeDownloadStatic(downloadItem);
176 }
177 
WebDownloader_StartDownload(int32_t nwebId,const char * url)178 extern "C" void WebDownloader_StartDownload(int32_t nwebId, const char* url)
179 {
180     if (!g_nwebCApi->impl_WebDownloader_StartDownload) {
181         WVLOG_E("WebDownloader_StartDownload not found.");
182         return;
183     }
184     g_nwebCApi->impl_WebDownloader_StartDownload(nwebId, url);
185 }
186 
WebDownloader_CreateDownloadDelegateCallback(WebDownloadDelegateCallback ** callback)187 extern "C" void WebDownloader_CreateDownloadDelegateCallback(WebDownloadDelegateCallback** callback)
188 {
189     if (!g_nwebCApi || !g_nwebCApi->impl_WebDownloader_CreateDownloadDelegateCallback) {
190         WVLOG_E("WebDownloader_CreateDownloadDelegateCallback not found.");
191         return;
192     }
193 
194     return g_nwebCApi->impl_WebDownloader_CreateDownloadDelegateCallback(callback);
195 }
196 
WebDownload_Continue(const WebBeforeDownloadCallbackWrapper * wrapper,const char * downloadPath)197 extern "C" void WebDownload_Continue(const WebBeforeDownloadCallbackWrapper* wrapper, const char* downloadPath)
198 {
199     if (!g_nwebCApi->impl_WebDownload_Continue) {
200         WVLOG_E("WebDownload_Continue not found.");
201         return;
202     }
203     g_nwebCApi->impl_WebDownload_Continue(wrapper, downloadPath);
204 }
205 
WebDownload_CancelBeforeDownload(const WebBeforeDownloadCallbackWrapper * wrapper)206 extern "C" void WebDownload_CancelBeforeDownload(const WebBeforeDownloadCallbackWrapper* wrapper)
207 {
208     if (!g_nwebCApi->impl_WebDownload_CancelBeforeDownload) {
209         WVLOG_E("WebDownload_CancelBeforeDownload not found.");
210         return;
211     }
212     g_nwebCApi->impl_WebDownload_CancelBeforeDownload(wrapper);
213 }
214 
WebDownload_PauseBeforeDownload(const WebBeforeDownloadCallbackWrapper * wrapper)215 extern "C" void WebDownload_PauseBeforeDownload(const WebBeforeDownloadCallbackWrapper* wrapper)
216 {
217     if (!g_nwebCApi->impl_WebDownload_PauseBeforeDownload) {
218         WVLOG_E("WebDownload_PauseBeforeDownload not found.");
219         return;
220     }
221     g_nwebCApi->impl_WebDownload_PauseBeforeDownload(wrapper);
222 }
223 
WebDownload_ResumeBeforeDownload(const WebBeforeDownloadCallbackWrapper * wrapper)224 extern "C" void WebDownload_ResumeBeforeDownload(const WebBeforeDownloadCallbackWrapper* wrapper)
225 {
226     if (!g_nwebCApi->impl_WebDownload_ResumeBeforeDownload) {
227         WVLOG_E("WebDownload_ResumeBeforeDownload not found.");
228         return;
229     }
230     g_nwebCApi->impl_WebDownload_ResumeBeforeDownload(wrapper);
231 }
232 
WebDownload_Cancel(const WebDownloadItemCallbackWrapper * wrapper)233 extern "C" void WebDownload_Cancel(const WebDownloadItemCallbackWrapper* wrapper)
234 {
235     if (!g_nwebCApi->impl_WebDownload_Cancel) {
236         WVLOG_E("WebDownload_Cancel not found.");
237         return;
238     }
239     g_nwebCApi->impl_WebDownload_Cancel(wrapper);
240 }
241 
WebDownload_Pause(const WebDownloadItemCallbackWrapper * wrapper)242 extern "C" void WebDownload_Pause(const WebDownloadItemCallbackWrapper* wrapper)
243 {
244     if (!g_nwebCApi->impl_WebDownload_Pause) {
245         WVLOG_E("WebDownload_Pause not found");
246         return;
247     }
248     g_nwebCApi->impl_WebDownload_Pause(wrapper);
249 }
250 
WebDownload_Resume(const WebDownloadItemCallbackWrapper * wrapper)251 extern "C" void WebDownload_Resume(const WebDownloadItemCallbackWrapper* wrapper)
252 {
253     if (!g_nwebCApi->impl_WebDownload_Resume) {
254         WVLOG_E("WebDownload_Resume not found.");
255         return;
256     }
257     g_nwebCApi->impl_WebDownload_Resume(wrapper);
258 }
259 
WebDownload_GetItemState(int32_t nwebId,long downloadItemId)260 extern "C" NWebDownloadItemState WebDownload_GetItemState(int32_t nwebId, long downloadItemId)
261 {
262     if (!g_nwebCApi || !g_nwebCApi->impl_WebDownload_GetItemState) {
263         return NWebDownloadItemState::MAX_DOWNLOAD_STATE;
264     }
265     return g_nwebCApi->impl_WebDownload_GetItemState(nwebId, downloadItemId);
266 }
267 
WebDownload_GetItemStateByGuid(const std::string & guid)268 extern "C" NWebDownloadItemState WebDownload_GetItemStateByGuid(const std::string& guid)
269 {
270     if (!g_nwebCApi || !g_nwebCApi->impl_WebDownload_GetItemStateByGuid) {
271         return NWebDownloadItemState::MAX_DOWNLOAD_STATE;
272     }
273     return g_nwebCApi->impl_WebDownload_GetItemStateByGuid(guid);
274 }
275 
WebDownloadItem_Guid(const NWebDownloadItem * downloadItem)276 extern "C" char* WebDownloadItem_Guid(const NWebDownloadItem* downloadItem)
277 {
278     if (!g_nwebCApi->impl_WebDownloadItem_Guid) {
279         WVLOG_E("WebDownloadItem_Guid not found.");
280         return nullptr;
281     }
282     return g_nwebCApi->impl_WebDownloadItem_Guid(downloadItem);
283 }
284 
WebDownloadItem_GetDownloadItemId(const NWebDownloadItem * downloadItem)285 extern "C" long WebDownloadItem_GetDownloadItemId(const NWebDownloadItem* downloadItem)
286 {
287     if (!g_nwebCApi->impl_WebDownloadItem_GetDownloadItemId) {
288         return false;
289     }
290     return g_nwebCApi->impl_WebDownloadItem_GetDownloadItemId(downloadItem);
291 }
292 
WebDownloadItem_GetState(const NWebDownloadItem * downloadItem)293 extern "C" NWebDownloadItemState WebDownloadItem_GetState(const NWebDownloadItem* downloadItem)
294 {
295     if (!g_nwebCApi->impl_WebDownloadItem_GetState) {
296         return NWebDownloadItemState::MAX_DOWNLOAD_STATE;
297     }
298     return g_nwebCApi->impl_WebDownloadItem_GetState(downloadItem);
299 }
300 
WebDownloadItem_CurrentSpeed(const NWebDownloadItem * downloadItem)301 extern "C" int WebDownloadItem_CurrentSpeed(const NWebDownloadItem* downloadItem)
302 {
303     if (!g_nwebCApi->impl_WebDownloadItem_CurrentSpeed) {
304         WVLOG_E("WebDownloadItem_CurrentSpeed not found.");
305         return 0;
306     }
307     return g_nwebCApi->impl_WebDownloadItem_CurrentSpeed(downloadItem);
308 }
309 
WebDownloadItem_PercentComplete(const NWebDownloadItem * downloadItem)310 extern "C" int WebDownloadItem_PercentComplete(const NWebDownloadItem* downloadItem)
311 {
312     if (!g_nwebCApi->impl_WebDownloadItem_PercentComplete) {
313         WVLOG_E("WebDownloadItem_TotalBytes not found.");
314         return 0;
315     }
316     return g_nwebCApi->impl_WebDownloadItem_PercentComplete(downloadItem);
317 }
318 
WebDownloadItem_TotalBytes(const NWebDownloadItem * downloadItem)319 extern "C" int64_t WebDownloadItem_TotalBytes(const NWebDownloadItem* downloadItem)
320 {
321     if (!g_nwebCApi->impl_WebDownloadItem_TotalBytes) {
322         WVLOG_E("WebDownloadItem_TotalBytes not found.");
323         return 0;
324     }
325     return g_nwebCApi->impl_WebDownloadItem_TotalBytes(downloadItem);
326 }
327 
WebDownloadItem_ReceivedBytes(const NWebDownloadItem * downloadItem)328 extern "C" int64_t WebDownloadItem_ReceivedBytes(const NWebDownloadItem* downloadItem)
329 {
330     if (!g_nwebCApi->impl_WebDownloadItem_ReceivedBytes) {
331         WVLOG_E("WebDownloadItem_ReceivedBytes not found.");
332         return 0;
333     }
334     return g_nwebCApi->impl_WebDownloadItem_ReceivedBytes(downloadItem);
335 }
336 
WebDownloadItem_FullPath(const NWebDownloadItem * downloadItem)337 extern "C" char* WebDownloadItem_FullPath(const NWebDownloadItem* downloadItem)
338 {
339     if (!g_nwebCApi->impl_WebDownloadItem_FullPath) {
340         WVLOG_E("WebDownloadItem_FullPath not found");
341         return nullptr;
342     }
343     return g_nwebCApi->impl_WebDownloadItem_FullPath(downloadItem);
344 }
345 
WebDownloadItem_Url(const NWebDownloadItem * downloadItem)346 extern "C" char* WebDownloadItem_Url(const NWebDownloadItem* downloadItem)
347 {
348     if (!g_nwebCApi->impl_WebDownloadItem_Url) {
349         WVLOG_E("WebDownloadItem_Url not found.");
350         return nullptr;
351     }
352     return g_nwebCApi->impl_WebDownloadItem_Url(downloadItem);
353 }
354 
WebDownloadItem_OriginalUrl(const NWebDownloadItem * downloadItem)355 extern "C" char* WebDownloadItem_OriginalUrl(const NWebDownloadItem* downloadItem)
356 {
357     if (!g_nwebCApi->impl_WebDownloadItem_OriginalUrl) {
358         WVLOG_E("WebDownloadItem_OriginalUrl not found.");
359         return nullptr;
360     }
361     return g_nwebCApi->impl_WebDownloadItem_OriginalUrl(downloadItem);
362 }
363 
WebDownloadItem_SuggestedFileName(const NWebDownloadItem * downloadItem)364 extern "C" char* WebDownloadItem_SuggestedFileName(const NWebDownloadItem* downloadItem)
365 {
366     if (!g_nwebCApi->impl_WebDownloadItem_SuggestedFileName) {
367         WVLOG_E("WebDownloadItem_SuggestedFileName not found.");
368         return nullptr;
369     }
370     return g_nwebCApi->impl_WebDownloadItem_SuggestedFileName(downloadItem);
371 }
372 
WebDownloadItem_ContentDisposition(const NWebDownloadItem * downloadItem)373 extern "C" char* WebDownloadItem_ContentDisposition(const NWebDownloadItem* downloadItem)
374 {
375     if (!g_nwebCApi->impl_WebDownloadItem_ContentDisposition) {
376         WVLOG_E("WebDownloadItem_ContentDisposition not found.");
377         return nullptr;
378     }
379     return g_nwebCApi->impl_WebDownloadItem_ContentDisposition(downloadItem);
380 }
381 
WebDownloadItem_ETag(const NWebDownloadItem * downloadItem)382 extern "C" char* WebDownloadItem_ETag(const NWebDownloadItem* downloadItem)
383 {
384     if (!g_nwebCApi->impl_WebDownloadItem_ETag) {
385         WVLOG_E("WebDownloadItem_ETag not found.");
386         return nullptr;
387     }
388     return g_nwebCApi->impl_WebDownloadItem_ETag(downloadItem);
389 }
390 
WebDownloadItem_MimeType(const NWebDownloadItem * downloadItem)391 extern "C" char* WebDownloadItem_MimeType(const NWebDownloadItem* downloadItem)
392 {
393     if (!g_nwebCApi->impl_WebDownloadItem_MimeType) {
394         WVLOG_E("WebDownloadItem_MimeType not found.");
395         return nullptr;
396     }
397     return g_nwebCApi->impl_WebDownloadItem_MimeType(downloadItem);
398 }
399 
WebDownloadItem_IsPaused(const NWebDownloadItem * downloadItem)400 extern "C" bool WebDownloadItem_IsPaused(const NWebDownloadItem* downloadItem)
401 {
402     if (!g_nwebCApi->impl_WebDownloadItem_IsPaused) {
403         WVLOG_E("WebDownloadItem_IsPaused not found.");
404         return false;
405     }
406     return g_nwebCApi->impl_WebDownloadItem_IsPaused(downloadItem);
407 }
408 
WebDownloadItem_Method(const NWebDownloadItem * downloadItem)409 extern "C" char* WebDownloadItem_Method(const NWebDownloadItem* downloadItem)
410 {
411     if (!g_nwebCApi->impl_WebDownloadItem_Method) {
412         WVLOG_E("WebDownloadItem_Method not found.");
413         return nullptr;
414     }
415     return g_nwebCApi->impl_WebDownloadItem_Method(downloadItem);
416 }
417 
WebDownloadItem_LastErrorCode(const NWebDownloadItem * downloadItem)418 extern "C" int WebDownloadItem_LastErrorCode(const NWebDownloadItem* downloadItem)
419 {
420     if (!g_nwebCApi->impl_WebDownloadItem_LastErrorCode) {
421         WVLOG_E("WebDownloadItem_LastErrorCode not found.");
422         return 0;
423     }
424     return g_nwebCApi->impl_WebDownloadItem_LastErrorCode(downloadItem);
425 }
426 
WebDownloadItem_ReceivedSlices(const NWebDownloadItem * downloadItem)427 extern "C" char* WebDownloadItem_ReceivedSlices(const NWebDownloadItem* downloadItem)
428 {
429     if (!g_nwebCApi->impl_WebDownloadItem_ReceivedSlices) {
430         WVLOG_E("WebDownloadItem_ReceivedSlices not found.");
431         return nullptr;
432     }
433     return g_nwebCApi->impl_WebDownloadItem_ReceivedSlices(downloadItem);
434 }
435 
WebDownloadItem_LastModified(const NWebDownloadItem * downloadItem)436 extern "C" char* WebDownloadItem_LastModified(const NWebDownloadItem* downloadItem)
437 {
438     if (!g_nwebCApi->impl_WebDownloadItem_LastModified) {
439         WVLOG_E("WebDownloadItem_LastModified not found.");
440         return nullptr;
441     }
442     return g_nwebCApi->impl_WebDownloadItem_LastModified(downloadItem);
443 }
444 
WebDownloadItem_NWebId(const NWebDownloadItem * downloadItem)445 extern "C" int WebDownloadItem_NWebId(const NWebDownloadItem* downloadItem)
446 {
447     if (!g_nwebCApi->impl_WebDownloadItem_NWebId) {
448         WVLOG_E("WebDownloadItem_NWebId not found.");
449         return -1;
450     }
451     return g_nwebCApi->impl_WebDownloadItem_NWebId(downloadItem);
452 }
453 
WebDownloadItem_CreateWebDownloadItem(NWebDownloadItem ** downloadItem)454 extern "C" void WebDownloadItem_CreateWebDownloadItem(NWebDownloadItem** downloadItem)
455 {
456     if (!g_nwebCApi->impl_WebDownloadItem_CreateWebDownloadItem) {
457         WVLOG_E("WebDownloadItem_CreateWebDownloadItem not found.");
458         return;
459     }
460     g_nwebCApi->impl_WebDownloadItem_CreateWebDownloadItem(downloadItem);
461 }
462 
WebDownloadItem_Destroy(NWebDownloadItem * downloadItem)463 extern "C" void WebDownloadItem_Destroy(NWebDownloadItem* downloadItem)
464 {
465     if (!g_nwebCApi->impl_WebDownloadItem_Destroy) {
466         WVLOG_E("WebDownloadItem_Destroy not found.");
467         return;
468     }
469     g_nwebCApi->impl_WebDownloadItem_Destroy(downloadItem);
470 }
471 
DestroyBeforeDownloadCallbackWrapper(WebBeforeDownloadCallbackWrapper * wrapper)472 extern "C" void DestroyBeforeDownloadCallbackWrapper(WebBeforeDownloadCallbackWrapper* wrapper)
473 {
474     if (!g_nwebCApi->impl_DestroyBeforeDownloadCallbackWrapper) {
475         WVLOG_E("DestroyBeforeDownloadCallbackWrapper not found.");
476         return;
477     }
478     g_nwebCApi->impl_DestroyBeforeDownloadCallbackWrapper(wrapper);
479 }
480 
DestroyDownloadItemCallbackWrapper(WebDownloadItemCallbackWrapper * wrapper)481 extern "C" void DestroyDownloadItemCallbackWrapper(WebDownloadItemCallbackWrapper* wrapper)
482 {
483     if (!g_nwebCApi->impl_DestroyDownloadItemCallbackWrapper) {
484         WVLOG_E("DestroyDownloadItemCallbackWrapper not found.");
485         return;
486     }
487     g_nwebCApi->impl_DestroyDownloadItemCallbackWrapper(wrapper);
488 }
489 
WebDownloadItem_SetGuid(NWebDownloadItem * downloadItem,const char * guid)490 extern "C" void WebDownloadItem_SetGuid(NWebDownloadItem* downloadItem, const char* guid)
491 {
492     if (!g_nwebCApi->impl_WebDownloadItem_SetGuid) {
493         WVLOG_E("WebDownloadItem_SetGuid not found.");
494         return;
495     }
496     g_nwebCApi->impl_WebDownloadItem_SetGuid(downloadItem, guid);
497 }
498 
WebDownloadItem_SetUrl(NWebDownloadItem * downloadItem,const char * url)499 extern "C" void WebDownloadItem_SetUrl(NWebDownloadItem* downloadItem, const char* url)
500 {
501     if (!g_nwebCApi->impl_WebDownloadItem_SetUrl) {
502         WVLOG_E("WebDownloadItem_SetUrl not found.");
503         return;
504     }
505     g_nwebCApi->impl_WebDownloadItem_SetUrl(downloadItem, url);
506 }
507 
WebDownloadItem_SetFullPath(NWebDownloadItem * downloadItem,const char * fullPath)508 extern "C" void WebDownloadItem_SetFullPath(NWebDownloadItem* downloadItem, const char* fullPath)
509 {
510     if (!g_nwebCApi->impl_WebDownloadItem_SetFullPath) {
511         WVLOG_E("WebDownloadItem_SetFullPath not found.");
512         return;
513     }
514     g_nwebCApi->impl_WebDownloadItem_SetFullPath(downloadItem, fullPath);
515 }
516 
WebDownloadItem_SetETag(NWebDownloadItem * downloadItem,const char * etag)517 extern "C" void WebDownloadItem_SetETag(NWebDownloadItem* downloadItem, const char* etag)
518 {
519     if (!g_nwebCApi->impl_WebDownloadItem_SetETag) {
520         WVLOG_E("WebDownloadItem_SetETag not found.");
521         return;
522     }
523     g_nwebCApi->impl_WebDownloadItem_SetETag(downloadItem, etag);
524 }
525 
WebDownloadItem_SetLastModified(NWebDownloadItem * downloadItem,const char * lastModified)526 extern "C" void WebDownloadItem_SetLastModified(NWebDownloadItem* downloadItem, const char* lastModified)
527 {
528     if (!g_nwebCApi->impl_WebDownloadItem_SetLastModified) {
529         WVLOG_E("WebDownloadItem_SetLastModified not found.");
530         return;
531     }
532     g_nwebCApi->impl_WebDownloadItem_SetLastModified(downloadItem, lastModified);
533 }
534 
WebDownloadItem_SetMimeType(NWebDownloadItem * downloadItem,const char * mimeType)535 extern "C" void WebDownloadItem_SetMimeType(NWebDownloadItem* downloadItem, const char* mimeType)
536 {
537     if (!g_nwebCApi->impl_WebDownloadItem_SetMimeType) {
538         WVLOG_E("WebDownloadItem_SetMimeType not found.");
539         return;
540     }
541     g_nwebCApi->impl_WebDownloadItem_SetMimeType(downloadItem, mimeType);
542 }
543 
WebDownloadItem_SetReceivedBytes(NWebDownloadItem * downloadItem,int64_t receivedBytes)544 extern "C" void WebDownloadItem_SetReceivedBytes(NWebDownloadItem* downloadItem, int64_t receivedBytes)
545 {
546     if (!g_nwebCApi->impl_WebDownloadItem_SetReceivedBytes) {
547         WVLOG_E("WebDownloadItem_SetReceivedBytes not found.");
548         return;
549     }
550     g_nwebCApi->impl_WebDownloadItem_SetReceivedBytes(downloadItem, receivedBytes);
551 }
552 
WebDownloadItem_SetTotalBytes(NWebDownloadItem * downloadItem,int64_t totalBytes)553 extern "C" void WebDownloadItem_SetTotalBytes(NWebDownloadItem* downloadItem, int64_t totalBytes)
554 {
555     if (!g_nwebCApi->impl_WebDownloadItem_SetTotalBytes) {
556         WVLOG_E("WebDownloadItem_SetTotalBytes not found.");
557         return;
558     }
559     g_nwebCApi->impl_WebDownloadItem_SetTotalBytes(downloadItem, totalBytes);
560 }
561 
WebDownloadItem_SetReceivedSlices(NWebDownloadItem * downloadItem,const char * receivedSlices)562 extern "C" void WebDownloadItem_SetReceivedSlices(NWebDownloadItem* downloadItem, const char* receivedSlices)
563 {
564     if (!g_nwebCApi->impl_WebDownloadItem_SetReceivedSlices) {
565         WVLOG_E("WebDownloadItem_SetReceivedSlices not found.");
566         return;
567     }
568     g_nwebCApi->impl_WebDownloadItem_SetReceivedSlices(downloadItem, receivedSlices);
569 }
570 
571 namespace OHOS::NWeb {
LoadNWebSDK()572 bool NWebHelper::LoadNWebSDK()
573 {
574     if (nwebEngine_ == nullptr) {
575         WVLOG_E("web engine is nullptr");
576         return false;
577     }
578 
579     return ::LoadNWebSDK();
580 }
581 
Instance()582 NWebHelper& NWebHelper::Instance()
583 {
584     static NWebHelper helper;
585     return helper;
586 }
587 
TryPreReadLib(bool isFirstTimeStartUpWeb,const std::string & bundlePath)588 void NWebHelper::TryPreReadLib(bool isFirstTimeStartUpWeb, const std::string& bundlePath)
589 {
590     g_isFirstTimeStartUp = isFirstTimeStartUpWeb;
591     if (isFirstTimeStartUpWeb) {
592         WVLOG_D("first time startup, need to wait until the nweb init stage");
593         return;
594     }
595 
596     ArkWeb::ArkWebNWebWebviewBridgeHelper::PreloadLibFile(true, bundlePath);
597 }
598 
TryPreReadLibForFirstlyAppStartUp(const std::string & bundlePath)599 static void TryPreReadLibForFirstlyAppStartUp(const std::string& bundlePath)
600 {
601     if (g_isFirstTimeStartUp) {
602         std::thread preReadThread(
603             [bundlePath]() { ArkWeb::ArkWebNWebWebviewBridgeHelper::PreloadLibFile(true, bundlePath); });
604 
605         preReadThread.detach();
606     }
607 }
608 
Init(bool from_ark)609 bool NWebHelper::Init(bool from_ark)
610 {
611     return LoadWebEngine(from_ark, false);
612 }
613 
InitAndRun(bool from_ark)614 bool NWebHelper::InitAndRun(bool from_ark)
615 {
616     return LoadWebEngine(from_ark, true);
617 }
618 
GetWebEngine(bool fromArk)619 bool NWebHelper::GetWebEngine(bool fromArk)
620 {
621     if (nwebEngine_) {
622         return true;
623     }
624 
625     if (bundlePath_.empty()) {
626         auto ctx = AbilityRuntime::ApplicationContext::GetApplicationContext();
627         if (!ctx) {
628             WVLOG_E("failed to get application context");
629             return false;
630         }
631 
632         SetBundlePath(ctx->GetBundleCodeDir());
633         if (bundlePath_.empty()) {
634             WVLOG_E("bundle path is empty");
635             return false;
636         }
637     }
638 
639     TryPreReadLibForFirstlyAppStartUp(bundlePath_);
640 
641     if (!ArkWeb::ArkWebNWebWebviewBridgeHelper::GetInstance().Init(fromArk, bundlePath_)) {
642         WVLOG_E("failed to init arkweb nweb bridge helper");
643         return false;
644     }
645 
646     auto appMgrClient = std::make_unique<OHOS::AppExecFwk::AppMgrClient>();
647     if (appMgrClient->ConnectAppMgrService() == OHOS::AppExecFwk::AppMgrResultCode::RESULT_OK) {
648         WVLOG_I("call func NotifyProcessDependedOnWeb and return code is %{public}d",
649             appMgrClient->NotifyProcessDependedOnWeb());
650     }
651 
652     nwebEngine_ = NWebEngine::GetInstance();
653     if (nwebEngine_ == nullptr) {
654         WVLOG_E("failed to get web engine instance");
655         return false;
656     }
657 
658     return true;
659 }
660 
InitWebEngine()661 bool NWebHelper::InitWebEngine()
662 {
663     if (initFlag_) {
664         WVLOG_I("web engine has been initialized");
665         return true;
666     }
667 
668     auto ctx = AbilityRuntime::ApplicationContext::GetApplicationContext();
669     if (!ctx) {
670         WVLOG_E("failed to get application context");
671         return false;
672     }
673 
674     if (ctx->GetBaseDir().empty()) {
675         WVLOG_E("base dir of application context is empty");
676         return false;
677     }
678 
679     auto initArgs = std::make_shared<NWebEngineInitArgsImpl>();
680     NWebAdapterHelper::Instance().ParseConfig(initArgs);
681 
682     initArgs->AddArg(std::string("--user-data-dir=").append(ctx->GetBaseDir()));
683     initArgs->AddArg(std::string("--bundle-installation-dir=").append(bundlePath_));
684     if (!customSchemeCmdLine_.empty()) {
685         initArgs->AddArg(std::string("--ohos-custom-scheme=").append(customSchemeCmdLine_));
686     }
687 
688     std::string systemRegion = OHOS::Global::I18n::LocaleConfig::GetSystemRegion();
689     std::string systemLanguage = OHOS::Global::I18n::LocaleConfig::GetSystemLanguage();
690 
691     size_t dashPos = systemLanguage.find('-');
692     if (dashPos == std::string::npos) {
693         initArgs->AddArg(std::string("--lang=").append(systemLanguage + "-" + systemRegion));
694     } else {
695         initArgs->AddArg(std::string("--lang=").append(systemLanguage.substr(0, dashPos) + "-" + systemRegion));
696     }
697 
698     for (auto backForwardCacheCmdLine : backForwardCacheCmdLine_) {
699         initArgs->AddArg(backForwardCacheCmdLine);
700         WVLOG_I("add command line when init web engine: %{public}s", backForwardCacheCmdLine.c_str());
701     }
702 
703     nwebEngine_->InitializeWebEngine(initArgs);
704     initFlag_ = true;
705 
706     WVLOG_I("succeed to init web engine");
707     return true;
708 }
709 
LoadWebEngine(bool fromArk,bool runFlag)710 bool NWebHelper::LoadWebEngine(bool fromArk, bool runFlag)
711 {
712     if (!GetWebEngine(fromArk)) {
713         return false;
714     }
715 
716     if (runFlag) {
717         return InitWebEngine();
718     }
719 
720     return true;
721 }
722 
LoadFuncSymbol(const char * funcName)723 void* NWebHelper::LoadFuncSymbol(const char* funcName)
724 {
725     return ArkWeb::ArkWebNWebWebviewBridgeHelper::GetInstance().LoadFuncSymbol(funcName);
726 }
727 
ReadConfigIfNeeded()728 void NWebAdapterHelper::ReadConfigIfNeeded()
729 {
730     NWebConfigHelper::Instance().ReadConfigIfNeeded();
731 }
732 
SetBundlePath(const std::string & path)733 void NWebHelper::SetBundlePath(const std::string& path)
734 {
735     bundlePath_ = path;
736 }
737 
CreateNWeb(std::shared_ptr<NWebCreateInfo> create_info)738 std::shared_ptr<NWeb> NWebHelper::CreateNWeb(std::shared_ptr<NWebCreateInfo> create_info)
739 {
740     if (nwebEngine_ == nullptr) {
741         WVLOG_E("web engine is nullptr");
742         return nullptr;
743     }
744 
745     return nwebEngine_->CreateNWeb(create_info);
746 }
747 
GetCookieManager()748 std::shared_ptr<NWebCookieManager> NWebHelper::GetCookieManager()
749 {
750     if (!LoadWebEngine(true, true)) {
751         WVLOG_E("failed to load web engine");
752         return nullptr;
753     }
754 
755     return nwebEngine_->GetCookieManager();
756 }
757 
GetNWeb(int32_t nweb_id)758 std::shared_ptr<NWeb> NWebHelper::GetNWeb(int32_t nweb_id)
759 {
760     if (nwebEngine_ == nullptr) {
761         WVLOG_E("web engine is nullptr");
762         return nullptr;
763     }
764 
765     return nwebEngine_->GetNWeb(nweb_id);
766 }
767 
SetWebTag(int32_t nweb_id,const char * webTag)768 void NWebHelper::SetWebTag(int32_t nweb_id, const char* webTag)
769 {
770     if (!LoadWebEngine(true, false)) {
771         WVLOG_E("failed to load web engine");
772         return;
773     }
774 
775     nwebEngine_->SetWebTag(nweb_id, webTag);
776 }
777 
SetHttpDns(std::shared_ptr<NWebDOHConfig> config)778 void NWebHelper::SetHttpDns(std::shared_ptr<NWebDOHConfig> config)
779 {
780     if (nwebEngine_ == nullptr) {
781         WVLOG_E("web engine is nullptr");
782         return;
783     }
784 
785     auto downloadManager = nwebEngine_->GetDownloadManager();
786     if (!downloadManager) {
787         WVLOG_E("download manager is nullptr");
788         return;
789     }
790 
791     downloadManager->SetHttpDns(config);
792 }
793 
PrepareForPageLoad(std::string url,bool preconnectable,int32_t numSockets)794 void NWebHelper::PrepareForPageLoad(std::string url, bool preconnectable, int32_t numSockets)
795 {
796     if (nwebEngine_ == nullptr) {
797         WVLOG_E("web engine is nullptr");
798         return;
799     }
800 
801     nwebEngine_->PrepareForPageLoad(url, preconnectable, numSockets);
802 }
803 
GetDataBase()804 std::shared_ptr<NWebDataBase> NWebHelper::GetDataBase()
805 {
806     if (nwebEngine_ == nullptr) {
807         WVLOG_E("web engine is nullptr");
808         return nullptr;
809     }
810 
811     return nwebEngine_->GetDataBase();
812 }
813 
GetWebStorage()814 std::shared_ptr<NWebWebStorage> NWebHelper::GetWebStorage()
815 {
816     if (nwebEngine_ == nullptr) {
817         WVLOG_E("web engine is nullptr");
818         return nullptr;
819     }
820 
821     return nwebEngine_->GetWebStorage();
822 }
823 
SetConnectionTimeout(const int32_t & timeout)824 void NWebHelper::SetConnectionTimeout(const int32_t& timeout)
825 {
826     if (nwebEngine_ == nullptr) {
827         WVLOG_E("web engine is nullptr");
828         return;
829     }
830 
831     auto downloadManager = nwebEngine_->GetDownloadManager();
832     if (!downloadManager) {
833         WVLOG_E("download manager is nullptr");
834         return;
835     }
836 
837     downloadManager->SetConnectionTimeout(timeout);
838     WVLOG_I("timeout value in NWebHelper: %{public}d", timeout);
839 }
840 
AddIntelligentTrackingPreventionBypassingList(const std::vector<std::string> & hosts)841 void NWebHelper::AddIntelligentTrackingPreventionBypassingList(const std::vector<std::string>& hosts)
842 {
843     if (nwebEngine_ == nullptr) {
844         WVLOG_E("web engine is nullptr");
845         return;
846     }
847 
848     nwebEngine_->AddIntelligentTrackingPreventionBypassingList(hosts);
849 }
850 
RemoveIntelligentTrackingPreventionBypassingList(const std::vector<std::string> & hosts)851 void NWebHelper::RemoveIntelligentTrackingPreventionBypassingList(const std::vector<std::string>& hosts)
852 {
853     if (nwebEngine_ == nullptr) {
854         WVLOG_E("web engine is nullptr");
855         return;
856     }
857 
858     nwebEngine_->RemoveIntelligentTrackingPreventionBypassingList(hosts);
859 }
860 
ClearIntelligentTrackingPreventionBypassingList()861 void NWebHelper::ClearIntelligentTrackingPreventionBypassingList()
862 {
863     if (nwebEngine_ == nullptr) {
864         WVLOG_E("web engine is nullptr");
865         return;
866     }
867 
868     nwebEngine_->ClearIntelligentTrackingPreventionBypassingList();
869 }
870 
GetDefaultUserAgent()871 std::string NWebHelper::GetDefaultUserAgent()
872 {
873     if (!LoadWebEngine(true, true)) {
874         WVLOG_E("failed to load web engine");
875         return "";
876     }
877 
878     return nwebEngine_->GetDefaultUserAgent();
879 }
880 
PauseAllTimers()881 void NWebHelper::PauseAllTimers()
882 {
883     if (nwebEngine_ == nullptr) {
884         WVLOG_E("web engine is nullptr");
885         return;
886     }
887 
888     nwebEngine_->PauseAllTimers();
889 }
890 
ResumeAllTimers()891 void NWebHelper::ResumeAllTimers()
892 {
893     if (nwebEngine_ == nullptr) {
894         WVLOG_E("web engine is nullptr");
895         return;
896     }
897 
898     nwebEngine_->ResumeAllTimers();
899 }
900 
PrefetchResource(const std::shared_ptr<NWebEnginePrefetchArgs> & pre_args,const std::map<std::string,std::string> & additional_http_headers,const std::string & cache_key,const uint32_t & cache_valid_time)901 void NWebHelper::PrefetchResource(const std::shared_ptr<NWebEnginePrefetchArgs>& pre_args,
902     const std::map<std::string, std::string>& additional_http_headers, const std::string& cache_key,
903     const uint32_t& cache_valid_time)
904 {
905     if (nwebEngine_ == nullptr) {
906         WVLOG_E("web engine is nullptr");
907         return;
908     }
909 
910     nwebEngine_->PrefetchResource(pre_args, additional_http_headers, cache_key, cache_valid_time);
911 }
912 
ClearPrefetchedResource(const std::vector<std::string> & cache_key_list)913 void NWebHelper::ClearPrefetchedResource(const std::vector<std::string>& cache_key_list)
914 {
915     if (nwebEngine_ == nullptr) {
916         WVLOG_E("web engine is nullptr");
917         return;
918     }
919 
920     nwebEngine_->ClearPrefetchedResource(cache_key_list);
921 }
922 
SetRenderProcessMode(RenderProcessMode mode)923 void NWebHelper::SetRenderProcessMode(RenderProcessMode mode)
924 {
925     if (nwebEngine_ == nullptr) {
926         WVLOG_E("web engine is nullptr");
927         return;
928     }
929 
930     nwebEngine_->SetRenderProcessMode(mode);
931 }
932 
GetRenderProcessMode()933 RenderProcessMode NWebHelper::GetRenderProcessMode()
934 {
935     if (nwebEngine_ == nullptr) {
936         WVLOG_E("web engine is nullptr");
937         return RenderProcessMode::SINGLE_MODE;
938     }
939 
940     return nwebEngine_->GetRenderProcessMode();
941 }
942 
SetHostIP(const std::string & hostName,const std::string & address,int32_t aliveTime)943 void NWebHelper::SetHostIP(const std::string& hostName, const std::string& address, int32_t aliveTime)
944 {
945     if (nwebEngine_ == nullptr) {
946         WVLOG_E("web engine is nullptr");
947         return;
948     }
949 
950     nwebEngine_->SetHostIP(hostName, address, aliveTime);
951 }
952 
EnableBackForwardCache(bool enableNativeEmbed,bool enableMediaTakeOver)953 void NWebHelper::EnableBackForwardCache(bool enableNativeEmbed, bool enableMediaTakeOver)
954 {
955     this->backForwardCacheCmdLine_.emplace_back("--enable-bfcache");
956     if (enableNativeEmbed) {
957         this->backForwardCacheCmdLine_.emplace_back("--enable-cache-native-embed");
958     }
959 
960     if (enableMediaTakeOver) {
961         this->backForwardCacheCmdLine_.emplace_back("--enable-cache-media-take-over");
962     }
963 }
964 
ClearHostIP(const std::string & hostName)965 void NWebHelper::ClearHostIP(const std::string& hostName)
966 {
967     if (nwebEngine_ == nullptr) {
968         WVLOG_E("web engine is nullptr");
969         return;
970     }
971 
972     nwebEngine_->ClearHostIP(hostName);
973 }
974 
WarmupServiceWorker(const std::string & url)975 void NWebHelper::WarmupServiceWorker(const std::string& url)
976 {
977     if (nwebEngine_ == nullptr) {
978         WVLOG_E("web engine is nullptr");
979         return;
980     }
981 
982     nwebEngine_->WarmupServiceWorker(url);
983 }
984 
SetWholeWebDrawing()985 void NWebHelper::SetWholeWebDrawing()
986 {
987     WVLOG_I("===== Engine set whole =====");
988     if (nwebEngine_ == nullptr) {
989         WVLOG_E("web engine is nullptr");
990         return;
991     }
992     WVLOG_I("===== go webview engine =====");
993     nwebEngine_->SetWholeWebDrawing();
994 }
995 
GetAdsBlockManager()996 std::shared_ptr<NWebAdsBlockManager> NWebHelper::GetAdsBlockManager()
997 {
998     if (!LoadWebEngine(true, false)) {
999         WVLOG_E("failed to load web engine");
1000         return nullptr;
1001     }
1002 
1003     return nwebEngine_->GetAdsBlockManager();
1004 }
1005 
Instance()1006 NWebAdapterHelper& NWebAdapterHelper::Instance()
1007 {
1008     static NWebAdapterHelper helper;
1009     return helper;
1010 }
1011 
Init(bool from_ark)1012 bool NWebAdapterHelper::Init(bool from_ark)
1013 {
1014     return NWebHelper::Instance().Init(from_ark);
1015 }
1016 
GetCurrentRealTimeNs()1017 static int64_t GetCurrentRealTimeNs()
1018 {
1019     struct timespec ts = { 0, 0 };
1020     if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0) {
1021         return 0;
1022     }
1023     return (ts.tv_sec * NS_TO_S + ts.tv_nsec);
1024 }
1025 
CreateNWeb(sptr<Surface> surface,std::shared_ptr<NWebEngineInitArgsImpl> initArgs,uint32_t width,uint32_t height,bool incognitoMode)1026 std::shared_ptr<NWeb> NWebAdapterHelper::CreateNWeb(sptr<Surface> surface,
1027     std::shared_ptr<NWebEngineInitArgsImpl> initArgs, uint32_t width, uint32_t height, bool incognitoMode)
1028 {
1029     int64_t startTime = GetCurrentRealTimeNs();
1030     if (surface == nullptr) {
1031         WVLOG_E("fail to create nweb, input surface is nullptr");
1032         return nullptr;
1033     }
1034     if (width > NWEB_SURFACE_MAX_WIDTH || height > NWEB_SURFACE_MAX_HEIGHT) {
1035         WVLOG_E("input size %{public}u*%{public}u is invalid.", width, height);
1036         return nullptr;
1037     }
1038     auto createInfo = NWebSurfaceAdapter::Instance().GetCreateInfo(surface, initArgs, width, height, incognitoMode);
1039     NWebConfigHelper::Instance().ParseConfig(initArgs);
1040 
1041     // obtain bundle path
1042     std::shared_ptr<AbilityRuntime::ApplicationContext> ctx =
1043         AbilityRuntime::ApplicationContext::GetApplicationContext();
1044     if (ctx) {
1045         std::string bundleName = ctx->GetBundleName();
1046         initArgs->AddArg(std::string("--bundle-name=").append(bundleName));
1047     }
1048 
1049     auto nweb = NWebHelper::Instance().CreateNWeb(createInfo);
1050     if (nweb == nullptr) {
1051         WVLOG_E("fail to create nweb instance");
1052         return nullptr;
1053     }
1054     int64_t endTime = GetCurrentRealTimeNs();
1055     EventReport::ReportCreateWebInstanceTime(nweb->GetWebId(), endTime - startTime);
1056     return nweb;
1057 }
1058 
CreateNWeb(void * enhanceSurfaceInfo,std::shared_ptr<NWebEngineInitArgsImpl> initArgs,uint32_t width,uint32_t height,bool incognitoMode)1059 std::shared_ptr<NWeb> NWebAdapterHelper::CreateNWeb(void* enhanceSurfaceInfo,
1060     std::shared_ptr<NWebEngineInitArgsImpl> initArgs, uint32_t width, uint32_t height, bool incognitoMode)
1061 {
1062     int64_t startTime = GetCurrentRealTimeNs();
1063     if (enhanceSurfaceInfo == nullptr) {
1064         WVLOG_E("fail to create nweb, input surface is nullptr");
1065         return nullptr;
1066     }
1067     if (width > NWEB_SURFACE_MAX_WIDTH || height > NWEB_SURFACE_MAX_HEIGHT) {
1068         WVLOG_E("input size %{public}u*%{public}u is invalid.", width, height);
1069         return nullptr;
1070     }
1071     auto createInfo =
1072         NWebEnhanceSurfaceAdapter::Instance().GetCreateInfo(enhanceSurfaceInfo, initArgs, width, height, incognitoMode);
1073     auto nweb = NWebHelper::Instance().CreateNWeb(createInfo);
1074     if (nweb == nullptr) {
1075         WVLOG_E("fail to create nweb instance");
1076         return nullptr;
1077     }
1078     int64_t endTime = GetCurrentRealTimeNs();
1079     EventReport::ReportCreateWebInstanceTime(nweb->GetWebId(), endTime - startTime);
1080     return nweb;
1081 }
1082 
ParseConfig(std::shared_ptr<NWebEngineInitArgsImpl> initArgs)1083 void NWebAdapterHelper::ParseConfig(std::shared_ptr<NWebEngineInitArgsImpl> initArgs)
1084 {
1085     NWebConfigHelper::Instance().ParseConfig(initArgs);
1086 }
1087 
GetPerfConfig(const std::string & settingName)1088 std::vector<FrameRateSetting> NWebAdapterHelper::GetPerfConfig(const std::string& settingName)
1089 {
1090     auto perfConfig = NWebConfigHelper::Instance().GetPerfConfig(settingName);
1091     return perfConfig;
1092 }
1093 
ParsePerfConfig(const std::string & configNodeName,const std::string & argsNodeName)1094 std::string NWebAdapterHelper::ParsePerfConfig(const std::string& configNodeName, const std::string& argsNodeName)
1095 {
1096     std::string config = NWebConfigHelper::Instance().ParsePerfConfig(configNodeName, argsNodeName);
1097     return config;
1098 }
1099 
TrimMemoryByPressureLevel(int32_t memoryLevel)1100 void NWebHelper::TrimMemoryByPressureLevel(int32_t memoryLevel)
1101 {
1102     if (nwebEngine_ == nullptr) {
1103         WVLOG_E("nweb engine is nullptr");
1104         return;
1105     }
1106 
1107     nwebEngine_->TrimMemoryByPressureLevel(memoryLevel);
1108 }
1109 } // namespace OHOS::NWeb
1110