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