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