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