summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorJocelyn Turcotte <jocelyn.turcotte@digia.com>2013-08-19 17:48:19 +0200
committerJocelyn Turcotte <jocelyn.turcotte@digia.com>2013-08-20 18:16:15 +0200
commitdddfe170077e022f0d428569fe8d5298736a2b68 (patch)
tree87d6045362ce89da7427b7b385b56a4619aab0f5 /lib
parent75d9159924fcb2b4c11a43c87e8e62332fdf38fd (diff)
Implement the basic parts of QWebEngineHistory.
Mark the remaining methods as not implemented to allow enabling most of the dependent code in the demo browser and in API tests. Add two new tests to cover cases that might be problematic with the index-based implementation. This also renames WebContentsAdapter::navigateHistory to navigateToOffset in order to avoid confusion with navigateToIndex. Change-Id: I7c5cb9f5f878e34206fdfe48334a2dc7d9d95a1d Reviewed-by: Zeno Albisser <zeno.albisser@digia.com> Reviewed-by: Andras Becsi <andras.becsi@digia.com>
Diffstat (limited to 'lib')
-rw-r--r--lib/quick/qquickwebengineview.cpp4
-rw-r--r--lib/web_contents_adapter.cpp54
-rw-r--r--lib/web_contents_adapter.h10
-rw-r--r--lib/widgets/Api/qwebenginehistory.cpp212
-rw-r--r--lib/widgets/Api/qwebenginehistory.h8
-rw-r--r--lib/widgets/Api/qwebenginehistory_p.h14
-rw-r--r--lib/widgets/Api/qwebenginepage.cpp4
-rw-r--r--lib/widgets/Api/qwebengineview.cpp5
8 files changed, 300 insertions, 11 deletions
diff --git a/lib/quick/qquickwebengineview.cpp b/lib/quick/qquickwebengineview.cpp
index 9a728cd78..80ddc287f 100644
--- a/lib/quick/qquickwebengineview.cpp
+++ b/lib/quick/qquickwebengineview.cpp
@@ -121,13 +121,13 @@ void QQuickWebEngineView::setUrl(const QUrl& url)
void QQuickWebEngineView::goBack()
{
Q_D(QQuickWebEngineView);
- d->adapter->navigateHistory(-1);
+ d->adapter->navigateToOffset(-1);
}
void QQuickWebEngineView::goForward()
{
Q_D(QQuickWebEngineView);
- d->adapter->navigateHistory(1);
+ d->adapter->navigateToOffset(1);
}
void QQuickWebEngineView::reload()
diff --git a/lib/web_contents_adapter.cpp b/lib/web_contents_adapter.cpp
index 73970094e..1c9976cbb 100644
--- a/lib/web_contents_adapter.cpp
+++ b/lib/web_contents_adapter.cpp
@@ -42,6 +42,7 @@
#include "content_browser_client_qt.h"
#include "browser_context_qt.h"
+#include "type_conversion.h"
#include "web_contents_adapter_client.h"
#include "web_contents_delegate_qt.h"
#include "web_contents_view_qt.h"
@@ -88,17 +89,12 @@ bool WebContentsAdapter::canGoForward() const
{
return webContents()->GetController().CanGoForward();
}
+
bool WebContentsAdapter::isLoading() const
{
return webContents()->IsLoading();
}
-void WebContentsAdapter::navigateHistory(int offset)
-{
- webContents()->GetController().GoToOffset(offset);
- webContents()->GetView()->Focus();
-}
-
void WebContentsAdapter::stop()
{
content::NavigationController& controller = webContents()->GetController();
@@ -135,6 +131,52 @@ QString WebContentsAdapter::pageTitle() const
return entry ? toQt(entry->GetTitle()) : QString();
}
+void WebContentsAdapter::navigateToIndex(int offset)
+{
+ webContents()->GetController().GoToIndex(offset);
+ webContents()->GetView()->Focus();
+}
+
+void WebContentsAdapter::navigateToOffset(int offset)
+{
+ webContents()->GetController().GoToOffset(offset);
+ webContents()->GetView()->Focus();
+}
+
+int WebContentsAdapter::navigationEntryCount()
+{
+ return webContents()->GetController().GetEntryCount();
+}
+
+int WebContentsAdapter::currentNavigationEntryIndex()
+{
+ return webContents()->GetController().GetCurrentEntryIndex();
+}
+
+QUrl WebContentsAdapter::getNavigationEntryOriginalUrl(int index)
+{
+ content::NavigationEntry *entry = webContents()->GetController().GetEntryAtIndex(index);
+ return entry ? toQt(entry->GetOriginalRequestURL()) : QUrl();
+}
+
+QUrl WebContentsAdapter::getNavigationEntryUrl(int index)
+{
+ content::NavigationEntry *entry = webContents()->GetController().GetEntryAtIndex(index);
+ return entry ? toQt(entry->GetURL()) : QUrl();
+}
+
+QString WebContentsAdapter::getNavigationEntryTitle(int index)
+{
+ content::NavigationEntry *entry = webContents()->GetController().GetEntryAtIndex(index);
+ return entry ? toQt(entry->GetTitle()) : QString();
+}
+
+void WebContentsAdapter::clearNavigationHistory()
+{
+ if (webContents()->GetController().CanPruneAllButVisible())
+ webContents()->GetController().PruneAllButVisible();
+}
+
content::WebContents *WebContentsAdapter::webContents() const
{
Q_D(const WebContentsAdapter);
diff --git a/lib/web_contents_adapter.h b/lib/web_contents_adapter.h
index c9d3a67ad..2bc6b9881 100644
--- a/lib/web_contents_adapter.h
+++ b/lib/web_contents_adapter.h
@@ -62,13 +62,21 @@ public:
bool canGoBack() const;
bool canGoForward() const;
bool isLoading() const;
- void navigateHistory(int);
void stop();
void reload();
void load(const QUrl&);
QUrl activeUrl() const;
QString pageTitle() const;
+ void navigateToIndex(int);
+ void navigateToOffset(int);
+ int navigationEntryCount();
+ int currentNavigationEntryIndex();
+ QUrl getNavigationEntryOriginalUrl(int index);
+ QUrl getNavigationEntryUrl(int index);
+ QString getNavigationEntryTitle(int index);
+ void clearNavigationHistory();
+
private:
inline content::WebContents* webContents() const;
Q_DECLARE_PRIVATE(WebContentsAdapter);
diff --git a/lib/widgets/Api/qwebenginehistory.cpp b/lib/widgets/Api/qwebenginehistory.cpp
index ca8d3a8d1..3f580d814 100644
--- a/lib/widgets/Api/qwebenginehistory.cpp
+++ b/lib/widgets/Api/qwebenginehistory.cpp
@@ -45,11 +45,108 @@
#include "qwebenginepage_p.h"
#include "web_contents_adapter.h"
+QWebEngineHistoryItemPrivate::QWebEngineHistoryItemPrivate(WebContentsAdapter *adapter, int index)
+ : adapter(adapter)
+ , index(index)
+{
+}
+
+QWebEngineHistoryItem::QWebEngineHistoryItem(QWebEngineHistoryItemPrivate *d)
+ : d(d)
+{
+}
+
+QWebEngineHistoryItem::QWebEngineHistoryItem(const QWebEngineHistoryItem &other)
+ : d(other.d)
+{
+}
+
+QWebEngineHistoryItem &QWebEngineHistoryItem::operator=(const QWebEngineHistoryItem &other)
+{
+ d = other.d;
+ return *this;
+}
+
+QWebEngineHistoryItem::~QWebEngineHistoryItem()
+{
+}
+
+QUrl QWebEngineHistoryItem::originalUrl() const
+{
+ Q_D(const QWebEngineHistoryItem);
+ return d->adapter ? d->adapter->getNavigationEntryOriginalUrl(d->index) : QUrl();
+}
+
+QUrl QWebEngineHistoryItem::url() const
+{
+ Q_D(const QWebEngineHistoryItem);
+ return d->adapter ? d->adapter->getNavigationEntryUrl(d->index) : QUrl();
+}
+
+QString QWebEngineHistoryItem::title() const
+{
+ Q_D(const QWebEngineHistoryItem);
+ return d->adapter ? d->adapter->getNavigationEntryTitle(d->index) : QString();
+}
+
+QDateTime QWebEngineHistoryItem::lastVisited() const
+{
+ qWarning("Not implemented: %s", __func__);
+ return QDateTime();
+}
+
+QIcon QWebEngineHistoryItem::icon() const
+{
+ qWarning("Not implemented: %s", __func__);
+ return QIcon();
+}
+
+QVariant QWebEngineHistoryItem::userData() const
+{
+ return QVariant();
+}
+
+void QWebEngineHistoryItem::setUserData(const QVariant& userData)
+{
+ qWarning("Not implemented: %s", __func__);
+}
+
+bool QWebEngineHistoryItem::isValid() const
+{
+ Q_D(const QWebEngineHistoryItem);
+ if (!d->adapter)
+ return false;
+ return d->index >= 0 && d->index < d->adapter->navigationEntryCount();
+}
+
QWebEngineHistoryPrivate::QWebEngineHistoryPrivate(WebContentsAdapter *adapter)
: adapter(adapter)
{
}
+QWebEngineHistoryPrivate::~QWebEngineHistoryPrivate()
+{
+ // Invalidate shared item references possibly still out there.
+ QList<QWebEngineHistoryItem>::iterator it, end;
+ for (it = items.begin(), end = items.end(); it != end; ++it)
+ it->d->adapter = 0;
+}
+
+void QWebEngineHistoryPrivate::updateItems() const
+{
+ // Keep track of items we return to be able to invalidate them
+ // and avoid dangling references to our adapter.
+ int entryCount = adapter->navigationEntryCount();
+ while (items.size() > entryCount) {
+ items.last().d->adapter = 0;
+ items.removeLast();
+ }
+ while (items.size() < entryCount) {
+ int nextIndex = items.size();
+ items.append(QWebEngineHistoryItem(new QWebEngineHistoryItemPrivate(adapter, nextIndex)));
+ }
+}
+
QWebEngineHistory::QWebEngineHistory(QWebEngineHistoryPrivate *d)
: d_ptr(d)
{
@@ -59,6 +156,37 @@ QWebEngineHistory::~QWebEngineHistory()
{
}
+void QWebEngineHistory::clear()
+{
+ Q_D(const QWebEngineHistory);
+ d->adapter->clearNavigationHistory();
+}
+
+QList<QWebEngineHistoryItem> QWebEngineHistory::items() const
+{
+ Q_D(const QWebEngineHistory);
+ d->updateItems();
+ return d->items;
+}
+
+QList<QWebEngineHistoryItem> QWebEngineHistory::backItems(int maxItems) const
+{
+ Q_D(const QWebEngineHistory);
+ d->updateItems();
+ const int end = currentItemIndex();
+ const int start = std::max(0, end - maxItems);
+ return d->items.mid(start, end - start);
+}
+
+QList<QWebEngineHistoryItem> QWebEngineHistory::forwardItems(int maxItems) const
+{
+ Q_D(const QWebEngineHistory);
+ d->updateItems();
+ const int start = currentItemIndex() + 1;
+ const int end = std::min(count(), start + maxItems);
+ return d->items.mid(start, end - start);
+}
+
bool QWebEngineHistory::canGoBack() const
{
Q_D(const QWebEngineHistory);
@@ -70,3 +198,87 @@ bool QWebEngineHistory::canGoForward() const
Q_D(const QWebEngineHistory);
return d->adapter->canGoForward();
}
+
+void QWebEngineHistory::back()
+{
+ Q_D(const QWebEngineHistory);
+ d->adapter->navigateToOffset(-1);
+}
+
+void QWebEngineHistory::forward()
+{
+ Q_D(const QWebEngineHistory);
+ d->adapter->navigateToOffset(1);
+}
+
+void QWebEngineHistory::goToItem(const QWebEngineHistoryItem &item)
+{
+ Q_D(const QWebEngineHistory);
+ Q_ASSERT(item.d->adapter == d->adapter);
+ d->adapter->navigateToIndex(item.d->index);
+}
+
+QWebEngineHistoryItem QWebEngineHistory::backItem() const
+{
+ return itemAt(currentItemIndex() - 1);
+}
+
+QWebEngineHistoryItem QWebEngineHistory::currentItem() const
+{
+ Q_D(const QWebEngineHistory);
+ d->updateItems();
+ return d->items[currentItemIndex()];
+}
+
+QWebEngineHistoryItem QWebEngineHistory::forwardItem() const
+{
+ return itemAt(currentItemIndex() + 1);
+}
+
+QWebEngineHistoryItem QWebEngineHistory::itemAt(int i) const
+{
+ Q_D(const QWebEngineHistory);
+ if (i >= 0 && i < count()) {
+ d->updateItems();
+ return d->items[i];
+ } else {
+ // Return an invalid item right away.
+ QWebEngineHistoryItem item(new QWebEngineHistoryItemPrivate(0, i));
+ Q_ASSERT(!item.isValid());
+ return item;
+ }
+}
+
+int QWebEngineHistory::currentItemIndex() const
+{
+ Q_D(const QWebEngineHistory);
+ return d->adapter->currentNavigationEntryIndex();
+}
+
+int QWebEngineHistory::count() const
+{
+ Q_D(const QWebEngineHistory);
+ return d->adapter->navigationEntryCount();
+}
+
+int QWebEngineHistory::maximumItemCount() const
+{
+ return 100;
+}
+
+void QWebEngineHistory::setMaximumItemCount(int count)
+{
+ qWarning("Not implemented: %s", __func__);
+}
+
+QDataStream& operator<<(QDataStream& stream, const QWebEngineHistory& history)
+{
+ qWarning("Not implemented: %s", __func__);
+ return stream;
+}
+
+QDataStream& operator>>(QDataStream& stream, QWebEngineHistory& history)
+{
+ qWarning("Not implemented: %s", __func__);
+ return stream;
+}
diff --git a/lib/widgets/Api/qwebenginehistory.h b/lib/widgets/Api/qwebenginehistory.h
index fb98bb94d..a1097df1c 100644
--- a/lib/widgets/Api/qwebenginehistory.h
+++ b/lib/widgets/Api/qwebenginehistory.h
@@ -28,6 +28,8 @@
#include <QtGui/qicon.h>
#include <QtWebEngineWidgets/qtwebenginewidgetsglobal.h>
+class QWebEngineHistory;
+class QWebEngineHistoryItemPrivate;
class QWebEnginePage;
class QWebEnginePagePrivate;
@@ -49,6 +51,12 @@ public:
void setUserData(const QVariant& userData);
bool isValid() const;
+private:
+ QWebEngineHistoryItem(QWebEngineHistoryItemPrivate *priv);
+ Q_DECLARE_PRIVATE_D(d.data(), QWebEngineHistoryItem);
+ QExplicitlySharedDataPointer<QWebEngineHistoryItemPrivate> d;
+ friend class QWebEngineHistory;
+ friend class QWebEngineHistoryPrivate;
};
diff --git a/lib/widgets/Api/qwebenginehistory_p.h b/lib/widgets/Api/qwebenginehistory_p.h
index 1a6d7e17e..52439b93f 100644
--- a/lib/widgets/Api/qwebenginehistory_p.h
+++ b/lib/widgets/Api/qwebenginehistory_p.h
@@ -42,14 +42,28 @@
#ifndef QWEBENGINEHISTORY_P_H
#define QWEBENGINEHISTORY_P_H
+#include <QtCore/qshareddata.h>
+
class WebContentsAdapter;
+class QWebEngineHistoryItemPrivate : public QSharedData
+{
+public:
+ QWebEngineHistoryItemPrivate(WebContentsAdapter *adapter = 0, int index = 0);
+
+ WebContentsAdapter *adapter;
+ int index;
+};
+
class QWebEngineHistoryPrivate
{
public:
QWebEngineHistoryPrivate(WebContentsAdapter *adapter);
+ ~QWebEngineHistoryPrivate();
+ void updateItems() const;
WebContentsAdapter *adapter;
+ mutable QList<QWebEngineHistoryItem> items;
};
#endif // QWEBENGINEHISTORY_P_H
diff --git a/lib/widgets/Api/qwebenginepage.cpp b/lib/widgets/Api/qwebenginepage.cpp
index 037d05aee..0bc6d85e1 100644
--- a/lib/widgets/Api/qwebenginepage.cpp
+++ b/lib/widgets/Api/qwebenginepage.cpp
@@ -228,10 +228,10 @@ void QWebEnginePage::triggerAction(WebAction action, bool)
Q_D(QWebEnginePage);
switch (action) {
case Back:
- d->adapter->navigateHistory(-1);
+ d->adapter->navigateToOffset(-1);
break;
case Forward:
- d->adapter->navigateHistory(1);
+ d->adapter->navigateToOffset(1);
break;
case Stop:
d->adapter->stop();
diff --git a/lib/widgets/Api/qwebengineview.cpp b/lib/widgets/Api/qwebengineview.cpp
index 1bf1acf4d..6ae19ca75 100644
--- a/lib/widgets/Api/qwebengineview.cpp
+++ b/lib/widgets/Api/qwebengineview.cpp
@@ -114,6 +114,11 @@ void QWebEngineView::load(const QUrl& url)
page()->load(url);
}
+QWebEngineHistory* QWebEngineView::history() const
+{
+ return page()->history();
+}
+
QString QWebEngineView::title() const
{
return page()->title();