• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     std::string arkWebInstallPath = OHOS::system::GetParameter("persist.arkwebcore.install_path", "");
685     if (!arkWebInstallPath.empty()) {
686         initArgs->AddArg(std::string("--arkwebcore-install-path=").append(arkWebInstallPath));
687     }
688     if (!customSchemeCmdLine_.empty()) {
689         initArgs->AddArg(std::string("--ohos-custom-scheme=").append(customSchemeCmdLine_));
690     }
691 
692     std::string systemRegion = OHOS::Global::I18n::LocaleConfig::GetSystemRegion();
693     std::string systemLanguage = OHOS::Global::I18n::LocaleConfig::GetSystemLanguage();
694 
695     size_t dashPos = systemLanguage.find('-');
696     if (dashPos == std::string::npos) {
697         initArgs->AddArg(std::string("--lang=").append(systemLanguage + "-" + systemRegion));
698     } else {
699         initArgs->AddArg(std::string("--lang=").append(systemLanguage.substr(0, dashPos) + "-" + systemRegion));
700     }
701 
702     for (auto backForwardCacheCmdLine : backForwardCacheCmdLine_) {
703         initArgs->AddArg(backForwardCacheCmdLine);
704         WVLOG_I("add command line when init web engine: %{public}s", backForwardCacheCmdLine.c_str());
705     }
706 
707     // Append API version.
708     std::shared_ptr<AppExecFwk::ApplicationInfo> appInfo = ctx->GetApplicationInfo();
709     if (appInfo) {
710       std::string apiVersion = std::to_string(appInfo->apiTargetVersion);
711       initArgs->AddArg(std::string("--user-api-version=").append(apiVersion));
712       WVLOG_D("apiTargetVersion: %{public}s", apiVersion.c_str());
713     }
714 
715     nwebEngine_->InitializeWebEngine(initArgs);
716     initFlag_ = true;
717 
718     WVLOG_I("succeed to init web engine");
719     return true;
720 }
721 
LoadWebEngine(bool fromArk,bool runFlag)722 bool NWebHelper::LoadWebEngine(bool fromArk, bool runFlag)
723 {
724     if (!GetWebEngine(fromArk)) {
725         return false;
726     }
727 
728     if (runFlag) {
729         return InitWebEngine();
730     }
731 
732     return true;
733 }
734 
LoadFuncSymbol(const char * funcName)735 void* NWebHelper::LoadFuncSymbol(const char* funcName)
736 {
737     return ArkWeb::ArkWebNWebWebviewBridgeHelper::GetInstance().LoadFuncSymbol(funcName);
738 }
739 
ReadConfigIfNeeded()740 void NWebAdapterHelper::ReadConfigIfNeeded()
741 {
742     NWebConfigHelper::Instance().ReadConfigIfNeeded();
743 }
744 
SetBundlePath(const std::string & path)745 void NWebHelper::SetBundlePath(const std::string& path)
746 {
747     bundlePath_ = path;
748 }
749 
CreateNWeb(std::shared_ptr<NWebCreateInfo> create_info)750 std::shared_ptr<NWeb> NWebHelper::CreateNWeb(std::shared_ptr<NWebCreateInfo> create_info)
751 {
752     if (nwebEngine_ == nullptr) {
753         WVLOG_E("web engine is nullptr");
754         return nullptr;
755     }
756 
757     return nwebEngine_->CreateNWeb(create_info);
758 }
759 
GetCookieManager()760 std::shared_ptr<NWebCookieManager> NWebHelper::GetCookieManager()
761 {
762     if (!LoadWebEngine(true, true)) {
763         WVLOG_E("failed to load web engine");
764         return nullptr;
765     }
766 
767     return nwebEngine_->GetCookieManager();
768 }
769 
GetNWeb(int32_t nweb_id)770 std::shared_ptr<NWeb> NWebHelper::GetNWeb(int32_t nweb_id)
771 {
772     if (nwebEngine_ == nullptr) {
773         WVLOG_E("web engine is nullptr");
774         return nullptr;
775     }
776 
777     return nwebEngine_->GetNWeb(nweb_id);
778 }
779 
SetWebTag(int32_t nweb_id,const char * webTag)780 void NWebHelper::SetWebTag(int32_t nweb_id, const char* webTag)
781 {
782     if (!LoadWebEngine(true, false)) {
783         WVLOG_E("failed to load web engine");
784         return;
785     }
786 
787     nwebEngine_->SetWebTag(nweb_id, webTag);
788 }
789 
SetHttpDns(std::shared_ptr<NWebDOHConfig> config)790 void NWebHelper::SetHttpDns(std::shared_ptr<NWebDOHConfig> config)
791 {
792     if (nwebEngine_ == nullptr) {
793         WVLOG_E("web engine is nullptr");
794         return;
795     }
796 
797     auto downloadManager = nwebEngine_->GetDownloadManager();
798     if (!downloadManager) {
799         WVLOG_E("download manager is nullptr");
800         return;
801     }
802 
803     downloadManager->SetHttpDns(config);
804 }
805 
SetProxyOverride(const std::vector<std::string> & proxyUrls,const std::vector<std::string> & proxySchemeFilters,const std::vector<std::string> & bypassRules,const bool & reverseBypass,std::shared_ptr<NWebProxyChangedCallback> callback)806 void NWebHelper::SetProxyOverride(const std::vector<std::string>& proxyUrls,
807                                   const std::vector<std::string>& proxySchemeFilters,
808                                   const std::vector<std::string>& bypassRules,
809                                   const bool& reverseBypass,
810                                   std::shared_ptr<NWebProxyChangedCallback> callback)
811 {
812     if (!LoadWebEngine(true, true)) {
813         WVLOG_E("failed to load web engine");
814         return;
815     }
816 
817     nwebEngine_->SetProxyOverride(proxyUrls, proxySchemeFilters, bypassRules, reverseBypass, callback);
818 }
819 
RemoveProxyOverride(std::shared_ptr<NWebProxyChangedCallback> callback)820 void NWebHelper::RemoveProxyOverride(std::shared_ptr<NWebProxyChangedCallback> callback)
821 {
822     if (!LoadWebEngine(true, true)) {
823         WVLOG_E("failed to load web engine");
824         return;
825     }
826 
827     nwebEngine_->RemoveProxyOverride(callback);
828 }
829 
PrepareForPageLoad(std::string url,bool preconnectable,int32_t numSockets)830 void NWebHelper::PrepareForPageLoad(std::string url, bool preconnectable, int32_t numSockets)
831 {
832     if (nwebEngine_ == nullptr) {
833         WVLOG_E("web engine is nullptr");
834         return;
835     }
836 
837     nwebEngine_->PrepareForPageLoad(url, preconnectable, numSockets);
838 }
839 
GetDataBase()840 std::shared_ptr<NWebDataBase> NWebHelper::GetDataBase()
841 {
842     if (nwebEngine_ == nullptr) {
843         WVLOG_E("web engine is nullptr");
844         return nullptr;
845     }
846 
847     return nwebEngine_->GetDataBase();
848 }
849 
GetWebStorage()850 std::shared_ptr<NWebWebStorage> NWebHelper::GetWebStorage()
851 {
852     if (nwebEngine_ == nullptr) {
853         WVLOG_E("web engine is nullptr");
854         return nullptr;
855     }
856 
857     return nwebEngine_->GetWebStorage();
858 }
859 
SetConnectionTimeout(const int32_t & timeout)860 void NWebHelper::SetConnectionTimeout(const int32_t& timeout)
861 {
862     if (nwebEngine_ == nullptr) {
863         WVLOG_E("web engine is nullptr");
864         return;
865     }
866 
867     auto downloadManager = nwebEngine_->GetDownloadManager();
868     if (!downloadManager) {
869         WVLOG_E("download manager is nullptr");
870         return;
871     }
872 
873     downloadManager->SetConnectionTimeout(timeout);
874     WVLOG_I("timeout value in NWebHelper: %{public}d", timeout);
875 }
876 
AddIntelligentTrackingPreventionBypassingList(const std::vector<std::string> & hosts)877 void NWebHelper::AddIntelligentTrackingPreventionBypassingList(const std::vector<std::string>& hosts)
878 {
879     if (nwebEngine_ == nullptr) {
880         WVLOG_E("web engine is nullptr");
881         return;
882     }
883 
884     nwebEngine_->AddIntelligentTrackingPreventionBypassingList(hosts);
885 }
886 
RemoveIntelligentTrackingPreventionBypassingList(const std::vector<std::string> & hosts)887 void NWebHelper::RemoveIntelligentTrackingPreventionBypassingList(const std::vector<std::string>& hosts)
888 {
889     if (nwebEngine_ == nullptr) {
890         WVLOG_E("web engine is nullptr");
891         return;
892     }
893 
894     nwebEngine_->RemoveIntelligentTrackingPreventionBypassingList(hosts);
895 }
896 
ClearIntelligentTrackingPreventionBypassingList()897 void NWebHelper::ClearIntelligentTrackingPreventionBypassingList()
898 {
899     if (nwebEngine_ == nullptr) {
900         WVLOG_E("web engine is nullptr");
901         return;
902     }
903 
904     nwebEngine_->ClearIntelligentTrackingPreventionBypassingList();
905 }
906 
GetDefaultUserAgent()907 std::string NWebHelper::GetDefaultUserAgent()
908 {
909     if (nwebEngine_ == nullptr) {
910         WVLOG_E("web engine is nullptr");
911         return "";
912     }
913 
914     return nwebEngine_->GetDefaultUserAgent();
915 }
916 
PauseAllTimers()917 void NWebHelper::PauseAllTimers()
918 {
919     if (nwebEngine_ == nullptr) {
920         WVLOG_E("web engine is nullptr");
921         return;
922     }
923 
924     nwebEngine_->PauseAllTimers();
925 }
926 
ResumeAllTimers()927 void NWebHelper::ResumeAllTimers()
928 {
929     if (nwebEngine_ == nullptr) {
930         WVLOG_E("web engine is nullptr");
931         return;
932     }
933 
934     nwebEngine_->ResumeAllTimers();
935 }
936 
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)937 void NWebHelper::PrefetchResource(const std::shared_ptr<NWebEnginePrefetchArgs>& pre_args,
938     const std::map<std::string, std::string>& additional_http_headers, const std::string& cache_key,
939     const uint32_t& cache_valid_time)
940 {
941     if (nwebEngine_ == nullptr) {
942         WVLOG_E("web engine is nullptr");
943         return;
944     }
945 
946     nwebEngine_->PrefetchResource(pre_args, additional_http_headers, cache_key, cache_valid_time);
947 }
948 
ClearPrefetchedResource(const std::vector<std::string> & cache_key_list)949 void NWebHelper::ClearPrefetchedResource(const std::vector<std::string>& cache_key_list)
950 {
951     if (nwebEngine_ == nullptr) {
952         WVLOG_E("web engine is nullptr");
953         return;
954     }
955 
956     nwebEngine_->ClearPrefetchedResource(cache_key_list);
957 }
958 
SetRenderProcessMode(RenderProcessMode mode)959 void NWebHelper::SetRenderProcessMode(RenderProcessMode mode)
960 {
961     if (nwebEngine_ == nullptr) {
962         WVLOG_E("web engine is nullptr");
963         return;
964     }
965 
966     nwebEngine_->SetRenderProcessMode(mode);
967 }
968 
GetRenderProcessMode()969 RenderProcessMode NWebHelper::GetRenderProcessMode()
970 {
971     if (nwebEngine_ == nullptr) {
972         WVLOG_E("web engine is nullptr");
973         return RenderProcessMode::SINGLE_MODE;
974     }
975 
976     return nwebEngine_->GetRenderProcessMode();
977 }
978 
SetHostIP(const std::string & hostName,const std::string & address,int32_t aliveTime)979 void NWebHelper::SetHostIP(const std::string& hostName, const std::string& address, int32_t aliveTime)
980 {
981     if (nwebEngine_ == nullptr) {
982         WVLOG_E("web engine is nullptr");
983         return;
984     }
985 
986     nwebEngine_->SetHostIP(hostName, address, aliveTime);
987 }
988 
EnableBackForwardCache(bool enableNativeEmbed,bool enableMediaTakeOver)989 void NWebHelper::EnableBackForwardCache(bool enableNativeEmbed, bool enableMediaTakeOver)
990 {
991     this->backForwardCacheCmdLine_.emplace_back("--enable-bfcache");
992     if (enableNativeEmbed) {
993         this->backForwardCacheCmdLine_.emplace_back("--enable-cache-native-embed");
994     }
995 
996     if (enableMediaTakeOver) {
997         this->backForwardCacheCmdLine_.emplace_back("--enable-cache-media-take-over");
998     }
999 }
1000 
ClearHostIP(const std::string & hostName)1001 void NWebHelper::ClearHostIP(const std::string& hostName)
1002 {
1003     if (nwebEngine_ == nullptr) {
1004         WVLOG_E("web engine is nullptr");
1005         return;
1006     }
1007 
1008     nwebEngine_->ClearHostIP(hostName);
1009 }
1010 
WarmupServiceWorker(const std::string & url)1011 void NWebHelper::WarmupServiceWorker(const std::string& url)
1012 {
1013     if (nwebEngine_ == nullptr) {
1014         WVLOG_E("web engine is nullptr");
1015         return;
1016     }
1017 
1018     nwebEngine_->WarmupServiceWorker(url);
1019 }
1020 
SetWholeWebDrawing()1021 void NWebHelper::SetWholeWebDrawing()
1022 {
1023     WVLOG_I("===== Engine set whole =====");
1024     if (nwebEngine_ == nullptr) {
1025         WVLOG_E("web engine is nullptr");
1026         return;
1027     }
1028     WVLOG_I("===== go webview engine =====");
1029     nwebEngine_->SetWholeWebDrawing();
1030 }
1031 
GetAdsBlockManager()1032 std::shared_ptr<NWebAdsBlockManager> NWebHelper::GetAdsBlockManager()
1033 {
1034     if (!LoadWebEngine(true, false)) {
1035         WVLOG_E("failed to load web engine");
1036         return nullptr;
1037     }
1038 
1039     return nwebEngine_->GetAdsBlockManager();
1040 }
1041 
Instance()1042 NWebAdapterHelper& NWebAdapterHelper::Instance()
1043 {
1044     static NWebAdapterHelper helper;
1045     return helper;
1046 }
1047 
Init(bool from_ark)1048 bool NWebAdapterHelper::Init(bool from_ark)
1049 {
1050     return NWebHelper::Instance().Init(from_ark);
1051 }
1052 
GetCurrentRealTimeNs()1053 static int64_t GetCurrentRealTimeNs()
1054 {
1055     struct timespec ts = { 0, 0 };
1056     if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0) {
1057         return 0;
1058     }
1059     return (ts.tv_sec * NS_TO_S + ts.tv_nsec);
1060 }
1061 
CreateNWeb(sptr<Surface> surface,std::shared_ptr<NWebEngineInitArgsImpl> initArgs,uint32_t width,uint32_t height,bool incognitoMode)1062 std::shared_ptr<NWeb> NWebAdapterHelper::CreateNWeb(sptr<Surface> surface,
1063     std::shared_ptr<NWebEngineInitArgsImpl> initArgs, uint32_t width, uint32_t height, bool incognitoMode)
1064 {
1065     int64_t startTime = GetCurrentRealTimeNs();
1066     if (surface == nullptr) {
1067         WVLOG_E("fail to create nweb, input surface is nullptr");
1068         return nullptr;
1069     }
1070     if (width > NWEB_SURFACE_MAX_WIDTH || height > NWEB_SURFACE_MAX_HEIGHT) {
1071         WVLOG_E("input size %{public}u*%{public}u is invalid.", width, height);
1072         return nullptr;
1073     }
1074     auto createInfo = NWebSurfaceAdapter::Instance().GetCreateInfo(surface, initArgs, width, height, incognitoMode);
1075     NWebConfigHelper::Instance().ParseConfig(initArgs);
1076 
1077     // obtain bundle path
1078     std::shared_ptr<AbilityRuntime::ApplicationContext> ctx =
1079         AbilityRuntime::ApplicationContext::GetApplicationContext();
1080     if (ctx) {
1081         std::string bundleName = ctx->GetBundleName();
1082         initArgs->AddArg(std::string("--bundle-name=").append(bundleName));
1083     }
1084 
1085     auto nweb = NWebHelper::Instance().CreateNWeb(createInfo);
1086     if (nweb == nullptr) {
1087         WVLOG_E("fail to create nweb instance");
1088         return nullptr;
1089     }
1090     int64_t endTime = GetCurrentRealTimeNs();
1091     EventReport::ReportCreateWebInstanceTime(nweb->GetWebId(), endTime - startTime);
1092     return nweb;
1093 }
1094 
CreateNWeb(void * enhanceSurfaceInfo,std::shared_ptr<NWebEngineInitArgsImpl> initArgs,uint32_t width,uint32_t height,bool incognitoMode)1095 std::shared_ptr<NWeb> NWebAdapterHelper::CreateNWeb(void* enhanceSurfaceInfo,
1096     std::shared_ptr<NWebEngineInitArgsImpl> initArgs, uint32_t width, uint32_t height, bool incognitoMode)
1097 {
1098     int64_t startTime = GetCurrentRealTimeNs();
1099     if (enhanceSurfaceInfo == nullptr) {
1100         WVLOG_E("fail to create nweb, input surface is nullptr");
1101         return nullptr;
1102     }
1103     if (width > NWEB_SURFACE_MAX_WIDTH || height > NWEB_SURFACE_MAX_HEIGHT) {
1104         WVLOG_E("input size %{public}u*%{public}u is invalid.", width, height);
1105         return nullptr;
1106     }
1107     auto createInfo =
1108         NWebEnhanceSurfaceAdapter::Instance().GetCreateInfo(enhanceSurfaceInfo, initArgs, width, height, incognitoMode);
1109     auto nweb = NWebHelper::Instance().CreateNWeb(createInfo);
1110     if (nweb == nullptr) {
1111         WVLOG_E("fail to create nweb instance");
1112         return nullptr;
1113     }
1114     int64_t endTime = GetCurrentRealTimeNs();
1115     EventReport::ReportCreateWebInstanceTime(nweb->GetWebId(), endTime - startTime);
1116     return nweb;
1117 }
1118 
ParseConfig(std::shared_ptr<NWebEngineInitArgsImpl> initArgs)1119 void NWebAdapterHelper::ParseConfig(std::shared_ptr<NWebEngineInitArgsImpl> initArgs)
1120 {
1121     NWebConfigHelper::Instance().ParseConfig(initArgs);
1122 }
1123 
GetPerfConfig(const std::string & settingName)1124 std::vector<FrameRateSetting> NWebAdapterHelper::GetPerfConfig(const std::string& settingName)
1125 {
1126     auto perfConfig = NWebConfigHelper::Instance().GetPerfConfig(settingName);
1127     return perfConfig;
1128 }
1129 
ParsePerfConfig(const std::string & configNodeName,const std::string & argsNodeName)1130 std::string NWebAdapterHelper::ParsePerfConfig(const std::string& configNodeName, const std::string& argsNodeName)
1131 {
1132     std::string config = NWebConfigHelper::Instance().ParsePerfConfig(configNodeName, argsNodeName);
1133     return config;
1134 }
1135 
TrimMemoryByPressureLevel(int32_t memoryLevel)1136 void NWebHelper::TrimMemoryByPressureLevel(int32_t memoryLevel)
1137 {
1138     if (nwebEngine_ == nullptr) {
1139         WVLOG_E("nweb engine is nullptr");
1140         return;
1141     }
1142 
1143     nwebEngine_->TrimMemoryByPressureLevel(memoryLevel);
1144 }
1145 } // namespace OHOS::NWeb
1146