1 /*
2 * Copyright (c) 2023 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 "native_interface_arkweb.h"
17
18 #include <memory>
19 #include <mutex>
20 #include <unordered_map>
21 #include <vector>
22
23 #include "parameters.h"
24
25 #include "arkweb_error_code.h"
26 #include "arkweb_type.h"
27 #include "arkweb_utils.h"
28 #include "event_handler.h"
29 #include "native_arkweb_utils.h"
30 #include "native_javascript_execute_callback.h"
31 #include "nweb.h"
32 #include "nweb_helper.h"
33 #include "nweb_log.h"
34
35 #include "arkweb_utils.h"
36
37 namespace {
38 std::mutex g_mtxMap; // the mutex to protect the shared resource
39 std::unordered_map<std::string, NativeArkWeb_OnValidCallback> g_validMap;
40 std::unordered_map<std::string, NativeArkWeb_OnDestroyCallback> g_destroyMap;
41 constexpr uint32_t MAX_DATABASE_SIZE_IN_MB = 100;
42 constexpr uint32_t MAX_KEYS_COUNT = 100;
43 constexpr size_t MAX_KEY_LENGTH = 2048;
44 std::mutex g_mtxMainHandler;
45 std::shared_ptr<OHOS::AppExecFwk::EventHandler> g_mainHandler = nullptr;
46 } // namespace
47
48 namespace OHOS::NWeb {
49
50 class NWebJsProxyCallbackImpl : public NWebJsProxyCallback {
51 public:
NWebJsProxyCallbackImpl(const char * methodName,NativeArkWeb_OnJavaScriptProxyCallback methodCallback)52 NWebJsProxyCallbackImpl(const char *methodName, NativeArkWeb_OnJavaScriptProxyCallback methodCallback)
53 : methodName_(methodName), methodCallback_(methodCallback) {
54 }
55 ~NWebJsProxyCallbackImpl() = default;
56
GetMethodName()57 std::string GetMethodName() override
58 {
59 return methodName_;
60 }
61
GetMethodCallback()62 NativeArkWeb_OnJavaScriptProxyCallback GetMethodCallback() override
63 {
64 return methodCallback_;
65 }
66
67 private:
68 std::string methodName_;
69 NativeArkWeb_OnJavaScriptProxyCallback methodCallback_ = nullptr;
70 };
71
72 class NWebSaveCookieCallbackImpl : public NWebBoolValueCallback {
73 public:
NWebSaveCookieCallbackImpl(std::function<void (ArkWeb_ErrorCode errorCode)> callback)74 explicit NWebSaveCookieCallbackImpl(
75 std::function<void(ArkWeb_ErrorCode errorCode)> callback) : callback_(callback) {}
76 ~NWebSaveCookieCallbackImpl() = default;
77
OnReceiveValue(bool result)78 void OnReceiveValue(bool result) override
79 {
80 WVLOG_D("save cookie received result, result = %{public}d", result);
81 if (callback_) {
82 ArkWeb_ErrorCode errorCode =
83 result ? ArkWeb_ErrorCode::ARKWEB_SUCCESS : ArkWeb_ErrorCode::ARKWEB_COOKIE_SAVE_FAILED;
84 callback_(errorCode);
85 }
86 }
87 private:
88 std::function<void(ArkWeb_ErrorCode errorCode)> callback_;
89 };
90
91 }; // namespace OHOS::NWeb
92
93 using namespace OHOS;
OH_NativeArkWeb_RunJavaScript(const char * webTag,const char * jsCode,NativeArkWeb_OnJavaScriptCallback callback)94 void OH_NativeArkWeb_RunJavaScript(const char* webTag, const char* jsCode, NativeArkWeb_OnJavaScriptCallback callback)
95 {
96 std::weak_ptr<OHOS::NWeb::NWeb> nwebWeak = OH_NativeArkWeb_GetWebInstanceByWebTag(webTag);
97 if (auto nweb = nwebWeak.lock()) {
98 auto callbackImpl = std::make_shared<OHOS::NWeb::NativeJavaScriptExecuteCallback>(callback);
99 WVLOG_I("native RunJavaScript webTag: %{public}s", webTag);
100 nweb->ExecuteJavaScript(jsCode, callbackImpl, false);
101 } else {
102 WVLOG_E("native RunJavaScript get nweb null: %{public}s", webTag);
103 }
104 }
105
OH_NativeArkWeb_RegisterJavaScriptProxy(const char * webTag,const char * objName,const char ** methodList,NativeArkWeb_OnJavaScriptProxyCallback * callback,int32_t size,bool isNeedRefresh)106 void OH_NativeArkWeb_RegisterJavaScriptProxy(const char* webTag, const char* objName, const char** methodList,
107 NativeArkWeb_OnJavaScriptProxyCallback* callback, int32_t size, bool isNeedRefresh)
108 {
109 WVLOG_I("native OH_NativeArkWeb_RegisterJavaScriptProxy webTag:%{public}s", webTag);
110 std::vector<std::shared_ptr<OHOS::NWeb::NWebJsProxyCallback>> proxyCallbacks;
111 for (int i = 0; i < size; i++) {
112 std::shared_ptr<OHOS::NWeb::NWebJsProxyCallback> proxyCallback =
113 std::make_shared<OHOS::NWeb::NWebJsProxyCallbackImpl>(methodList[i], callback[i]);
114 proxyCallbacks.push_back(proxyCallback);
115 }
116
117 std::weak_ptr<OHOS::NWeb::NWeb> nwebWeak = OH_NativeArkWeb_GetWebInstanceByWebTag(webTag);
118 if (auto nweb = nwebWeak.lock()) {
119 nweb->RegisterNativeArkJSFunction(objName, proxyCallbacks);
120 if (isNeedRefresh) {
121 nweb->Reload();
122 }
123 } else {
124 WVLOG_E("native RegisterJavaScriptProxy get nweb null: %{public}s", webTag);
125 }
126 }
127
OH_NativeArkWeb_UnregisterJavaScriptProxy(const char * webTag,const char * objName)128 void OH_NativeArkWeb_UnregisterJavaScriptProxy(const char* webTag, const char* objName)
129 {
130 WVLOG_I("native OH_NativeArkWeb_RegisterJavaScriptProxy: %{public}s", webTag);
131 std::weak_ptr<OHOS::NWeb::NWeb> nwebWeak = OH_NativeArkWeb_GetWebInstanceByWebTag(webTag);
132 if (auto nweb = nwebWeak.lock()) {
133 nweb->UnRegisterNativeArkJSFunction(objName);
134 } else {
135 WVLOG_E("native RegisterJavaScriptProxy get nweb null: %{public}s", webTag);
136 }
137 }
138
OH_NativeArkWeb_SetDestroyCallback(const char * webTag,NativeArkWeb_OnDestroyCallback callback)139 void OH_NativeArkWeb_SetDestroyCallback(const char* webTag, NativeArkWeb_OnDestroyCallback callback)
140 {
141 WVLOG_I("native RegisterDestroyCallback, webTag: %{public}s", webTag);
142 std::lock_guard<std::mutex> guard(g_mtxMap);
143 g_destroyMap[webTag] = callback;
144 std::weak_ptr<OHOS::NWeb::NWeb> nwebWeak = OH_NativeArkWeb_GetWebInstanceByWebTag(webTag);
145 if (auto nweb = nwebWeak.lock()) {
146 WVLOG_I("native RegisterNativeDestroyCallback call nweb");
147 nweb->RegisterNativeDestroyCallback(webTag, callback);
148 } else {
149 WVLOG_E("native RegisterDestroyCallback get nweb null: %{public}s", webTag);
150 }
151 }
152
OH_NativeArkWeb_GetDestroyCallback(const char * webTag)153 NativeArkWeb_OnDestroyCallback OH_NativeArkWeb_GetDestroyCallback(const char* webTag)
154 {
155 WVLOG_I("native OH_Web_GetDestroyCallback, webTag: %{public}s", webTag);
156 std::lock_guard<std::mutex> guard(g_mtxMap);
157 std::unordered_map<std::string, NativeArkWeb_OnDestroyCallback>::iterator iter;
158 if ((iter = g_destroyMap.find(webTag)) != g_destroyMap.end()) {
159 return iter->second;
160 }
161 return nullptr;
162 }
163
OH_NativeArkWeb_SetJavaScriptProxyValidCallback(const char * webTag,NativeArkWeb_OnValidCallback callback)164 void OH_NativeArkWeb_SetJavaScriptProxyValidCallback(const char* webTag, NativeArkWeb_OnValidCallback callback)
165 {
166 WVLOG_I("native RegisterValidCallback, webTag: %{public}s", webTag);
167 std::lock_guard<std::mutex> guard(g_mtxMap);
168 g_validMap[webTag] = callback;
169 std::weak_ptr<OHOS::NWeb::NWeb> nwebWeak = OH_NativeArkWeb_GetWebInstanceByWebTag(webTag);
170 if (auto nweb = nwebWeak.lock()) {
171 WVLOG_I("native OH_NativeArkWeb_SetJavaScriptProxyValidCallback call nweb");
172 nweb->RegisterNativeValideCallback(webTag, callback);
173 } else {
174 WVLOG_E("native RegisterDestroyCallback get nweb null: %{public}s", webTag);
175 }
176 }
177
OH_NativeArkWeb_GetJavaScriptProxyValidCallback(const char * webTag)178 NativeArkWeb_OnValidCallback OH_NativeArkWeb_GetJavaScriptProxyValidCallback(const char* webTag)
179 {
180 WVLOG_I("native OH_Web_GetValidCallback, webTag: %{public}s", webTag);
181 std::lock_guard<std::mutex> guard(g_mtxMap);
182 std::unordered_map<std::string, NativeArkWeb_OnValidCallback>::iterator iter;
183 if ((iter = g_validMap.find(webTag)) != g_validMap.end()) {
184 return iter->second;
185 }
186 return nullptr;
187 }
188
189 template<typename Fn>
LoadFunction(const char * functionName,Fn * fnOut)190 static bool LoadFunction(const char* functionName, Fn* fnOut)
191 {
192 void* fn = OHOS::NWeb::NWebHelper::Instance().LoadFuncSymbol(functionName);
193 if (!fn) {
194 WVLOG_E("%{public}s not found.", functionName);
195 return false;
196 }
197 *fnOut = reinterpret_cast<Fn>(fn);
198 return true;
199 }
200
OH_NativeArkWeb_LoadData(const char * webTag,const char * data,const char * mimeType,const char * encoding,const char * baseUrl,const char * historyUrl)201 ArkWeb_ErrorCode OH_NativeArkWeb_LoadData(const char* webTag,
202 const char* data,
203 const char* mimeType,
204 const char* encoding,
205 const char* baseUrl,
206 const char* historyUrl)
207 {
208 WVLOG_I("native OH_NativeArkWeb_LoadData, webTag: %{public}s", webTag);
209 if (!OHOS::NWeb::NWebHelper::Instance().LoadWebEngine(true, false)) {
210 WVLOG_E("NativeArkWeb webEngineHandle is nullptr");
211 return ArkWeb_ErrorCode::ARKWEB_LIBRARY_OPEN_FAILURE;
212 }
213
214 ArkWeb_ErrorCode (*loadData)(const char* webTag,
215 const char* data,
216 const char* mimeType,
217 const char* encoding,
218 const char* baseUrl,
219 const char* historyUrl) = nullptr;
220
221 #define ARKWEB_NATIVE_LOAD_FN_PTR(apiMember, funcImpl) LoadFunction(#funcImpl, &(apiMember))
222 ARKWEB_NATIVE_LOAD_FN_PTR(loadData, OH_NativeArkWeb_LoadData);
223 #undef ARKWEB_NATIVE_LOAD_FN_PTR
224 if (!loadData) {
225 WVLOG_E("OH_NativeArkWeb_LoadData failed to load function loadData");
226 return ArkWeb_ErrorCode::ARKWEB_LIBRARY_SYMBOL_NOT_FOUND;
227 }
228 return loadData(webTag, data, mimeType, encoding, baseUrl, historyUrl);
229 }
230
OH_NativeArkWeb_RegisterAsyncThreadJavaScriptProxy(const char * webTag,const ArkWeb_ProxyObjectWithResult * proxyObject,const char * permission)231 void OH_NativeArkWeb_RegisterAsyncThreadJavaScriptProxy(const char* webTag,
232 const ArkWeb_ProxyObjectWithResult* proxyObject,
233 const char* permission)
234 {
235 WVLOG_I("native OH_NativeArkWeb_RegisterAsyncThreadJavaScriptProxy, webTag: %{public}s", webTag);
236 if (!OHOS::NWeb::NWebHelper::Instance().LoadWebEngine(true, false)) {
237 WVLOG_E("NativeArkWeb webEngineHandle is nullptr");
238 return;
239 }
240
241 void (*registerAsyncThreadJavaScriptProxy)(const char* webTag,
242 const ArkWeb_ProxyObjectWithResult* proxyObject,
243 const char* permission) = nullptr;
244
245 #define ARKWEB_NATIVE_LOAD_FN_PTR(apiMember, funcImpl) LoadFunction(#funcImpl, &(apiMember))
246 ARKWEB_NATIVE_LOAD_FN_PTR(registerAsyncThreadJavaScriptProxy, OH_NativeArkWeb_RegisterAsyncThreadJavaScriptProxy);
247 #undef ARKWEB_NATIVE_LOAD_FN_PTR
248 if (!registerAsyncThreadJavaScriptProxy) {
249 WVLOG_E("failed to load function OH_NativeArkWeb_RegisterAsyncThreadJavaScriptProxy");
250 return;
251 }
252 return registerAsyncThreadJavaScriptProxy(webTag, proxyObject, permission);
253 }
254
OH_NativeArkWeb_GetBlanklessInfoWithKey(const char * webTag,const char * key)255 ArkWeb_BlanklessInfo OH_NativeArkWeb_GetBlanklessInfoWithKey(const char* webTag, const char* key)
256 {
257 if (!OHOS::system::GetBoolParameter("web.blankless.enabled", false) || IS_CALLING_FROM_M114()) {
258 WVLOG_E("blankless OH_NativeArkWeb_GetBlanklessInfoWithKey capability not supported");
259 return { ArkWeb_BlanklessErrorCode::ARKWEB_BLANKLESS_ERR_DEVICE_NOT_SUPPORT, 0.0, 0 };
260 }
261
262 size_t keyLen = strlen(key);
263 if (keyLen == 0 || keyLen > MAX_KEY_LENGTH) {
264 WVLOG_E("blankless OH_NativeArkWeb_GetBlanklessInfoWithKey key length is invalid");
265 return { ArkWeb_BlanklessErrorCode::ARKWEB_BLANKLESS_ERR_INVALID_ARGS, 0.0, 0 };
266 }
267
268 if (!OHOS::NWeb::NWebHelper::Instance().LoadWebEngine(true, false)) {
269 WVLOG_E("blankless OH_NativeArkWeb_GetBlanklessInfoWithKey load web engine failed");
270 return { ArkWeb_BlanklessErrorCode::ARKWEB_BLANKLESS_ERR_UNKNOWN, 0.0, 0 };
271 }
272
273 ArkWeb_BlanklessInfo (*getBlanklessInfoWithKey)(const char* webTag, const char* key) = nullptr;
274
275 #define ARKWEB_NATIVE_LOAD_FN_PTR(apiMember, funcImpl) LoadFunction(#funcImpl, &(apiMember))
276 ARKWEB_NATIVE_LOAD_FN_PTR(getBlanklessInfoWithKey, OH_NativeArkWeb_GetBlanklessInfoWithKey);
277 #undef ARKWEB_NATIVE_LOAD_FN_PTR
278 if (!getBlanklessInfoWithKey) {
279 WVLOG_E("blankless OH_NativeArkWeb_GetBlanklessInfoWithKey failed to load function");
280 return { ArkWeb_BlanklessErrorCode::ARKWEB_BLANKLESS_ERR_UNKNOWN, 0.0, 0 };
281 }
282
283 return getBlanklessInfoWithKey(webTag, key);
284 }
285
OH_NativeArkWeb_SetBlanklessLoadingWithKey(const char * webTag,const char * key,bool isStarted)286 ArkWeb_BlanklessErrorCode OH_NativeArkWeb_SetBlanklessLoadingWithKey(const char* webTag,
287 const char* key,
288 bool isStarted)
289 {
290 if (!OHOS::system::GetBoolParameter("web.blankless.enabled", false) || IS_CALLING_FROM_M114()) {
291 WVLOG_E("blankless OH_NativeArkWeb_SetBlanklessLoadingWithKey capability not supported");
292 return ArkWeb_BlanklessErrorCode::ARKWEB_BLANKLESS_ERR_DEVICE_NOT_SUPPORT;
293 }
294
295 size_t keyLen = strlen(key);
296 if (keyLen == 0 || keyLen > MAX_KEY_LENGTH) {
297 WVLOG_E("blankless OH_NativeArkWeb_SetBlanklessLoadingWithKey key length is invalid");
298 return ArkWeb_BlanklessErrorCode::ARKWEB_BLANKLESS_ERR_INVALID_ARGS;
299 }
300
301 if (!OHOS::NWeb::NWebHelper::Instance().LoadWebEngine(true, false)) {
302 WVLOG_E("blankless OH_NativeArkWeb_SetBlanklessLoadingWithKey load web engine failed");
303 return ArkWeb_BlanklessErrorCode::ARKWEB_BLANKLESS_ERR_UNKNOWN;
304 }
305
306 ArkWeb_BlanklessErrorCode (*setBlanklessLoadingWithKey)(const char* webTag,
307 const char* key,
308 bool isStarted) = nullptr;
309
310 #define ARKWEB_NATIVE_LOAD_FN_PTR(apiMember, funcImpl) LoadFunction(#funcImpl, &(apiMember))
311 ARKWEB_NATIVE_LOAD_FN_PTR(setBlanklessLoadingWithKey, OH_NativeArkWeb_SetBlanklessLoadingWithKey);
312 #undef ARKWEB_NATIVE_LOAD_FN_PTR
313 if (!setBlanklessLoadingWithKey) {
314 WVLOG_E("blankless OH_NativeArkWeb_SetBlanklessLoadingWithKey failed to load function");
315 return ArkWeb_BlanklessErrorCode::ARKWEB_BLANKLESS_ERR_UNKNOWN;
316 }
317
318 return setBlanklessLoadingWithKey(webTag, key, isStarted);
319 }
320
OH_NativeArkWeb_ClearBlanklessLoadingCache(const char * key[],uint32_t size)321 void OH_NativeArkWeb_ClearBlanklessLoadingCache(const char* key[], uint32_t size)
322 {
323 if (!OHOS::system::GetBoolParameter("web.blankless.enabled", false) || IS_CALLING_FROM_M114()) {
324 WVLOG_E("blankless OH_NativeArkWeb_ClearBlanklessLoadingCache capability not supported");
325 return;
326 }
327
328 std::vector<std::string> keys;
329 if (key == nullptr) {
330 OHOS::NWeb::NWebHelper::Instance().ClearBlanklessLoadingCache(keys);
331 return;
332 }
333
334 if (size > MAX_KEYS_COUNT) {
335 WVLOG_W("blankless OH_NativeArkWeb_ClearBlanklessLoadingCache array size should not exceed 100");
336 size = MAX_KEYS_COUNT;
337 }
338
339 for (uint32_t idx = 0; idx < size; idx++) {
340 if (key[idx] == nullptr) {
341 continue;
342 }
343 size_t keyLen = strlen(key[idx]);
344 if (keyLen == 0 || keyLen > MAX_KEY_LENGTH) {
345 continue;
346 }
347 keys.push_back(key[idx]);
348 }
349
350 if (keys.size() == 0) {
351 WVLOG_W("blankless OH_NativeArkWeb_ClearBlanklessLoadingCache valid keys are 0");
352 return;
353 }
354 OHOS::NWeb::NWebHelper::Instance().ClearBlanklessLoadingCache(keys);
355 }
356
OH_NativeArkWeb_SetBlanklessLoadingCacheCapacity(uint32_t capacity)357 uint32_t OH_NativeArkWeb_SetBlanklessLoadingCacheCapacity(uint32_t capacity)
358 {
359 if (!OHOS::system::GetBoolParameter("web.blankless.enabled", false) || IS_CALLING_FROM_M114()) {
360 WVLOG_E("blankless OH_NativeArkWeb_SetBlanklessLoadingCacheCapacity capability not supported");
361 return 0;
362 }
363
364 if (capacity > MAX_DATABASE_SIZE_IN_MB) {
365 capacity = MAX_DATABASE_SIZE_IN_MB;
366 }
367
368 OHOS::NWeb::NWebHelper::Instance().SetBlanklessLoadingCacheCapacity(static_cast<int32_t>(capacity));
369 return capacity;
370 }
371
GetMainThreadEventHandler()372 static std::shared_ptr<OHOS::AppExecFwk::EventHandler> GetMainThreadEventHandler()
373 {
374 std::lock_guard<std::mutex> guard(g_mtxMainHandler);
375 if (!g_mainHandler) {
376 std::shared_ptr<OHOS::AppExecFwk::EventRunner> runner = OHOS::AppExecFwk::EventRunner::GetMainEventRunner();
377 if (!runner) {
378 return nullptr;
379 }
380 g_mainHandler = std::make_shared<OHOS::AppExecFwk::EventHandler>(runner);
381 }
382 return g_mainHandler;
383 }
384
PostSaveCookieToUIThread(OH_ArkWeb_OnCookieSaveCallback callback)385 static void PostSaveCookieToUIThread(OH_ArkWeb_OnCookieSaveCallback callback)
386 {
387 auto mainHandler = GetMainThreadEventHandler();
388 if (!mainHandler) {
389 WVLOG_E("get main event runner failed");
390 if (callback) {
391 callback(ArkWeb_ErrorCode::ARKWEB_COOKIE_SAVE_FAILED);
392 }
393 return;
394 }
395
396 bool succ = mainHandler->PostTask([callback]() {
397 std::shared_ptr<OHOS::NWeb::NWebCookieManager> cookieManager =
398 OHOS::NWeb::NWebHelper::Instance().GetCookieManager();
399 if (cookieManager == nullptr) {
400 WVLOG_E("cookieManager is nullptr");
401 if (callback) {
402 callback(ArkWeb_ErrorCode::ARKWEB_COOKIE_MANAGER_INITIALIZE_FAILED);
403 }
404 return;
405 }
406 auto callbackImpl = std::make_shared<OHOS::NWeb::NWebSaveCookieCallbackImpl>(callback);
407 cookieManager->Store(callbackImpl);
408 }, "", 0, OHOS::AppExecFwk::EventQueue::Priority::HIGH, {});
409 if (!succ) {
410 WVLOG_E("post cookie task to UI thread failed");
411 if (callback) {
412 callback(ArkWeb_ErrorCode::ARKWEB_COOKIE_SAVE_FAILED);
413 }
414 }
415 }
416
OH_ArkWebCookieManager_SaveCookieSync()417 ArkWeb_ErrorCode OH_ArkWebCookieManager_SaveCookieSync()
418 {
419 if (getpid() != gettid() && !OHOS::NWeb::NWebHelper::Instance().HasLoadWebEngine()) {
420 WVLOG_E("cookieManager not initialize");
421 return ArkWeb_ErrorCode::ARKWEB_COOKIE_MANAGER_NOT_INITIALIZED;
422 }
423
424 std::shared_ptr<OHOS::NWeb::NWebCookieManager> cookieManager =
425 OHOS::NWeb::NWebHelper::Instance().GetCookieManager();
426 if (cookieManager == nullptr) {
427 WVLOG_E("cookieManager is nullptr");
428 return ArkWeb_ErrorCode::ARKWEB_COOKIE_MANAGER_INITIALIZE_FAILED;
429 }
430
431 if (!cookieManager->Store()) {
432 return ArkWeb_ErrorCode::ARKWEB_COOKIE_SAVE_FAILED;
433 }
434 return ArkWeb_ErrorCode::ARKWEB_SUCCESS;
435 }
436
OH_ArkWebCookieManager_SaveCookieAsync(OH_ArkWeb_OnCookieSaveCallback callback)437 void OH_ArkWebCookieManager_SaveCookieAsync(OH_ArkWeb_OnCookieSaveCallback callback)
438 {
439 if (getpid() != gettid() && !OHOS::NWeb::NWebHelper::Instance().HasLoadWebEngine()) {
440 WVLOG_D("post save cookie to UI thread");
441 PostSaveCookieToUIThread(callback);
442 return;
443 }
444
445 std::shared_ptr<OHOS::NWeb::NWebCookieManager> cookieManager =
446 OHOS::NWeb::NWebHelper::Instance().GetCookieManager();
447 if (cookieManager == nullptr) {
448 WVLOG_E("cookieManager is nullptr");
449 if (callback) {
450 callback(ArkWeb_ErrorCode::ARKWEB_COOKIE_MANAGER_INITIALIZE_FAILED);
451 }
452 return;
453 }
454
455 auto callbackImpl = std::make_shared<OHOS::NWeb::NWebSaveCookieCallbackImpl>(callback);
456 cookieManager->Store(callbackImpl);
457 }
458
OH_NativeArkWeb_SetActiveWebEngineVersion(ArkWebEngineVersion webEngineVersion)459 void OH_NativeArkWeb_SetActiveWebEngineVersion(ArkWebEngineVersion webEngineVersion) {
460 OHOS::ArkWeb::setActiveWebEngineVersion(
461 static_cast<OHOS::ArkWeb::ArkWebEngineVersion>(static_cast<int>(webEngineVersion)));
462 }
463
OH_NativeArkWeb_GetActiveWebEngineVersion()464 ArkWebEngineVersion OH_NativeArkWeb_GetActiveWebEngineVersion() {
465 return static_cast<ArkWebEngineVersion>(static_cast<int>(OHOS::ArkWeb::getActiveWebEngineVersion()));
466 }
467
OH_NativeArkWeb_IsActiveWebEngineEvergreen()468 bool OH_NativeArkWeb_IsActiveWebEngineEvergreen() {
469 return OHOS::ArkWeb::IsActiveWebEngineEvergreen();
470 }