Skip to content

Commit 40f00d2

Browse files
committed
Implement IBrowserHost.GetNavigationEntries
Added TaskNavigationEntryVisitor - currently there is no async method exposed on IBrowserHost (be nice if someone else contributed a PR that implemented this). Resolve #1503
1 parent b10a7ef commit 40f00d2

11 files changed

+320
-0
lines changed

CefSharp.Core/CefSharp.Core.vcxproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@
252252
<ItemGroup>
253253
<ClInclude Include="BrowserSettings.h" />
254254
<ClInclude Include="Cef.h" />
255+
<ClInclude Include="Internals\CefNavigationEntryVisitorAdapter.h" />
255256
<ClInclude Include="Internals\CefGetGeolocationCallbackWrapper.h" />
256257
<ClInclude Include="Internals\CefAuthCallbackWrapper.h" />
257258
<ClInclude Include="Internals\CefBeforeDownloadCallbackWrapper.h" />

CefSharp.Core/CefSharp.Core.vcxproj.filters

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,9 @@
256256
<ClInclude Include="Internals\CefResolveCallbackAdapter.h">
257257
<Filter>Header Files</Filter>
258258
</ClInclude>
259+
<ClInclude Include="Internals\CefNavigationEntryVisitorAdapter.h">
260+
<Filter>Header Files</Filter>
261+
</ClInclude>
259262
</ItemGroup>
260263
<ItemGroup>
261264
<ClInclude Include="Internals\CefSharpBrowserWrapper.h">

CefSharp.Core/Internals/CefBrowserHostWrapper.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "CefTaskScheduler.h"
1313
#include "Cef.h"
1414
#include "RequestContext.h"
15+
#include "CefNavigationEntryVisitorAdapter.h"
1516

1617
void CefBrowserHostWrapper::DragTargetDragEnter(IDragData^ dragData, MouseEvent^ mouseEvent, DragOperationsMask allowedOperations)
1718
{
@@ -332,6 +333,15 @@ void CefBrowserHostWrapper::WasHidden(bool hidden)
332333
_browserHost->WasHidden(hidden);
333334
}
334335

