1/****************************************************************************
2**
3** Copyright (C) 2015 The Qt Company Ltd.
4** Contact: http://www.qt.io/licensing/
5**
6** This file is part of the test suite of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL21$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see http://www.qt.io/terms-conditions. For further
15** information use the contact form at http://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 2.1 or version 3 as published by the Free
20** Software Foundation and appearing in the file LICENSE.LGPLv21 and
21** LICENSE.LGPLv3 included in the packaging of this file. Please review the
22** following information to ensure the GNU Lesser General Public License
23** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
24** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25**
26** As a special exception, The Qt Company gives you certain additional
27** rights. These rights are described in The Qt Company LGPL Exception
28** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29**
30** $QT_END_LICENSE$
31**
32****************************************************************************/
33
34#ifndef MALICIOUSPLUGINTARGET
35#define MALICIOUSPLUGINTARGET contacts_maliciousplugin
36#endif
37
38#ifndef MALICIOUSPLUGINNAME
39#define MALICIOUSPLUGINNAME maliciousplugin
40#endif
41
42#define makestr(x) (#x)
43#define makename(x) makestr(x)
44
45#include "maliciousplugin_p.h"
46#include <QtContacts/qcontactmanager.h>
47
48#include <QThread>
49#include <QMutex>
50#include <QSet>
51#include <QDebug>
52
53QTCONTACTS_USE_NAMESPACE
54
55class MaliciousThreadObject : public QObject
56{
57 Q_OBJECT
58public:
59 MaliciousThreadObject() {}
60
61public slots:
62 void activateRequest(QContactAbstractRequest* req)
63 {
64 mutex.lock();
65 if (activeRequests.contains(value: req)) {
66 QContactManagerEngine::updateRequestState(req, state: QContactAbstractRequest::ActiveState);
67 }
68 mutex.unlock();
69 }
70
71 void finishRequest(QContactAbstractRequest* req)
72 {
73 QContactManager::Error errorResult = QContactManager::NoError;
74 QMap<int, QContactManager::Error> errorsResult;
75 QList<QContactId> idResult;
76 QList<QContact> contactResult;
77 QList<QContactRelationship> relResult;
78
79 mutex.lock();
80 if (activeRequests.contains(value: req)) {
81 switch(req->type()) {
82 case QContactAbstractRequest::ContactIdFetchRequest:
83 QContactManagerEngine::updateContactIdFetchRequest(req: static_cast<QContactIdFetchRequest*>(req), result: idResult, error: errorResult, QContactAbstractRequest::FinishedState);
84 break;
85
86 case QContactAbstractRequest::ContactFetchRequest:
87 QContactManagerEngine::updateContactFetchRequest(req: static_cast<QContactFetchRequest*>(req), result: contactResult, error: errorResult, QContactAbstractRequest::FinishedState);
88 break;
89
90 default:
91 QContactManagerEngine::updateRequestState(req, state: QContactAbstractRequest::FinishedState);
92 break;
93 }
94 }
95 activeRequests.remove(value: req);
96 mutex.unlock();
97 }
98public:
99 QMutex mutex;
100
101 QSet<QContactAbstractRequest*> activeRequests;
102};
103
104class MaliciousThread : public QThread
105{
106 Q_OBJECT;
107public:
108 MaliciousThread();
109 ~MaliciousThread();
110 QObject* eventLoopQuitHack;
111};
112
113MaliciousThread::MaliciousThread()
114{
115 eventLoopQuitHack = new QObject;
116 eventLoopQuitHack->moveToThread(thread: this);
117 connect(asender: eventLoopQuitHack, SIGNAL(destroyed(QObject*)), SLOT(quit()), atype: Qt::DirectConnection);
118}
119
120MaliciousThread::~MaliciousThread()
121{
122 eventLoopQuitHack->deleteLater();
123 wait();
124}
125
126MaliciousAsyncManagerEngine::MaliciousAsyncManagerEngine()
127 : QContactManagerEngine()
128{
129 thread = new MaliciousThread();
130 threadObject = new MaliciousThreadObject();
131 threadObject->moveToThread(thread);
132
133 connect(sender: this, SIGNAL(doStartRequest(QContactAbstractRequest*)), receiver: threadObject, SLOT(activateRequest(QContactAbstractRequest*)), Qt::BlockingQueuedConnection);
134 connect(sender: this, SIGNAL(doFinishRequest(QContactAbstractRequest*)), receiver: threadObject, SLOT(finishRequest(QContactAbstractRequest*)), Qt::BlockingQueuedConnection);
135
136 thread->start();
137}
138
139MaliciousAsyncManagerEngine::~MaliciousAsyncManagerEngine()
140{
141 delete thread;
142 delete threadObject;
143}
144
145QString MaliciousAsyncManagerEngine::managerName() const
146{
147 return QString(makename(MALICIOUSPLUGINNAME));
148}
149
150bool MaliciousAsyncManagerEngine::startRequest(QContactAbstractRequest* req)
151{
152 threadObject->mutex.lock();
153 threadObject->activeRequests.insert(value: req);
154 threadObject->mutex.unlock();
155
156 // Spawn a thread to do stuff on another thread
157 emit doStartRequest(req);
158 emit doFinishRequest(req);
159
160 return true;
161}
162
163bool MaliciousAsyncManagerEngine::cancelRequest(QContactAbstractRequest *req)
164{
165 updateRequestState(req, state: QContactAbstractRequest::CanceledState);
166 QContactManagerEngine::cancelRequest(req);
167 return true;
168}
169
170void MaliciousAsyncManagerEngine::requestDestroyed(QContactAbstractRequest *req)
171{
172 threadObject->mutex.lock();
173 threadObject->activeRequests.remove(value: req);
174 threadObject->mutex.unlock();
175 QContactManagerEngine::requestDestroyed(req);
176}
177
178QString MaliciousEngineFactory::managerName() const
179{
180 return QString(makename(MALICIOUSPLUGINNAME));
181}
182
183QContactManagerEngine* MaliciousEngineFactory::engine(const QMap<QString, QString>& parameters, QContactManager::Error* error)
184{
185 Q_UNUSED(parameters);
186 *error = QContactManager::NoError;
187 return new MaliciousAsyncManagerEngine();
188}
189
190#include "maliciousplugin.moc"
191

source code of qtpim/tests/auto/contacts/qcontactasync/maliciousplugin/maliciousplugin.cpp