• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "web_cookie_manager.h"
17 #include "nweb_cookie_manager.h"
18 #include "nweb_helper.h"
19 #include "web_errors.h"
20 #include "cj_lambda.h"
21 #include "webview_log.h"
22 
23 namespace OHOS {
24 namespace NWeb {
25 const int DEFAULT_VALUE = -1;
CjGetCookie(const std::string & url,bool incognitoMode,int32_t & errCode)26 std::string WebCookieManager::CjGetCookie(const std::string &url, bool incognitoMode, int32_t& errCode)
27 {
28     std::string cookieContent = "";
29     std::shared_ptr<NWebCookieManager> cookieManager = NWebHelper::Instance().GetCookieManager();
30     bool isValid = true;
31     if (cookieManager != nullptr) {
32         cookieContent = cookieManager->ReturnCookie(url, isValid, incognitoMode);
33     }
34     if (cookieContent == "" && !isValid) {
35         errCode = NWebError::INVALID_URL;
36         return "";
37     }
38     errCode = NWebError::NO_ERROR;
39     return cookieContent;
40 }
41 
CjSetCookie(const std::string & url,const std::string & value,bool incognitoMode)42 int32_t WebCookieManager::CjSetCookie(const std::string& url, const std::string& value, bool incognitoMode)
43 {
44     int isSet = DEFAULT_VALUE;
45     std::shared_ptr<NWebCookieManager> cookieManager = NWebHelper::Instance().GetCookieManager();
46     if (cookieManager != nullptr) {
47         isSet = cookieManager->SetCookie(url, value, incognitoMode);
48     }
49     return isSet;
50 }
51 
CjSetCookie(const std::string & url,const std::string & value,bool incognitoMode,bool includeHttpOnly)52 int32_t WebCookieManager::CjSetCookie(const std::string& url, const std::string& value, bool incognitoMode,
53     bool includeHttpOnly)
54 {
55     int isSet = DEFAULT_VALUE;
56     std::shared_ptr<NWebCookieManager> cookieManager = NWebHelper::Instance().GetCookieManager();
57     if (cookieManager != nullptr) {
58         isSet = cookieManager->SetCookieSync(url, value, incognitoMode, includeHttpOnly);
59     }
60     return isSet;
61 }
62 
CjPutAcceptCookieEnabled(bool accept)63 void WebCookieManager::CjPutAcceptCookieEnabled(bool accept)
64 {
65     std::shared_ptr<NWebCookieManager> cookieManager = NWebHelper::Instance().GetCookieManager();
66     if (cookieManager != nullptr) {
67         cookieManager->PutAcceptCookieEnabled(accept);
68     }
69 }
70 
CjIsCookieAllowed()71 bool WebCookieManager::CjIsCookieAllowed()
72 {
73     bool accept = true;
74     std::shared_ptr<NWebCookieManager> cookieManager = NWebHelper::Instance().GetCookieManager();
75     if (cookieManager != nullptr) {
76         accept = cookieManager->IsAcceptCookieAllowed();
77     }
78     return accept;
79 }
80 
CjPutAcceptThirdPartyCookieEnabled(bool accept)81 void WebCookieManager::CjPutAcceptThirdPartyCookieEnabled(bool accept)
82 {
83     std::shared_ptr<NWebCookieManager> cookieManager = NWebHelper::Instance().GetCookieManager();
84     if (cookieManager != nullptr) {
85         cookieManager->PutAcceptThirdPartyCookieEnabled(accept);
86     }
87 }
88 
CjIsThirdPartyCookieAllowed()89 bool WebCookieManager::CjIsThirdPartyCookieAllowed()
90 {
91     bool accept = true;
92     std::shared_ptr<NWebCookieManager> cookieManager = NWebHelper::Instance().GetCookieManager();
93     if (cookieManager != nullptr) {
94         accept = cookieManager->IsThirdPartyCookieAllowed();
95     }
96     return accept;
97 }
98 
CjExistCookie(bool incognitoMode)99 bool WebCookieManager::CjExistCookie(bool incognitoMode)
100 {
101     bool exist = true;
102     std::shared_ptr<NWebCookieManager> cookieManager = NWebHelper::Instance().GetCookieManager();
103     if (cookieManager != nullptr) {
104         exist = cookieManager->ExistCookies(incognitoMode);
105     }
106     return exist;
107 }
108 
CjDeleteEntireCookie(bool incognitoMode)109 void WebCookieManager::CjDeleteEntireCookie(bool incognitoMode)
110 {
111     std::shared_ptr<NWebCookieManager> cookieManager = NWebHelper::Instance().GetCookieManager();
112     if (cookieManager != nullptr) {
113         cookieManager->DeleteCookieEntirely(nullptr, incognitoMode);
114     }
115 }
116 
CjDeleteSessionCookie()117 void WebCookieManager::CjDeleteSessionCookie()
118 {
119     std::shared_ptr<NWebCookieManager> cookieManager = NWebHelper::Instance().GetCookieManager();
120     if (cookieManager != nullptr) {
121         cookieManager->DeleteSessionCookies(nullptr);
122     }
123 }
124 
CjSaveCookie(void (* callbackRef)(void))125 void WebCookieManager::CjSaveCookie(void (*callbackRef)(void))
126 {
127     std::shared_ptr<OHOS::NWeb::NWebCookieManager> cookieManager =
128         OHOS::NWeb::NWebHelper::Instance().GetCookieManager();
129     if (cookieManager == nullptr) {
130         return;
131     } else {
132         auto callbackImpl = std::make_shared<OHOS::NWeb::NWebSaveCookieCallbackImpl>(CJLambda::Create(callbackRef));
133         cookieManager->Store(callbackImpl);
134     }
135 }
136 
OnReceiveValue(bool result)137 void NWebSaveCookieCallbackImpl::OnReceiveValue(bool result)
138 {
139     WEBVIEWLOGD("save cookie received result, result = %{public}d", result);
140     callback_();
141 }
142 }
143 }