| 1 | // Copyright (C) 2020 The Qt Company Ltd. |
| 2 | // Copyright (C) 2021 Intel Corporation. |
| 3 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 4 | |
| 5 | #ifndef QLIBRARY_P_H |
| 6 | #define QLIBRARY_P_H |
| 7 | |
| 8 | // |
| 9 | // W A R N I N G |
| 10 | // ------------- |
| 11 | // |
| 12 | // This file is not part of the Qt API. It exists for the convenience |
| 13 | // of the QLibrary class. This header file may change from |
| 14 | // version to version without notice, or even be removed. |
| 15 | // |
| 16 | // We mean it. |
| 17 | // |
| 18 | |
| 19 | #include "QtCore/qlibrary.h" |
| 20 | |
| 21 | #include "QtCore/private/qfactoryloader_p.h" |
| 22 | #include "QtCore/qloggingcategory.h" |
| 23 | #include "QtCore/qmutex.h" |
| 24 | #include "QtCore/qplugin.h" |
| 25 | #include "QtCore/qpointer.h" |
| 26 | #include "QtCore/qstringlist.h" |
| 27 | #ifdef Q_OS_WIN |
| 28 | # include "QtCore/qt_windows.h" |
| 29 | #endif |
| 30 | |
| 31 | #include <memory> |
| 32 | |
| 33 | QT_REQUIRE_CONFIG(library); |
| 34 | |
| 35 | QT_BEGIN_NAMESPACE |
| 36 | |
| 37 | Q_DECLARE_LOGGING_CATEGORY(qt_lcDebugPlugins) |
| 38 | |
| 39 | struct QLibraryScanResult |
| 40 | { |
| 41 | qsizetype pos; |
| 42 | qsizetype length; |
| 43 | #if defined(Q_OF_MACH_O) |
| 44 | bool isEncrypted = false; |
| 45 | #endif |
| 46 | }; |
| 47 | |
| 48 | class QLibraryStore; |
| 49 | class QLibraryPrivate |
| 50 | { |
| 51 | public: |
| 52 | #ifdef Q_OS_WIN |
| 53 | using Handle = HINSTANCE; |
| 54 | #else |
| 55 | using Handle = void *; |
| 56 | #endif |
| 57 | enum UnloadFlag { UnloadSys, NoUnloadSys }; |
| 58 | |
| 59 | struct Deleter { |
| 60 | // QLibraryPrivate::release() is not, yet, and cannot easily be made, noexcept: |
| 61 | void operator()(QLibraryPrivate *p) const { p->release(); } |
| 62 | }; |
| 63 | using UniquePtr = std::unique_ptr<QLibraryPrivate, Deleter>; |
| 64 | |
| 65 | const QString fileName; |
| 66 | const QString fullVersion; |
| 67 | |
| 68 | bool load(); |
| 69 | QtPluginInstanceFunction loadPlugin(); // loads and resolves instance |
| 70 | bool unload(UnloadFlag flag = UnloadSys); |
| 71 | void release(); |
| 72 | QFunctionPointer resolve(const char *); |
| 73 | |
| 74 | QLibrary::LoadHints loadHints() const |
| 75 | { return QLibrary::LoadHints(loadHintsInt.loadRelaxed()); } |
| 76 | void setLoadHints(QLibrary::LoadHints lh); |
| 77 | QObject *pluginInstance(); |
| 78 | |
| 79 | static QLibraryPrivate *findOrCreate(const QString &fileName, const QString &version = QString(), |
| 80 | QLibrary::LoadHints loadHints = { }); |
| 81 | static QStringList suffixes_sys(const QString &fullVersion); |
| 82 | static QStringList prefixes_sys(); |
| 83 | |
| 84 | QAtomicPointer<std::remove_pointer<QtPluginInstanceFunction>::type> instanceFactory; |
| 85 | QAtomicPointer<std::remove_pointer<Handle>::type> pHnd; |
| 86 | |
| 87 | // the mutex protects the fields below |
| 88 | QMutex mutex; |
| 89 | QPointer<QObject> inst; // used by QFactoryLoader |
| 90 | QPluginParsedMetaData metaData; |
| 91 | QString errorString; |
| 92 | QString qualifiedFileName; |
| 93 | |
| 94 | void updatePluginState(); |
| 95 | bool isPlugin(); |
| 96 | |
| 97 | private: |
| 98 | explicit QLibraryPrivate(const QString &canonicalFileName, const QString &version, QLibrary::LoadHints loadHints); |
| 99 | ~QLibraryPrivate(); |
| 100 | void mergeLoadHints(QLibrary::LoadHints loadHints); |
| 101 | |
| 102 | bool load_sys(); |
| 103 | bool unload_sys(); |
| 104 | QFunctionPointer resolve_sys(const char *); |
| 105 | |
| 106 | QAtomicInt loadHintsInt; |
| 107 | |
| 108 | /// counts how many QLibrary or QPluginLoader are attached to us, plus 1 if it's loaded |
| 109 | QAtomicInt libraryRefCount; |
| 110 | /// counts how many times load() or loadPlugin() were called |
| 111 | QAtomicInt libraryUnloadCount; |
| 112 | |
| 113 | enum { IsAPlugin, IsNotAPlugin, MightBeAPlugin } pluginState; |
| 114 | friend class QLibraryStore; |
| 115 | }; |
| 116 | |
| 117 | QT_END_NAMESPACE |
| 118 | |
| 119 | #endif // QLIBRARY_P_H |
| 120 | |