Messagelib

localdatabasemanager.cpp
1 /*
2  SPDX-FileCopyrightText: 2016-2021 Laurent Montel <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 #include "localdatabasemanager.h"
7 #include "backoffmodemanager.h"
8 #include "checkphishingurlutil.h"
9 #include "createdatabasefilejob.h"
10 #include "localdatabasemanager_p.h"
11 #include "urlhashing.h"
12 #include "webengineviewer_debug.h"
13 
14 #include <KSharedConfig>
15 
16 #include <QCryptographicHash>
17 
18 using namespace WebEngineViewer;
19 
20 Q_GLOBAL_STATIC(LocalDataBaseManagerPrivate, s_localDataBaseManager)
21 
22 LocalDataBaseManager::LocalDataBaseManager(LocalDataBaseManagerPrivate *impl, QObject *parent)
23  : QObject(parent)
24  , d(impl)
25 {
26  qRegisterMetaType<WebEngineViewer::UpdateDataBaseInfo>();
27  qRegisterMetaType<WebEngineViewer::CreatePhishingUrlDataBaseJob::DataBaseDownloadResult>();
28  qRegisterMetaType<WebEngineViewer::CreatePhishingUrlDataBaseJob::ContraintsCompressionType>();
29 }
30 
31 LocalDataBaseManager::LocalDataBaseManager(QObject *parent)
32  : LocalDataBaseManager(s_localDataBaseManager, parent)
33 {
34 }
35 
36 LocalDataBaseManager::~LocalDataBaseManager() = default;
37 
38 void LocalDataBaseManager::initialize()
39 {
40  d->initialize();
41 }
42 
43 void LocalDataBaseManager::checkUrl(const QUrl &url)
44 {
45  if (d->mDataBaseOk) {
46  // TODO fixme short hash! we don't need to use it.
47  WebEngineViewer::UrlHashing urlHashing(url);
48  QHash<QByteArray, QByteArray> hashList = urlHashing.hashList();
49  QHash<QByteArray, QByteArray> conflictHashs;
50 
52  while (i.hasNext()) {
53  i.next();
54  const QByteArray ba = i.value();
55  QByteArray result = d->mFile.searchHash(ba);
56  if (ba.contains(result)) {
57  conflictHashs.insert(i.key().toBase64(), ba.toBase64());
58  }
59  }
60  if (conflictHashs.isEmpty()) {
61  Q_EMIT checkUrlFinished(url, WebEngineViewer::CheckPhishingUrlUtil::Ok);
62  } else {
63  qCWarning(WEBENGINEVIEWER_LOG) << " We need to Check Server Database";
64  if (d->mNewClientState.isEmpty()) {
65  qCWarning(WEBENGINEVIEWER_LOG) << "Database client state is unknown";
66  Q_EMIT checkUrlFinished(url, WebEngineViewer::CheckPhishingUrlUtil::Unknown);
67  } else {
68  auto job = new WebEngineViewer::SearchFullHashJob(this);
69  job->setDatabaseState(QStringList() << d->mNewClientState);
70  job->setSearchHashs(conflictHashs);
71  job->setSearchFullHashForUrl(url);
72  connect(job, &SearchFullHashJob::result, this, [this](CheckPhishingUrlUtil::UrlStatus status, const QUrl &url) {
73  Q_EMIT checkUrlFinished(url, status);
74  });
75  job->start();
76  }
77  }
78  } else {
79  qCWarning(WEBENGINEVIEWER_LOG) << "Database not ok";
80  Q_EMIT checkUrlFinished(url, WebEngineViewer::CheckPhishingUrlUtil::Unknown);
81  }
82  if (d->mFile.checkFileChanged()) {
83  d->mFile.reload();
84  }
85 }
QHash::iterator insert(const Key &key, const T &value)
Q_GLOBAL_STATIC(Internal::StaticControl, s_instance) class ControlPrivate
The LocalDataBaseManager class.
bool isEmpty() const const
bool contains(char ch) const const
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QByteArray toBase64(QByteArray::Base64Options options) const const
Q_EMITQ_EMIT
The SearchFullHashJob class.
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Sun Nov 28 2021 23:05:44 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.