summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAllan Sandfeld Jensen <allan.jensen@digia.com>2014-07-28 16:35:16 +0200
committerAllan Sandfeld Jensen <allan.jensen@digia.com>2014-07-30 12:45:12 +0200
commita99922affdc7953f092dcfcf34ea741567a5bddc (patch)
tree3c6a0c272060c4e35b345e17803d76c4e29b641e
parente1b98a164799d90f51336c718724fe8e6db01568 (diff)
Add api to get the favicon URL
Adds one of the missing pieces of the QWebFrame and QWebView APIs. Unlike the QtWebKit version this only fetches the favicon URL, and not the icon. This is because we do not want to implement an icon database, and that the icon would be loaded asynchronous anyway, bringing no guarantee to be a valid icon/image yet. Change-Id: I227311ae3676044da850e687b82bee752b5079c8 Reviewed-by: Jocelyn Turcotte <jocelyn.turcotte@digia.com>
-rw-r--r--examples/webenginewidgets/browser/browserapplication.cpp16
-rw-r--r--examples/webenginewidgets/browser/browserapplication.h7
-rw-r--r--examples/webenginewidgets/browser/tabwidget.cpp6
-rw-r--r--examples/webenginewidgets/browser/urllineedit.cpp11
-rw-r--r--examples/webenginewidgets/browser/webview.cpp32
-rw-r--r--examples/webenginewidgets/browser/webview.h9
-rw-r--r--src/core/web_contents_adapter.cpp12
-rw-r--r--src/core/web_contents_adapter.h1
-rw-r--r--src/webenginewidgets/api/qwebenginepage.cpp9
-rw-r--r--src/webenginewidgets/api/qwebenginepage.h4
-rw-r--r--src/webenginewidgets/api/qwebengineview.cpp6
-rw-r--r--src/webenginewidgets/api/qwebengineview.h3
-rw-r--r--src/webenginewidgets/doc/src/qwebenginepage_lgpl.qdoc17
-rw-r--r--src/webenginewidgets/doc/src/qwebengineview_lgpl.qdoc15
14 files changed, 126 insertions, 22 deletions
diff --git a/examples/webenginewidgets/browser/browserapplication.cpp b/examples/webenginewidgets/browser/browserapplication.cpp
index d1563ef05..89fc698a7 100644
--- a/examples/webenginewidgets/browser/browserapplication.cpp
+++ b/examples/webenginewidgets/browser/browserapplication.cpp
@@ -74,7 +74,7 @@
DownloadManager *BrowserApplication::s_downloadManager = 0;
HistoryManager *BrowserApplication::s_historyManager = 0;
-NetworkAccessManager *BrowserApplication::s_networkAccessManager = 0;
+QNetworkAccessManager *BrowserApplication::s_networkAccessManager = 0;
BookmarksManager *BrowserApplication::s_bookmarksManager = 0;
BrowserApplication::BrowserApplication(int &argc, char **argv)
@@ -433,7 +433,7 @@ DownloadManager *BrowserApplication::downloadManager()
return s_downloadManager;
}
-NetworkAccessManager *BrowserApplication::networkAccessManager()
+QNetworkAccessManager *BrowserApplication::networkAccessManager()
{
#if defined(QWEBENGINEPAGE_SETNETWORKACCESSMANAGER)
if (!s_networkAccessManager) {
@@ -442,7 +442,10 @@ NetworkAccessManager *BrowserApplication::networkAccessManager()
}
return s_networkAccessManager;
#else
- return 0;
+ if (!s_networkAccessManager) {
+ s_networkAccessManager = new QNetworkAccessManager();
+ }
+ return s_networkAccessManager;
#endif
}
@@ -468,7 +471,12 @@ QIcon BrowserApplication::icon(const QUrl &url) const
if (!icon.isNull())
return icon.pixmap(16, 16);
#endif
+ return defaultIcon();
+}
+
+QIcon BrowserApplication::defaultIcon() const
+{
if (m_defaultIcon.isNull())
m_defaultIcon = QIcon(QLatin1String(":defaulticon.png"));
- return m_defaultIcon.pixmap(16, 16);
+ return m_defaultIcon;
}
diff --git a/examples/webenginewidgets/browser/browserapplication.h b/examples/webenginewidgets/browser/browserapplication.h
index b17f1cea5..777bef06e 100644
--- a/examples/webenginewidgets/browser/browserapplication.h
+++ b/examples/webenginewidgets/browser/browserapplication.h
@@ -51,6 +51,7 @@
QT_BEGIN_NAMESPACE
class QLocalServer;
+class QNetworkAccessManager;
QT_END_NAMESPACE
class BookmarksManager;
@@ -58,7 +59,6 @@ class BrowserMainWindow;
class CookieJar;
class DownloadManager;
class HistoryManager;
-class NetworkAccessManager;
class BrowserApplication : public QApplication
{
Q_OBJECT
@@ -73,6 +73,7 @@ public:
BrowserMainWindow *mainWindow();
QList<BrowserMainWindow*> mainWindows();
QIcon icon(const QUrl &url) const;
+ QIcon defaultIcon() const;
void saveSession();
bool canRestoreSession() const;
@@ -80,7 +81,7 @@ public:
static HistoryManager *historyManager();
static CookieJar *cookieJar();
static DownloadManager *downloadManager();
- static NetworkAccessManager *networkAccessManager();
+ static QNetworkAccessManager *networkAccessManager();
static BookmarksManager *bookmarksManager();
#if defined(Q_WS_MAC)
@@ -106,7 +107,7 @@ private:
static HistoryManager *s_historyManager;
static DownloadManager *s_downloadManager;
- static NetworkAccessManager *s_networkAccessManager;
+ static QNetworkAccessManager *s_networkAccessManager;
static BookmarksManager *s_bookmarksManager;
QList<QPointer<BrowserMainWindow> > m_mainWindows;
diff --git a/examples/webenginewidgets/browser/tabwidget.cpp b/examples/webenginewidgets/browser/tabwidget.cpp
index 2503f4b8a..3cb17366f 100644
--- a/examples/webenginewidgets/browser/tabwidget.cpp
+++ b/examples/webenginewidgets/browser/tabwidget.cpp
@@ -452,12 +452,8 @@ WebView *TabWidget::newTab(bool makeCurrent)
urlLineEdit->setWebView(webView);
connect(webView, SIGNAL(loadStarted()),
this, SLOT(webViewLoadStarted()));
- connect(webView, SIGNAL(loadFinished(bool)),
- this, SLOT(webViewIconChanged()));
-#if defined(QWEBENGINEVIEW_ICONCHANGED)
connect(webView, SIGNAL(iconChanged()),
this, SLOT(webViewIconChanged()));
-#endif
connect(webView, SIGNAL(titleChanged(QString)),
this, SLOT(webViewTitleChanged(QString)));
connect(webView, SIGNAL(urlChanged(QUrl)),
@@ -617,7 +613,7 @@ void TabWidget::webViewIconChanged()
WebView *webView = qobject_cast<WebView*>(sender());
int index = webViewIndex(webView);
if (-1 != index) {
- QIcon icon = BrowserApplication::instance()->icon(webView->url());
+ QIcon icon = webView->icon();
setTabIcon(index, icon);
}
}
diff --git a/examples/webenginewidgets/browser/urllineedit.cpp b/examples/webenginewidgets/browser/urllineedit.cpp
index 306e56e9a..730d3f771 100644
--- a/examples/webenginewidgets/browser/urllineedit.cpp
+++ b/examples/webenginewidgets/browser/urllineedit.cpp
@@ -263,8 +263,6 @@ UrlLineEdit::UrlLineEdit(QWidget *parent)
m_iconLabel->resize(16, 16);
setLeftWidget(m_iconLabel);
m_defaultBaseColor = palette().color(QPalette::Base);
-
- webViewIconChanged();
}
void UrlLineEdit::setWebView(WebView *webView)
@@ -274,12 +272,8 @@ void UrlLineEdit::setWebView(WebView *webView)
m_iconLabel->m_webView = webView;
connect(webView, SIGNAL(urlChanged(QUrl)),
this, SLOT(webViewUrlChanged(QUrl)));
- connect(webView, SIGNAL(loadFinished(bool)),
- this, SLOT(webViewIconChanged()));
-#if defined(QWEBENGINEVIEW_ICONCHANGED)
connect(webView, SIGNAL(iconChanged()),
this, SLOT(webViewIconChanged()));
-#endif
connect(webView, SIGNAL(loadProgress(int)),
this, SLOT(update()));
}
@@ -292,9 +286,8 @@ void UrlLineEdit::webViewUrlChanged(const QUrl &url)
void UrlLineEdit::webViewIconChanged()
{
- QUrl url = (m_webView) ? m_webView->url() : QUrl();
- QIcon icon = BrowserApplication::instance()->icon(url);
- QPixmap pixmap(icon.pixmap(16, 16));
+ Q_ASSERT(m_webView);
+ QPixmap pixmap = m_webView->icon().pixmap(16, 16);
m_iconLabel->setPixmap(pixmap);
}
diff --git a/examples/webenginewidgets/browser/webview.cpp b/examples/webenginewidgets/browser/webview.cpp
index edbaadca9..d4dd2649c 100644
--- a/examples/webenginewidgets/browser/webview.cpp
+++ b/examples/webenginewidgets/browser/webview.cpp
@@ -311,6 +311,7 @@ WebView::WebView(QWidget* parent)
: QWebEngineView(parent)
, m_progress(0)
, m_page(new WebPage(this))
+ , m_iconReply(0)
{
setPage(m_page);
#if defined(QWEBENGINEPAGE_STATUSBARMESSAGE)
@@ -323,6 +324,8 @@ WebView::WebView(QWidget* parent)
this, SLOT(loadFinished()));
connect(page(), SIGNAL(loadingUrl(QUrl)),
this, SIGNAL(urlChanged(QUrl)));
+ connect(page(), SIGNAL(iconUrlChanged(QUrl)),
+ this, SLOT(onIconUrlChanged(QUrl)));
#if defined(QWEBENGINEPAGE_DOWNLOADREQUESTED)
connect(page(), SIGNAL(downloadRequested(QNetworkRequest)),
this, SLOT(downloadRequested(QNetworkRequest)));
@@ -423,6 +426,35 @@ QUrl WebView::url() const
return m_initialUrl;
}
+QIcon WebView::icon() const
+{
+ if (!m_icon.isNull())
+ return m_icon;
+ return BrowserApplication::instance()->defaultIcon();
+}
+
+void WebView::onIconUrlChanged(const QUrl &url)
+{
+ QNetworkRequest iconRequest(url);
+ m_iconReply = BrowserApplication::networkAccessManager()->get(iconRequest);
+ m_iconReply->setParent(this);
+ connect(m_iconReply, SIGNAL(finished()), this, SLOT(iconLoaded()));
+}
+
+void WebView::iconLoaded()
+{
+ m_icon = QIcon();
+ if (m_iconReply) {
+ QByteArray data = m_iconReply->readAll();
+ QPixmap pixmap;
+ pixmap.loadFromData(data);
+ m_icon.addPixmap(pixmap);
+ m_iconReply->deleteLater();
+ m_iconReply = 0;
+ }
+ emit iconChanged();
+}
+
void WebView::mousePressEvent(QMouseEvent *event)
{
m_page->m_pressedButtons = event->buttons();
diff --git a/examples/webenginewidgets/browser/webview.h b/examples/webenginewidgets/browser/webview.h
index aaf2aab13..352954c8d 100644
--- a/examples/webenginewidgets/browser/webview.h
+++ b/examples/webenginewidgets/browser/webview.h
@@ -42,6 +42,7 @@
#ifndef WEBVIEW_H
#define WEBVIEW_H
+#include <QIcon>
#include <QWebEngineView>
QT_BEGIN_NAMESPACE
@@ -98,6 +99,7 @@ public:
void loadUrl(const QUrl &url);
QUrl url() const;
+ QIcon icon() const;
QString lastStatusBarText() const;
inline int progress() const { return m_progress; }
@@ -108,6 +110,9 @@ protected:
void contextMenuEvent(QContextMenuEvent *event);
void wheelEvent(QWheelEvent *event);
+signals:
+ void iconChanged();
+
private slots:
void setProgress(int progress);
void loadFinished();
@@ -115,12 +120,16 @@ private slots:
void downloadRequested(const QNetworkRequest &request);
void openLinkInNewTab();
void onFeaturePermissionRequested(const QUrl &securityOrigin, QWebEnginePage::Feature);
+ void onIconUrlChanged(const QUrl &url);
+ void iconLoaded();
private:
QString m_statusBarText;
QUrl m_initialUrl;
int m_progress;
WebPage *m_page;
+ QIcon m_icon;
+ QNetworkReply *m_iconReply;
};
#endif
diff --git a/src/core/web_contents_adapter.cpp b/src/core/web_contents_adapter.cpp
index 235534f18..3dcef4ba4 100644
--- a/src/core/web_contents_adapter.cpp
+++ b/src/core/web_contents_adapter.cpp
@@ -62,6 +62,7 @@
#include "content/public/browser/child_process_security_policy.h"
#include "content/public/browser/navigation_entry.h"
#include "content/public/browser/render_view_host.h"
+#include "content/public/browser/favicon_status.h"
#include "content/public/common/page_state.h"
#include "content/public/common/page_zoom.h"
#include "content/public/common/renderer_preferences.h"
@@ -472,6 +473,17 @@ QUrl WebContentsAdapter::requestedUrl() const
return QUrl();
}
+QUrl WebContentsAdapter::iconUrl() const
+{
+ Q_D(const WebContentsAdapter);
+ if (content::NavigationEntry* entry = d->webContents->GetController().GetVisibleEntry()) {
+ content::FaviconStatus favicon = entry->GetFavicon();
+ if (favicon.valid)
+ return toQt(favicon.url);
+ }
+ return QUrl();
+}
+
QString WebContentsAdapter::pageTitle() const
{
Q_D(const WebContentsAdapter);
diff --git a/src/core/web_contents_adapter.h b/src/core/web_contents_adapter.h
index 8e665ee2c..a20d55959 100644
--- a/src/core/web_contents_adapter.h
+++ b/src/core/web_contents_adapter.h
@@ -76,6 +76,7 @@ public:
QUrl requestedUrl() const;
QString pageTitle() const;
QString selectedText() const;
+ QUrl iconUrl() const;
void undo();
void redo();
diff --git a/src/webenginewidgets/api/qwebenginepage.cpp b/src/webenginewidgets/api/qwebenginepage.cpp
index 8e87c1cf7..212fd696e 100644
--- a/src/webenginewidgets/api/qwebenginepage.cpp
+++ b/src/webenginewidgets/api/qwebenginepage.cpp
@@ -196,7 +196,8 @@ void QWebEnginePagePrivate::urlChanged(const QUrl &url)
void QWebEnginePagePrivate::iconChanged(const QUrl &url)
{
- Q_UNUSED(url)
+ Q_Q(QWebEnginePage);
+ Q_EMIT q->iconUrlChanged(url);
}
void QWebEnginePagePrivate::loadProgressChanged(int progress)
@@ -802,6 +803,12 @@ QUrl QWebEnginePage::requestedUrl() const
return d->adapter->requestedUrl();
}
+QUrl QWebEnginePage::iconUrl() const
+{
+ Q_D(const QWebEnginePage);
+ return d->adapter->iconUrl();
+}
+
qreal QWebEnginePage::zoomFactor() const
{
Q_D(const QWebEnginePage);
diff --git a/src/webenginewidgets/api/qwebenginepage.h b/src/webenginewidgets/api/qwebenginepage.h
index 6d8d2ddd8..bd1e5c7a9 100644
--- a/src/webenginewidgets/api/qwebenginepage.h
+++ b/src/webenginewidgets/api/qwebenginepage.h
@@ -77,6 +77,7 @@ class QWEBENGINEWIDGETS_EXPORT QWebEnginePage : public QObject {
Q_PROPERTY(qreal zoomFactor READ zoomFactor WRITE setZoomFactor)
Q_PROPERTY(QString title READ title)
Q_PROPERTY(QUrl url READ url WRITE setUrl)
+ Q_PROPERTY(QUrl iconUrl READ iconUrl)
public:
enum WebAction {
@@ -171,6 +172,7 @@ public:
void setUrl(const QUrl &url);
QUrl url() const;
QUrl requestedUrl() const;
+ QUrl iconUrl() const;
qreal zoomFactor() const;
void setZoomFactor(qreal factor);
@@ -197,6 +199,8 @@ Q_SIGNALS:
// Ex-QWebFrame signals
void titleChanged(const QString &title);
void urlChanged(const QUrl &url);
+ // Was iconChanged() in QWebFrame
+ void iconUrlChanged(const QUrl &url);
protected:
virtual QWebEnginePage *createWindow(WebWindowType type);
diff --git a/src/webenginewidgets/api/qwebengineview.cpp b/src/webenginewidgets/api/qwebengineview.cpp
index b11af3e7a..03bb474bb 100644
--- a/src/webenginewidgets/api/qwebengineview.cpp
+++ b/src/webenginewidgets/api/qwebengineview.cpp
@@ -82,6 +82,7 @@ void QWebEngineViewPrivate::bind(QWebEngineView *view, QWebEnginePage *page)
if (view && page) {
QObject::connect(page, &QWebEnginePage::titleChanged, view, &QWebEngineView::titleChanged);
QObject::connect(page, &QWebEnginePage::urlChanged, view, &QWebEngineView::urlChanged);
+ QObject::connect(page, &QWebEnginePage::iconUrlChanged, view, &QWebEngineView::iconUrlChanged);
QObject::connect(page, &QWebEnginePage::loadStarted, view, &QWebEngineView::loadStarted);
QObject::connect(page, &QWebEnginePage::loadProgress, view, &QWebEngineView::loadProgress);
QObject::connect(page, &QWebEnginePage::loadFinished, view, &QWebEngineView::loadFinished);
@@ -168,6 +169,11 @@ QUrl QWebEngineView::url() const
return page()->url();
}
+QUrl QWebEngineView::iconUrl() const
+{
+ return page()->iconUrl();
+}
+
bool QWebEngineView::hasSelection() const
{
return page()->hasSelection();
diff --git a/src/webenginewidgets/api/qwebengineview.h b/src/webenginewidgets/api/qwebengineview.h
index 02f599d45..c3ebfba07 100644
--- a/src/webenginewidgets/api/qwebengineview.h
+++ b/src/webenginewidgets/api/qwebengineview.h
@@ -39,6 +39,7 @@ class QWEBENGINEWIDGETS_EXPORT QWebEngineView : public QWidget {
Q_OBJECT
Q_PROPERTY(QString title READ title)
Q_PROPERTY(QUrl url READ url WRITE setUrl)
+ Q_PROPERTY(QUrl iconUrl READ iconUrl)
Q_PROPERTY(QString selectedText READ selectedText)
Q_PROPERTY(bool hasSelection READ hasSelection)
Q_PROPERTY(qreal zoomFactor READ zoomFactor WRITE setZoomFactor)
@@ -59,6 +60,7 @@ public:
QString title() const;
void setUrl(const QUrl &url);
QUrl url() const;
+ QUrl iconUrl() const;
bool hasSelection() const;
QString selectedText() const;
@@ -88,6 +90,7 @@ Q_SIGNALS:
void titleChanged(const QString& title);
void selectionChanged();
void urlChanged(const QUrl&);
+ void iconUrlChanged(const QUrl&);
protected:
virtual QWebEngineView *createWindow(QWebEnginePage::WebWindowType type);
diff --git a/src/webenginewidgets/doc/src/qwebenginepage_lgpl.qdoc b/src/webenginewidgets/doc/src/qwebenginepage_lgpl.qdoc
index 20564d724..41dd0a31b 100644
--- a/src/webenginewidgets/doc/src/qwebenginepage_lgpl.qdoc
+++ b/src/webenginewidgets/doc/src/qwebenginepage_lgpl.qdoc
@@ -425,6 +425,13 @@
*/
/*!
+ \property QWebEnginePage::iconUrl
+ \brief the url of the icon associated with the frame currently viewed.
+
+ \sa iconUrlChanged()
+*/
+
+/*!
\property QWebEnginePage::requestedUrl
The URL requested to loaded by the frame currently viewed. The URL may differ from
@@ -544,3 +551,13 @@
\sa url()
*/
+
+/*!
+ \fn void QWebEnginePage::iconUrlChanged(const QUrl &url)
+
+ This signal is emitted when the icon ("favicon") associated with the main frame is
+ found or changed. The new URL is specified by \a url.
+
+
+ \sa iconUrl()
+*/
diff --git a/src/webenginewidgets/doc/src/qwebengineview_lgpl.qdoc b/src/webenginewidgets/doc/src/qwebengineview_lgpl.qdoc
index bdfd4b20a..5c42d252b 100644
--- a/src/webenginewidgets/doc/src/qwebengineview_lgpl.qdoc
+++ b/src/webenginewidgets/doc/src/qwebengineview_lgpl.qdoc
@@ -193,6 +193,13 @@
*/
/*!
+ \property QWebEngineView::iconUrl
+ \brief the url of the icon associated with the web page currently viewed
+
+ \sa iconUrlChanged()
+*/
+
+/*!
\property QWebEngineView::hasSelection
\brief whether this page contains selected content or not.
@@ -315,6 +322,14 @@
*/
/*!
+ \fn void QWebEngineView::iconUrlChanged(const QUrl &url)
+
+ This signal is emitted whenever the icon \a url of the view changes.
+
+ \sa iconUrl()
+*/
+
+/*!
\fn void QWebEngineView::loadStarted()
This signal is emitted when a new load of the page is started.