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 "DomainsDatabaseHelper.h"
22 #include "UserAgentHelper.h"
24 // Define the public static domain constants.
25 const QString DomainsDatabaseHelper::CONNECTION_NAME = "domains_database";
26 const QString DomainsDatabaseHelper::DOMAINS_TABLE = "domains";
28 // Define the private static schema constants.
29 const int DomainsDatabaseHelper::SCHEMA_VERSION = 4;
31 // Define the public static database field names.
32 const QString DomainsDatabaseHelper::_ID = "_id";
33 const QString DomainsDatabaseHelper::DOMAIN_NAME = "domain_name";
34 const QString DomainsDatabaseHelper::JAVASCRIPT = "javascript";
35 const QString DomainsDatabaseHelper::DOM_STORAGE = "dom_storage";
36 const QString DomainsDatabaseHelper::USER_AGENT = "user_agent";
37 const QString DomainsDatabaseHelper::ZOOM_FACTOR = "zoom_factor";
38 const QString DomainsDatabaseHelper::CUSTOM_ZOOM_FACTOR = "custom_zoom_factor";
40 // Construct the class.
41 DomainsDatabaseHelper::DomainsDatabaseHelper() {}
43 void DomainsDatabaseHelper::addDatabase()
45 // Add the domain settings database.
46 QSqlDatabase domainsDatabase = QSqlDatabase::addDatabase("QSQLITE", CONNECTION_NAME);
48 // Set the database name.
49 domainsDatabase.setDatabaseName(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + "/domains.db");
52 if (domainsDatabase.open()) // Opening the database succeeded.
54 // Check to see if the domains table already exists.
55 if (domainsDatabase.tables().contains(DOMAINS_TABLE))
57 // Query the database schema version.
58 QSqlQuery getSchemaVersionQuery = domainsDatabase.exec("PRAGMA user_version");
60 // Move to the first record.
61 getSchemaVersionQuery.first();
63 // Get the current schema version.
64 int currentSchemaVersion = getSchemaVersionQuery.value(0).toInt();
66 // Check to see if the schama has been updated.
67 if (SCHEMA_VERSION > currentSchemaVersion)
69 // Run schema update code.
70 switch (currentSchemaVersion)
72 // Upgrade from schema version 0 to schema version 1.
75 // Add the JavaScript column.
76 domainsDatabase.exec("ALTER TABLE " + DOMAINS_TABLE + " ADD COLUMN " + JAVASCRIPT + " INTEGER DEFAULT 0");
78 // Fallthrough to the next case.
82 // Upgrade from schema version 1 to schema version 2.
85 // Add the User Agent column.
86 domainsDatabase.exec("ALTER TABLE " + DOMAINS_TABLE + " ADD COLUMN " + USER_AGENT + " TEXT DEFAULT '" + UserAgentHelper::SYSTEM_DEFAULT_DATABASE + "'");
88 // Fallthrough to the next case.
92 // Upgrade from schema version 2 to schema version 3.
95 // Add the Zoom Factor columns.
96 domainsDatabase.exec("ALTER TABLE " + DOMAINS_TABLE + " ADD COLUMN " + ZOOM_FACTOR + " INTEGER DEFAULT 0");
97 domainsDatabase.exec("ALTER TABLE " + DOMAINS_TABLE + " ADD COLUMN " + CUSTOM_ZOOM_FACTOR + " REAL DEFAULT 1.0");
99 // Fallthrough to the next case.
103 // Upgrade from schema version 3 to schema version 4.
105 // Add the DOM Storage column.
106 domainsDatabase.exec("ALTER TABLE " + DOMAINS_TABLE + " ADD COLUMN " + DOM_STORAGE + " INTEGER DEFAULT 0");
109 // Update the schema version.
110 domainsDatabase.exec("PRAGMA user_version = " + QString::number(SCHEMA_VERSION));
115 // Instantiate a create table query.
116 QSqlQuery createTableQuery(domainsDatabase);
118 // Prepare the create table query.
119 createTableQuery.prepare("CREATE TABLE " + DOMAINS_TABLE + "(" +
120 _ID + " INTEGER PRIMARY KEY, " +
121 DOMAIN_NAME + " TEXT, " +
122 JAVASCRIPT + " INTEGER DEFAULT 0, " +
123 DOM_STORAGE + " INTEGER DEFAULT 0, " +
124 USER_AGENT + " TEXT DEFAULT '" + UserAgentHelper::SYSTEM_DEFAULT_DATABASE + "', " +
125 ZOOM_FACTOR + " INTEGER DEFAULT 0, " +
126 CUSTOM_ZOOM_FACTOR + " REAL DEFAULT 1.0)"
129 // Execute the query.
130 if (!createTableQuery.exec())
133 qDebug().noquote().nospace() << "Error creating table: " << domainsDatabase.lastError();
136 // Set the schema version.
137 domainsDatabase.exec("PRAGMA user_version = " + QString::number(SCHEMA_VERSION));
140 else // Opening the database failed.
142 // Write the last database error message to the debug output.
143 qDebug().noquote().nospace() << "Error opening database: " << domainsDatabase.lastError();
147 QSqlQuery DomainsDatabaseHelper::getDomainQuery(const QString &hostname)
149 // Get a handle for the domains database.
150 QSqlDatabase domainsDatabase = QSqlDatabase::database(CONNECTION_NAME);
152 // Instantiate the all domain names query.
153 QSqlQuery allDomainNamesQuery(domainsDatabase);
155 // Set the query to be forward only (increases performance while iterating over the query).
156 allDomainNamesQuery.setForwardOnly(true);
158 // Prepare the query.
159 allDomainNamesQuery.prepare("SELECT " + _ID + "," + DOMAIN_NAME + " FROM " + DOMAINS_TABLE);
161 // Execute the query.
162 allDomainNamesQuery.exec();
164 // Create a domains settings map.
165 QMap<QString, int> domainSettingsMap;
167 // Populate the domain settings map.
168 while (allDomainNamesQuery.next())
170 // Add the domain name and database ID to the map.
171 domainSettingsMap.insert(allDomainNamesQuery.record().field(DomainsDatabaseHelper::DOMAIN_NAME).value().toString(),
172 allDomainNamesQuery.record().field(DomainsDatabaseHelper::_ID).value().toInt());
175 // Initialize the database ID tracker.
178 // Get the database ID if the hostname is found in the domain settings set.
179 if (domainSettingsMap.contains(hostname))
181 databaseId = domainSettingsMap.value(hostname);
184 // Create a subdomain string.
185 QString subdomain = hostname;
187 // Check all the subdomains of the hostname.
188 while ((databaseId == -1) && subdomain.contains(".")) // Stop checking when a match is found or there are no more `.` in the hostname.
190 // Check to see if the domain settings map contains the subdomain with a `*.` prepended.
191 if (domainSettingsMap.contains("*." + subdomain))
193 // Get the database ID.
194 databaseId = domainSettingsMap.value("*." + subdomain);
197 // Strip out the first subdomain.
198 subdomain = subdomain.section('.', 1);
201 // Instantiate the domain lookup query.
202 QSqlQuery domainLookupQuery(domainsDatabase);
204 // Prepare the domain lookup query.
205 domainLookupQuery.prepare("SELECT * FROM " + DOMAINS_TABLE + " WHERE " + _ID + " = " + QString::number(databaseId));
207 // Execute the query.
208 domainLookupQuery.exec();
210 // Move to the first entry.
211 domainLookupQuery.first();
214 return domainLookupQuery;