1 /* 2 * Copyright (c) 2022 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 #ifndef NWEB_NAPI_WEB_COOKIE_MANAGER_H 17 #define NWEB_NAPI_WEB_COOKIE_MANAGER_H 18 19 #include <cstddef> 20 #include <iosfwd> 21 #include <string> 22 #include <uv.h> 23 24 #include "napi/native_api.h" 25 #include "napi/native_node_api.h" 26 #include "nweb_value_callback.h" 27 28 namespace OHOS { 29 namespace NWeb { 30 const std::string WEB_COOKIE_MANAGER_CLASS_NAME = "WebCookieManager"; 31 32 class NapiWebCookieManager { 33 public: NapiWebCookieManager()34 NapiWebCookieManager() {} 35 36 ~NapiWebCookieManager() = default; 37 38 struct WebCookieManagerParam { 39 napi_env env_; 40 napi_ref callback_; 41 napi_deferred deferred_; 42 }; 43 44 static napi_value Init(napi_env env, napi_value exports); 45 46 private: 47 static napi_value JsConstructor(napi_env env, napi_callback_info info); 48 49 static bool GetStringPara(napi_env env, napi_value argv, std::string& outValue); 50 51 static bool GetBooleanPara(napi_env env, napi_value argv, bool& outValue); 52 53 static napi_value JsGetCookie(napi_env env, napi_callback_info info); 54 55 static napi_value JsSetCookie(napi_env env, napi_callback_info info); 56 57 static napi_value JsIsCookieAllowed(napi_env env, napi_callback_info info); 58 59 static napi_value JsPutAcceptCookieEnabled(napi_env env, napi_callback_info info); 60 61 static napi_value JsIsThirdPartyCookieAllowed(napi_env env, napi_callback_info info); 62 63 static napi_value JsPutAcceptThirdPartyCookieEnabled(napi_env env, napi_callback_info info); 64 65 static napi_value JsExistCookie(napi_env env, napi_callback_info info); 66 67 static napi_value JsDeleteEntireCookie(napi_env env, napi_callback_info info); 68 69 static napi_value JsDeleteSessionCookie(napi_env env, napi_callback_info info); 70 71 static napi_value JsSaveCookieAsync(napi_env env, napi_callback_info info); 72 }; 73 74 class NWebSaveCookieCallbackImpl : public OHOS::NWeb::NWebValueCallback<bool> { 75 public: NWebSaveCookieCallbackImpl(napi_env env,napi_ref callback,napi_deferred deferred)76 NWebSaveCookieCallbackImpl(napi_env env, napi_ref callback, napi_deferred deferred) 77 : env_(env), callback_(callback), deferred_(deferred) {} 78 ~NWebSaveCookieCallbackImpl() = default; 79 80 void OnReceiveValue(bool result) override; 81 82 static void UvJsCallbackThreadWoker(uv_work_t *work, int status); 83 private: 84 napi_env env_; 85 napi_ref callback_; 86 napi_deferred deferred_; 87 }; 88 } // namespace NWeb 89 } // namespace OHOS 90 91 #endif // NWEB_NAPI_WEB_COOKIE_MANAGER_H