service/NativeDocumentService.h
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #pragma once | ||
| 2 | |||
| 3 | #include "NativeMailDocument.h" | ||
| 4 | #include "NativeImageResources.h" | ||
| 5 | #include "NativeDocumentCache.h" | ||
| 6 | #include <QObject> | ||
| 7 | #include <QCache> | ||
| 8 | #include <QFont> | ||
| 9 | #include <QPointer> | ||
| 10 | #include <QThreadPool> | ||
| 11 | #include <functional> | ||
| 12 | #include <memory> | ||
| 13 | #include <optional> | ||
| 14 | |||
| 15 | // One application-owned CPU worker; at most eight pending visible consumers. | ||
| 16 | // Cancellation never invokes a callback against a destroyed consumer. | ||
| 17 | class NativeDocumentService : public QObject { | ||
| 18 | public: | ||
| 19 | using Callback = std::function<void(NativeMailDocument)>; | ||
| 20 | explicit NativeDocumentService(QObject *parent = nullptr, QString cacheDirectory = {}); | ||
| 21 | ~NativeDocumentService() override; | ||
| 22 | void prepare(QObject *owner, const QByteArray &html, Callback callback, | ||
| 23 |
5/6✓ Branch 15 → 16 taken 1 time.
✓ Branch 21 → 22 taken 1 time.
✓ Branch 31 → 32 taken 1 time.
✓ Branch 33 → 34 taken 1 time.
✗ Branch 33 → 35 not taken.
✓ Branch 39 → 40 taken 1 time.
|
2183 | NativeImageContext images = {}, std::optional<QFont> displayFont = std::nullopt); |
| 24 | // Large decoded HTML stays as a shared QString until the worker encodes it. | ||
| 25 | void prepareDecodedHtml(QObject *owner, const QString &html, Callback callback, | ||
| 26 | NativeImageContext images = {}, | ||
| 27 | std::optional<QFont> displayFont = std::nullopt, | ||
| 28 | QByteArray htmlDigest = {}); | ||
| 29 | void cancel(QObject *owner); | ||
| 30 | static NativeDocumentService *shared(); | ||
| 31 | |||
| 32 | private: | ||
| 33 | friend class TestNativeMailDocument; | ||
| 34 | struct Request { | ||
| 35 | QPointer<QObject> owner; | ||
| 36 | QByteArray input; | ||
| 37 | QString decodedInput; | ||
| 38 | Callback callback; | ||
| 39 | NativeImageContext images; | ||
| 40 | QFont defaultFont; | ||
| 41 | QByteArray cacheKey; | ||
| 42 | QByteArray preparedKey; | ||
| 43 | std::shared_ptr<std::atomic_bool> cancelled = std::make_shared<std::atomic_bool>(false); | ||
| 44 | }; | ||
| 45 | std::optional<NativeMailDocument> preparedResult(const Request &request); | ||
| 46 | void enqueue(Request request); | ||
| 47 | void startNext(); | ||
| 48 | void stop(); | ||
| 49 | bool m_stopping = false; | ||
| 50 | QThreadPool m_pool; | ||
| 51 | std::unique_ptr<NativeDocumentCache> m_cache; | ||
| 52 | // Service-thread-owned LRU. Hot visible reads bypass the CPU worker. | ||
| 53 | // Charges conservatively include text, nodes and Qt | ||
| 54 | // structure overhead; retained consumer fragments are measured separately. | ||
| 55 | QCache<QByteArray, NativeMailDocument> m_prepared{8 * 1024 * 1024}; | ||
| 56 | QList<Request> m_pending; | ||
| 57 | std::optional<Request> m_active; | ||
| 58 | }; | ||
| 59 |