X-Git-Url: https://gitweb.stoutner.com/?a=blobdiff_plain;f=src%2Fdatabases%2FCookiesDatabase.cpp;h=e8a870b82d15b00a034e05ccf1e233efb3cc9097;hb=refs%2Fheads%2Fmaster;hp=207f45b89795c3ead31d00313cb78654890056c7;hpb=0d23ee5e9b43b247cdda0a4cbb73f8b1a70f4500;p=PrivacyBrowserPC.git diff --git a/src/databases/CookiesDatabase.cpp b/src/databases/CookiesDatabase.cpp index 207f45b..a392dee 100644 --- a/src/databases/CookiesDatabase.cpp +++ b/src/databases/CookiesDatabase.cpp @@ -1,20 +1,20 @@ -/* - * Copyright © 2022 Soren Stoutner . +/* SPDX-License-Identifier: GPL-3.0-or-later + * SPDX-FileCopyrightText: 2022-2024 Soren Stoutner * - * This file is part of Privacy Browser PC . + * This file is part of Privacy Browser PC . * - * Privacy Browser PC is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * This program is free software: you can redistribute it and/or modify it under + * the terms of the GNU General Public License as published by the Free Software + * Foundation, either version 3 of the License, or (at your option) any later + * version. * - * Privacy Browser PC is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. * - * You should have received a copy of the GNU General Public License - * along with Privacy Browser PC. If not, see . + * You should have received a copy of the GNU General Public License along with + * this program. If not, see . */ // Application headers. @@ -23,19 +23,17 @@ // Define the private static schema constants. const int CookiesDatabase::SCHEMA_VERSION = 0; -// Define the public static database constants. -const QString CookiesDatabase::CONNECTION_NAME = "cookies_database"; -const QString CookiesDatabase::COOKIES_TABLE = "cookies"; - -// Define the public static database field names. -const QString CookiesDatabase::_ID = "_id"; -const QString CookiesDatabase::DOMAIN = "domain"; -const QString CookiesDatabase::NAME = "name"; -const QString CookiesDatabase::PATH = "path"; -const QString CookiesDatabase::EXPIRATION_DATE = "expiration_date"; -const QString CookiesDatabase::HTTP_ONLY = "http_only"; -const QString CookiesDatabase::SECURE = "secure"; -const QString CookiesDatabase::VALUE = "value"; +// Define the public static constants. +const QString CookiesDatabase::CONNECTION_NAME = QLatin1String("cookies_database"); +const QString CookiesDatabase::COOKIES_TABLE = QLatin1String("cookies"); +const QString CookiesDatabase::DOMAIN = QLatin1String("domain"); +const QString CookiesDatabase::EXPIRATION_DATE = QLatin1String("expiration_date"); +const QString CookiesDatabase::HTTP_ONLY = QLatin1String("http_only"); +const QString CookiesDatabase::ID = QLatin1String("_id"); +const QString CookiesDatabase::NAME = QLatin1String("name"); +const QString CookiesDatabase::PATH = QLatin1String("path"); +const QString CookiesDatabase::SECURE = QLatin1String("secure"); +const QString CookiesDatabase::VALUE = QLatin1String("value"); // Construct the class. CookiesDatabase::CookiesDatabase() {} @@ -46,7 +44,7 @@ void CookiesDatabase::addDatabase() QSqlDatabase cookiesDatabase = QSqlDatabase::addDatabase(QStringLiteral("QSQLITE"), CONNECTION_NAME); // Set the database name. - cookiesDatabase.setDatabaseName(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + "/cookies.db"); + cookiesDatabase.setDatabaseName(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + QLatin1String("/cookies.db")); // Open the database. if (cookiesDatabase.open()) // Opening the database succeeded. @@ -54,8 +52,11 @@ void CookiesDatabase::addDatabase() // Check to see if the cookies table already exists. if (cookiesDatabase.tables().contains(COOKIES_TABLE)) // The cookies table exists. { + // Create a schema version query. + QSqlQuery schemaVersionQuery(cookiesDatabase); + // Query the database schema version. - QSqlQuery schemaVersionQuery = cookiesDatabase.exec(QStringLiteral("PRAGMA user_version")); + schemaVersionQuery.exec(QLatin1String("PRAGMA user_version")); // Move to the first record. schemaVersionQuery.first(); @@ -72,8 +73,11 @@ void CookiesDatabase::addDatabase() // Upgrade code here. } + // Create an update schema version version. + QSqlQuery updateSchemaVersionQuery = QSqlQuery(cookiesDatabase); + // Update the schema version. - cookiesDatabase.exec("PRAGMA user_version = " + QString::number(SCHEMA_VERSION)); + updateSchemaVersionQuery.exec(QLatin1String("PRAGMA user_version = ") + QString::number(SCHEMA_VERSION)); } } else // The cookies table does not exist. @@ -82,16 +86,15 @@ void CookiesDatabase::addDatabase() QSqlQuery createTableQuery(cookiesDatabase); // Prepare the create table query. - createTableQuery.prepare("CREATE TABLE " + COOKIES_TABLE + "(" + - _ID + " INTEGER PRIMARY KEY, " + - DOMAIN + " TEXT NOT NULL, " + - NAME + " TEXT NOT NULL, " + - PATH + " TEXT NOT NULL, " + - EXPIRATION_DATE + " TEXT, " + - HTTP_ONLY + " INTEGER NOT NULL DEFAULT 0, " + - SECURE + " INTEGER NOT NULL DEFAULT 0, " + - VALUE + " TEXT NOT NULL)" - ); + createTableQuery.prepare(QLatin1String("CREATE TABLE ") + COOKIES_TABLE + QLatin1String("(") + + ID + QLatin1String(" INTEGER PRIMARY KEY, ") + + DOMAIN + QLatin1String(" TEXT NOT NULL, ") + + NAME + QLatin1String(" TEXT NOT NULL, ") + + PATH + QLatin1String(" TEXT NOT NULL, ") + + EXPIRATION_DATE + QLatin1String(" TEXT, ") + + HTTP_ONLY + QLatin1String(" INTEGER NOT NULL DEFAULT 0, ") + + SECURE + QLatin1String(" INTEGER NOT NULL DEFAULT 0, ") + + VALUE + QLatin1String(" TEXT NOT NULL)")); // Execute the query. if (!createTableQuery.exec()) @@ -100,8 +103,11 @@ void CookiesDatabase::addDatabase() qDebug().noquote().nospace() << "Error creating table: " << cookiesDatabase.lastError(); } + // Create an set schema version version. + QSqlQuery setSchemaVersionQuery(cookiesDatabase); + // Set the schema version. - cookiesDatabase.exec("PRAGMA user_version = " + QString::number(SCHEMA_VERSION)); + setSchemaVersionQuery.exec(QLatin1String("PRAGMA user_version = ") + QString::number(SCHEMA_VERSION)); } } else // Opening the database failed. @@ -128,18 +134,24 @@ void CookiesDatabase::addCookie(const QNetworkCookie &cookie) QSqlQuery addCookieQuery(cookiesDatabase); // Prepare the add cookie query. - addCookieQuery.prepare("INSERT INTO " + COOKIES_TABLE + " (" + DOMAIN + ", " + NAME + ", " + PATH + ", " + EXPIRATION_DATE + ", " + HTTP_ONLY + ", " + SECURE + ", " + VALUE + ") " - "VALUES (:domain, :name, :path, :expiration_date, :http_only, :secure, :value)" - ); + addCookieQuery.prepare(QLatin1String("INSERT INTO ") + COOKIES_TABLE + QLatin1String(" (") + + DOMAIN + QLatin1String(", ") + + NAME + QLatin1String(", ") + + PATH + QLatin1String(", ") + + EXPIRATION_DATE + QLatin1String(", ") + + HTTP_ONLY + QLatin1String(", ") + + SECURE + QLatin1String(", ") + + VALUE + QLatin1String(") ") + + QLatin1String("VALUES (:domain, :name, :path, :expiration_date, :http_only, :secure, :value)")); // Bind the values. - addCookieQuery.bindValue(":domain", cookie.domain()); - addCookieQuery.bindValue(":name", QString(cookie.name())); - addCookieQuery.bindValue(":path", cookie.path()); - addCookieQuery.bindValue(":expiration_date", cookie.expirationDate()); - addCookieQuery.bindValue(":http_only", cookie.isHttpOnly()); - addCookieQuery.bindValue(":secure", cookie.isSecure()); - addCookieQuery.bindValue(":value", QString(cookie.value())); + addCookieQuery.bindValue(QLatin1String(":domain"), cookie.domain()); + addCookieQuery.bindValue(QLatin1String(":name"), QLatin1String(cookie.name())); + addCookieQuery.bindValue(QLatin1String(":path"), cookie.path()); + addCookieQuery.bindValue(QLatin1String(":expiration_date"), cookie.expirationDate()); + addCookieQuery.bindValue(QLatin1String(":http_only"), cookie.isHttpOnly()); + addCookieQuery.bindValue(QLatin1String(":secure"), cookie.isSecure()); + addCookieQuery.bindValue(QLatin1String(":value"), QLatin1String(cookie.value())); // Execute the query. addCookieQuery.exec(); @@ -158,7 +170,7 @@ int CookiesDatabase::cookieCount() countCookiesQuery.setForwardOnly(true); // Prepare the query. - countCookiesQuery.prepare("SELECT " + _ID + " FROM " + COOKIES_TABLE); + countCookiesQuery.prepare(QLatin1String("SELECT ") + ID + QLatin1String(" FROM ") + COOKIES_TABLE); // Execute the query. countCookiesQuery.exec(); @@ -189,7 +201,7 @@ void CookiesDatabase::deleteAllCookies() QSqlQuery deleteAllCookiesQuery(cookiesDatabase); // Prepare the delete all cookies query. - deleteAllCookiesQuery.prepare("DELETE FROM " + COOKIES_TABLE); + deleteAllCookiesQuery.prepare(QLatin1String("DELETE FROM ") + COOKIES_TABLE); // Execute the query. deleteAllCookiesQuery.exec(); @@ -204,12 +216,15 @@ void CookiesDatabase::deleteCookie(const QNetworkCookie &cookie) QSqlQuery deleteCookieQuery(cookiesDatabase); // Prepare the delete cookie query. - deleteCookieQuery.prepare("DELETE FROM " + COOKIES_TABLE + " WHERE " + DOMAIN + " = :domain AND " + NAME + " = :name AND " + PATH + " = :path"); + deleteCookieQuery.prepare(QLatin1String("DELETE FROM ") + COOKIES_TABLE + QLatin1String(" WHERE ") + + DOMAIN + QLatin1String(" = :domain AND ") + + NAME + QLatin1String(" = :name AND ") + + PATH + QLatin1String(" = :path")); // Bind the values. - deleteCookieQuery.bindValue(":domain", cookie.domain()); - deleteCookieQuery.bindValue(":name", QString(cookie.name())); - deleteCookieQuery.bindValue(":path", cookie.path()); + deleteCookieQuery.bindValue(QLatin1String(":domain"), cookie.domain()); + deleteCookieQuery.bindValue(QLatin1String(":name"), QLatin1String(cookie.name())); + deleteCookieQuery.bindValue(QLatin1String(":path"), cookie.path()); // Execute the query. deleteCookieQuery.exec(); @@ -223,11 +238,11 @@ QList* CookiesDatabase::getCookies() // Instantiate a cookies query. QSqlQuery cookiesQuery(cookiesDatabase); - // Set the query to be forward only. + // Set the query to be forward only, which is more performant. cookiesQuery.setForwardOnly(true); // Prepare the cookies query. - cookiesQuery.prepare("SELECT * FROM " + COOKIES_TABLE); + cookiesQuery.prepare(QLatin1String("SELECT * FROM ") + COOKIES_TABLE); // Execute the query. cookiesQuery.exec(); @@ -270,10 +285,10 @@ QNetworkCookie* CookiesDatabase::getCookieById(const int &id) cookieQuery.setForwardOnly(true); // Prepare the cookies query. - cookieQuery.prepare("SELECT * FROM " + COOKIES_TABLE + " WHERE " + _ID + " = :id"); + cookieQuery.prepare(QLatin1String("SELECT * FROM ") + COOKIES_TABLE + QLatin1String(" WHERE ") + ID + QLatin1String(" = :id")); // Bind the values. - cookieQuery.bindValue(":id", id); + cookieQuery.bindValue(QLatin1String(":id"), id); // Execute the query. cookieQuery.exec(); @@ -309,12 +324,15 @@ bool CookiesDatabase::isDurable(const QNetworkCookie &cookie) isDurableQuery.setForwardOnly(true); // Prepare the is durable query. - isDurableQuery.prepare("SELECT " + _ID + " FROM " + COOKIES_TABLE + " WHERE " + DOMAIN + " = :domain AND " + NAME + " = :name AND " + PATH + " = :path"); + isDurableQuery.prepare(QLatin1String("SELECT ") + ID + QLatin1String(" FROM ") + COOKIES_TABLE + QLatin1String(" WHERE ") + + DOMAIN + QLatin1String(" = :domain AND ") + + NAME + QLatin1String(" = :name AND ") + + PATH + QLatin1String(" = :path")); // Bind the values. - isDurableQuery.bindValue(":domain", cookie.domain()); - isDurableQuery.bindValue(":name", QString(cookie.name())); - isDurableQuery.bindValue(":path", cookie.path()); + isDurableQuery.bindValue(QLatin1String(":domain"), cookie.domain()); + isDurableQuery.bindValue(QLatin1String(":name"), QLatin1String(cookie.name())); + isDurableQuery.bindValue(QLatin1String(":path"), cookie.path()); // Execute the query. isDurableQuery.exec(); @@ -335,13 +353,19 @@ bool CookiesDatabase::isUpdate(const QNetworkCookie &cookie) QSqlQuery isUpdateQuery(cookiesDatabase); // Prepare the is update query. - isUpdateQuery.prepare("SELECT " + EXPIRATION_DATE + " , " + HTTP_ONLY + " , " + SECURE + " , " + VALUE + " FROM " + COOKIES_TABLE + " WHERE " + DOMAIN + " = :domain AND " + - NAME + " = :name AND " + PATH + " = :path"); + isUpdateQuery.prepare(QLatin1String("SELECT ") + EXPIRATION_DATE + QLatin1String(" , ") + + HTTP_ONLY + QLatin1String(" , ") + + SECURE + QLatin1String(" , ") + + VALUE + + QLatin1String(" FROM ") + COOKIES_TABLE + + QLatin1String(" WHERE ") + DOMAIN + QLatin1String(" = :domain AND ") + + NAME + QLatin1String(" = :name AND ") + + PATH + QLatin1String(" = :path")); // Bind the values. - isUpdateQuery.bindValue(":domain", cookie.domain()); - isUpdateQuery.bindValue(":name", QString(cookie.name())); - isUpdateQuery.bindValue(":path", cookie.path()); + isUpdateQuery.bindValue(QLatin1String(":domain"), cookie.domain()); + isUpdateQuery.bindValue(QLatin1String(":name"), QLatin1String(cookie.name())); + isUpdateQuery.bindValue(QLatin1String(":path"), cookie.path()); // Execute the query. isUpdateQuery.exec(); @@ -353,10 +377,10 @@ bool CookiesDatabase::isUpdate(const QNetworkCookie &cookie) if (isUpdateQuery.isValid()) // The cookie exists in the database. { // Check to see if the cookie data has changed. - if ((QDateTime::fromString(isUpdateQuery.value(0).toString(), Qt::ISODate) != cookie.expirationDate()) || - (isUpdateQuery.value(1).toBool() != cookie.isHttpOnly()) || - (isUpdateQuery.value(2).toBool() != cookie.isSecure()) || - (isUpdateQuery.value(3).toString().toUtf8() != cookie.value())) // The cookies data has changed. + if ((QDateTime::fromString(isUpdateQuery.value(EXPIRATION_DATE).toString(), Qt::ISODate) != cookie.expirationDate()) || + (isUpdateQuery.value(HTTP_ONLY).toBool() != cookie.isHttpOnly()) || + (isUpdateQuery.value(SECURE).toBool() != cookie.isSecure()) || + (isUpdateQuery.value(VALUE).toString().toUtf8() != cookie.value())) // The cookies data has changed. { //qDebug() << "The durable cookie data has changed."; @@ -386,18 +410,24 @@ void CookiesDatabase::updateCookie(const QNetworkCookie &cookie) // Create the update cookie query. QSqlQuery updateCookieQuery(cookiesDatabase); - // Prepare the edit cookie query. - updateCookieQuery.prepare("UPDATE " + COOKIES_TABLE + " SET " + EXPIRATION_DATE + " = :expiration_date , " + HTTP_ONLY + " = :http_only , " + SECURE + " = :secure , " + - VALUE + " = :value WHERE " + DOMAIN + " = :domain AND " + NAME + " = :name AND " + PATH + " = :path"); + // Prepare the update cookie query. + updateCookieQuery.prepare(QLatin1String("UPDATE ") + COOKIES_TABLE + + QLatin1String(" SET ") + EXPIRATION_DATE + QLatin1String(" = :expiration_date , ") + + HTTP_ONLY + QLatin1String(" = :http_only , ") + + SECURE + QLatin1String(" = :secure , ") + + VALUE + QLatin1String(" = :value ") + + QLatin1String("WHERE ") + DOMAIN + QLatin1String(" = :domain AND ") + + NAME + QLatin1String(" = :name AND ") + + PATH + QLatin1String(" = :path")); // Bind the values. - updateCookieQuery.bindValue(":domain", cookie.domain()); - updateCookieQuery.bindValue(":name", QString(cookie.name())); - updateCookieQuery.bindValue(":path", cookie.path()); - updateCookieQuery.bindValue(":expiration_date", cookie.expirationDate()); - updateCookieQuery.bindValue(":http_only", cookie.isHttpOnly()); - updateCookieQuery.bindValue(":secure", cookie.isSecure()); - updateCookieQuery.bindValue(":value", QString(cookie.value())); + updateCookieQuery.bindValue(QLatin1String(":domain"), cookie.domain()); + updateCookieQuery.bindValue(QLatin1String(":name"), QLatin1String(cookie.name())); + updateCookieQuery.bindValue(QLatin1String(":path"), cookie.path()); + updateCookieQuery.bindValue(QLatin1String(":expiration_date"), cookie.expirationDate()); + updateCookieQuery.bindValue(QLatin1String(":http_only"), cookie.isHttpOnly()); + updateCookieQuery.bindValue(QLatin1String(":secure"), cookie.isSecure()); + updateCookieQuery.bindValue(QLatin1String(":value"), QLatin1String(cookie.value())); // Execute the query. updateCookieQuery.exec(); @@ -415,12 +445,15 @@ void CookiesDatabase::updateCookie(const QNetworkCookie &oldCookie, const QNetwo oldCookieQuery.setForwardOnly(true); // Prepare the old cookie query. - oldCookieQuery.prepare("SELECT " + _ID + " FROM " + COOKIES_TABLE + " WHERE " + DOMAIN + " = :domain AND " + NAME + " = :name AND " + PATH + " = :path"); + oldCookieQuery.prepare(QLatin1String("SELECT ") + ID + QLatin1String(" FROM ") + COOKIES_TABLE + + QLatin1String(" WHERE ") + DOMAIN + QLatin1String(" = :domain AND ") + + NAME + QLatin1String(" = :name AND ") + + PATH + QLatin1String(" = :path")); // Bind the values. - oldCookieQuery.bindValue(":domain", oldCookie.domain()); - oldCookieQuery.bindValue(":name", QString(oldCookie.name())); - oldCookieQuery.bindValue(":path", oldCookie.path()); + oldCookieQuery.bindValue(QLatin1String(":domain"), oldCookie.domain()); + oldCookieQuery.bindValue(QLatin1String(":name"), QLatin1String(oldCookie.name())); + oldCookieQuery.bindValue(QLatin1String(":path"), oldCookie.path()); // Execute the query. oldCookieQuery.exec(); @@ -432,18 +465,25 @@ void CookiesDatabase::updateCookie(const QNetworkCookie &oldCookie, const QNetwo QSqlQuery updateCookieQuery(cookiesDatabase); // Prepare the update cookie query. - updateCookieQuery.prepare("UPDATE " + COOKIES_TABLE + " SET " + DOMAIN + " = :domain , " + NAME + " = :name , " + PATH + " = :path , " + EXPIRATION_DATE + " = :expiration_date , " + - HTTP_ONLY + " = :http_only , " + SECURE + " = :secure , " + VALUE + " = :value WHERE " + _ID + " = :id"); + updateCookieQuery.prepare(QLatin1String("UPDATE ") + COOKIES_TABLE + + QLatin1String(" SET ") + DOMAIN + QLatin1String(" = :domain , ") + + NAME + QLatin1String(" = :name , ") + + PATH + QLatin1String(" = :path , ") + + EXPIRATION_DATE + QLatin1String(" = :expiration_date , ") + + HTTP_ONLY + QLatin1String(" = :http_only , ") + + SECURE + QLatin1String(" = :secure , ") + + VALUE + QLatin1String(" = :value ") + + QLatin1String("WHERE ") + ID + QLatin1String(" = :id")); // Bind the values. - updateCookieQuery.bindValue(":id", oldCookieQuery.value(0).toLongLong()); - updateCookieQuery.bindValue(":domain", newCookie.domain()); - updateCookieQuery.bindValue(":name", QString(newCookie.name())); - updateCookieQuery.bindValue(":path", newCookie.path()); - updateCookieQuery.bindValue(":expiration_date", newCookie.expirationDate()); - updateCookieQuery.bindValue(":http_only", newCookie.isHttpOnly()); - updateCookieQuery.bindValue(":secure", newCookie.isSecure()); - updateCookieQuery.bindValue(":value", QString(newCookie.value())); + updateCookieQuery.bindValue(QLatin1String(":id"), oldCookieQuery.value(0).toLongLong()); + updateCookieQuery.bindValue(QLatin1String(":domain"), newCookie.domain()); + updateCookieQuery.bindValue(QLatin1String(":name"), QLatin1String(newCookie.name())); + updateCookieQuery.bindValue(QLatin1String(":path"), newCookie.path()); + updateCookieQuery.bindValue(QLatin1String(":expiration_date"), newCookie.expirationDate()); + updateCookieQuery.bindValue(QLatin1String(":http_only"), newCookie.isHttpOnly()); + updateCookieQuery.bindValue(QLatin1String(":secure"), newCookie.isSecure()); + updateCookieQuery.bindValue(QLatin1String(":value"), QLatin1String(newCookie.value())); // Execute the query. updateCookieQuery.exec();