1 /* 2 * Copyright (c) 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 #ifndef ARK_WEB_COOKIE_MANAGER_WRAPPER_H_ 17 #define ARK_WEB_COOKIE_MANAGER_WRAPPER_H_ 18 #pragma once 19 20 #include "include/nweb_cookie_manager.h" 21 #include "ohos_nweb/include/ark_web_cookie_manager.h" 22 23 namespace OHOS::ArkWeb { 24 25 class ArkWebCookieManagerWrapper : public OHOS::NWeb::NWebCookieManager { 26 public: 27 ArkWebCookieManagerWrapper(ArkWebRefPtr<ArkWebCookieManager> ark_web_cookie_manager); 28 ~ArkWebCookieManagerWrapper() = default; 29 30 /** 31 * @brief Ensures all cookies currently accessible through the ReturnCookie 32 * API are written to persistent storage. 33 * 34 * @param true if store cookie success else return false. 35 */ 36 bool Store() override; 37 38 /** 39 * @brief Ensures all cookies currently accessible through the ReturnCookie 40 * API are written to persistent storage. 41 * 42 * @param callback a callback to be executed when cookies has Stored. 43 */ 44 void Store(std::shared_ptr<OHOS::NWeb::NWebBoolValueCallback> callback) override; 45 46 /** 47 * @brief Sets a single cookie (key-value pair) for the given URL sync. 48 * 49 * @param url the URL for which the cookie is to be set. 50 * @param value the cookie as a string, using the format of the 'Set-Cookie' 51 * HTTP response header. 52 * @param incognito_mode true if web is in the incognito mode, flase 53 * otherwise. 54 * 55 * @return 0 if set cookie success else return error id. 56 */ 57 int SetCookie(const std::string& url, const std::string& value, bool incognito_mode) override; 58 59 /** 60 * @brief Sets a single cookie (key-value pair) for the given URL. 61 * 62 * @param url the URL for which the cookie is to be set. 63 * @param value the cookie as a string, using the format of the 'Set-Cookie' 64 * HTTP response header. 65 * @param callback a callback to be executed when the cookie has been set. 66 */ 67 void SetCookie(const std::string& url, const std::string& value, 68 std::shared_ptr<OHOS::NWeb::NWebBoolValueCallback> callback) override; 69 70 /** 71 * @brief Gets whether there are stored cookies. 72 * 73 * @param incognito_mode true if web is in the incognito mode, flase 74 * otherwise. 75 * 76 * @return true if there are stored cookies else false. 77 */ 78 bool ExistCookies(bool incognito_mode) override; 79 80 /** 81 * @brief Gets whether there are stored cookies. 82 * 83 * @param callback a callback to be executed when the cookie has checked. 84 */ 85 void ExistCookies(std::shared_ptr<OHOS::NWeb::NWebBoolValueCallback> callback) override; 86 87 /** 88 * @brief Gets all the cookies for the given URL. This is sync method 89 * 90 * @param url the URL for which the cookies are requested. 91 * @param incognito_mode true if web is in the incognito mode, flase 92 * otherwise. 93 * 94 * @return the cookie value for given URL. 95 */ 96 std::string ReturnCookie(const std::string& url, bool& is_valid, bool incognito_mode) override; 97 98 /** 99 * @brief Gets all the cookies for the given URL. 100 * 101 * @param url the URL for which the cookies are requested. 102 * @param callback a callback which is executed when the cookies have been 103 * gotten. 104 */ 105 void ReturnCookie(const std::string& url, std::shared_ptr<OHOS::NWeb::NWebStringValueCallback> callback) override; 106 107 /** 108 * @brief Configs a single cookie (key-value pair) for the given URL. 109 * 110 * @param url the URL for which the cookie is to be set. 111 * @param value the cookie as a string, using the format of the 'Set-Cookie' 112 * HTTP response header. 113 * @param callback a callback to be executed when the cookie has been set. 114 */ 115 void ConfigCookie(const std::string& url, const std::string& value, 116 std::shared_ptr<OHOS::NWeb::NWebLongValueCallback> callback) override; 117 118 /** 119 * @brief Removes all session cookies, which are cookies without an expiration 120 * date. 121 * 122 * @param callback a callback to be executed when all session cookies has 123 * removed. 124 */ 125 void DeleteSessionCookies(std::shared_ptr<OHOS::NWeb::NWebBoolValueCallback> callback) override; 126 127 /** 128 * @brief Removes all cookies. 129 * 130 * @param callback a callback to be executed when all cookies has removed. 131 * @param incognito_mode true if web is in the incognito mode, flase 132 * otherwise. 133 */ 134 void DeleteCookieEntirely( 135 std::shared_ptr<OHOS::NWeb::NWebBoolValueCallback> callback, bool incognito_mode) override; 136 137 /** 138 * @brief Get whether the instance can send and accept cookies. 139 * 140 * @return true if the instance send and accept cookies. 141 */ 142 bool IsAcceptCookieAllowed() override; 143 144 /** 145 * @brief Sets whether the instance should send and accept cookies. By default 146 * this is set to be true and the nweb accepts cookies. 147 * 148 * @param accept whether the instance should send and accept cookies. 149 */ 150 void PutAcceptCookieEnabled(bool accept) override; 151 152 /** 153 * @brief Get whether the instance allows setting cookies of third parties 154 * 155 * @return true if the instance allows the setting of third-party cookies. 156 */ 157 bool IsThirdPartyCookieAllowed() override; 158 159 /** 160 * @brief Get whether instances can send and accept cookies for file scheme 161 * URLs. 162 * 163 * @return true if instances send and accept cookies for file scheme URLs. 164 */ 165 bool IsFileURLSchemeCookiesAllowed() override; 166 167 /** 168 * @brief Set whether the instance allows setting cookies of third parties. By 169 * default, this value is set to be false. Nweb does not allow the 170 * setting of third-party cookies. 171 * 172 * @param accept whether the instance allows the setting of third-party 173 * cookies. 174 */ 175 void PutAcceptThirdPartyCookieEnabled(bool accept) override; 176 177 /** 178 * @brief Sets whether the instance should send and accept cookies for file 179 * scheme URLs. 180 * 181 * @param allow whether the instance should send and accept cookies for file 182 * scheme URLs. 183 */ 184 void PutAcceptFileURLSchemeCookiesEnabled(bool allow) override; 185 186 /** 187 * @brief Gets all the cookies for the given URL. This is sync method 188 * 189 * @param url URL to which the cookie to be obtained belongs. A complete URL is recommended. 190 * @param incognito True indicates that the memory cookies of the webview in privacy mode are obtained, 191 * and false indicates that cookies in non-privacy mode are obtained. 192 * @param includeHttpOnly If true HTTP-only cookies will also be included in the cookieValue. 193 * @param cookieValue Get the cookie value corresponding to the URL. 194 * @return the cookie value for given URL. 195 */ 196 std::string ReturnCookieWithHttpOnly( 197 const std::string& url, bool& is_valid, bool incognito_mode, bool includeHttpOnly) override; 198 199 /** 200 * @brief Sets a single cookie (key-value pair) for the given URL sync. 201 * 202 * @param url Specifies the URL to which the cookie belongs. A complete URL is recommended. 203 * @param cookieValue The value of the cookie to be set. 204 * @param incognito True indicates that cookies of the corresponding URL are set in privacy mode, 205 * and false indicates that cookies of the corresponding URL are set in non-privacy mode. 206 * @param includeHttpOnly If true, HTTP-only cookies can also be overwritten. 207 * @return 0 if set cookie success else return error id. 208 */ 209 int SetCookieWithHttpOnly( 210 const std::string& url, const std::string& value, bool incognito_mode, bool includeHttpOnly) override; 211 212 void GetCookieAsync(const std::string& url, bool incognitoMode, 213 std::shared_ptr<OHOS::NWeb::NWebStringValueCallback> callback) override; 214 215 int SetCookieSync( 216 const std::string& url, const std::string& value, bool incognitoMode, bool includeHttpOnly) override; 217 218 void SetCookieAsync(const std::string& url, const std::string& value, bool incognitoMode, bool includeHttpOnly, 219 std::shared_ptr<OHOS::NWeb::NWebLongValueCallback> callback) override; 220 221 private: 222 ArkWebRefPtr<ArkWebCookieManager> ark_web_cookie_manager_; 223 }; 224 225 } // namespace OHOS::ArkWeb 226 227 #endif // ARK_WEB_COOKIE_MANAGER_WRAPPER_H_ 228