X-Git-Url: https://gitweb.stoutner.com/?p=PrivacyBrowserPC.git;a=blobdiff_plain;f=src%2Fhelpers%2FDomainsDatabaseHelper.cpp;h=2d63bd9c785c956abcc688e18b6ddbc27878fa2f;hp=b801a0bf4047882f9895637076012608e2fc4201;hb=2facce32fb6d97b52a7dc148044cae4b36a65d4c;hpb=16118809a11aa423f453a03c47f5263e9dd8b662 diff --git a/src/helpers/DomainsDatabaseHelper.cpp b/src/helpers/DomainsDatabaseHelper.cpp index b801a0b..2d63bd9 100644 --- a/src/helpers/DomainsDatabaseHelper.cpp +++ b/src/helpers/DomainsDatabaseHelper.cpp @@ -19,20 +19,25 @@ // Application headers. #include "DomainsDatabaseHelper.h" +#include "UserAgentHelper.h" // Define the public static domain constants. const QString DomainsDatabaseHelper::CONNECTION_NAME = "domains_database"; const QString DomainsDatabaseHelper::DOMAINS_TABLE = "domains"; // Define the private static schema constants. -const int DomainsDatabaseHelper::SCHEMA_VERSION = 1; +const int DomainsDatabaseHelper::SCHEMA_VERSION = 4; // Define the public static database field names. const QString DomainsDatabaseHelper::_ID = "_id"; const QString DomainsDatabaseHelper::DOMAIN_NAME = "domain_name"; const QString DomainsDatabaseHelper::JAVASCRIPT = "javascript"; +const QString DomainsDatabaseHelper::DOM_STORAGE = "dom_storage"; +const QString DomainsDatabaseHelper::USER_AGENT = "user_agent"; +const QString DomainsDatabaseHelper::ZOOM_FACTOR = "zoom_factor"; +const QString DomainsDatabaseHelper::CUSTOM_ZOOM_FACTOR = "custom_zoom_factor"; -// The default constructor. +// Construct the class. DomainsDatabaseHelper::DomainsDatabaseHelper() {} void DomainsDatabaseHelper::addDatabase() @@ -64,15 +69,41 @@ void DomainsDatabaseHelper::addDatabase() // Run schema update code. switch (currentSchemaVersion) { - // Upgrade from schema version 0. + // Upgrade from schema version 0 to schema version 1. case 0: { // Add the JavaScript column. domainsDatabase.exec("ALTER TABLE " + DOMAINS_TABLE + " ADD COLUMN " + JAVASCRIPT + " INTEGER DEFAULT 0"); - // Set the default value. - domainsDatabase.exec("UPDATE " + DOMAINS_TABLE + " SET " + JAVASCRIPT + " = 0" ); + // Fallthrough to the next case. + [[fallthrough]]; } + + // Upgrade from schema version 1 to schema version 2. + case 1: + { + // Add the User Agent column. + domainsDatabase.exec("ALTER TABLE " + DOMAINS_TABLE + " ADD COLUMN " + USER_AGENT + " TEXT DEFAULT '" + UserAgentHelper::SYSTEM_DEFAULT_DATABASE + "'"); + + // Fallthrough to the next case. + [[fallthrough]]; + } + + // Upgrade from schema version 2 to schema version 3. + case 2: + { + // Add the Zoom Factor columns. + domainsDatabase.exec("ALTER TABLE " + DOMAINS_TABLE + " ADD COLUMN " + ZOOM_FACTOR + " INTEGER DEFAULT 0"); + domainsDatabase.exec("ALTER TABLE " + DOMAINS_TABLE + " ADD COLUMN " + CUSTOM_ZOOM_FACTOR + " REAL DEFAULT 1.0"); + + // Fallthrough to the next case. + [[fallthrough]]; + } + + // Upgrade from schema version 3 to schema version 4. + case 3: + // Add the DOM Storage column. + domainsDatabase.exec("ALTER TABLE " + DOMAINS_TABLE + " ADD COLUMN " + DOM_STORAGE + " INTEGER DEFAULT 0"); } // Update the schema version. @@ -88,7 +119,11 @@ void DomainsDatabaseHelper::addDatabase() createTableQuery.prepare("CREATE TABLE " + DOMAINS_TABLE + "(" + _ID + " INTEGER PRIMARY KEY, " + DOMAIN_NAME + " TEXT, " + - JAVASCRIPT + " INTEGER DEFAULT 0)" + JAVASCRIPT + " INTEGER DEFAULT 0, " + + DOM_STORAGE + " INTEGER DEFAULT 0, " + + USER_AGENT + " TEXT DEFAULT '" + UserAgentHelper::SYSTEM_DEFAULT_DATABASE + "', " + + ZOOM_FACTOR + " INTEGER DEFAULT 0, " + + CUSTOM_ZOOM_FACTOR + " REAL DEFAULT 1.0)" ); // Execute the query. @@ -114,20 +149,60 @@ QSqlQuery DomainsDatabaseHelper::getDomainQuery(const QString &hostname) // Get a handle for the domains database. QSqlDatabase domainsDatabase = QSqlDatabase::database(CONNECTION_NAME); - // Instantiate a domain lookup query. - QSqlQuery domainLookupQuery(domainsDatabase); + // Instantiate the all domain names query. + QSqlQuery allDomainNamesQuery(domainsDatabase); - // Create a hostname field. - QSqlField hostnameField(DOMAIN_NAME, QVariant::String); + // Set the query to be forward only (increases performance while iterating over the query). + allDomainNamesQuery.setForwardOnly(true); - // Set the hostname field value. - hostnameField.setValue(hostname); + // Prepare the query. + allDomainNamesQuery.prepare("SELECT " + _ID + "," + DOMAIN_NAME + " FROM " + DOMAINS_TABLE); + + // Execute the query. + allDomainNamesQuery.exec(); - // SQL escape the hostname field. - QString sqlEscapedHostname = domainsDatabase.driver()->formatValue(hostnameField); + // Create a domains settings map. + QMap domainSettingsMap; + + // Populate the domain settings map. + while (allDomainNamesQuery.next()) + { + // Add the domain name and database ID to the map. + domainSettingsMap.insert(allDomainNamesQuery.record().field(DomainsDatabaseHelper::DOMAIN_NAME).value().toString(), + allDomainNamesQuery.record().field(DomainsDatabaseHelper::_ID).value().toInt()); + } + + // Initialize the database ID tracker. + int databaseId = -1; + + // Get the database ID if the hostname is found in the domain settings set. + if (domainSettingsMap.contains(hostname)) + { + databaseId = domainSettingsMap.value(hostname); + } + + // Create a subdomain string. + QString subdomain = hostname; + + // Check all the subdomains of the hostname. + while ((databaseId == -1) && subdomain.contains(".")) // Stop checking when a match is found or there are no more `.` in the hostname. + { + // Check to see if the domain settings map contains the subdomain with a `*.` prepended. + if (domainSettingsMap.contains("*." + subdomain)) + { + // Get the database ID. + databaseId = domainSettingsMap.value("*." + subdomain); + } + + // Strip out the first subdomain. + subdomain = subdomain.section('.', 1); + } + + // Instantiate the domain lookup query. + QSqlQuery domainLookupQuery(domainsDatabase); // Prepare the domain lookup query. - domainLookupQuery.prepare("SELECT * FROM " + DOMAINS_TABLE + " WHERE " + DOMAIN_NAME + " = " + sqlEscapedHostname); + domainLookupQuery.prepare("SELECT * FROM " + DOMAINS_TABLE + " WHERE " + _ID + " = " + QString::number(databaseId)); // Execute the query. domainLookupQuery.exec(); @@ -138,4 +213,3 @@ QSqlQuery DomainsDatabaseHelper::getDomainQuery(const QString &hostname) // Return the query. return domainLookupQuery; } -