Messagelib

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

KDE's Doxygen guidelines are available online.