2 * Copyright © 2022 Soren Stoutner <soren@stoutner.com>.
4 * This file is part of Privacy Browser PC <https://www.stoutner.com/privacy-browser-pc>.
6 * Privacy Browser PC is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * Privacy Browser PC is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with Privacy Browser PC. If not, see <http://www.gnu.org/licenses/>.
20 // Application headers.
21 #include "DomainsDatabase.h"
22 #include "helpers/UserAgentHelper.h"
24 // Define the private static schema constants.
25 const int DomainsDatabase::SCHEMA_VERSION = 5;
27 // Define the public static database constants.
28 const QString DomainsDatabase::CONNECTION_NAME = "domains_database";
29 const QString DomainsDatabase::DOMAINS_TABLE = "domains";
31 // Define the public static database field names.
32 const QString DomainsDatabase::_ID = "_id";
33 const QString DomainsDatabase::DOMAIN_NAME = "domain_name";
34 const QString DomainsDatabase::JAVASCRIPT = "javascript";
35 const QString DomainsDatabase::LOCAL_STORAGE = "local_storage";
36 const QString DomainsDatabase::DOM_STORAGE = "dom_storage";
37 const QString DomainsDatabase::USER_AGENT = "user_agent";
38 const QString DomainsDatabase::ZOOM_FACTOR = "zoom_factor";
39 const QString DomainsDatabase::CUSTOM_ZOOM_FACTOR = "custom_zoom_factor";
41 // Construct the class.
42 DomainsDatabase::DomainsDatabase() {}
44 void DomainsDatabase::addDatabase()
46 // Add the domain settings database.
47 QSqlDatabase domainsDatabase = QSqlDatabase::addDatabase(QStringLiteral("QSQLITE"), CONNECTION_NAME);
49 // Set the database name.
50 domainsDatabase.setDatabaseName(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + "/domains.db");
53 if (domainsDatabase.open()) // Opening the database succeeded.
55 // Check to see if the domains table already exists.
56 if (domainsDatabase.tables().contains(DOMAINS_TABLE)) // The domains table already exists.
58 // Query the database schema version.
59 QSqlQuery schemaVersionQuery = domainsDatabase.exec(QStringLiteral("PRAGMA user_version"));
61 // Move to the first record.
62 schemaVersionQuery.first();
64 // Get the current schema version.
65 int currentSchemaVersion = schemaVersionQuery.value(0).toInt();
67 // Check to see if the schema has been updated.
68 if (currentSchemaVersion < SCHEMA_VERSION)
70 // Run the schema update code.
71 switch (currentSchemaVersion)
73 // Upgrade from schema version 0 to schema version 1.
76 // Add the JavaScript column.
77 domainsDatabase.exec("ALTER TABLE " + DOMAINS_TABLE + " ADD COLUMN " + JAVASCRIPT + " INTEGER DEFAULT 0");
79 // Fallthrough to the next case.
83 // Upgrade from schema version 1 to schema version 2.
86 // Add the User Agent column.
87 domainsDatabase.exec("ALTER TABLE " + DOMAINS_TABLE + " ADD COLUMN " + USER_AGENT + " TEXT DEFAULT '" + UserAgentHelper::SYSTEM_DEFAULT_DATABASE + "'");
89 // Fallthrough to the next case.
93 // Upgrade from schema version 2 to schema version 3.
96 // Add the Zoom Factor columns.
97 domainsDatabase.exec("ALTER TABLE " + DOMAINS_TABLE + " ADD COLUMN " + ZOOM_FACTOR + " INTEGER DEFAULT 0");
98 domainsDatabase.exec("ALTER TABLE " + DOMAINS_TABLE + " ADD COLUMN " + CUSTOM_ZOOM_FACTOR + " REAL DEFAULT 1.0");
100 // Fallthrough to the next case.
104 // Upgrade from schema version 3 to schema version 4.
107 // Add the DOM Storage column.
108 domainsDatabase.exec("ALTER TABLE " + DOMAINS_TABLE + " ADD COLUMN " + DOM_STORAGE + " INTEGER DEFAULT 0");
110 // Fallthrough to the next case.
114 // Upgrade from schema version 4 to schema version 5.
117 // Add the Local Storage column.
118 domainsDatabase.exec("ALTER TABLE " + DOMAINS_TABLE + " ADD COLUMN " + LOCAL_STORAGE + " INTEGER DEFAULT 0");
120 // Fallthrough to the next case.
125 // Update the schema version.
126 domainsDatabase.exec("PRAGMA user_version = " + QString::number(SCHEMA_VERSION));
129 else // The domains table does not exist.
131 // Instantiate a create table query.
132 QSqlQuery createTableQuery(domainsDatabase);
134 // Prepare the create table query.
135 createTableQuery.prepare("CREATE TABLE " + DOMAINS_TABLE + "(" +
136 _ID + " INTEGER PRIMARY KEY, " +
137 DOMAIN_NAME + " TEXT, " +
138 JAVASCRIPT + " INTEGER DEFAULT 0, " +
139 LOCAL_STORAGE + " INTEGER DEFAULT 0, " +
140 DOM_STORAGE + " INTEGER DEFAULT 0, " +
141 USER_AGENT + " TEXT DEFAULT '" + UserAgentHelper::SYSTEM_DEFAULT_DATABASE + "', " +
142 ZOOM_FACTOR + " INTEGER DEFAULT 0, " +
143 CUSTOM_ZOOM_FACTOR + " REAL DEFAULT 1.0)"
146 // Execute the query.
147 if (!createTableQuery.exec())
150 qDebug().noquote().nospace() << "Error creating table: " << domainsDatabase.lastError();
153 // Set the schema version.
154 domainsDatabase.exec("PRAGMA user_version = " + QString::number(SCHEMA_VERSION));
157 else // Opening the database failed.
159 // Write the last database error message to the debug output.
160 qDebug().noquote().nospace() << "Error opening database: " << domainsDatabase.lastError();
164 QSqlQuery DomainsDatabase::getDomainQuery(const QString &hostname)
166 // Get a handle for the domains database.
167 QSqlDatabase domainsDatabase = QSqlDatabase::database(CONNECTION_NAME);
169 // Instantiate the all domain names query.
170 QSqlQuery allDomainNamesQuery(domainsDatabase);
172 // Set the query to be forward only (increases performance while iterating over the query).
173 allDomainNamesQuery.setForwardOnly(true);
175 // Prepare the query.
176 allDomainNamesQuery.prepare("SELECT " + _ID + "," + DOMAIN_NAME + " FROM " + DOMAINS_TABLE);
178 // Execute the query.
179 allDomainNamesQuery.exec();
181 // Create a domains settings map.
182 QMap<QString, int> domainSettingsMap;
184 // Populate the domain settings map.
185 while (allDomainNamesQuery.next())
187 // Add the domain name and database ID to the map.
188 domainSettingsMap.insert(allDomainNamesQuery.record().field(DOMAIN_NAME).value().toString(), allDomainNamesQuery.record().field(_ID).value().toInt());
191 // Initialize the database ID tracker.
194 // Get the database ID if the hostname is found in the domain settings set.
195 if (domainSettingsMap.contains(hostname))
197 databaseId = domainSettingsMap.value(hostname);
200 // Create a subdomain string.
201 QString subdomain = hostname;
203 // Check all the subdomains of the hostname.
204 while ((databaseId == -1) && subdomain.contains(".")) // Stop checking when a match is found or there are no more `.` in the hostname.
206 // Check to see if the domain settings map contains the subdomain with a `*.` prepended.
207 if (domainSettingsMap.contains("*." + subdomain))
209 // Get the database ID.
210 databaseId = domainSettingsMap.value("*." + subdomain);
213 // Strip out the first subdomain.
214 subdomain = subdomain.section('.', 1);
217 // Instantiate the domain lookup query.
218 QSqlQuery domainLookupQuery(domainsDatabase);
220 // Prepare the domain lookup query.
221 domainLookupQuery.prepare("SELECT * FROM " + DOMAINS_TABLE + " WHERE " + _ID + " = " + QString::number(databaseId));
223 // Execute the query.
224 domainLookupQuery.exec();
226 // Move to the first entry.
227 domainLookupQuery.first();
230 return domainLookupQuery;