summaryrefslogtreecommitdiffstats
path: root/src/webenginequick
diff options
context:
space:
mode:
authorPeter Varga <pvarga@inf.u-szeged.hu>2020-06-19 11:17:50 +0200
committerQt Cherry-pick Bot <cherrypick_bot@qt-project.org>2021-06-28 11:52:05 +0000
commitd411328f2a7ff9993bcce5b1db74280a39c90981 (patch)
tree2b9ccabfa204ce5796c20e8f90c99b577e1a7154 /src/webenginequick
parent45099f1e9e51aeec2266c46b63fa1ecf8670be5a (diff)
Add API for favicon database
[ChangeLog][QtWebEngineCore][QWebEngineProfile] Add new API to access icon database asynchronously. [ChangeLog][QtWebEngineQuick] image:/favicon/ URLs now can be used to access icon database. Task-number: QTBUG-51184 Change-Id: I6096ad9a4210670ed59458c4fa099a02595e8a1e Reviewed-by: Allan Sandfeld Jensen <allan.jensen@qt.io> (cherry picked from commit 2ad450018e8ae22f4c426a421fa5c0995feb1e16) Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
Diffstat (limited to 'src/webenginequick')
-rw-r--r--src/webenginequick/api/qquickwebenginefaviconprovider.cpp179
-rw-r--r--src/webenginequick/api/qquickwebenginefaviconprovider_p_p.h50
-rw-r--r--src/webenginequick/api/qquickwebengineprofile.h1
-rw-r--r--src/webenginequick/api/qquickwebengineview_p.h3
-rw-r--r--src/webenginequick/doc/src/qtwebengine-features.qdoc39
-rw-r--r--src/webenginequick/doc/src/webengineview_lgpl.qdoc12
6 files changed, 250 insertions, 34 deletions
diff --git a/src/webenginequick/api/qquickwebenginefaviconprovider.cpp b/src/webenginequick/api/qquickwebenginefaviconprovider.cpp
index 23397003e..d19954620 100644
--- a/src/webenginequick/api/qquickwebenginefaviconprovider.cpp
+++ b/src/webenginequick/api/qquickwebenginefaviconprovider.cpp
@@ -39,10 +39,13 @@
#include "qquickwebenginefaviconprovider_p_p.h"
-#include "qquickwebengineview_p.h"
+#include "profile_adapter.h"
+#include "qquickwebenginesettings_p.h"
#include "qquickwebengineview_p_p.h"
#include "web_contents_adapter.h"
+#include <QtCore/QMimeDatabase>
+#include <QtCore/QTimer>
#include <QtGui/QIcon>
#include <QtGui/QPixmap>
@@ -95,14 +98,155 @@ static QPixmap extractPixmap(const QIcon &icon, const QSize &requestedSize)
return iconPixmap.scaled(requestedSize, Qt::KeepAspectRatio, Qt::SmoothTransformation).copy();
}
+static bool isIconURL(const QUrl &url)
+{
+ QMimeType mimeType = QMimeDatabase().mimeTypeForFile(url.path(), QMimeDatabase::MatchExtension);
+
+ // Check file extension.
+ if (mimeType.name().startsWith(QLatin1String("image")))
+ return true;
+
+ // Check if it is an image data: URL.
+ if (url.scheme() == QLatin1String("data") && url.path().startsWith(QLatin1String("image")))
+ return true;
+
+ return false;
+}
+
static QQuickWebEngineView *findViewById(const QString &id, QList<QQuickWebEngineView *> *views)
{
+ QQuickWebEngineView *result = nullptr;
for (QQuickWebEngineView *view : *views) {
- if (view->icon() == QQuickWebEngineFaviconProvider::faviconProviderUrl(QUrl(id)))
- return view;
+ if (isIconURL(QUrl(id))) {
+ if (view->icon() == QQuickWebEngineFaviconProvider::faviconProviderUrl(QUrl(id))) {
+ result = view;
+ break;
+ }
+ } else if (view->url() == QUrl(id)) {
+ result = view;
+ break;
+ }
+ }
+
+ return result;
+}
+
+FaviconImageResponseRunnable::FaviconImageResponseRunnable(const QString &id,
+ const QSize &requestedSize,
+ QList<QQuickWebEngineView *> *views)
+ : m_id(id), m_requestedSize(requestedSize), m_views(views)
+{
+}
+
+void FaviconImageResponseRunnable::run()
+{
+ if (tryNextView() == -1) {
+ // There is no non-otr view to access icon database.
+ Q_EMIT done(QPixmap());
+ }
+}
+
+void FaviconImageResponseRunnable::iconRequestDone(const QIcon &icon)
+{
+ if (icon.isNull()) {
+ if (tryNextView() == -1) {
+ // Ran out of views.
+ Q_EMIT done(QPixmap());
+ }
+ return;
+ }
+
+ Q_EMIT done(extractPixmap(icon, m_requestedSize).copy());
+}
+
+int FaviconImageResponseRunnable::tryNextView()
+{
+ for (; m_nextViewIndex < m_views->size(); ++m_nextViewIndex) {
+ QQuickWebEngineView *view = m_views->at(m_nextViewIndex);
+ if (view->profile()->isOffTheRecord())
+ continue;
+
+ requestIconOnUIThread(view);
+
+ return m_nextViewIndex++;
}
- return nullptr;
+ return -1;
+}
+
+void FaviconImageResponseRunnable::requestIconOnUIThread(QQuickWebEngineView *view)
+{
+ QTimer *timer = new QTimer();
+ timer->moveToThread(qApp->thread());
+ timer->setSingleShot(true);
+ QObject::connect(timer, &QTimer::timeout, [=]() {
+ QtWebEngineCore::ProfileAdapter *profileAdapter = view->d_ptr->profileAdapter();
+ bool touchIconsEnabled = view->profile()->settings()->touchIconsEnabled();
+ if (isIconURL(QUrl(m_id))) {
+ profileAdapter->requestIconForIconURL(QUrl(m_id),
+ qMax(m_requestedSize.width(), m_requestedSize.height()),
+ touchIconsEnabled,
+ [this](const QIcon &icon, const QUrl &) { iconRequestDone(icon); });
+ } else {
+ profileAdapter->requestIconForPageURL(QUrl(m_id),
+ qMax(m_requestedSize.width(), m_requestedSize.height()),
+ touchIconsEnabled,
+ [this](const QIcon &icon, const QUrl &, const QUrl &) { iconRequestDone(icon); });
+ }
+ timer->deleteLater();
+ });
+ QMetaObject::invokeMethod(timer, "start", Qt::QueuedConnection, Q_ARG(int, 0));
+}
+
+FaviconImageResponse::FaviconImageResponse()
+{
+ Q_EMIT finished();
+}
+
+FaviconImageResponse::FaviconImageResponse(const QString &id, const QSize &requestedSize,
+ QList<QQuickWebEngineView *> *views, QThreadPool *pool)
+{
+ if (QQuickWebEngineView *view = findViewById(id, views)) {
+ QTimer *timer = new QTimer();
+ timer->moveToThread(qApp->thread());
+ timer->setSingleShot(true);
+ QObject::connect(timer, &QTimer::timeout, [=]() {
+ QIcon icon = view->d_ptr->adapter->icon();
+ if (icon.isNull())
+ startRunnable(id, requestedSize, views, pool);
+ else
+ handleDone(extractPixmap(icon, requestedSize).copy());
+ timer->deleteLater();
+ });
+ QMetaObject::invokeMethod(timer, "start", Qt::QueuedConnection, Q_ARG(int, 0));
+ } else {
+ startRunnable(id, requestedSize, views, pool);
+ }
+}
+
+FaviconImageResponse::~FaviconImageResponse() { }
+
+void FaviconImageResponse::handleDone(QPixmap pixmap)
+{
+ if (m_runnable)
+ delete m_runnable;
+ m_image = pixmap.toImage();
+ Q_EMIT finished();
+}
+
+QQuickTextureFactory *FaviconImageResponse::textureFactory() const
+{
+ return QQuickTextureFactory::textureFactoryForImage(m_image);
+}
+
+void FaviconImageResponse::startRunnable(const QString &id, const QSize &requestedSize,
+ QList<QQuickWebEngineView *> *views, QThreadPool *pool)
+{
+ m_runnable = new FaviconImageResponseRunnable(id, requestedSize, views);
+ m_runnable->setAutoDelete(false);
+ connect(m_runnable, &FaviconImageResponseRunnable::done, this,
+ &FaviconImageResponse::handleDone);
+ pool->start(m_runnable);
}
QString QQuickWebEngineFaviconProvider::identifier()
@@ -128,31 +272,18 @@ QUrl QQuickWebEngineFaviconProvider::faviconProviderUrl(const QUrl &url)
return providerUrl;
}
-QQuickWebEngineFaviconProvider::QQuickWebEngineFaviconProvider()
- : QQuickImageProvider(QQuickImageProvider::Pixmap)
-{
-}
+QQuickWebEngineFaviconProvider::QQuickWebEngineFaviconProvider() { }
QQuickWebEngineFaviconProvider::~QQuickWebEngineFaviconProvider() { }
-QPixmap QQuickWebEngineFaviconProvider::requestPixmap(const QString &id, QSize *size,
- const QSize &requestedSize)
+QQuickImageResponse *
+QQuickWebEngineFaviconProvider::requestImageResponse(const QString &id, const QSize &requestedSize)
{
- Q_UNUSED(size);
- Q_UNUSED(requestedSize);
-
- if (m_views.isEmpty())
- return QPixmap();
-
- QQuickWebEngineView *view = findViewById(id, &m_views);
- if (!view)
- return QPixmap();
-
- QIcon icon = view->d_ptr->adapter->icon();
- if (icon.isNull())
- return QPixmap();
+ if (m_views.empty())
+ return new FaviconImageResponse;
- return extractPixmap(icon, requestedSize).copy();
+ FaviconImageResponse *response = new FaviconImageResponse(id, requestedSize, &m_views, &m_pool);
+ return response;
}
QT_END_NAMESPACE
diff --git a/src/webenginequick/api/qquickwebenginefaviconprovider_p_p.h b/src/webenginequick/api/qquickwebenginefaviconprovider_p_p.h
index 922f8b3ac..68e23fbcf 100644
--- a/src/webenginequick/api/qquickwebenginefaviconprovider_p_p.h
+++ b/src/webenginequick/api/qquickwebenginefaviconprovider_p_p.h
@@ -53,13 +53,57 @@
#include <QtWebEngineQuick/private/qtwebenginequickglobal_p.h>
#include <QtCore/QList>
+#include <QtCore/QRunnable>
+#include <QtCore/QThreadPool>
+#include <QtGui/QImage>
#include <QtQuick/QQuickImageProvider>
QT_BEGIN_NAMESPACE
class QQuickWebEngineView;
-class Q_WEBENGINE_PRIVATE_EXPORT QQuickWebEngineFaviconProvider : public QQuickImageProvider
+class FaviconImageResponseRunnable : public QObject, public QRunnable
+{
+ Q_OBJECT
+
+public:
+ FaviconImageResponseRunnable(const QString &id, const QSize &requestedSize,
+ QList<QQuickWebEngineView *> *views);
+ void run() override;
+ void iconRequestDone(const QIcon &icon);
+
+signals:
+ void done(QPixmap pixmap);
+
+private:
+ int tryNextView();
+ void requestIconOnUIThread(QQuickWebEngineView *view);
+
+ QString m_id;
+ QSize m_requestedSize;
+ QList<QQuickWebEngineView *> *m_views;
+ int m_nextViewIndex = 0;
+};
+
+class FaviconImageResponse : public QQuickImageResponse
+{
+public:
+ FaviconImageResponse();
+ FaviconImageResponse(const QString &id, const QSize &requestedSize,
+ QList<QQuickWebEngineView *> *views, QThreadPool *pool);
+ ~FaviconImageResponse();
+ void handleDone(QPixmap pixmap);
+ QQuickTextureFactory *textureFactory() const override;
+
+private:
+ void startRunnable(const QString &id, const QSize &requestedSize,
+ QList<QQuickWebEngineView *> *views, QThreadPool *pool);
+
+ FaviconImageResponseRunnable *m_runnable = nullptr;
+ QImage m_image;
+};
+
+class Q_WEBENGINE_PRIVATE_EXPORT QQuickWebEngineFaviconProvider : public QQuickAsyncImageProvider
{
public:
static QString identifier();
@@ -71,9 +115,11 @@ public:
void attach(QQuickWebEngineView *view) { m_views.append(view); }
void detach(QQuickWebEngineView *view) { m_views.removeAll(view); }
- QPixmap requestPixmap(const QString &, QSize *, const QSize &) override;
+ QQuickImageResponse *requestImageResponse(const QString &id,
+ const QSize &requestedSize) override;
private:
+ QThreadPool m_pool;
QList<QQuickWebEngineView *> m_views;
};
diff --git a/src/webenginequick/api/qquickwebengineprofile.h b/src/webenginequick/api/qquickwebengineprofile.h
index f93c3a5d4..8147f0432 100644
--- a/src/webenginequick/api/qquickwebengineprofile.h
+++ b/src/webenginequick/api/qquickwebengineprofile.h
@@ -177,6 +177,7 @@ private:
QQuickWebEngineProfile(QQuickWebEngineProfilePrivate *, QObject *parent = nullptr);
QQuickWebEngineSettings *settings() const;
+ friend class FaviconImageResponseRunnable;
friend class QQuickWebEngineSettings;
friend class QQuickWebEngineSingleton;
friend class QQuickWebEngineViewPrivate;
diff --git a/src/webenginequick/api/qquickwebengineview_p.h b/src/webenginequick/api/qquickwebengineview_p.h
index b3c092856..2719fa448 100644
--- a/src/webenginequick/api/qquickwebengineview_p.h
+++ b/src/webenginequick/api/qquickwebengineview_p.h
@@ -563,7 +563,8 @@ private:
friend class QtWebEngineCore::RenderWidgetHostViewQtDelegateQuick;
friend class QQuickContextMenuBuilder;
- friend class QQuickWebEngineFaviconProvider;
+ friend class FaviconImageResponse;
+ friend class FaviconImageResponseRunnable;
#ifndef QT_NO_ACCESSIBILITY
friend class QQuickWebEngineViewAccessible;
#endif // QT_NO_ACCESSIBILITY
diff --git a/src/webenginequick/doc/src/qtwebengine-features.qdoc b/src/webenginequick/doc/src/qtwebengine-features.qdoc
index 431367765..24bc9d7aa 100644
--- a/src/webenginequick/doc/src/qtwebengine-features.qdoc
+++ b/src/webenginequick/doc/src/qtwebengine-features.qdoc
@@ -54,6 +54,7 @@
\li \l{View Source}
\li \l{webrtc_feature}{WebRTC}
\li \l{Web Notifications}
+ \li \l{Favicon Handling}
\endlist
\section1 Audio and Video Codecs
@@ -641,4 +642,42 @@
{WebEngineView.Notifications}.
Support for this feature was added in Qt 5.13.0.
+
+ \section1 Favicon Handling
+
+ For accessing icons a \c QQuickImageProvider is registered. This provider can be
+ accessed by a special URL where the scheme is "image:" and the host is "favicon".
+ For example,
+ \qml
+ Image {
+ source: "image://favicon/url"
+ }
+ \endqml
+
+ The \c url can be the URL of the favicon. For example,
+ \qml
+ Image {
+ source: "image://favicon/https://www.qt.io/hubfs/2016_Qt_Logo/qt_logo_green_rgb_16x16.png"
+ }
+ \endqml
+
+ The \c url also can be a page URL to access its icon. For example,
+ \qml
+ Image {
+ source: "image://favicon/https://www.qt.io/"
+ }
+ \endqml
+
+ If more than one icon is available, the \l {Image::sourceSize} property can be
+ specified to choose the icon with the desired size. If \l {Image::sourceSize}
+ is not specified or 0, the largest available icon will be chosen.
+
+ The image provider looks up the requested icon in the existing \l {WebEngineView}
+ instances. First, it tries to match the currently displayed icons. If no match
+ has been found it requests the icon from the database. Each profile has its
+ own icon database and it is stored in the persistent storage thus the stored icons
+ can be accessed without network connection too. The icon must be previously loaded
+ to be stored in the database.
+
+ \note The icon database is not available for off-the-record profiles.
*/
diff --git a/src/webenginequick/doc/src/webengineview_lgpl.qdoc b/src/webenginequick/doc/src/webengineview_lgpl.qdoc
index 1ea484703..11dceae69 100644
--- a/src/webenginequick/doc/src/webengineview_lgpl.qdoc
+++ b/src/webenginequick/doc/src/webengineview_lgpl.qdoc
@@ -212,8 +212,7 @@
\readonly
An internal URL for accessing the currently displayed web site icon,
- also known as favicon or shortcut icon. The icon is already downloaded
- and stored by the \QWE's favicon manager.
+ also known as favicon or shortcut icon.
This read-only URL corresponds to the image used within a mobile browser
application to represent a bookmarked page on the device's home screen.
@@ -229,11 +228,10 @@
}
\endqml
- Specifying the \c{sourceSize} property of the \c{Image} element informs
- the \QWE's favicon provider about the requested size. The
- favicon provider tries to find the best fit among the web page candidate
- icons. If \c{sourceSize} property is not specified, the provider provides
- the icon with the largest resolution.
+ Specifying the \l {Image::sourceSize} property informs
+ the \QWE's favicon provider about the requested size and resizes the
+ icon to it. If \l {Image::sourceSize} property is not specified,
+ the provider provides the icon with the largest available resolution.
*/
/*!