336+
void CefBrowserHostWrapper::GetNavigationEntries(INavigationEntryVisitor^ visitor, bool currentOnly)
337+
{
338+
ThrowIfDisposed();
339+
340+
auto navEntryVisitor = new CefNavigationEntryVisitorAdapter(visitor);
341+
342+
_browserHost->GetNavigationEntries(navEntryVisitor, currentOnly);
343+
}
344+
335345
void CefBrowserHostWrapper::NotifyMoveOrResizeStarted()
336346
{
337347
ThrowIfDisposed();

CefSharp.Core/Internals/CefBrowserHostWrapper.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ namespace CefSharp
8585

8686
virtual void WasHidden(bool hidden);
8787

88+
virtual void GetNavigationEntries(INavigationEntryVisitor^ visitor, bool currentOnly);
89+
8890
virtual property int WindowlessFrameRate
8991
{
9092
int get();
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright © 2010-2016 The CefSharp Authors. All rights reserved.
2+
//
3+
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
4+
5+
#pragma once
6+
7+
#include "Stdafx.h"
8+
#include "include\cef_browser.h"
9+
10+
namespace CefSharp
11+
{
12+
namespace Internals
13+
{
14+
private class CefNavigationEntryVisitorAdapter : public CefNavigationEntryVisitor
15+
{
16+
private:
17+
gcroot<INavigationEntryVisitor^> _handler;
18+
19+
public:
20+
CefNavigationEntryVisitorAdapter(INavigationEntryVisitor^ handler)
21+
{
22+
_handler = handler;
23+
}
24+
25+
~CefNavigationEntryVisitorAdapter()
26+
{
27+
delete _handler;
28+
_handler = nullptr;
29+
}
30+
31+
bool Visit(CefRefPtr<CefNavigationEntry> entry,
32+
bool current,
33+
int index,
34+
int total) OVERRIDE
35+
{
36+
NavigationEntry navEntry;
37+
38+
if (entry->IsValid())
39+
{
40+
auto time = entry->GetCompletionTime();
41+
DateTime completionTime = CefSharp::Internals::CefTimeUtils::ConvertCefTimeToDateTime(time.GetDoubleT());
42+
navEntry = NavigationEntry(current, completionTime, StringUtils::ToClr(entry->GetDisplayURL()), entry->GetHttpStatusCode(), StringUtils::ToClr(entry->GetOriginalURL()), StringUtils::ToClr(entry->GetTitle()), (TransitionType)entry->GetTransitionType(), StringUtils::ToClr(entry->GetURL()), entry->HasPostData(), true);
43+
}
44+
else
45+
{
46+
//Invalid nav entry
47+
navEntry = NavigationEntry(current, DateTime::MinValue, nullptr, -1, nullptr, nullptr, (TransitionType)-1, nullptr, false, false);
48+
}
49+
50+
return _handler->Visit(navEntry, current, index, total);
51+
}
52+
53+
IMPLEMENT_REFCOUNTING(CefNavigationEntryVisitorAdapter);
54+
};
55+
}
56+
}
57+

CefSharp/CefSharp.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@
8888
<Compile Include="IBrowserSettings.cs" />
8989
<Compile Include="IDomNode.cs" />
9090
<Compile Include="IFindHandler.cs" />
91+
<Compile Include="INavigationEntryVisitor.cs" />
92+
<Compile Include="Internals\CefTimeUtils.cs" />
9193
<Compile Include="Internals\CommandLineArgsParser.cs" />
9294
<Compile Include="Internals\MethodParameter.cs" />
9395
<Compile Include="IPluginHandler.cs" />
@@ -98,6 +100,7 @@
98100
<Compile Include="IResponseFilter.cs" />
99101
<Compile Include="IRunContextMenuCallback.cs" />
100102
<Compile Include="MenuItemType.cs" />
103+
<Compile Include="NavigationEntry.cs" />
101104
<Compile Include="PdfPrintSettings.cs" />
102105
<Compile Include="ICookieManager.cs" />
103106
<Compile Include="ILoadHandler.cs" />
@@ -194,6 +197,7 @@
194197
<Compile Include="TaskCookieVisitor.cs" />
195198
<Compile Include="TaskPrintToPdfCallback.cs" />
196199
<Compile Include="TaskResolveCallbackHandler.cs" />
200+
<Compile Include="TaskNavigationEntryVisitor.cs" />
197201
<Compile Include="TransitionType.cs" />
198202
<Compile Include="LoadingStateChangedEventArgs.cs" />
199203
<Compile Include="ICompletionCallback.cs" />

CefSharp/IBrowserHost.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,15 @@ public interface IBrowserHost : IDisposable
272272
/// </summary>
273273
void WasResized();
274274

275+
/// <summary>
276+
/// Retrieve a snapshot of current navigation entries as values sent to the
277+
/// specified visitor.
278+
/// </summary>
279+
/// <param name="visitor">visitor</param>
280+
/// <param name="currentOnly">If true only the current navigation
281+
/// entry will be sent, otherwise all navigation entries will be sent.</param>
282+
void GetNavigationEntries(INavigationEntryVisitor visitor, bool currentOnly);
283+
275284
/// <summary>
276285
/// Gets/sets the maximum rate in frames per second (fps) that CefRenderHandler::
277286
/// OnPaint will be called for a windowless browser. The actual fps may be
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright © 2010-2016 The CefSharp Authors. All rights reserved.
2+
//
3+
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
4+
5+
using System;
6+
7+
namespace CefSharp
8+
{
9+
/// <summary>
10+
/// Callback interface for IBrowserHost.GetNavigationEntries.
11+
/// The methods of this class will be called on the CEF UI thread.
12+
/// </summary>
13+
public interface INavigationEntryVisitor : IDisposable
14+
{
15+
/// <summary>
16+
/// Method that will be executed.
17+
/// </summary>
18+
/// <param name="entry">if the navigationEntry will be invalid then </param>
19+
/// <param name="current">is true if this entry is the currently loaded navigation entry</param>
20+
/// <param name="index">is the 0-based index of this entry</param>
21+
/// <param name="total">is the total number of entries.</param>
22+
/// <returns>Return true to continue visiting entries or false to stop.</returns>
23+
bool Visit(NavigationEntry entry, bool current, int index, int total);
24+
}
25+
}

CefSharp/Internals/CefTimeUtils.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright © 2010-2016 The CefSharp Authors. All rights reserved.
2+
//
3+
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
4+
5+
using System;
6+
7+
namespace CefSharp.Internals
8+
{
9+
public static class CefTimeUtils
10+
{
11+
public static DateTime ConvertCefTimeToDateTime(double epoch)
12+
{
13+
if (epoch == 0)
14+
{
15+
return DateTime.MinValue;
16+
}
17+
return new DateTime(1970, 1, 1, 0, 0, 0).AddSeconds(epoch).ToLocalTime();
18+
}
19+
}
20+
}

CefSharp/NavigationEntry.cs

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
// Copyright © 2010-2016 The CefSharp Authors. All rights reserved.
2+
//
3+
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
4+
5+
using System;
6+
7+
namespace CefSharp
8+
{
9+
/// <summary>
10+
/// Represents an entry in navigation history.
11+
/// </summary>
12+
public struct NavigationEntry
13+
{
14+
private DateTime completionTime;
15+
private string displayUrl;
16+
private int httpStatusCode;
17+
private string originalUrl;
18+
private string title;
19+
private TransitionType transitionType;
20+
private string url;
21+
private bool hasPostData;
22+
private bool isValid;
23+
private bool isCurrent;
24+
25+
/// <summary>
26+
/// NavigationEntry
27+
/// </summary>
28+
/// <param name="completionTime">completionTime</param>
29+
/// <param name="displayUrl">displayUrl</param>
30+
/// <param name="httpStatusCode">httpStatusCode</param>
31+
/// <param name="originalUrl">originalUrl</param>
32+
/// <param name="title">title</param>
33+
/// <param name="transitionType">transitionType</param>
34+
/// <param name="url">url</param>
35+
/// <param name="hasPostData">hasPostData</param>
36+
/// <param name="isValid">isValid</param>
37+
/// <param name="isCurrent">is the current entry</param>
38+
public NavigationEntry(bool isCurrent, DateTime completionTime, string displayUrl, int httpStatusCode, string originalUrl, string title, TransitionType transitionType, string url, bool hasPostData, bool isValid)
39+
{
40+
this.isCurrent = isCurrent;
41+
this.completionTime = completionTime;
42+
this.displayUrl = displayUrl;
43+
this.httpStatusCode = httpStatusCode;
44+
this.originalUrl = originalUrl;
45+
this.title = title;
46+
this.transitionType = transitionType;
47+
this.url = url;
48+
this.hasPostData = hasPostData;
49+
this.isValid = isValid;
50+
}
51+
52+
/// <summary>
53+
/// Returns the time for the last known successful navigation completion.
54+
/// </summary>
55+
public DateTime CompletionTime
56+
{
57+
get { return completionTime; }
58+
}
59+
60+
/// <summary>
61+
/// Returns a display-friendly version of the URL.
62+
/// </summary>
63+
public string DisplayUrl
64+
{
65+
get { return displayUrl; }
66+
}
67+
68+
/// <summary>
69+
/// Returns the HTTP status code for the last known successful navigation response.
70+
/// </summary>
71+
public int HttpStatusCode
72+
{
73+
get { return httpStatusCode; }
74+
}
75+
76+
/// <summary>
77+
/// Returns the original URL that was entered by the user before any redirects.
78+
/// </summary>
79+
public string OriginalUrl
80+
{
81+
get { return originalUrl; }
82+
}
83+
84+
/// <summary>
85+
/// Returns the title set by the page.
86+
/// </summary>
87+
public string Title
88+
{
89+
get { return title; }
90+
}
91+
92+
/// <summary>
93+
/// Returns the transition type which indicates what the user did to move to this page from the previous page.
94+
/// </summary>
95+
public TransitionType TransitionType
96+
{
97+
get { return transitionType; }
98+
}
99+
100+
/// <summary>
101+
/// Returns the actual URL of the page.
102+
/// </summary>
103+
public string Url
104+
{
105+
get { return url; }
106+
}
107+
108+
/// <summary>
109+
/// Returns true if this navigation includes post data.
110+
/// </summary>
111+
public bool HasPostData
112+
{
113+
get { return hasPostData; }
114+
}
115+
116+
/// <summary>
117+
/// Returns true if this object is valid.
118+
/// </summary>
119+
public bool IsValid
120+
{
121+
get { return isValid;}
122+
}
123+
124+
/// <summary>
125+
/// If true if this entry is the currently loaded navigation entry
126+
/// </summary>
127+
public bool IsCurrent
128+
{
129+
get { return isCurrent; }
130+
}
131+
}
132+
}

0 commit comments

Comments
 (0)