• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "ohos_web_permission_data_base_adapter_impl.h"
17 
18 #include <cinttypes>
19 #include <unistd.h>
20 
21 #include "application_context.h"
22 #include "nweb_log.h"
23 #include "rdb_sql_utils.h"
24 
25 using namespace OHOS::NativeRdb;
26 using namespace OHOS::NWeb;
27 
28 namespace {
29 const int32_t RDB_VERSION = 1;
30 const std::string WEB_PERMISSION_DATABASE_FILE = "web_permission.db";
31 
32 const std::string ID_COL = "_id";
33 const std::string GEOLOCATION_TABLE_NAME = "geolocation";
34 const std::string PERMISSION_ORIGIN_COL = "origin";
35 const std::string PERMISSION_RESULT_COL = "result";
36 
37 const std::string CREATE_TABLE = "CREATE TABLE " + GEOLOCATION_TABLE_NAME
38     + " (" + ID_COL + " INTEGER PRIMARY KEY, "
39     + PERMISSION_ORIGIN_COL + " TEXT, " + PERMISSION_RESULT_COL + " INTEGER, "
40     + " UNIQUE (" + PERMISSION_ORIGIN_COL + ") ON CONFLICT REPLACE);";
41 
42 const std::string WEB_PATH = "/web";
43 }
44 
OnCreate(OHOS::NativeRdb::RdbStore & store)45 int32_t PermissionDataBaseRdbOpenCallBack::OnCreate(OHOS::NativeRdb::RdbStore& store)
46 {
47     WVLOG_I("web permission database opened, create table: %{public}s", CREATE_TABLE.c_str());
48     return store.ExecuteSql(CREATE_TABLE);
49 }
50 
OnUpgrade(OHOS::NativeRdb::RdbStore & rdbStore,int32_t currentVersion,int32_t targetVersion)51 int32_t PermissionDataBaseRdbOpenCallBack::OnUpgrade(OHOS::NativeRdb::RdbStore& rdbStore,
52     int32_t currentVersion, int32_t targetVersion)
53 {
54     WVLOG_I("web permission database upgrade");
55     return OHOS::NativeRdb::E_OK;
56 }
57 
GetInstance()58 OhosWebPermissionDataBaseAdapterImpl& OhosWebPermissionDataBaseAdapterImpl::GetInstance()
59 {
60     WVLOG_I("webdatabase get permission data base instance");
61     static OhosWebPermissionDataBaseAdapterImpl instance;
62     return instance;
63 }
64 
CreateDataBase(const std::string & dataBeseName,OHOS::NativeRdb::RdbOpenCallback & callBack)65 std::shared_ptr<OHOS::NativeRdb::RdbStore> OhosWebPermissionDataBaseAdapterImpl::CreateDataBase(
66     const std::string& dataBeseName, OHOS::NativeRdb::RdbOpenCallback& callBack)
67 {
68     WVLOG_I("web permission database create rdb store");
69     std::shared_ptr<OHOS::NativeRdb::RdbStore> rdbStore = nullptr;
70     std::shared_ptr<AbilityRuntime::ApplicationContext> context =
71         OHOS::AbilityRuntime::ApplicationContext::GetApplicationContext();
72     if (context == nullptr) {
73         WVLOG_E("web permission database get context failed");
74         return rdbStore;
75     }
76 
77     std::string databaseDir = context->GetCacheDir() + WEB_PATH;
78     if (access(databaseDir.c_str(), F_OK) != 0) {
79         WVLOG_I("web permission database fail to access cache web dir:%{public}s", databaseDir.c_str());
80         return rdbStore;
81     }
82 
83     std::string bundleName = context->GetBundleName();
84     std::string name = dataBeseName;
85     int32_t errorCode = E_OK;
86     std::string realPath = RdbSqlUtils::GetDefaultDatabasePath(databaseDir, name, errorCode);
87     RdbStoreConfig config("");
88     config.SetPath(std::move(realPath));
89     config.SetBundleName(bundleName);
90     config.SetName(std::move(name));
91     config.SetArea(context->GetArea());
92     WVLOG_I("web permission database databaseDir=%{public}s", databaseDir.c_str());
93     WVLOG_I("web permission database bundleName=%{public}s", bundleName.c_str());
94 
95     int32_t errCode = NativeRdb::E_OK;
96     rdbStore = RdbHelper::GetRdbStore(config, RDB_VERSION, callBack, errCode);
97     WVLOG_I("web permission database create rdb store end, errCode=%{public}d", errCode);
98     return rdbStore;
99 }
100 
OhosWebPermissionDataBaseAdapterImpl()101 OhosWebPermissionDataBaseAdapterImpl::OhosWebPermissionDataBaseAdapterImpl()
102 {
103     PermissionDataBaseRdbOpenCallBack callBack;
104     rdbStore_ = OhosWebPermissionDataBaseAdapterImpl::CreateDataBase(WEB_PERMISSION_DATABASE_FILE, callBack);
105 }
106 
KeyToTableName(const WebPermissionType & key) const107 std::string OhosWebPermissionDataBaseAdapterImpl::KeyToTableName(const WebPermissionType& key) const
108 {
109     if (key == WebPermissionType::GEOLOCATION) {
110         return GEOLOCATION_TABLE_NAME;
111     }
112     return "";
113 }
114 
ExistPermissionByOrigin(const std::string & origin,const WebPermissionType & key) const115 bool OhosWebPermissionDataBaseAdapterImpl::ExistPermissionByOrigin(const std::string& origin,
116     const WebPermissionType& key) const
117 {
118     WVLOG_I("web permission database check exist permissions origin=%{public}s info", origin.c_str());
119     if (rdbStore_ == nullptr || origin.empty()) {
120         return false;
121     }
122     std::string tableName = KeyToTableName(key);
123     if (tableName.empty()) {
124         return false;
125     }
126 
127     std::vector<std::string> columns;
128     NativeRdb::AbsRdbPredicates dirAbsPred(tableName);
129     dirAbsPred.EqualTo(PERMISSION_ORIGIN_COL, origin);
130     auto resultSet = rdbStore_->Query(dirAbsPred, columns);
131     if ((resultSet == nullptr) || (resultSet->GoToFirstRow() != NativeRdb::E_OK)) {
132         WVLOG_E("web permissions database rdb store query failed");
133         return false;
134     }
135     return true;
136 }
137 
GetPermissionResultByOrigin(const std::string & origin,const WebPermissionType & key,bool & result) const138 bool OhosWebPermissionDataBaseAdapterImpl::GetPermissionResultByOrigin(const std::string& origin,
139     const WebPermissionType& key, bool& result) const
140 {
141     WVLOG_I("web permission database get permissions origin=%{public}s info", origin.c_str());
142     if (rdbStore_ == nullptr || origin.empty()) {
143         return false;
144     }
145     std::string tableName = KeyToTableName(key);
146     if (tableName.empty()) {
147         return false;
148     }
149 
150     std::vector<std::string> columns;
151     NativeRdb::AbsRdbPredicates dirAbsPred(tableName);
152     dirAbsPred.EqualTo(PERMISSION_ORIGIN_COL, origin);
153     auto resultSet = rdbStore_->Query(dirAbsPred, columns);
154     if ((resultSet == nullptr) || (resultSet->GoToFirstRow() != NativeRdb::E_OK)) {
155         WVLOG_E("web permissions database rdb store query failed");
156         return false;
157     }
158 
159     int32_t columnIndex;
160     int32_t resultVal = 0;
161     resultSet->GetColumnIndex(PERMISSION_RESULT_COL, columnIndex);
162     resultSet->GetInt(columnIndex, resultVal);
163     result = resultVal ? true : false;
164     return true;
165 }
166 
SetPermissionByOrigin(const std::string & origin,const WebPermissionType & key,bool result)167 void OhosWebPermissionDataBaseAdapterImpl::SetPermissionByOrigin(const std::string& origin,
168     const WebPermissionType& key, bool result)
169 {
170     WVLOG_I("web permission database set info origin:%{public}s key:%{public}d", origin.c_str(), key);
171     if (rdbStore_ == nullptr || origin.empty()) {
172         return;
173     }
174     std::string tableName = KeyToTableName(key);
175     if (tableName.empty()) {
176         return;
177     }
178     int32_t errCode;
179     int64_t outRowId;
180     NativeRdb::ValuesBucket valuesBucket;
181     valuesBucket.Clear();
182     valuesBucket.PutString(PERMISSION_ORIGIN_COL, origin);
183     valuesBucket.PutInt(PERMISSION_RESULT_COL, (int)result);
184     errCode = rdbStore_->Insert(outRowId, tableName, valuesBucket);
185     WVLOG_I("web permission database set info end, errCode=%{public}d", errCode);
186 }
187 
ClearPermissionByOrigin(const std::string & origin,const WebPermissionType & key)188 void OhosWebPermissionDataBaseAdapterImpl::ClearPermissionByOrigin(const std::string& origin,
189     const WebPermissionType& key)
190 {
191     if (rdbStore_ == nullptr || origin.empty()) {
192         return;
193     }
194     WVLOG_I("web permission database clear origin:%{public}s info", origin.c_str());
195     std::string tableName = KeyToTableName(key);
196     if (tableName.empty()) {
197         return;
198     }
199     int32_t deletedRows = 0;
200     NativeRdb::AbsRdbPredicates dirAbsPred(tableName);
201     dirAbsPred.EqualTo(PERMISSION_ORIGIN_COL, origin);
202     int32_t ret = rdbStore_->Delete(deletedRows, dirAbsPred);
203     WVLOG_I("web permission database clear info: ret=%{public}d, deletedRows=%{public}d", ret, deletedRows);
204 }
205 
ClearAllPermission(const WebPermissionType & key)206 void OhosWebPermissionDataBaseAdapterImpl::ClearAllPermission(const WebPermissionType& key)
207 {
208     if (rdbStore_ == nullptr) {
209         return;
210     }
211     std::string tableName = KeyToTableName(key);
212     if (tableName.empty()) {
213         return;
214     }
215     WVLOG_I("web permission database clear all permission:%{public}s info", tableName.c_str());
216 
217     int32_t deletedRows = 0;
218     NativeRdb::AbsRdbPredicates dirAbsPred(tableName);
219     int32_t ret = rdbStore_->Delete(deletedRows, dirAbsPred);
220     WVLOG_I("web permission database clear all info: ret=%{public}d, deletedRows=%{public}d", ret, deletedRows);
221 }
222 
GetOriginsByPermission(const WebPermissionType & key,std::vector<std::string> & origins) const223 void OhosWebPermissionDataBaseAdapterImpl::GetOriginsByPermission(const WebPermissionType& key,
224     std::vector<std::string>& origins) const
225 {
226     if (rdbStore_ == nullptr) {
227         return;
228     }
229     std::string tableName = KeyToTableName(key);
230     if (tableName.empty()) {
231         return;
232     }
233     std::vector<std::string> columns;
234     NativeRdb::AbsRdbPredicates dirAbsPred(tableName);
235     auto resultSet = rdbStore_->Query(dirAbsPred, columns);
236     if ((resultSet == nullptr) || (resultSet->GoToFirstRow() != NativeRdb::E_OK)) {
237         WVLOG_E("web permissions database rdb store query failed");
238         return;
239     }
240     int32_t columnIndex;
241     resultSet->GetColumnIndex(PERMISSION_ORIGIN_COL, columnIndex);
242     WVLOG_I("web permission database origin columnIndex:%{public}d", columnIndex);
243     do {
244         std::string origin;
245         resultSet->GetString(columnIndex, origin);
246         origins.push_back(origin);
247     } while (resultSet->GoToNextRow() == NativeRdb::E_OK);
248 }