]> gitweb.stoutner.com Git - PrivacyBrowserPC.git/blob - src/helpers/DomainsDatabaseHelper.cpp
Add local storage domain settings.
[PrivacyBrowserPC.git] / src / helpers / DomainsDatabaseHelper.cpp
1 /*
2  * Copyright © 2022 Soren Stoutner <soren@stoutner.com>.
3  *
4  * This file is part of Privacy Browser PC <https://www.stoutner.com/privacy-browser-pc>.
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 // Application headers.
21 #include "DomainsDatabaseHelper.h"
22 #include "UserAgentHelper.h"
23
24 // Define the public static domain constants.
25 const QString DomainsDatabaseHelper::CONNECTION_NAME = "domains_database";
26 const QString DomainsDatabaseHelper::DOMAINS_TABLE = "domains";
27
28 // Define the private static schema constants.
29 const int DomainsDatabaseHelper::SCHEMA_VERSION = 5;
30
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::LOCAL_STORAGE = "local_storage";
36 const QString DomainsDatabaseHelper::DOM_STORAGE = "dom_storage";
37 const QString DomainsDatabaseHelper::USER_AGENT = "user_agent";
38 const QString DomainsDatabaseHelper::ZOOM_FACTOR = "zoom_factor";
39 const QString DomainsDatabaseHelper::CUSTOM_ZOOM_FACTOR = "custom_zoom_factor";
40
41 // Construct the class.
42 DomainsDatabaseHelper::DomainsDatabaseHelper() {}
43
44 void DomainsDatabaseHelper::addDatabase()
45 {
46     // Add the domain settings database.
47     QSqlDatabase domainsDatabase = QSqlDatabase::addDatabase("QSQLITE", CONNECTION_NAME);
48
49     // Set the database name.
50     domainsDatabase.setDatabaseName(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + "/domains.db");
51
52     // Open the database.
53     if (domainsDatabase.open())  // Opening the database succeeded.
54     {
55         // Check to see if the domains table already exists.
56         if (domainsDatabase.tables().contains(DOMAINS_TABLE))
57         {
58             // Query the database schema version.
59             QSqlQuery getSchemaVersionQuery = domainsDatabase.exec("PRAGMA user_version");
60
61             // Move to the first record.
62             getSchemaVersionQuery.first();
63
64             // Get the current schema version.
65             int currentSchemaVersion = getSchemaVersionQuery.value(0).toInt();
66
67             // Check to see if the schama has been updated.
68             if (SCHEMA_VERSION > currentSchemaVersion)
69             {
70                 // Run schema update code.
71                 switch (currentSchemaVersion)
72                 {
73                     // Upgrade from schema version 0 to schema version 1.
74                     case 0:
75                     {
76                         // Add the JavaScript column.
77                         domainsDatabase.exec("ALTER TABLE " + DOMAINS_TABLE + " ADD COLUMN " + JAVASCRIPT + " INTEGER DEFAULT 0");
78
79                         // Fallthrough to the next case.
80                         [[fallthrough]];
81                     }
82
83                     // Upgrade from schema version 1 to schema version 2.
84                     case 1:
85                     {
86                         // Add the User Agent column.
87                         domainsDatabase.exec("ALTER TABLE " + DOMAINS_TABLE + " ADD COLUMN " + USER_AGENT + " TEXT DEFAULT '" + UserAgentHelper::SYSTEM_DEFAULT_DATABASE + "'");
88
89                         // Fallthrough to the next case.
90                         [[fallthrough]];
91                     }
92
93                     // Upgrade from schema version 2 to schema version 3.
94                     case 2:
95                     {
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");
99
100                         // Fallthrough to the next case.
101                         [[fallthrough]];
102                     }
103
104                     // Upgrade from schema version 3 to schema version 4.
105                     case 3:
106                     {
107                         // Add the DOM Storage column.
108                         domainsDatabase.exec("ALTER TABLE " + DOMAINS_TABLE + " ADD COLUMN " + DOM_STORAGE + " INTEGER DEFAULT 0");
109
110                         // Fallthrough to the next case.
111                         [[fallthrough]];
112                     }
113
114                     case 4:
115                     {
116                         // Add the Local Storage column.
117                         domainsDatabase.exec("ALTER TABLE " + DOMAINS_TABLE + " ADD COLUMN " + LOCAL_STORAGE + " INTEGER DEFAULT 0");
118
119                         // Fallthrough to the next case.
120                         // [[fallthrough]];
121                     }
122                 }
123
124                 // Update the schema version.
125                 domainsDatabase.exec("PRAGMA user_version = " + QString::number(SCHEMA_VERSION));
126             }
127         }
128         else
129         {
130             // Instantiate a create table query.
131             QSqlQuery createTableQuery(domainsDatabase);
132
133             // Prepare the create table query.
134             createTableQuery.prepare("CREATE TABLE " + DOMAINS_TABLE + "(" +
135                 _ID + " INTEGER PRIMARY KEY, " +
136                 DOMAIN_NAME + " TEXT, " +
137                 JAVASCRIPT + " INTEGER DEFAULT 0, " +
138                 LOCAL_STORAGE + " INTEGER DEFAULT 0, " +
139                 DOM_STORAGE + " INTEGER DEFAULT 0, " +
140                 USER_AGENT + " TEXT DEFAULT '" + UserAgentHelper::SYSTEM_DEFAULT_DATABASE + "', " +
141                 ZOOM_FACTOR + " INTEGER DEFAULT 0, " +
142                 CUSTOM_ZOOM_FACTOR + " REAL DEFAULT 1.0)"
143             );
144
145             // Execute the query.
146             if (!createTableQuery.exec())
147             {
148                 // Log any errors.
149                 qDebug().noquote().nospace() << "Error creating table:  " << domainsDatabase.lastError();
150             }
151
152             // Set the schema version.
153             domainsDatabase.exec("PRAGMA user_version = " + QString::number(SCHEMA_VERSION));
154         }
155     }
156     else  // Opening the database failed.
157     {
158         // Write the last database error message to the debug output.
159         qDebug().noquote().nospace() << "Error opening database:  " << domainsDatabase.lastError();
160     }
161 };
162
163 QSqlQuery DomainsDatabaseHelper::getDomainQuery(const QString &hostname)
164 {
165     // Get a handle for the domains database.
166     QSqlDatabase domainsDatabase = QSqlDatabase::database(CONNECTION_NAME);
167
168     // Instantiate the all domain names query.
169     QSqlQuery allDomainNamesQuery(domainsDatabase);
170
171     // Set the query to be forward only (increases performance while iterating over the query).
172     allDomainNamesQuery.setForwardOnly(true);
173
174     // Prepare the query.
175     allDomainNamesQuery.prepare("SELECT " + _ID + "," + DOMAIN_NAME + " FROM " + DOMAINS_TABLE);
176
177     // Execute the query.
178     allDomainNamesQuery.exec();
179
180     // Create a domains settings map.
181     QMap<QString, int> domainSettingsMap;
182
183     // Populate the domain settings map.
184     while (allDomainNamesQuery.next())
185     {
186         // Add the domain name and database ID to the map.
187         domainSettingsMap.insert(allDomainNamesQuery.record().field(DomainsDatabaseHelper::DOMAIN_NAME).value().toString(),
188                                  allDomainNamesQuery.record().field(DomainsDatabaseHelper::_ID).value().toInt());
189     }
190
191     // Initialize the database ID tracker.
192     int databaseId = -1;
193
194     // Get the database ID if the hostname is found in the domain settings set.
195     if (domainSettingsMap.contains(hostname))
196     {
197         databaseId = domainSettingsMap.value(hostname);
198     }
199
200     // Create a subdomain string.
201     QString subdomain = hostname;
202
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.
205     {
206         // Check to see if the domain settings map contains the subdomain with a `*.` prepended.
207         if (domainSettingsMap.contains("*." + subdomain))
208         {
209             // Get the database ID.
210             databaseId = domainSettingsMap.value("*." + subdomain);
211         }
212
213         // Strip out the first subdomain.
214         subdomain = subdomain.section('.', 1);
215     }
216
217     // Instantiate the domain lookup query.
218     QSqlQuery domainLookupQuery(domainsDatabase);
219
220     // Prepare the domain lookup query.
221     domainLookupQuery.prepare("SELECT * FROM " + DOMAINS_TABLE + " WHERE " + _ID + " = " + QString::number(databaseId));
222
223     // Execute the query.
224     domainLookupQuery.exec();
225
226     // Move to the first entry.
227     domainLookupQuery.first();
228
229     // Return the query.
230     return domainLookupQuery;
231 }