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