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 "ohos_nweb/bridge/ark_web_data_base_wrapper.h"
17 
18 #include "base/bridge/ark_web_bridge_macros.h"
19 
20 namespace OHOS::ArkWeb {
21 
ArkWebDataBaseWrapper(ArkWebRefPtr<ArkWebDataBase> ark_web_data_base)22 ArkWebDataBaseWrapper::ArkWebDataBaseWrapper(ArkWebRefPtr<ArkWebDataBase> ark_web_data_base)
23     : ark_web_data_base_(ark_web_data_base)
24 {}
25 
ClearAllPermission(int type,bool incognito)26 void ArkWebDataBaseWrapper::ClearAllPermission(int type, bool incognito)
27 {
28     ark_web_data_base_->ClearAllPermission(type, incognito);
29 }
30 
GetHttpAuthCredentials(const std::string & host,const std::string & realm,std::string & user_name,char * password,uint32_t password_size)31 void ArkWebDataBaseWrapper::GetHttpAuthCredentials(
32     const std::string& host, const std::string& realm, std::string& user_name, char* password, uint32_t password_size)
33 {
34     ArkWebString stHost = ArkWebStringClassToStruct(host);
35     ArkWebString stRealm = ArkWebStringClassToStruct(realm);
36 
37     ArkWebString stUserName;
38     ark_web_data_base_->GetHttpAuthCredentials(stHost, stRealm, stUserName, password, password_size);
39     user_name = ArkWebStringStructToClass(stUserName);
40 
41     ArkWebStringStructRelease(stHost);
42     ArkWebStringStructRelease(stRealm);
43     ArkWebStringStructRelease(stUserName);
44 }
45 
SaveHttpAuthCredentials(const std::string & host,const std::string & realm,const std::string & user_name,const char * password)46 void ArkWebDataBaseWrapper::SaveHttpAuthCredentials(
47     const std::string& host, const std::string& realm, const std::string& user_name, const char* password)
48 {
49     ArkWebString stHost = ArkWebStringClassToStruct(host);
50     ArkWebString stRealm = ArkWebStringClassToStruct(realm);
51     ArkWebString stUserName = ArkWebStringClassToStruct(user_name);
52 
53     ark_web_data_base_->SaveHttpAuthCredentials(stHost, stRealm, stUserName, password);
54 
55     ArkWebStringStructRelease(stHost);
56     ArkWebStringStructRelease(stRealm);
57     ArkWebStringStructRelease(stUserName);
58 }
59 
ExistHttpAuthCredentials()60 bool ArkWebDataBaseWrapper::ExistHttpAuthCredentials()
61 {
62     return ark_web_data_base_->ExistHttpAuthCredentials();
63 }
64 
DeleteHttpAuthCredentials()65 void ArkWebDataBaseWrapper::DeleteHttpAuthCredentials()
66 {
67     ark_web_data_base_->DeleteHttpAuthCredentials();
68 }
69 
GetOriginsByPermission(int type,bool incognito)70 std::vector<std::string> ArkWebDataBaseWrapper::GetOriginsByPermission(int type, bool incognito)
71 {
72     ArkWebStringVector stOrigins = ark_web_data_base_->GetOriginsByPermission(type, incognito);
73 
74     std::vector<std::string> objOrigins = ArkWebStringVectorStructToClass(stOrigins);
75 
76     ArkWebStringVectorStructRelease(stOrigins);
77     return objOrigins;
78 }
79 
GetPermissionResultByOrigin(const std::string & origin,int type,bool & result,bool incognito)80 bool ArkWebDataBaseWrapper::GetPermissionResultByOrigin(
81     const std::string& origin, int type, bool& result, bool incognito)
82 {
83     ArkWebString stOrigin = ArkWebStringClassToStruct(origin);
84 
85     bool flag = ark_web_data_base_->GetPermissionByOrigin(stOrigin, type, result, incognito);
86 
87     ArkWebStringStructRelease(stOrigin);
88     return flag;
89 }
90 
SetPermissionByOrigin(const std::string & origin,int type,bool result,bool incognito)91 int ArkWebDataBaseWrapper::SetPermissionByOrigin(const std::string& origin, int type, bool result, bool incognito)
92 {
93     ArkWebString stOrigin = ArkWebStringClassToStruct(origin);
94 
95     int code = ark_web_data_base_->SetPermissionByOrigin(stOrigin, type, result, incognito);
96 
97     ArkWebStringStructRelease(stOrigin);
98     return code;
99 }
100 
ExistPermissionByOrigin(const std::string & origin,int type,bool incognito)101 bool ArkWebDataBaseWrapper::ExistPermissionByOrigin(const std::string& origin, int type, bool incognito)
102 {
103     ArkWebString stOrigin = ArkWebStringClassToStruct(origin);
104 
105     bool flag = ark_web_data_base_->ExistPermissionByOrigin(stOrigin, type, incognito);
106 
107     ArkWebStringStructRelease(stOrigin);
108     return flag;
109 }
110 
ClearPermissionByOrigin(const std::string & origin,int type,bool incognito)111 int ArkWebDataBaseWrapper::ClearPermissionByOrigin(const std::string& origin, int type, bool incognito)
112 {
113     ArkWebString stOrigin = ArkWebStringClassToStruct(origin);
114 
115     int code = ark_web_data_base_->ClearPermissionByOrigin(stOrigin, type, incognito);
116 
117     ArkWebStringStructRelease(stOrigin);
118     return code;
119 }
120 
121 } // namespace OHOS::ArkWeb
122