Use CefSharp instead of CefGlue. Abstracted the Cef bits so we can make future changes easier

This commit is contained in:
Timothy Baldridge 2019-12-26 16:26:53 -07:00
parent 54644f06f8
commit 9a6d93524c
9 changed files with 114 additions and 54 deletions

View File

@ -15,7 +15,7 @@ using Wabbajack.Common;
using Wabbajack.Lib.LibCefHelpers;
using Wabbajack.Lib.NexusApi;
using Wabbajack.Lib.Validation;
using Xilium.CefGlue.Common;
using Wabbajack.Lib.WebAutomation;
using File = Alphaleonis.Win32.Filesystem.File;
namespace Wabbajack.Lib.Downloaders
@ -64,16 +64,15 @@ namespace Wabbajack.Lib.Downloaders
_authedClient = (await GetAuthedClient()) ?? throw new Exception("not logged into LL, TODO");
}
public static async Task<Helpers.Cookie[]> GetAndCacheLoversLabCookies(BaseCefBrowser browser, Action<string> updateStatus, CancellationToken cancel)
public static async Task<Helpers.Cookie[]> GetAndCacheLoversLabCookies(IWebDriver browser, Action<string> updateStatus, CancellationToken cancel)
{
updateStatus("Please Log Into Lovers Lab");
browser.Address = "https://www.loverslab.com/login";
await browser.NavigateTo(new Uri("https://www.loverslab.com/login"));
async Task<bool> CleanAds()
{
try
{
await browser.EvaluateJavaScript<string>(
await browser.EvaluateJavaScript(
"document.querySelectorAll(\".ll_adblock\").forEach(function (itm) { itm.innerHTML = \"\";});");
}
catch (Exception ex)
@ -87,7 +86,7 @@ namespace Wabbajack.Lib.Downloaders
{
cancel.ThrowIfCancellationRequested();
await CleanAds();
cookies = (await Helpers.GetCookies("loverslab.com"));
cookies = (await browser.GetCookies("loverslab.com"));
if (cookies.FirstOrDefault(c => c.Name == "ips4_member_id") != null)
break;
await Task.Delay(500, cancel);

View File

@ -8,8 +8,8 @@ using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Alphaleonis.Win32.Filesystem;
using CefSharp;
using Wabbajack.Common;
using Xilium.CefGlue;
namespace Wabbajack.Lib.LibCefHelpers
{
@ -69,7 +69,7 @@ namespace Wabbajack.Lib.LibCefHelpers
public static async Task<Cookie[]> GetCookies(string domainEnding)
{
var manager = CefCookieManager.GetGlobal(null);
var manager = Cef.GetGlobalCookieManager();
var visitor = new CookieVisitor();
if (!manager.VisitAllCookies(visitor))
return new Cookie[0];
@ -78,13 +78,18 @@ namespace Wabbajack.Lib.LibCefHelpers
return (await visitor.Task).Where(c => c.Domain.EndsWith(domainEnding)).ToArray();
}
private class CookieVisitor : CefCookieVisitor
private class CookieVisitor : ICookieVisitor
{
TaskCompletionSource<List<Cookie>> _source = new TaskCompletionSource<List<Cookie>>();
public Task<List<Cookie>> Task => _source.Task;
public List<Cookie> Cookies { get; } = new List<Cookie>();
protected override bool Visit(CefCookie cookie, int count, int total, out bool delete)
public void Dispose()
{
_source.SetResult(Cookies);
}
public bool Visit(CefSharp.Cookie cookie, int count, int total, ref bool deleteCookie)
{
Cookies.Add(new Cookie
{
@ -95,15 +100,9 @@ namespace Wabbajack.Lib.LibCefHelpers
});
if (count == total)
_source.SetResult(Cookies);
delete = false;
deleteCookie = false;
return true;
}
protected override void Dispose(bool disposing)
{
if (disposing)
_source.SetResult(Cookies);
}
}
public class Cookie

View File

@ -1,7 +1,6 @@
using ReactiveUI;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
@ -9,22 +8,18 @@ using System.Net.Http;
using System.Net.Http.Headers;
using System.Reflection;
using System.Security.Authentication;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Documents;
using Syroot.Windows.IO;
using Wabbajack.Common;
using Wabbajack.Lib.Downloaders;
using Wabbajack.Lib.LibCefHelpers;
using WebSocketSharp;
using Xilium.CefGlue;
using Xilium.CefGlue.Common;
using Xilium.CefGlue.Common.Handlers;
using Xilium.CefGlue.WPF;
using static Wabbajack.Lib.NexusApi.NexusApiUtils;
using System.Threading;
using CefSharp;
using CefSharp.Handler;
using Newtonsoft.Json;
using Wabbajack.Lib.WebAutomation;
namespace Wabbajack.Lib.NexusApi
{
@ -98,30 +93,13 @@ namespace Wabbajack.Lib.NexusApi
return result;
}
class RefererHandler : RequestHandler
{
private string _referer;
public RefererHandler(string referer)
{
_referer = referer;
}
protected override bool OnBeforeBrowse(CefBrowser browser, CefFrame frame, CefRequest request, bool userGesture, bool isRedirect)
{
base.OnBeforeBrowse(browser, frame, request, userGesture, isRedirect);
if (request.ReferrerURL == null)
request.SetReferrer(_referer, CefReferrerPolicy.Default);
return false;
}
}
public static async Task<string> SetupNexusLogin(BaseCefBrowser browser, Action<string> updateStatus, CancellationToken cancel)
public static async Task<string> SetupNexusLogin(IWebDriver browser, Action<string> updateStatus, CancellationToken cancel)
{
updateStatus("Please Log Into the Nexus");
browser.Address = "https://users.nexusmods.com/auth/continue?client_id=nexus&redirect_uri=https://www.nexusmods.com/oauth/callback&response_type=code&referrer=//www.nexusmods.com";
await browser.NavigateTo(new Uri("https://users.nexusmods.com/auth/continue?client_id=nexus&redirect_uri=https://www.nexusmods.com/oauth/callback&response_type=code&referrer=//www.nexusmods.com"));
while (true)
{
var cookies = await Helpers.GetCookies("nexusmods.com");
var cookies = await browser.GetCookies("nexusmods.com");
if (cookies.Any(c => c.Name == "member_id"))
break;
cancel.ThrowIfCancellationRequested();
@ -147,7 +125,7 @@ namespace Wabbajack.Lib.NexusApi
await Task.Delay(1000, cancel);
// open a web browser to get user permission
browser.Address = $"https://www.nexusmods.com/sso?id={guid}&application={Consts.AppName}";
await browser.NavigateTo(new Uri($"https://www.nexusmods.com/sso?id={guid}&application={Consts.AppName}"));
using (cancel.Register(() =>
{
api_key.SetCanceled();

View File

@ -153,6 +153,8 @@
<Compile Include="ViewModel.cs" />
<Compile Include="VortexCompiler.cs" />
<Compile Include="VortexInstaller.cs" />
<Compile Include="WebAutomation\CefSharpWrapper.cs" />
<Compile Include="WebAutomation\IWebDriver.cs" />
<Compile Include="WebAutomation\WebAutomation.cs" />
<Compile Include="WebAutomation\WebAutomationWindow.xaml.cs">
<DependentUpon>WebAutomationWindow.xaml</DependentUpon>
@ -188,8 +190,8 @@
<PackageReference Include="AlphaFS">
<Version>2.2.6</Version>
</PackageReference>
<PackageReference Include="CefGlue.Wpf">
<Version>75.1.28</Version>
<PackageReference Include="CefSharp.OffScreen">
<Version>75.1.143</Version>
</PackageReference>
<PackageReference Include="Ceras">
<Version>4.1.7</Version>

View File

@ -0,0 +1,60 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Remoting.Channels;
using System.Text;
using System.Threading.Tasks;
using CefSharp;
using Wabbajack.Lib.LibCefHelpers;
namespace Wabbajack.Lib.WebAutomation
{
public class CefSharpWrapper : IWebDriver
{
private IWebBrowser _browser;
public CefSharpWrapper(IWebBrowser browser)
{
_browser = browser;
}
public Task NavigateTo(Uri uri)
{
var tcs = new TaskCompletionSource<bool>();
EventHandler<LoadingStateChangedEventArgs> handler = null;
handler = (sender, e) =>
{
if (!e.IsLoading)
{
_browser.LoadingStateChanged -= handler;
tcs.SetResult(true);
}
};
_browser.LoadingStateChanged += handler;
_browser.Load(uri.ToString());
return tcs.Task;
}
public async Task<string> EvaluateJavaScript(string text)
{
var result = await _browser.EvaluateScriptAsync(text);
if (!result.Success)
throw new Exception(result.Message);
return (string)result.Result;
}
public Task<Helpers.Cookie[]> GetCookies(string domainPrefix)
{
return Helpers.GetCookies(domainPrefix);
}
public async Task WaitForInitialized()
{
while (!_browser.IsBrowserInitialized)
await Task.Delay(100);
}
}
}

View File

@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Wabbajack.Lib.LibCefHelpers;
namespace Wabbajack.Lib.WebAutomation
{
public interface IWebDriver
{
Task NavigateTo(Uri uri);
Task<string> EvaluateJavaScript(string text);
Task<Helpers.Cookie[]> GetCookies(string domainPrefix);
}
}

View File

@ -10,6 +10,7 @@ using ReactiveUI;
using Wabbajack.Common;
using Wabbajack.Lib.Downloaders;
using Wabbajack.Lib.NexusApi;
using Wabbajack.Lib.WebAutomation;
using Wabbajack.UserInterventions;
namespace Wabbajack
@ -60,14 +61,16 @@ namespace Wabbajack
case RequestNexusAuthorization c:
await WrapBrowserJob(msg, async (vm, cancel) =>
{
var key = await NexusApiClient.SetupNexusLogin(vm.Browser, m => vm.Instructions = m, cancel.Token);
await vm.Driver.WaitForInitialized();
var key = await NexusApiClient.SetupNexusLogin(new CefSharpWrapper(vm.Browser), m => vm.Instructions = m, cancel.Token);
c.Resume(key);
});
break;
case RequestLoversLabLogin c:
await WrapBrowserJob(msg, async (vm, cancel) =>
{
var data = await LoversLabDownloader.GetAndCacheLoversLabCookies(vm.Browser, m => vm.Instructions = m, cancel.Token);
await vm.Driver.WaitForInitialized();
var data = await LoversLabDownloader.GetAndCacheLoversLabCookies(new CefSharpWrapper(vm.Browser), m => vm.Instructions = m, cancel.Token);
c.Resume(data);
});
break;

View File

@ -3,10 +3,13 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CefSharp;
using CefSharp.Wpf;
using ReactiveUI;
using ReactiveUI.Fody.Helpers;
using Wabbajack.Lib;
using Wabbajack.Lib.LibCefHelpers;
using Wabbajack.Lib.WebAutomation;
using Xilium.CefGlue.WPF;
namespace Wabbajack
@ -16,14 +19,14 @@ namespace Wabbajack
[Reactive]
public string Instructions { get; set; }
public WpfCefBrowser Browser { get; } = new WpfCefBrowser();
public IWebBrowser Browser { get; } = new ChromiumWebBrowser();
public CefSharpWrapper Driver => new CefSharpWrapper(Browser);
[Reactive]
public IReactiveCommand BackCommand { get; set; }
private WebBrowserVM(string url = "http://www.wabbajack.org")
{
Browser.Address = url;
Instructions = "Wabbajack Web Browser";
}

View File

@ -476,8 +476,8 @@
<PackageReference Include="AlphaFS">
<Version>2.2.6</Version>
</PackageReference>
<PackageReference Include="CefGlue.Wpf">
<Version>75.1.28</Version>
<PackageReference Include="CefSharp.Wpf">
<Version>75.1.143</Version>
</PackageReference>
<PackageReference Include="CommonMark.NET">
<Version>0.15.1</Version>