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 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 * 57 * @param allow whether the instance to use third-party cookies. 58 */ 59 virtual void PutAcceptThirdPartyCookieEnabled(bool accept) = 0; 60 61 /** 62 * @brief Get whether instances can send and accept cookies for file scheme URLs. 63 * 64 * @return true if instances send and accept cookies for file scheme URLs. 65 */ 66 virtual bool IsFileURLSchemeCookiesAllowed() const = 0; 67 68 /** 69 * @brief Set whether the instance should send and accept cookies for file scheme URLs. 70 * 71 * @param allow whether the instance should send and accept cookies for file scheme URLs. 72 */ 73 virtual void PutAcceptFileURLSchemeCookiesEnabled(bool allow) = 0; 74 75 /** 76 * @brief Get all the cookies for the given URL. 77 * 78 * @param url the URL for which the cookies are requested. 79 * @param callback a callback which is executed when the cookies have been gotten. 80 */ 81 virtual void ReturnCookie(const std::string &url, 82 std::shared_ptr<NWebValueCallback<std::string>> callback) = 0; 83 84 /** 85 * @brief Get all the cookies for the given URL. 86 * 87 * @param url the URL for which the cookies are requested. 88 * @return the cookie value for given URL. 89 */ 90 virtual std::string ReturnCookie(const std::string &url) = 0; 91 92 /** 93 * @brief Set a single cookie (key-value pair) for the given URL. 94 * 95 * @param url the URL for which the cookie is to be set. 96 * @param value the cookie as a string, using the format of the 'Set-Cookie' HTTP response header. 97 * @param callback a callback to be executed when the cookie has been set. 98 */ 99 virtual void SetCookie(const std::string &url, 100 const std::string &value, 101 std::shared_ptr<NWebValueCallback<bool>> callback) = 0; 102 103 /** 104 * @brief Set a single cookie (key-value pair) for the given URL sync. 105 * 106 * @param url the URL for which the cookie is to be set. 107 * @param value the cookie as a string, using the format of the 'Set-Cookie' HTTP response header. 108 * @return 0 if success else return error id. 109 */ 110 virtual int SetCookie(const std::string &url, 111 const std::string &value) = 0; 112 113 /** 114 * @brief Get whether there are stored cookies. 115 * 116 * @param callback a callback to be executed when the cookie has checked. 117 */ 118 virtual void ExistCookies(std::shared_ptr<NWebValueCallback<bool>> callback) = 0; 119 120 /** 121 * @brief Get whether there are stored cookies. 122 * 123 * @return true if there are stored cookies else false. 124 */ 125 virtual bool ExistCookies() = 0; 126 127 /** 128 * @brief Ensure all cookies currently accessible through the ReturnCookie API are written to 129 * persistent storage. 130 * 131 * @param callback a callback to be executed when cookies has Stored. 132 */ 133 virtual void Store(std::shared_ptr<NWebValueCallback<bool>> callback) = 0; 134 135 /** 136 * @brief Ensure all cookies currently accessible through the ReturnCookie API are written to 137 * persistent storage sync. 138 * 139 * @return true if success else false. 140 */ 141 virtual bool Store() = 0; 142 143 /** 144 * @brief Remove all session cookies, which are cookies without an expiration date. 145 * 146 * @param callback a callback to be executed when all session cookies has removed. 147 */ 148 virtual void DeleteSessionCookies(std::shared_ptr<NWebValueCallback<bool>> callback) = 0; 149 150 /** 151 * @brief Remove all cookies. 152 * 153 * @param callback a callback to be executed when all cookies has removed. 154 */ 155 virtual void DeleteCookieEntirely(std::shared_ptr<NWebValueCallback<bool>> callback) = 0; 156 }; 157 } // namespace OHOS::NWeb 158 159 #endif // NWEB_COOKIE_MANAGER_H 160