| 1 |
/**************************************************************************** |
| 2 |
** |
| 3 |
** Copyright (C) 2018 The Qt Company Ltd and/or its subsidiary(-ies). |
| 4 |
** Contact: http://www.qt-project.org/legal |
| 5 |
** |
| 6 |
** This file is part of the QtSystems module of the Qt Toolkit. |
| 7 |
** |
| 8 |
** $QT_BEGIN_LICENSE:LGPL$ |
| 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 https://www.qt.io/terms-conditions. For further |
| 15 |
** information use the contact form at https://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 3 as published by the Free Software |
| 20 |
** Foundation and appearing in the file LICENSE.LGPL3 included in the |
| 21 |
** packaging of this file. Please review the following information to |
| 22 |
** ensure the GNU Lesser General Public License version 3 requirements |
| 23 |
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
| 24 |
** |
| 25 |
** GNU General Public License Usage |
| 26 |
** Alternatively, this file may be used under the terms of the GNU |
| 27 |
** General Public License version 2.0 or (at your option) the GNU General |
| 28 |
** Public license version 3 or any later version approved by the KDE Free |
| 29 |
** Qt Foundation. The licenses are as published by the Free Software |
| 30 |
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
| 31 |
** included in the packaging of this file. Please review the following |
| 32 |
** information to ensure the GNU General Public License requirements will |
| 33 |
** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
| 34 |
** https://www.gnu.org/licenses/gpl-3.0.html. |
| 35 |
** |
| 36 |
** $QT_END_LICENSE$ |
| 37 |
** |
| 38 |
****************************************************************************/ |
| 39 |
|
| 40 |
#include "qservicemanager.h" |
| 41 |
#include "qserviceplugininterface.h" |
| 42 |
#include "qserviceinterfacedescriptor_p.h" |
| 43 |
#include "qremoteserviceregister_p.h" |
| 44 |
#include "qremoteserviceregisterentry_p.h" |
| 45 |
#include "qserviceoperations_p.h" |
| 46 |
#include "qservicereply.h" |
| 47 |
#include "qservicerequest_p.h" |
| 48 |
#include "qservicedebuglog_p.h" |
| 49 |
|
| 50 |
#include "databasemanager_p.h" |
| 51 |
|
| 52 |
#include <QObject> |
| 53 |
#include <QMetaMethod> |
| 54 |
#include <QPluginLoader> |
| 55 |
#include <QFile> |
| 56 |
#include <QCoreApplication> |
| 57 |
#include <QDir> |
| 58 |
#include <QSystemSemaphore> |
| 59 |
#include <QScopedPointer> |
| 60 |
|
| 61 |
QT_BEGIN_NAMESPACE |
| 62 |
|
| 63 |
QString QServiceManager::resolveLibraryPath(const QString &libNameOrPath) |
| 64 |
{ |
| 65 |
if (QFile::exists(libNameOrPath)) |
| 66 |
return libNameOrPath; |
| 67 |
|
| 68 |
// try to find plug-in via QLibrary |
| 69 |
QStringList paths = QCoreApplication::libraryPaths(); |
| 70 |
#ifdef QTM_PLUGIN_PATH |
| 71 |
paths << QLatin1String(QTM_PLUGIN_PATH)+QLatin1String("/serviceframework"); |
| 72 |
#endif |
| 73 |
for (int i=0; i<paths.count(); i++) { |
| 74 |
QString libPath = QDir::toNativeSeparators(paths[i]) + QDir::separator() + libNameOrPath; |
| 75 |
|
| 76 |
QLibrary lib(libPath); |
| 77 |
if (lib.load()) { |
| 78 |
lib.unload(); |
| 79 |
return lib.fileName(); |
| 80 |
} |
| 81 |
} |
| 82 |
return QString(); |
| 83 |
} |
| 84 |
|
| 85 |
class QServicePluginCleanup : public QObject |
| 86 |
{ |
| 87 |
Q_OBJECT |
| 88 |
public: |
| 89 |
QServicePluginCleanup(QPluginLoader *loader, QObject *parent = 0) |
| 90 |
: QObject(parent), |
| 91 |
m_loader(loader) |
| 92 |
{ |
| 93 |
} |
| 94 |
|
| 95 |
~QServicePluginCleanup() |
| 96 |
{ |
| 97 |
if (m_loader) { |
| 98 |
//m_loader->unload(); |
| 99 |
delete m_loader; |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
QPluginLoader *m_loader; |
| 104 |
}; |
| 105 |
|
| 106 |
class QServiceManagerPrivate : public QObject |
| 107 |
{ |
| 108 |
Q_OBJECT |
| 109 |
public: |
| 110 |
QServiceManager *manager; |
| 111 |
DatabaseManager *dbManager; |
| 112 |
QServiceOperations *ops; |
| 113 |
QService::Scope scope; |
| 114 |
QServiceManager::Error error; |
| 115 |
|
| 116 |
QServiceManagerPrivate(QServiceManager *parent = 0) |
| 117 |
: QObject(parent), |
| 118 |
manager(parent), |
| 119 |
dbManager(new DatabaseManager), |
| 120 |
ops(0) |
| 121 |
{ |
| 122 |
connect(dbManager, SIGNAL(serviceAdded(QString, DatabaseManager::DbScope)), |
| 123 |
SLOT(serviceAdded(QString, DatabaseManager::DbScope))); |
| 124 |
connect(dbManager, SIGNAL(serviceRemoved(QString, DatabaseManager::DbScope)), |
| 125 |
SLOT(serviceRemoved(QString, DatabaseManager::DbScope))); |
| 126 |
} |
| 127 |
|
| 128 |
~QServiceManagerPrivate() |
| 129 |
{ |
| 130 |
delete dbManager; |
| 131 |
} |
| 132 |
|
| 133 |
void setError(QServiceManager::Error err) |
| 134 |
{ |
| 135 |
if (error != err) |
| 136 |
{ |
| 137 |
error = err; |
| 138 |
manager->errorChanged(); |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
void setError() |
| 143 |
{ |
| 144 |
QServiceManager::Error prev = error; |
| 145 |
switch (dbManager->lastError().code()) { |
| 146 |
case DBError::NoError: |
| 147 |
error = QServiceManager::NoError; |
| 148 |
break; |
| 149 |
case DBError::DatabaseNotOpen: |
| 150 |
case DBError::InvalidDatabaseConnection: |
| 151 |
case DBError::CannotCreateDbDir: |
| 152 |
case DBError::CannotOpenServiceDb: |
| 153 |
case DBError::NoWritePermissions: |
| 154 |
case DBError::InvalidDatabaseFile: |
| 155 |
error = QServiceManager::StorageAccessError; |
| 156 |
break; |
| 157 |
case DBError::LocationAlreadyRegistered: |
| 158 |
error = QServiceManager::ServiceAlreadyExists; |
| 159 |
break; |
| 160 |
case DBError::IfaceImplAlreadyRegistered: |
| 161 |
error = QServiceManager::ImplementationAlreadyExists; |
| 162 |
break; |
| 163 |
case DBError::NotFound: |
| 164 |
error = QServiceManager::ComponentNotFound; |
| 165 |
break; |
| 166 |
case DBError::InvalidDescriptorScope: |
| 167 |
error = QServiceManager::InvalidServiceInterfaceDescriptor; |
| 168 |
break; |
| 169 |
case DBError::SqlError: |
| 170 |
case DBError::IfaceIDNotExternal: |
| 171 |
case DBError::ExternalIfaceIDFound: |
| 172 |
case DBError::UnknownError: |
| 173 |
error = QServiceManager::UnknownError; |
| 174 |
break; |
| 175 |
} |
| 176 |
if (prev != error) |
| 177 |
manager->errorChanged(); |
| 178 |
} |
| 179 |
|
| 180 |
private Q_SLOTS: |
| 181 |
void serviceAdded(const QString &service, DatabaseManager::DbScope dbScope) |
| 182 |
{ |
| 183 |
QService::Scope s = (dbScope == DatabaseManager::SystemScope ? |
| 184 |
QService::SystemScope : QService::UserScope); |
| 185 |
emit manager->serviceAdded(service, s); |
| 186 |
} |
| 187 |
|
| 188 |
void serviceRemoved(const QString &service, DatabaseManager::DbScope dbScope) |
| 189 |
{ |
| 190 |
QService::Scope s = (dbScope == DatabaseManager::SystemScope ? |
| 191 |
QService::SystemScope : QService::UserScope); |
| 192 |
emit manager->serviceRemoved(service, s); |
| 193 |
} |
| 194 |
}; |
| 195 |
|
| 196 |
/*! |
| 197 |
\class QServiceManager |
| 198 |
\ingroup servicefw |
| 199 |
\inmodule QtServiceFramework |
| 200 |
\brief The QServiceManager class enables the loading of service plugins |
| 201 |
and the (de)registration of services. |
| 202 |
|
| 203 |
A service is a stand-alone component that can be used by multiple clients. |
| 204 |
Each service implementation must derive from QObject. Clients request a |
| 205 |
reference to a service via \l loadInterface() or \l loadLocalTypedInterface(). |
| 206 |
|
| 207 |
Services are separate deliveries in the form of plug-ins. New services can be (de)registered |
| 208 |
at any time via \l addService() and \l removeService() respectively. Such an event is |
| 209 |
published via the \l serviceAdded() and \l serviceRemoved() signal. |
| 210 |
Each service plug-in must implement QServicePluginInterface. |
| 211 |
|
| 212 |
Each plug-in may support multiple interfaces and may even provide multiple implementations |
| 213 |
for the same interface. Individual implementations are identified via |
| 214 |
QServiceInterfaceDescriptor. For a more detailed explanation of services and how they relate to |
| 215 |
interface and their implementations please see QServiceInterfaceDescriptor. |
| 216 |
|
| 217 |
\sa QServicePluginInterface |
| 218 |
*/ |
| 219 |
|
| 220 |
/*! |
| 221 |
\enum QServiceManager::Error |
| 222 |
Defines the possible errors for the service manager. |
| 223 |
|
| 224 |
\value NoError No error occurred. |
| 225 |
\value StorageAccessError The service data storage is not accessible. This could be because the caller does not have the required permissions. |
| 226 |
\value InvalidServiceLocation The service was not found at its specified \l{QServiceInterfaceDescriptor::Location}{location}. |
| 227 |
\value InvalidServiceXml The XML defining the service metadata is invalid. |
| 228 |
\value InvalidServiceInterfaceDescriptor The service interface descriptor is invalid, or refers to an interface implementation that cannot be accessed in the current scope. |
| 229 |
\value ServiceAlreadyExists Another service has previously been registered with the same \l{QServiceInterfaceDescriptor::Location}{location}. |
| 230 |
\value ImplementationAlreadyExists Another service that implements the same interface version has previously been registered. |
| 231 |
\value PluginLoadingFailed The service plugin cannot be loaded. |
| 232 |
\value ComponentNotFound The service or interface implementation has not been registered. |
| 233 |
\value ServiceCapabilityDenied The security session does not permit service access based on its capabilities. |
| 234 |
\value UnknownError An unknown error occurred. |
| 235 |
*/ |
| 236 |
|
| 237 |
/*! |
| 238 |
\fn void QServiceManager::serviceAdded(const QString& serviceName, QService::Scope scope) |
| 239 |
|
| 240 |
This signal is emited whenever a new service with the given |
| 241 |
\a serviceName has been registered with the service manager. |
| 242 |
\a scope indicates where the service was added. |
| 243 |
|
| 244 |
If the manager scope is QService::SystemScope, it will not receive |
| 245 |
notifications about services added in the user scope. |
| 246 |
|
| 247 |
\sa addService() |
| 248 |
*/ |
| 249 |
|
| 250 |
/*! |
| 251 |
\fn void QServiceManager::serviceRemoved(const QString& serviceName, QService::Scope scope) |
| 252 |
|
| 253 |
This signal is emited whenever a service with the given |
| 254 |
\a serviceName has been deregistered with the service manager. |
| 255 |
\a scope indicates where the service was added. |
| 256 |
|
| 257 |
If the manager scope is QService::SystemScope, it will not receive |
| 258 |
notifications about services removed in the user scope. |
| 259 |
|
| 260 |
\sa removeService() |
| 261 |
*/ |
| 262 |
|
| 263 |
/*! |
| 264 |
Creates a service manager with the given \a parent. |
| 265 |
|
| 266 |
The scope will default to QService::UserScope. |
| 267 |
|
| 268 |
The service manager will also ensure that a background thread is started to handle |
| 269 |
service manager requests. If you need to supress this behavior so that all requests |
| 270 |
are handled in the foreground (in the main GUI thread) export the following environment |
| 271 |
variable: |
| 272 |
\code |
| 273 |
env QT_NO_SFW_BACKGROUND_OPERATION /path/to/my_sfw_app |
| 274 |
\endcode |
| 275 |
*/ |
| 276 |
QServiceManager::QServiceManager(QObject *parent) |
| 277 |
: QObject(parent), |
| 278 |
d(new QServiceManagerPrivate(this)) |
| 279 |
{ |
| 280 |
qRegisterMetaType<QService::UnrecoverableIPCError>("QService::UnrecoverableIPCError"); |
| 281 |
d->scope = QService::UserScope; |
| 282 |
} |
| 283 |
|
| 284 |
/*! |
| 285 |
Creates a service manager with the given \a scope and \a parent. |
| 286 |
*/ |
| 287 |
QServiceManager::QServiceManager(QService::Scope scope, QObject *parent) |
| 288 |
: QObject(parent), |
| 289 |
d(new QServiceManagerPrivate(this)) |
| 290 |
{ |
| 291 |
d->scope = scope; |
| 292 |
} |
| 293 |
|
| 294 |
/*! |
| 295 |
Destroys the service manager. |
| 296 |
*/ |
| 297 |
QServiceManager::~QServiceManager() |
| 298 |
{ |
| 299 |
if (d->ops) |
| 300 |
d->ops->disengage(); |
| 301 |
delete d; |
| 302 |
} |
| 303 |
|
| 304 |
/*! |
| 305 |
Returns the scope used for registering and searching of services. |
| 306 |
*/ |
| 307 |
QService::Scope QServiceManager::scope() const |
| 308 |
{ |
| 309 |
return d->scope; |
| 310 |
} |
| 311 |
|
| 312 |
/*! |
| 313 |
Returns a list of the services that provide the interface specified by |
| 314 |
\a interfaceName. If \a interfaceName is empty, this function returns |
| 315 |
a list of all available services in this manager's scope. |
| 316 |
*/ |
| 317 |
QStringList QServiceManager::findServices(const QString& interfaceName) const |
| 318 |
{ |
| 319 |
d->setError(QServiceManager::NoError); |
| 320 |
QStringList services; |
| 321 |
services = d->dbManager->getServiceNames(interfaceName, |
| 322 |
d->scope == QService::SystemScope ? DatabaseManager::SystemScope : DatabaseManager::UserScope); |
| 323 |
d->setError(); |
| 324 |
return services; |
| 325 |
} |
| 326 |
|
| 327 |
/*! |
| 328 |
Returns a list of the interfaces that match the specified \a filter. |
| 329 |
*/ |
| 330 |
QList<QServiceInterfaceDescriptor> QServiceManager::findInterfaces(const QServiceFilter& filter) const |
| 331 |
{ |
| 332 |
d->setError(QServiceManager::NoError); |
| 333 |
QList<QServiceInterfaceDescriptor> descriptors = d->dbManager->getInterfaces(filter, |
| 334 |
d->scope == QService::SystemScope ? DatabaseManager::SystemScope : DatabaseManager::UserScope); |
| 335 |
if (descriptors.isEmpty() && d->dbManager->lastError().code() != DBError::NoError) { |
| 336 |
d->setError(); |
| 337 |
return QList<QServiceInterfaceDescriptor>(); |
| 338 |
} |
| 339 |
return descriptors; |
| 340 |
} |
| 341 |
|
| 342 |
/*! |
| 343 |
Returns a list of the interfaces provided by the service named |
| 344 |
\a serviceName. If \a serviceName is empty, this function returns |
| 345 |
a list of all available interfaces in this manager's scope. |
| 346 |
*/ |
| 347 |
QList<QServiceInterfaceDescriptor> QServiceManager::findInterfaces(const QString& serviceName) const |
| 348 |
{ |
| 349 |
QServiceFilter filter; |
| 350 |
if (!serviceName.isEmpty()) |
| 351 |
filter.setServiceName(serviceName); |
| 352 |
return findInterfaces(filter); |
| 353 |
} |
| 354 |
|
| 355 |
/*! |
| 356 |
Loads and returns the interface specified by \a interfaceName. |
| 357 |
|
| 358 |
The caller takes ownership of the returned pointer. |
| 359 |
|
| 360 |
This function returns a null pointer if the requested service cannot be found. |
| 361 |
|
| 362 |
\sa setInterfaceDefault(), interfaceDefault() |
| 363 |
*/ |
| 364 |
QObject* QServiceManager::loadInterface(const QString& interfaceName) |
| 365 |
{ |
| 366 |
return loadInterface(interfaceDefault(interfaceName)); |
| 367 |
} |
| 368 |
|
| 369 |
/*! |
| 370 |
\internal |
| 371 |
Private helper function to get the QObject for an InterProcess service. |
| 372 |
*/ |
| 373 |
QObject *QServiceManager::loadInterProcessService(const QServiceInterfaceDescriptor &descriptor, |
| 374 |
const QString &serviceLocation) const |
| 375 |
{ |
| 376 |
//ipc service |
| 377 |
const int majorversion = descriptor.majorVersion(); |
| 378 |
const int minorversion = descriptor.minorVersion(); |
| 379 |
QString version = QString::number(majorversion) + QLatin1String(".") + QString::number(minorversion); |
| 380 |
|
| 381 |
QRemoteServiceRegister::Entry serviceEntry; |
| 382 |
serviceEntry.d->iface = descriptor.interfaceName(); |
| 383 |
serviceEntry.d->service = descriptor.serviceName(); |
| 384 |
serviceEntry.d->ifaceVersion = version; |
| 385 |
|
| 386 |
QObject* service = QRemoteServiceRegisterPrivate::proxyForService(serviceEntry, serviceLocation); |
| 387 |
if (!service) |
| 388 |
d->setError(QServiceManager::InvalidServiceLocation); |
| 389 |
|
| 390 |
//client owns proxy object |
| 391 |
return service; |
| 392 |
} |
| 393 |
|
| 394 |
/*! |
| 395 |
\internal |
| 396 |
Private helper function to get the QObject for an InProcess (plugin-based) service. |
| 397 |
*/ |
| 398 |
QObject *QServiceManager::loadInProcessService(const QServiceInterfaceDescriptor& descriptor, |
| 399 |
const QString &serviceFilePath) const |
| 400 |
{ |
| 401 |
QScopedPointer<QPluginLoader> loader(new QPluginLoader(serviceFilePath)); |
| 402 |
QObject *obj = 0; |
| 403 |
|
| 404 |
// pluginIFace is same for all service instances of the same plugin |
| 405 |
// calling loader->unload deletes pluginIFace automatically if no other |
| 406 |
// service instance is around |
| 407 |
QServicePluginInterface *pluginIFace = qobject_cast<QServicePluginInterface *>(loader->instance()); |
| 408 |
if (pluginIFace) { |
| 409 |
//check initialization first as the service may be a pre-registered one |
| 410 |
bool doLoading = true; |
| 411 |
QString serviceInitialized = descriptor.customAttribute(QLatin1String(SERVICE_INITIALIZED_ATTR)); |
| 412 |
if (!serviceInitialized.isEmpty() && (serviceInitialized == QLatin1String("NO"))) { |
| 413 |
// open/create the semaphore using the service's name as identifier |
| 414 |
QSystemSemaphore semaphore(descriptor.serviceName(), 1); |
| 415 |
if (semaphore.error() != QSystemSemaphore::NoError) { |
| 416 |
//try to create it |
| 417 |
semaphore.setKey(descriptor.serviceName(), 1, QSystemSemaphore::Create); |
| 418 |
} |
| 419 |
if (semaphore.error() == QSystemSemaphore::NoError && semaphore.acquire()) { |
| 420 |
pluginIFace->installService(); |
| 421 |
DatabaseManager::DbScope scope = d->scope == QService::UserScope ? |
| 422 |
DatabaseManager::UserOnlyScope : DatabaseManager::SystemScope; |
| 423 |
d->dbManager->serviceInitialized(descriptor.serviceName(), scope); |
| 424 |
semaphore.release(); |
| 425 |
} else { |
| 426 |
qWarning() << semaphore.errorString(); |
| 427 |
doLoading = false; |
| 428 |
|
| 429 |
} |
| 430 |
} |
| 431 |
if (doLoading) { |
| 432 |
obj = pluginIFace->createInstance(descriptor); |
| 433 |
if (obj) { |
| 434 |
QServicePluginCleanup *cleanup = new QServicePluginCleanup(loader.take()); |
| 435 |
QObject::connect(obj, SIGNAL(destroyed()), cleanup, SLOT(deleteLater())); |
| 436 |
} else { |
| 437 |
qWarning() << "Cannot create object instance for " |
| 438 |
<< descriptor.interfaceName() << ":" |
| 439 |
<< serviceFilePath; |
| 440 |
} |
| 441 |
} |
| 442 |
} else { |
| 443 |
qWarning() << "QServiceManager::loadInterface():" << serviceFilePath << loader->errorString(); |
| 444 |
} |
| 445 |
return obj; |
| 446 |
} |
| 447 |
|
| 448 |
/*! |
| 449 |
Loads and returns the interface specified by \a descriptor. |
| 450 |
|
| 451 |
The caller takes ownership of the returned pointer. |
| 452 |
|
| 453 |
This function returns a null pointer if the requested service cannot be found. |
| 454 |
|
| 455 |
\sa loadInterfaceRequest() |
| 456 |
*/ |
| 457 |
QObject* QServiceManager::loadInterface(const QServiceInterfaceDescriptor& descriptor) |
| 458 |
{ |
| 459 |
qServiceLog() << "class" << "QServiceManager" |
| 460 |
<< "event" << "loadInterface" |
| 461 |
<< "interface" << descriptor.interfaceName() |
| 462 |
<< "service" << descriptor.serviceName(); |
| 463 |
|
| 464 |
d->setError(QServiceManager::NoError); |
| 465 |
if (!descriptor.isValid()) { |
| 466 |
d->setError(QServiceManager::InvalidServiceInterfaceDescriptor); |
| 467 |
return 0; |
| 468 |
} |
| 469 |
|
| 470 |
QObject *obj = 0; |
| 471 |
int serviceType = descriptor.attribute(QServiceInterfaceDescriptor::ServiceType).toInt(); |
| 472 |
QString location = descriptor.attribute(QServiceInterfaceDescriptor::Location).toString(); |
| 473 |
|
| 474 |
if (serviceType == QService::InterProcess) |
| 475 |
{ |
| 476 |
return loadInterProcessService(descriptor, location); |
| 477 |
} |
| 478 |
else |
| 479 |
{ |
| 480 |
QString serviceFilePath = resolveLibraryPath(location); |
| 481 |
if (serviceFilePath.isEmpty()) |
| 482 |
{ |
| 483 |
d->setError(QServiceManager::InvalidServiceLocation); |
| 484 |
return 0; |
| 485 |
} |
| 486 |
|
| 487 |
obj = loadInProcessService(descriptor, serviceFilePath); |
| 488 |
if (!obj) |
| 489 |
d->setError(QServiceManager::PluginLoadingFailed); |
| 490 |
} |
| 491 |
return obj; |
| 492 |
} |
| 493 |
|
| 494 |
/*! |
| 495 |
\fn QServiceReply *loadInterfaceRequest(const QString &interfaceName) |
| 496 |
Initiate a background request to load the interface specified by \a interfaceName, and |
| 497 |
return a QServiceReply object to track the results of the request. |
| 498 |
*/ |
| 499 |
|
| 500 |
/*! |
| 501 |
\fn QServiceReply *loadInterfaceRequest(const QServiceInterfaceDescriptor& descriptor) |
| 502 |
Initiate a background request to load the interface specified by \a descriptor, and |
| 503 |
return a QServiceReply object to track the results of the request. |
| 504 |
*/ |
| 505 |
|
| 506 |
|
| 507 |
/*! |
| 508 |
\fn QServiceReplyTyped<T> *loadLocalTypedInterfaceRequest(const QString& interfaceName) |
| 509 |
Initiate a background request to load the interface specified by \a interfaceName, and |
| 510 |
return a QServiceReplyTyped object to track the results of the request. |
| 511 |
\code |
| 512 |
// a member variable to track the reply |
| 513 |
QServiceReply<SomeKnownService> *m_reply; |
| 514 |
|
| 515 |
// make the request |
| 516 |
m_reply = *mgr->loadLocalTypedInterfaceRequest<SomeKnownService>(sksIfaceName); |
| 517 |
connect(m_reply, SIGNAL(finished()), this, SLOT(handleServiceInfo)); |
| 518 |
|
| 519 |
// ...later, in the handler |
| 520 |
SomeKnownService *svc = m_reply->proxyObject(); |
| 521 |
\endcode |
| 522 |
*/ |
| 523 |
|
| 524 |
/*! |
| 525 |
\fn QServiceReplyTyped<T> *loadLocalTypedInterfaceRequest(const QServiceInterfaceDescriptor& descriptor) |
| 526 |
Initiate a background request to load the interface specified by \a interfaceName, and |
| 527 |
return a QServiceReplyTyped object to track the results of the request. |
| 528 |
*/ |
| 529 |
|
| 530 |
/*! |
| 531 |
\internal |
| 532 |
Initiate a background request to load and return the interface specified by \a interfaceName, |
| 533 |
and using the given \a reply object, which (if non-null) will be a typed sub-class object. |
| 534 |
If it is null a default QServiceReply is constructed. |
| 535 |
*/ |
| 536 |
QServiceReply *QServiceManager::loadInterfaceRequest(const QString &interfaceName) |
| 537 |
{ |
| 538 |
QServiceReply *reply = new QServiceReply; |
| 539 |
|
| 540 |
if (!qgetenv("QT_NO_SFW_BACKGROUND_OPERATION").isEmpty()) |
| 541 |
{ |
| 542 |
qWarning("Turning off sfw background operations as requested."); |
| 543 |
return 0; |
| 544 |
} |
| 545 |
|
| 546 |
if (!d->ops) { |
| 547 |
d->ops = QServiceOperations::instance(); |
| 548 |
d->ops->engage(); |
| 549 |
} |
| 550 |
|
| 551 |
reply->setRequest(interfaceName); |
| 552 |
|
| 553 |
QServiceRequest req(interfaceName); |
| 554 |
req.setReply(reply); |
| 555 |
req.setScope(scope()); |
| 556 |
d->ops->initiateRequest(req); |
| 557 |
|
| 558 |
return reply; |
| 559 |
} |
| 560 |
|
| 561 |
/*! |
| 562 |
\internal |
| 563 |
Initiate a background request to load and return the interface specified by \a descriptor, |
| 564 |
and using the given \a reply object, which (if non-null) will be a typed sub-class object. |
| 565 |
If it is null a default QServiceReply is constructed. |
| 566 |
*/ |
| 567 |
QServiceReply *QServiceManager::loadInterfaceRequest(const QServiceInterfaceDescriptor &descriptor) |
| 568 |
{ |
| 569 |
QServiceReply *reply = new QServiceReply(); |
| 570 |
|
| 571 |
if (!d->ops) { |
| 572 |
d->ops = QServiceOperations::instance(); |
| 573 |
d->ops->engage(); |
| 574 |
} |
| 575 |
|
| 576 |
reply->setRequest(descriptor.interfaceName()); |
| 577 |
|
| 578 |
QServiceRequest req(descriptor); |
| 579 |
req.setReply(reply); |
| 580 |
req.setScope(scope()); |
| 581 |
d->ops->initiateRequest(req); |
| 582 |
|
| 583 |
return reply; |
| 584 |
} |
| 585 |
|
| 586 |
|
| 587 |
/*! |
| 588 |
If \a interfaceName is an out of process service this verifies the |
| 589 |
interface is running. If the service is in process this function |
| 590 |
always returns false. |
| 591 |
|
| 592 |
Use this function to verify the interface requested is running. This |
| 593 |
is useful is you only want to call loadInterface when the service |
| 594 |
is already running. This call does not guarantee that the service |
| 595 |
will remain running, as such a race condition exists if the service |
| 596 |
quits between this call being made and loadInterface being called. |
| 597 |
|
| 598 |
If the service can not be fount this returns false. |
| 599 |
|
| 600 |
\sa setInterfaceDefault(), interfaceDefault(), loadInterface() |
| 601 |
*/ |
| 602 |
bool QServiceManager::isInterfaceRunning(const QString& interfaceName) |
| 603 |
{ |
| 604 |
return isInterfaceRunning(interfaceDefault(interfaceName)); |
| 605 |
} |
| 606 |
|
| 607 |
/*! |
| 608 |
If \a descriptor is an out of process service this verifies the |
| 609 |
service is running. If the service is in process this function |
| 610 |
always returns false. |
| 611 |
|
| 612 |
Use this function to verify the interface requested is running. This |
| 613 |
is useful is you only want to call loadInterface when the service |
| 614 |
is already running. This call does not guarantee that the service |
| 615 |
will remain running, as such a race condition exists if the service |
| 616 |
quits between this call being made and loadInterface being called. |
| 617 |
|
| 618 |
If the service can not be found this returns false. Error is set |
| 619 |
if an error occurs. |
| 620 |
|
| 621 |
*/ |
| 622 |
bool QServiceManager::isInterfaceRunning(const QServiceInterfaceDescriptor& descriptor) |
| 623 |
{ |
| 624 |
d->setError(NoError); |
| 625 |
if (!descriptor.isValid()) { |
| 626 |
d->setError(InvalidServiceInterfaceDescriptor); |
| 627 |
return false; |
| 628 |
} |
| 629 |
|
| 630 |
const QString location = descriptor.attribute(QServiceInterfaceDescriptor::Location).toString(); |
| 631 |
const bool isInterProcess = (descriptor.attribute(QServiceInterfaceDescriptor::ServiceType).toInt() |
| 632 |
== QService::InterProcess); |
| 633 |
if (isInterProcess) { |
| 634 |
//ipc service |
| 635 |
const int majorversion = descriptor.majorVersion(); |
| 636 |
const int minorversion = descriptor.minorVersion(); |
| 637 |
QString version = QString::number(majorversion) + QLatin1String(".") + QString::number(minorversion); |
| 638 |
|
| 639 |
QRemoteServiceRegister::Entry serviceEntry; |
| 640 |
serviceEntry.d->iface = descriptor.interfaceName(); |
| 641 |
serviceEntry.d->service = descriptor.serviceName(); |
| 642 |
serviceEntry.d->ifaceVersion = version; |
| 643 |
|
| 644 |
return QRemoteServiceRegisterPrivate::isServiceRunning(serviceEntry, location); |
| 645 |
} |
| 646 |
|
| 647 |
return false; |
| 648 |
} |
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
/*! |
| 653 |
\fn T* QServiceManager::loadLocalTypedInterface(const QString& interfaceName) |
| 654 |
|
| 655 |
Loads the service object implementing \a interfaceName, |
| 656 |
as provided by the default service for this interface. |
| 657 |
The template class must be derived from QObject. |
| 658 |
|
| 659 |
If \a interfaceName is not a known interface the returned pointer will be null. |
| 660 |
|
| 661 |
Note that using this function implies that service and client share |
| 662 |
the implementation of T which means that service and client become tightly coupled. |
| 663 |
This may cause issue during later updates as certain changes may require code changes |
| 664 |
to the service and client. |
| 665 |
|
| 666 |
The caller takes ownership of the returned pointer. |
| 667 |
|
| 668 |
\sa setInterfaceDefault(), interfaceDefault() |
| 669 |
*/ |
| 670 |
|
| 671 |
|
| 672 |
/*! |
| 673 |
\fn T* QServiceManager::loadLocalTypedInterface(const QServiceInterfaceDescriptor& serviceDescriptor) |
| 674 |
|
| 675 |
Loads the service object identified by \a serviceDescriptor. |
| 676 |
The template class must be derived from QObject. |
| 677 |
|
| 678 |
If the \a serviceDescriptor is not valid the returned pointer will be null. |
| 679 |
|
| 680 |
Note that using this function implies that service and client share |
| 681 |
the implementation of T which means that service and client become tightly coupled. |
| 682 |
This may cause issue during later updates as certain changes may require code changes |
| 683 |
to the service and client. |
| 684 |
|
| 685 |
The caller takes ownership of the returned pointer. |
| 686 |
|
| 687 |
*/ |
| 688 |
|
| 689 |
/*! |
| 690 |
Registers the service defined by the XML file at \a xmlFilePath. |
| 691 |
Returns true if the registration succeeded, and false otherwise. |
| 692 |
|
| •693 |
If a previously unknown interface is added the newly registered service automatically |
| 694 |
becomes the new default service provider for the new interface. |
| 695 |
|
| 696 |
A service plugin cannot be added if another service is already registered |
| 697 |
with the same plugin file path. A service plugin also cannot be added if |
| 698 |
the service is already registered and implements any of the same interface |
| 699 |
versions that the new plugin implements. |
| 700 |
|
| 701 |
\sa removeService(), setInterfaceDefault() |
| 702 |
*/ |
| 703 |
bool QServiceManager::addService(const QString& xmlFilePath) |
| 704 |
{ |
| 705 |
QFile *f = new QFile(xmlFilePath); |
| 706 |
bool b = addService(f); |
| 707 |
delete f; |
| 708 |
return b; |
| 709 |
} |
| 710 |
|
| 711 |
/*! |
| 712 |
Registers the service defined by the XML data from the given \a device. |
| 713 |
Returns true if the registration succeeded, and false otherwise. If a |
| •714 |
previously unknown interface is added the newly registered service |
| 715 |
automatically becomes the new default service provider for the new |
| 716 |
interface. |
| 717 |
|
| 718 |
Registering a service also causes QServicePluginInterface::installService() |
| 719 |
to be called on the service. If the service plugin is not accessible |
| 720 |
(e.g. if the plugin file is not found) and \c installService() cannot |
| 721 |
be invoked on the service, the registration fails and this method returns |
| 722 |
false. |
| 723 |
|
| 724 |
A service plugin cannot be added if another service is already registered |
| 725 |
with the same plugin file path. A service plugin also cannot be added if |
| 726 |
the service is already registered and implements any of the same interface |
| 727 |
versions that the new plugin implements. |
| 728 |
|
| 729 |
Services are always added based on the \l scope() of the current |
| 730 |
service manager instance. |
| 731 |
|
| 732 |
\sa removeService(), setInterfaceDefault() |
| 733 |
*/ |
| 734 |
bool QServiceManager::addService(QIODevice *device) |
| 735 |
{ |
| 736 |
d->setError(QServiceManager::NoError); |
| 737 |
ServiceMetaData parser(device); |
| 738 |
if (!parser.extractMetadata()) { |
| 739 |
d->setError(QServiceManager::InvalidServiceXml); |
| 740 |
return false; |
| 741 |
} |
| 742 |
const ServiceMetaDataResults data = parser.parseResults(); |
| 743 |
|
| 744 |
DatabaseManager::DbScope scope = d->scope == QService::UserScope ? |
| 745 |
DatabaseManager::UserOnlyScope : DatabaseManager::SystemScope; |
| 746 |
ServiceMetaDataResults results = parser.parseResults(); |
| 747 |
|
| 748 |
bool result = d->dbManager->registerService(results, scope); |
| 749 |
|
| 750 |
if (results.type == QService::InterProcess) |
| 751 |
return result; |
| 752 |
|
| 753 |
//test the new plug-in |
| 754 |
if (result) { |
| 755 |
QPluginLoader *loader = new QPluginLoader(resolveLibraryPath(data.location)); |
| 756 |
QServicePluginInterface *pluginIFace = qobject_cast<QServicePluginInterface *>(loader->instance()); |
| 757 |
if (pluginIFace) { |
| 758 |
pluginIFace->installService(); |
| 759 |
} else { |
| 760 |
d->setError(QServiceManager::PluginLoadingFailed); |
| 761 |
result = false; |
| 762 |
qWarning() << "QServiceManager::addService()" << data.location << "->" |
| 763 |
<< resolveLibraryPath(data.location) << ":" |
| 764 |
<< loader->errorString() << " - Aborting registration"; |
| 765 |
d->dbManager->unregisterService(data.name, scope); |
| 766 |
} |
| 767 |
//loader->unload(); |
| 768 |
delete loader; |
| 769 |
} else { |
| 770 |
d->setError(); |
| 771 |
} |
| 772 |
|
| 773 |
return result; |
| 774 |
} |
| 775 |
|
| 776 |
/*! |
| 777 |
Unregisters the service specified by \a serviceName. |
| 778 |
|
| 779 |
Returns true if the unregistration succeeded, and false otherwise. |
| 780 |
|
| 781 |
If a default service implementation is removed and there are other implementations |
| 782 |
for the same interface, the service manager chooses the implementation with the |
| 783 |
highest version number as the new default. If there is more than one serivce |
| 784 |
with the same version number, the service manager makes a random choice with |
| 785 |
regards to the new default implementation. If this is |
| 786 |
not the desired behaviour the default selection should be updated |
| 787 |
via setInterfaceDefault(). |
| 788 |
|
| 789 |
Services are always removed based on the \l scope() of the current |
| 790 |
service manager instance. |
| 791 |
|
| 792 |
\sa addService() |
| 793 |
*/ |
| 794 |
bool QServiceManager::removeService(const QString& serviceName) |
| 795 |
{ |
| 796 |
d->setError(QServiceManager::NoError); |
| 797 |
if (serviceName.isEmpty()) { |
| 798 |
d->setError(QServiceManager::ComponentNotFound); |
| 799 |
return false; |
| 800 |
} |
| 801 |
|
| 802 |
// Call QServicePluginInterface::uninstallService() on all plugins that |
| 803 |
// match this service |
| 804 |
|
| 805 |
QSet<QString> pluginPathsSet; |
| 806 |
QList<QServiceInterfaceDescriptor> descriptors = findInterfaces(serviceName); |
| 807 |
for (int i=0; i<descriptors.count(); i++) { |
| 808 |
const QString loc = descriptors[i].attribute(QServiceInterfaceDescriptor::Location).toString(); |
| 809 |
const int type = descriptors[i].attribute(QServiceInterfaceDescriptor::ServiceType).toInt(); |
| 810 |
//exclude ipc services |
| 811 |
if (type <= QService::Plugin) |
| 812 |
pluginPathsSet << loc; |
| 813 |
} |
| 814 |
|
| 815 |
QList<QString> pluginPaths = pluginPathsSet.toList(); |
| 816 |
for (int i=0; i<pluginPaths.count(); i++) { |
| 817 |
QPluginLoader *loader = new QPluginLoader(resolveLibraryPath(pluginPaths[i])); |
| 818 |
QServicePluginInterface *pluginIFace = qobject_cast<QServicePluginInterface *>(loader->instance()); |
| 819 |
if (pluginIFace) |
| 820 |
pluginIFace->uninstallService(); |
| 821 |
else |
| 822 |
qWarning() << "QServiceManager: unable to invoke uninstallService() on removed service"; |
| 823 |
//loader->unload(); |
| 824 |
delete loader; |
| 825 |
} |
| 826 |
|
| 827 |
if (!d->dbManager->unregisterService(serviceName, d->scope == QService::UserScope ? |
| 828 |
DatabaseManager::UserOnlyScope : DatabaseManager::SystemScope)) { |
| 829 |
d->setError(); |
| 830 |
return false; |
| 831 |
} |
| 832 |
return true; |
| 833 |
} |
| 834 |
|
| 835 |
/*! |
| 836 |
Sets the default interface implementation for \a interfaceName to the |
| 837 |
matching interface implementation provided by \a service. |
| 838 |
|
| 839 |
If \a service provides more than one interface implementation for |
| 840 |
\a interfaceName, the newest version of the interface is set as the |
| 841 |
default. |
| 842 |
|
| 843 |
Returns true if the operation succeeded, and false otherwise. |
| 844 |
|
| 845 |
\b {Note:} When in system scope, the \a service must be a system-wide |
| 846 |
service rather than a user-specific service; otherwise, this will fail. |
| 847 |
*/ |
| 848 |
bool QServiceManager::setInterfaceDefault(const QString &service, const QString &interfaceName) |
| 849 |
{ |
| 850 |
d->setError(QServiceManager::NoError); |
| 851 |
if (service.isEmpty() || interfaceName.isEmpty()) { |
| 852 |
d->setError(QServiceManager::ComponentNotFound); |
| 853 |
return false; |
| 854 |
} |
| 855 |
DatabaseManager::DbScope scope = d->scope == QService::SystemScope ? |
| 856 |
DatabaseManager::SystemScope : DatabaseManager::UserScope; |
| 857 |
if (!d->dbManager->setInterfaceDefault(service, interfaceName, scope)) { |
| 858 |
d->setError(); |
| 859 |
return false; |
| 860 |
} |
| 861 |
return true; |
| 862 |
} |
| 863 |
|
| 864 |
/*! |
| 865 |
\overload |
| 866 |
|
| 867 |
Sets the interface implementation specified by \a descriptor to be the |
| 868 |
default implementation for the particular interface specified in the |
| 869 |
descriptor. |
| 870 |
|
| 871 |
Returns true if the operation succeeded, and false otherwise. |
| 872 |
|
| 873 |
\b {Note:} When in system scope, the \a descriptor must refer to a |
| 874 |
system-wide service rather than a user-specific service; otherwise, this |
| 875 |
will fail. |
| 876 |
*/ |
| 877 |
bool QServiceManager::setInterfaceDefault(const QServiceInterfaceDescriptor& descriptor) |
| 878 |
{ |
| 879 |
d->setError(QServiceManager::NoError); |
| 880 |
DatabaseManager::DbScope scope = d->scope == QService::SystemScope ? |
| 881 |
DatabaseManager::SystemScope : DatabaseManager::UserScope; |
| 882 |
if (!d->dbManager->setInterfaceDefault(descriptor, scope)) { |
| 883 |
d->setError(); |
| 884 |
return false; |
| 885 |
} |
| 886 |
return true; |
| 887 |
} |
| 888 |
|
| 889 |
/*! |
| 890 |
Returns the default interface implementation for the given \a interfaceName. |
| 891 |
*/ |
| 892 |
QServiceInterfaceDescriptor QServiceManager::interfaceDefault(const QString& interfaceName) const |
| 893 |
{ |
| 894 |
qDebug() << "QServiceManager::interfaceDefault" << interfaceName; |
| 895 |
d->setError(QServiceManager::NoError); |
| 896 |
DatabaseManager::DbScope scope = d->scope == QService::SystemScope ? |
| 897 |
DatabaseManager::SystemScope : DatabaseManager::UserScope; |
| 898 |
QServiceInterfaceDescriptor info = d->dbManager->interfaceDefault(interfaceName, scope); |
| 899 |
if (d->dbManager->lastError().code() != DBError::NoError) { |
| 900 |
d->setError(); |
| 901 |
qDebug() << "error" << d->dbManager->lastError().text(); |
| 902 |
return QServiceInterfaceDescriptor(); |
| 903 |
} |
| 904 |
return info; |
| 905 |
} |
| 906 |
|
| 907 |
/*! |
| 908 |
Returns the type of error that last occurred. |
| 909 |
*/ |
| 910 |
QServiceManager::Error QServiceManager::error() const |
| 911 |
{ |
| 912 |
return d->error; |
| 913 |
} |
| 914 |
|
| 915 |
/*! |
| 916 |
\internal |
| 917 |
*/ |
| 918 |
void QServiceManager::connectNotify(const QMetaMethod &signal) |
| 919 |
{ |
| 920 |
static const QMetaMethod serviceAddedSignal = QMetaMethod::fromSignal(&QServiceManager::serviceAdded); |
| 921 |
static const QMetaMethod serviceRemovedSignal = QMetaMethod::fromSignal(&QServiceManager::serviceRemoved); |
| 922 |
if (signal == serviceAddedSignal |
| 923 |
|| signal == serviceRemovedSignal) { |
| 924 |
if (d->scope != QService::SystemScope) |
| 925 |
d->dbManager->setChangeNotificationsEnabled(DatabaseManager::UserScope, true); |
| 926 |
d->dbManager->setChangeNotificationsEnabled(DatabaseManager::SystemScope, true); |
| 927 |
} |
| 928 |
} |
| 929 |
|
| 930 |
/*! |
| 931 |
\internal |
| 932 |
*/ |
| 933 |
void QServiceManager::disconnectNotify(const QMetaMethod &signal) |
| 934 |
{ |
| 935 |
static const QMetaMethod serviceAddedSignal = QMetaMethod::fromSignal(&QServiceManager::serviceAdded); |
| 936 |
static const QMetaMethod serviceRemovedSignal = QMetaMethod::fromSignal(&QServiceManager::serviceRemoved); |
| 937 |
if (signal == serviceAddedSignal |
| 938 |
|| signal == serviceRemovedSignal) { |
| 939 |
if (!isSignalConnected(serviceAddedSignal) |
| 940 |
&& !isSignalConnected(serviceRemovedSignal)) { |
| 941 |
if (d->scope != QService::SystemScope) |
| 942 |
d->dbManager->setChangeNotificationsEnabled(DatabaseManager::UserScope, false); |
| 943 |
d->dbManager->setChangeNotificationsEnabled(DatabaseManager::SystemScope, false); |
| 944 |
} |
| 945 |
} |
| 946 |
} |
| 947 |
|
| 948 |
bool QServiceManager::event(QEvent *e) |
| 949 |
{ |
| 950 |
if (e->type() == QEvent::ThreadChange) { |
| 951 |
qWarning() << "QServiceManager CANNOT BE MOVED THREADS!"; |
| 952 |
} |
| 953 |
|
| 954 |
return QObject::event(e); |
| 955 |
} |
| 956 |
|
| 957 |
#include "moc_qservicemanager.cpp" |
| 958 |
#include "qservicemanager.moc" |
| 959 |
QT_END_NAMESPACE |
| 960 |
|