From 3512a6cfe07cb7108485b3e39d586612aa5ed3d8 Mon Sep 17 00:00:00 2001
From: Jocelyn Turcotte <jocelyn.turcotte@digia.com>
Date: Thu, 20 Mar 2014 16:43:59 +0100
Subject: [PATCH] Implement QWebEngineHistoryItem::lastVisited

Change-Id: I105cb2a0a2479b146e2ab68db6d194ac2ac2d3f9
Reviewed-by: Andras Becsi <andras.becsi@digia.com>
Reviewed-by: Zeno Albisser <zeno.albisser@digia.com>
---
 src/core/type_conversion.h                                 | 7 +++++++
 src/core/web_contents_adapter.cpp                          | 7 +++++++
 src/core/web_contents_adapter.h                            | 1 +
 src/webenginewidgets/api/qwebenginehistory.cpp             | 4 ++--
 .../widgets/qwebenginehistory/tst_qwebenginehistory.cpp    | 7 +++++++
 5 files changed, 24 insertions(+), 2 deletions(-)

diff --git a/src/core/type_conversion.h b/src/core/type_conversion.h
index 5e489353c..5d5dc356d 100644
--- a/src/core/type_conversion.h
+++ b/src/core/type_conversion.h
@@ -43,11 +43,13 @@
 #define TYPE_CONVERSION_H
 
 #include <QColor>
+#include <QDateTime>
 #include <QMatrix4x4>
 #include <QRect>
 #include <QString>
 #include <QUrl>
 #include "base/files/file_path.h"
+#include "base/time/time.h"
 #include "third_party/skia/include/utils/SkMatrix44.h"
 #include "third_party/skia/include/core/SkColor.h"
 #include "ui/gfx/rect.h"
@@ -126,6 +128,11 @@ inline QMatrix4x4 toQt(const SkMatrix44 &m)
         m.get(3, 0), m.get(3, 1), m.get(3, 2), m.get(3, 3));
 }
 
+inline QDateTime toQt(base::Time time)
+{
+    return QDateTime::fromMSecsSinceEpoch(time.ToJavaTime());
+}
+
 inline base::FilePath::StringType toFilePathString(const QString &str)
 {
 #if defined(OS_WIN)
diff --git a/src/core/web_contents_adapter.cpp b/src/core/web_contents_adapter.cpp
index 7dad03e45..7fbda1ab6 100644
--- a/src/core/web_contents_adapter.cpp
+++ b/src/core/web_contents_adapter.cpp
@@ -565,6 +565,13 @@ QString WebContentsAdapter::getNavigationEntryTitle(int index)
     return entry ? toQt(entry->GetTitle()) : QString();
 }
 
+QDateTime WebContentsAdapter::getNavigationEntryTimestamp(int index)
+{
+    Q_D(WebContentsAdapter);
+    content::NavigationEntry *entry = d->webContents->GetController().GetEntryAtIndex(index);
+    return entry ? toQt(entry->GetTimestamp()) : QDateTime();
+}
+
 void WebContentsAdapter::clearNavigationHistory()
 {
     Q_D(WebContentsAdapter);
diff --git a/src/core/web_contents_adapter.h b/src/core/web_contents_adapter.h
index ba59252bf..6ce15aa6a 100644
--- a/src/core/web_contents_adapter.h
+++ b/src/core/web_contents_adapter.h
@@ -89,6 +89,7 @@ public:
     QUrl getNavigationEntryOriginalUrl(int index);
     QUrl getNavigationEntryUrl(int index);
     QString getNavigationEntryTitle(int index);
+    QDateTime getNavigationEntryTimestamp(int index);
     void clearNavigationHistory();
     void serializeNavigationHistory(QDataStream &output);
     void setZoomFactor(qreal);
diff --git a/src/webenginewidgets/api/qwebenginehistory.cpp b/src/webenginewidgets/api/qwebenginehistory.cpp
index 9b76fa127..cbd3fb3b1 100644
--- a/src/webenginewidgets/api/qwebenginehistory.cpp
+++ b/src/webenginewidgets/api/qwebenginehistory.cpp
@@ -97,8 +97,8 @@ QString QWebEngineHistoryItem::title() const
 
 QDateTime QWebEngineHistoryItem::lastVisited() const
 {
-    qWarning("Not implemented: %s", __func__);
-    return QDateTime();
+    Q_D(const QWebEngineHistoryItem);
+    return d->page ? d->page->webContents()->getNavigationEntryTimestamp(d->index) : QDateTime();
 }
 
 QIcon QWebEngineHistoryItem::icon() const
diff --git a/tests/auto/widgets/qwebenginehistory/tst_qwebenginehistory.cpp b/tests/auto/widgets/qwebenginehistory/tst_qwebenginehistory.cpp
index cebfee67a..a5cbc6103 100644
--- a/tests/auto/widgets/qwebenginehistory/tst_qwebenginehistory.cpp
+++ b/tests/auto/widgets/qwebenginehistory/tst_qwebenginehistory.cpp
@@ -47,6 +47,7 @@ public Q_SLOTS:
 
 private Q_SLOTS:
     void title();
+    void lastVisited();
     void count();
     void back();
     void forward();
@@ -111,6 +112,12 @@ void tst_QWebEngineHistory::title()
     QCOMPARE(hist->currentItem().title(), QString("page5"));
 }
 
+void tst_QWebEngineHistory::lastVisited()
+{
+    // Check that the conversion from Chromium's internal time format went well.
+    QVERIFY(qAbs(hist->itemAt(0).lastVisited().secsTo(QDateTime::currentDateTime())) < 60);
+}
+
 /**
   * Check QWebEngineHistory::count() method
   */
-- 
GitLab