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_COOKIE_MANAGER_H 17 #define NWEB_COOKIE_MANAGER_H 18 19 #include <memory> 20 #include <string> 21 22 #include "nweb_export.h" 23 #include "nweb_value_callback.h" 24 25 namespace OHOS::NWeb { 26 class OHOS_NWEB_EXPORT NWebCookieManager { 27 public: 28 NWebCookieManager() = default; 29 30 virtual ~NWebCookieManager() = default; 31 32 /** 33 * @brief Get whether the instance can send and accept cookies. 34 * 35 * @return true if the instance send and accept cookies. 36 */ 37 virtual bool IsAcceptCookieAllowed() const = 0; 38 39 /** 40 * @brief Set whether the instance should send and accept cookies. 41 * By default this is set to be true and the nweb accepts cookies. 42 * 43 * @param accept whether the instance should send and accept cookies. 44 */ 45 virtual void PutAcceptCookieEnabled(bool accept) = 0; 46 47 /** 48 * @brief Gets whether the instance allows the setting of third-party cookies. 49 * 50 * @return true if the instance allows the setting of third-party cookies. 51 */ 52 virtual bool IsThirdPartyCookieAllowed() const = 0; 53 54 /** 55 * @brief Set whether the instance allows the use of third-party cookies. 56 * By default, this value is set to be false. Nweb does not allow the setting of third-party cookies. 57 * 58 * @param allow whether the instance allows the setting of third-party cookies. 59 */ 60 virtual void PutAcceptThirdPartyCookieEnabled(bool accept) = 0; 61 62 /** 63 * @brief Get whether instances can send and accept cookies for file scheme URLs. 64 * 65 * @return true if instances send and accept cookies for file scheme URLs. 66 */ 67 virtual bool IsFileURLSchemeCookiesAllowed() const = 0; 68 69 /** 70 * @brief Set whether the instance should send and accept cookies for file scheme URLs. 71 * 72 * @param allow whether the instance should send and accept cookies for file scheme URLs. 73 */ 74 virtual void PutAcceptFileURLSchemeCookiesEnabled(bool allow) = 0; 75 76 /** 77 * @brief Get all the cookies for the given URL. 78 * 79 * @param url the URL for which the cookies are requested. 80 * @param callback a callback which is executed when the cookies have been gotten. 81 */ 82 virtual void ReturnCookie(const std::string &url, 83 std::shared_ptr<NWebValueCallback<std::string>> callback) = 0; 84 85 /** 86 * @brief Get all the cookies for the given URL. 87 * 88 * @param url the URL for which the cookies are requested. 89 * @return the cookie value for given URL. 90 */ 91 virtual std::string ReturnCookie(const std::string &url, bool &is_valid) = 0; 92 93 /** 94 * @brief Set a single cookie (key-value pair) for the given URL. 95 * 96 * @param url the URL for which the cookie is to be set. 97 * @param value the cookie as a string, using the format of the 'Set-Cookie' HTTP response header. 98 * @param callback a callback to be executed when the cookie has been set. 99 */ 100 virtual void SetCookie(const std::string &url, 101 const std::string &value, 102 std::shared_ptr<NWebValueCallback<bool>> callback) = 0; 103 104 /** 105 * @brief Set a single cookie (key-value pair) for the given URL sync. 106 * 107 * @param url the URL for which the cookie is to be set. 108 * @param value the cookie as a string, using the format of the 'Set-Cookie' HTTP response header. 109 * @return 0 if success else return error id. 110 */ 111 virtual int SetCookie(const std::string &url, 112 const std::string &value) = 0; 113 114 /** 115 * @brief Get whether there are stored cookies. 116 * 117 * @param callback a callback to be executed when the cookie has checked. 118 */ 119 virtual void ExistCookies(std::shared_ptr<NWebValueCallback<bool>> callback) = 0; 120 121 /** 122 * @brief Get whether there are stored cookies. 123 * 124 * @return true if there are stored cookies else false. 125 */ 126 virtual bool ExistCookies() = 0; 127 128 /** 129 * @brief Ensure all cookies currently accessible through the ReturnCookie API are written to 130 * persistent storage. 131 * 132 * @param callback a callback to be executed when cookies has Stored. 133 */ 134 virtual void Store(std::shared_ptr<NWebValueCallback<bool>> callback) = 0; 135 136 /** 137 * @brief Ensure all cookies currently accessible through the ReturnCookie API are written to 138 * persistent storage sync. 139 * 140 * @return true if success else false. 141 */ 142 virtual bool Store() = 0; 143 144 /** 145 * @brief Remove all session cookies, which are cookies without an expiration date. 146 * 147 * @param callback a callback to be executed when all session cookies has removed. 148 */ 149 virtual void DeleteSessionCookies(std::shared_ptr<NWebValueCallback<bool>> callback) = 0; 150 151 /** 152 * @brief Remove all cookies. 153 * 154 * @param callback a callback to be executed when all cookies has removed. 155 */ 156 virtual void DeleteCookieEntirely(std::shared_ptr<NWebValueCallback<bool>> callback) = 0; 157 }; 158 } // namespace OHOS::NWeb 159 160 #endif // NWEB_COOKIE_MANAGER_H 161