• 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_dns_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_DNS_DATABASE_FILE = "web_dns.db";
31 
32     const std::string ID_COL = "_id";
33     const std::string DNS_TABLE_NAME = "dns";
34     const std::string HOSTNAME_COL = "hostname";
35 
36     const std::string CREATE_TABLE = "CREATE TABLE " + DNS_TABLE_NAME + " (" + ID_COL + " INTEGER PRIMARY KEY, " +
37                                      HOSTNAME_COL + " TEXT, " + " UNIQUE (" + HOSTNAME_COL + ") ON CONFLICT REPLACE);";
38 
39     const std::string WEB_PATH = "/web";
40 }
41 
OnCreate(OHOS::NativeRdb::RdbStore & rdbStore)42 int32_t DnsDataBaseRdbOpenCallBack::OnCreate(OHOS::NativeRdb::RdbStore& rdbStore)
43 {
44     WVLOG_I("web dns database opened, create table: %{public}s", CREATE_TABLE.c_str());
45     return rdbStore.ExecuteSql(CREATE_TABLE);
46 }
47 
OnUpgrade(OHOS::NativeRdb::RdbStore & rdbStore,int32_t currentVersion,int32_t targetVersion)48 int32_t DnsDataBaseRdbOpenCallBack::OnUpgrade(OHOS::NativeRdb::RdbStore& rdbStore,
49                                               int32_t currentVersion, int32_t targetVersion)
50 {
51     WVLOG_I("web dns database upgrade");
52     return OHOS::NativeRdb::E_OK;
53 }
54 
GetInstance()55 OhosWebDnsDataBaseAdapterImpl& OhosWebDnsDataBaseAdapterImpl::GetInstance()
56 {
57     static OhosWebDnsDataBaseAdapterImpl instance;
58     if (instance.rdbStore_ == nullptr) {
59         WVLOG_I("webdatabase CreateDataBase");
60         DnsDataBaseRdbOpenCallBack callBack;
61         instance.rdbStore_ = instance.CreateDataBase(WEB_DNS_DATABASE_FILE, callBack);
62     }
63     return instance;
64 }
65 
CreateDataBase(const std::string & dataBeseName,OHOS::NativeRdb::RdbOpenCallback & callBack)66 std::shared_ptr<OHOS::NativeRdb::RdbStore> OhosWebDnsDataBaseAdapterImpl::CreateDataBase(
67     const std::string& dataBeseName, OHOS::NativeRdb::RdbOpenCallback& callBack)
68 {
69     WVLOG_I("web dns database create rdb store");
70     std::shared_ptr<OHOS::NativeRdb::RdbStore> rdbStore = nullptr;
71     std::shared_ptr<AbilityRuntime::ApplicationContext> context =
72         OHOS::AbilityRuntime::ApplicationContext::GetApplicationContext();
73     if (context == nullptr) {
74         WVLOG_E("web dns database get context failed");
75         return rdbStore;
76     }
77 
78     std::string databaseDir = context->GetCacheDir() + WEB_PATH;
79     if (access(databaseDir.c_str(), F_OK) != 0) {
80         WVLOG_I("web dns database fail to access cache web dir:%{public}s", databaseDir.c_str());
81         return rdbStore;
82     }
83 
84     std::string bundleName = context->GetBundleName();
85     std::string name = dataBeseName;
86     int32_t errorCode = E_OK;
87     std::string realPath = RdbSqlUtils::GetDefaultDatabasePath(databaseDir, name, errorCode);
88     RdbStoreConfig config("");
89     config.SetPath(std::move(realPath));
90     config.SetBundleName(bundleName);
91     config.SetName(std::move(name));
92     config.SetArea(context->GetArea());
93     WVLOG_I("web dns database databaseDir=%{public}s", databaseDir.c_str());
94     WVLOG_I("web dns database bundleName=%{public}s", bundleName.c_str());
95 
96     int32_t errCode = NativeRdb::E_OK;
97     rdbStore = RdbHelper::GetRdbStore(config, RDB_VERSION, callBack, errCode);
98     WVLOG_I("web dns database create rdb store end, errCode=%{public}d", errCode);
99     return rdbStore;
100 }
101 
OhosWebDnsDataBaseAdapterImpl()102 OhosWebDnsDataBaseAdapterImpl::OhosWebDnsDataBaseAdapterImpl() {}
103 
ExistHostname(const std::string & hostname) const104 bool OhosWebDnsDataBaseAdapterImpl::ExistHostname(const std::string& hostname) const
105 {
106     if (rdbStore_ == nullptr || hostname.empty()) {
107         return false;
108     }
109 
110     std::vector<std::string> hostInfo;
111     GetHostnames(hostInfo);
112     if (hostInfo.empty()) {
113         WVLOG_I("web dns database get no hostinfo.");
114         return false;
115     }
116 
117     std::vector<std::string>::iterator it = find(hostInfo.begin(), hostInfo.end(), hostname);
118     if (it != hostInfo.end()) {
119         return true;
120     }
121     return false;
122 }
123 
InsertHostname(const std::string & hostname)124 void OhosWebDnsDataBaseAdapterImpl::InsertHostname(const std::string& hostname)
125 {
126     WVLOG_I("web dns database set info hostname:%{public}s", hostname.c_str());
127     if (rdbStore_ == nullptr || hostname.empty()) {
128         return;
129     }
130 
131     int32_t errCode;
132     int64_t outRowId;
133     NativeRdb::ValuesBucket valuesBucket;
134     valuesBucket.Clear();
135     valuesBucket.PutString(HOSTNAME_COL, hostname);
136     errCode = rdbStore_->Insert(outRowId, DNS_TABLE_NAME, valuesBucket);
137     WVLOG_I("web dns database set info end, errCode=%{public}d", errCode);
138 }
139 
GetHostnames(std::vector<std::string> & hostnames) const140 void OhosWebDnsDataBaseAdapterImpl::GetHostnames(std::vector<std::string>& hostnames) const
141 {
142     if (rdbStore_ == nullptr) {
143         return;
144     }
145 
146     std::vector<std::string> columns;
147     NativeRdb::AbsRdbPredicates dirAbsPred(DNS_TABLE_NAME);
148     auto resultSet = rdbStore_->Query(dirAbsPred, columns);
149     if ((resultSet == nullptr) || (resultSet->GoToFirstRow() != NativeRdb::E_OK)) {
150         WVLOG_E("web dns database rdb store query failed");
151         return;
152     }
153     int32_t columnIndex;
154     resultSet->GetColumnIndex(HOSTNAME_COL, columnIndex);
155     do {
156         std::string hostname;
157         resultSet->GetString(columnIndex, hostname);
158         hostnames.push_back(hostname);
159     } while (resultSet->GoToNextRow() == NativeRdb::E_OK);
160 }
161 
ClearAllHostname()162 void OhosWebDnsDataBaseAdapterImpl::ClearAllHostname()
163 {
164     if (rdbStore_ == nullptr) {
165         return;
166     }
167 
168     int32_t deletedRows = 0;
169     NativeRdb::AbsRdbPredicates dirAbsPred(DNS_TABLE_NAME);
170     int32_t ret = rdbStore_->Delete(deletedRows, dirAbsPred);
171     WVLOG_I("web dns database clear all hostname: ret=%{public}d, deletedRows=%{public}d", ret, deletedRows);
172 }