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_dns_data_base_adapter_impl.h"
17
18 #include <cinttypes>
19 #include <unistd.h>
20 #include "foundation/ability/ability_runtime/interfaces/kits/native/appkit/ability_runtime/context/application_context.h"
21 #include "nweb_log.h"
22 #include "sqlite_database_utils.h"
23
24 using namespace OHOS::NativeRdb;
25 using namespace OHOS::NWeb;
26
27 namespace {
28 const int32_t RDB_VERSION = 1;
29 const std::string WEB_DNS_DATABASE_FILE = "web_dns.db";
30
31 const std::string ID_COL = "_id";
32 const std::string DNS_TABLE_NAME = "dns";
33 const std::string HOSTNAME_COL = "hostname";
34
35 const std::string CREATE_TABLE = "CREATE TABLE " + DNS_TABLE_NAME + " (" + ID_COL + " INTEGER PRIMARY KEY, " +
36 HOSTNAME_COL + " TEXT, " + " UNIQUE (" + HOSTNAME_COL + ") ON CONFLICT REPLACE);";
37
38 const std::string WEB_PATH = "/web";
39 }
40
OnCreate(OHOS::NativeRdb::RdbStore & rdbStore)41 int32_t DnsDataBaseRdbOpenCallBack::OnCreate(OHOS::NativeRdb::RdbStore& rdbStore)
42 {
43 WVLOG_I("web dns database opened, create table: %{public}s", CREATE_TABLE.c_str());
44 return rdbStore.ExecuteSql(CREATE_TABLE);
45 }
46
OnUpgrade(OHOS::NativeRdb::RdbStore & rdbStore,int32_t currentVersion,int32_t targetVersion)47 int32_t DnsDataBaseRdbOpenCallBack::OnUpgrade(OHOS::NativeRdb::RdbStore& rdbStore,
48 int32_t currentVersion, int32_t targetVersion)
49 {
50 WVLOG_I("web dns database upgrade");
51 return OHOS::NativeRdb::E_OK;
52 }
53
GetInstance()54 OhosWebDnsDataBaseAdapterImpl& OhosWebDnsDataBaseAdapterImpl::GetInstance()
55 {
56 static OhosWebDnsDataBaseAdapterImpl instance;
57 if (instance.rdbStore_ == nullptr) {
58 WVLOG_I("webdatabase CreateDataBase");
59 DnsDataBaseRdbOpenCallBack callBack;
60 instance.rdbStore_ = instance.CreateDataBase(WEB_DNS_DATABASE_FILE, callBack);
61 }
62 return instance;
63 }
64
CreateDataBase(const std::string & dataBeseName,OHOS::NativeRdb::RdbOpenCallback & callBack)65 std::shared_ptr<OHOS::NativeRdb::RdbStore> OhosWebDnsDataBaseAdapterImpl::CreateDataBase(
66 const std::string& dataBeseName, OHOS::NativeRdb::RdbOpenCallback& callBack)
67 {
68 WVLOG_I("web dns 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 dns 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 dns 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 = SqliteDatabaseUtils::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 dns database databaseDir=%{public}s", databaseDir.c_str());
93 WVLOG_I("web dns 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 dns database create rdb store end, errCode=%{public}d", errCode);
98 return rdbStore;
99 }
100
OhosWebDnsDataBaseAdapterImpl()101 OhosWebDnsDataBaseAdapterImpl::OhosWebDnsDataBaseAdapterImpl() {}
102
ExistHostname(const std::string & hostname) const103 bool OhosWebDnsDataBaseAdapterImpl::ExistHostname(const std::string& hostname) const
104 {
105 if (rdbStore_ == nullptr || hostname.empty()) {
106 return false;
107 }
108
109 std::vector<std::string> hostInfo;
110 GetHostnames(hostInfo);
111 if (hostInfo.empty()) {
112 WVLOG_I("web dns database get no hostinfo.");
113 return false;
114 }
115
116 std::vector<std::string>::iterator it = find(hostInfo.begin(), hostInfo.end(), hostname);
117 if (it != hostInfo.end()) {
118 return true;
119 }
120 return false;
121 }
122
InsertHostname(const std::string & hostname)123 void OhosWebDnsDataBaseAdapterImpl::InsertHostname(const std::string& hostname)
124 {
125 WVLOG_I("web dns database set info hostname:%{public}s", hostname.c_str());
126 if (rdbStore_ == nullptr || hostname.empty()) {
127 return;
128 }
129
130 int32_t errCode;
131 int64_t outRowId;
132 NativeRdb::ValuesBucket valuesBucket;
133 valuesBucket.Clear();
134 valuesBucket.PutString(HOSTNAME_COL, hostname);
135 errCode = rdbStore_->Insert(outRowId, DNS_TABLE_NAME, valuesBucket);
136 WVLOG_I("web dns database set info end, errCode=%{public}d", errCode);
137 }
138
GetHostnames(std::vector<std::string> & hostnames) const139 void OhosWebDnsDataBaseAdapterImpl::GetHostnames(std::vector<std::string>& hostnames) const
140 {
141 if (rdbStore_ == nullptr) {
142 return;
143 }
144
145 std::vector<std::string> columns;
146 NativeRdb::AbsRdbPredicates dirAbsPred(DNS_TABLE_NAME);
147 std::unique_ptr<AbsSharedResultSet> resultSet = rdbStore_->Query(dirAbsPred, columns);
148 if ((resultSet == nullptr) || (resultSet->GoToFirstRow() != NativeRdb::E_OK)) {
149 WVLOG_E("web dns database rdb store query failed");
150 return;
151 }
152 int32_t columnIndex;
153 resultSet->GetColumnIndex(HOSTNAME_COL, columnIndex);
154 do {
155 std::string hostname;
156 resultSet->GetString(columnIndex, hostname);
157 hostnames.push_back(hostname);
158 } while (resultSet->GoToNextRow() == NativeRdb::E_OK);
159 }
160
ClearAllHostname()161 void OhosWebDnsDataBaseAdapterImpl::ClearAllHostname()
162 {
163 if (rdbStore_ == nullptr) {
164 return;
165 }
166
167 int32_t deletedRows = 0;
168 NativeRdb::AbsRdbPredicates dirAbsPred(DNS_TABLE_NAME);
169 int32_t ret = rdbStore_->Delete(deletedRows, dirAbsPred);
170 WVLOG_I("web dns database clear all hostname: ret=%{public}d, deletedRows=%{public}d", ret, deletedRows);
171 }