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