From 8fcb75d7e9a47764eb6745cf047e760c7ae356c7 Mon Sep 17 00:00:00 2001 From: Philip Beber Date: Thu, 1 Dec 2016 21:25:12 -0800 Subject: [PATCH 1/2] Added "OriginalUrl" property to DownloadItem which wraps "GetOriginalUrl" in CEF. Added a sidebar to the example app which shows the DownloadItems as received by CefSharp.Core/Internals/TypeConversion.h Added a View menu for hiding/showing the regular sidebar and the new download info sidebar. --- .gitignore | 2 + CefSharp.Core/Internals/TypeConversion.h | 1 + CefSharp.Example/CefExample.cs | 1 + CefSharp.Example/DownloadHandler.cs | 16 +- CefSharp.Wpf.Example/MainWindow.xaml | 4 + CefSharp.Wpf.Example/MainWindow.xaml.cs | 10 + .../ViewModels/BrowserTabViewModel.cs | 21 ++ .../Views/BrowserTabView.xaml | 237 +++++++++++++++++- .../Views/BrowserTabView.xaml.cs | 26 +- CefSharp/DownloadItem.cs | 5 + 10 files changed, 318 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index cc851f15e0..2dd9e157d8 100644 --- a/.gitignore +++ b/.gitignore @@ -16,6 +16,8 @@ *.swp *.user *.xml +*.VC.opendb +*.VC.db _ReSharper* bin/ diff --git a/CefSharp.Core/Internals/TypeConversion.h b/CefSharp.Core/Internals/TypeConversion.h index 7948d27b42..b9fedb061f 100644 --- a/CefSharp.Core/Internals/TypeConversion.h +++ b/CefSharp.Core/Internals/TypeConversion.h @@ -64,6 +64,7 @@ namespace CefSharp item->FullPath = StringUtils::ToClr(downloadItem->GetFullPath()); item->Id = downloadItem->GetId(); item->Url = StringUtils::ToClr(downloadItem->GetURL()); + item->OriginalUrl = StringUtils::ToClr(downloadItem->GetOriginalUrl()); item->SuggestedFileName = StringUtils::ToClr(downloadItem->GetSuggestedFileName()); item->ContentDisposition = StringUtils::ToClr(downloadItem->GetContentDisposition()); item->MimeType = StringUtils::ToClr(downloadItem->GetMimeType()); diff --git a/CefSharp.Example/CefExample.cs b/CefSharp.Example/CefExample.cs index 461bf441bd..0eb1f0dcde 100644 --- a/CefSharp.Example/CefExample.cs +++ b/CefSharp.Example/CefExample.cs @@ -24,6 +24,7 @@ public static class CefExample public const string BasicSchemeTestUrl = "custom://cefsharp/SchemeTest.html"; public const string ResponseFilterTestUrl = "custom://cefsharp/ResponseFilterTest.html"; public const string DraggableRegionTestUrl = "custom://cefsharp/DraggableRegionTest.html"; + public const string DownloadHandlerTestUrl = "custom://cefsharp/DownloadHandlerTest.html"; public const string TestResourceUrl = "http://test/resource/load"; public const string RenderProcessCrashedUrl = "http://processcrashed"; public const string TestUnicodeResourceUrl = "http://test/resource/loadUnicode"; diff --git a/CefSharp.Example/DownloadHandler.cs b/CefSharp.Example/DownloadHandler.cs index 8f487ed88e..8615f2c813 100644 --- a/CefSharp.Example/DownloadHandler.cs +++ b/CefSharp.Example/DownloadHandler.cs @@ -2,12 +2,23 @@ // // Use of this source code is governed by a BSD-style license that can be found in the LICENSE file. +using System; + namespace CefSharp.Example { public class DownloadHandler : IDownloadHandler { + public event EventHandler OnBeforeDownloadFired; + + public event EventHandler OnDownloadUpdatedFired; + public void OnBeforeDownload(IBrowser browser, DownloadItem downloadItem, IBeforeDownloadCallback callback) { + if (this.OnBeforeDownloadFired != null) + { + this.OnBeforeDownloadFired.Invoke(this, downloadItem); + } + if (!callback.IsDisposed) { using (callback) @@ -19,7 +30,10 @@ public void OnBeforeDownload(IBrowser browser, DownloadItem downloadItem, IBefor public void OnDownloadUpdated(IBrowser browser, DownloadItem downloadItem, IDownloadItemCallback callback) { - + if (this.OnDownloadUpdatedFired != null) + { + this.OnDownloadUpdatedFired.Invoke(this, downloadItem); + } } } } diff --git a/CefSharp.Wpf.Example/MainWindow.xaml b/CefSharp.Wpf.Example/MainWindow.xaml index 98c7aa32b1..cc5cb979f5 100644 --- a/CefSharp.Wpf.Example/MainWindow.xaml +++ b/CefSharp.Wpf.Example/MainWindow.xaml @@ -20,6 +20,10 @@ + + + + diff --git a/CefSharp.Wpf.Example/MainWindow.xaml.cs b/CefSharp.Wpf.Example/MainWindow.xaml.cs index 77a8a8885d..4ead0a2de7 100644 --- a/CefSharp.Wpf.Example/MainWindow.xaml.cs +++ b/CefSharp.Wpf.Example/MainWindow.xaml.cs @@ -131,6 +131,16 @@ private void CustomCommandBinding(object sender, ExecutedRoutedEventArgs e) cmd.Execute(null); } + if (param == "ToggleSidebar") + { + browserViewModel.ShowSidebar = !browserViewModel.ShowSidebar; + } + + if (param == "ToggleDownloadInfo") + { + browserViewModel.ShowDownloadInfo = !browserViewModel.ShowDownloadInfo; + } + //NOTE: Add as required //else if (param == "CustomRequest123") //{ diff --git a/CefSharp.Wpf.Example/ViewModels/BrowserTabViewModel.cs b/CefSharp.Wpf.Example/ViewModels/BrowserTabViewModel.cs index f72ee11d3f..aacec4beb6 100644 --- a/CefSharp.Wpf.Example/ViewModels/BrowserTabViewModel.cs +++ b/CefSharp.Wpf.Example/ViewModels/BrowserTabViewModel.cs @@ -70,6 +70,27 @@ public bool ShowSidebar set { Set(ref showSidebar, value); } } + private bool showDownloadInfo; + public bool ShowDownloadInfo + { + get { return showDownloadInfo; } + set { Set(ref showDownloadInfo, value); } + } + + private string lastDownloadAction; + public string LastDownloadAction + { + get { return lastDownloadAction; } + set { Set(ref lastDownloadAction, value); } + } + + private DownloadItem downloadItem; + public DownloadItem DownloadItem + { + get { return downloadItem; } + set { Set(ref downloadItem, value); } + } + public ICommand GoCommand { get; private set; } public ICommand HomeCommand { get; private set; } public ICommand ExecuteJavaScriptCommand { get; private set; } diff --git a/CefSharp.Wpf.Example/Views/BrowserTabView.xaml b/CefSharp.Wpf.Example/Views/BrowserTabView.xaml index 79f0b90dd4..7b3d95508f 100644 --- a/CefSharp.Wpf.Example/Views/BrowserTabView.xaml +++ b/CefSharp.Wpf.Example/Views/BrowserTabView.xaml @@ -14,7 +14,7 @@ - + @@ -44,8 +44,7 @@ GotKeyboardFocus="OnTextBoxGotKeyboardFocus" GotMouseCapture="OnTextBoxGotMouseCapture"> - + @@ -154,6 +153,238 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + { + var viewModel = (BrowserTabViewModel)this.DataContext; + viewModel.LastDownloadAction = downloadAction; + viewModel.DownloadItem = downloadItem; + }); + } + private void OnBrowserMouseLeftButtonDown(object sender, MouseButtonEventArgs e) { var point = e.GetPosition(browser); diff --git a/CefSharp/DownloadItem.cs b/CefSharp/DownloadItem.cs index f32473ef17..e9cd9e76f1 100644 --- a/CefSharp/DownloadItem.cs +++ b/CefSharp/DownloadItem.cs @@ -76,6 +76,11 @@ public class DownloadItem /// public string Url { get; set; } + /// + /// Returns the URL as it was before any redirects. + /// + public string OriginalUrl { get; set; } + /// /// Returns the suggested file name. /// From e0e3de9e514bd5b778fff655134f93895bfe6a9a Mon Sep 17 00:00:00 2001 From: Philip Beber Date: Fri, 2 Dec 2016 09:16:59 -0800 Subject: [PATCH 2/2] Pull request feedback --- CefSharp.Example/CefExample.cs | 1 - CefSharp.Example/DownloadHandler.cs | 10 ++++++---- CefSharp.Wpf.Example/Views/BrowserTabView.xaml | 5 +++-- CefSharp.Wpf.Example/Views/BrowserTabView.xaml.cs | 2 +- 4 files changed, 10 insertions(+), 8 deletions(-) diff --git a/CefSharp.Example/CefExample.cs b/CefSharp.Example/CefExample.cs index 0eb1f0dcde..461bf441bd 100644 --- a/CefSharp.Example/CefExample.cs +++ b/CefSharp.Example/CefExample.cs @@ -24,7 +24,6 @@ public static class CefExample public const string BasicSchemeTestUrl = "custom://cefsharp/SchemeTest.html"; public const string ResponseFilterTestUrl = "custom://cefsharp/ResponseFilterTest.html"; public const string DraggableRegionTestUrl = "custom://cefsharp/DraggableRegionTest.html"; - public const string DownloadHandlerTestUrl = "custom://cefsharp/DownloadHandlerTest.html"; public const string TestResourceUrl = "http://test/resource/load"; public const string RenderProcessCrashedUrl = "http://processcrashed"; public const string TestUnicodeResourceUrl = "http://test/resource/loadUnicode"; diff --git a/CefSharp.Example/DownloadHandler.cs b/CefSharp.Example/DownloadHandler.cs index 8615f2c813..44275d1f0a 100644 --- a/CefSharp.Example/DownloadHandler.cs +++ b/CefSharp.Example/DownloadHandler.cs @@ -14,9 +14,10 @@ public class DownloadHandler : IDownloadHandler public void OnBeforeDownload(IBrowser browser, DownloadItem downloadItem, IBeforeDownloadCallback callback) { - if (this.OnBeforeDownloadFired != null) + var handler = OnBeforeDownloadFired; + if (handler != null) { - this.OnBeforeDownloadFired.Invoke(this, downloadItem); + handler(this, downloadItem); } if (!callback.IsDisposed) @@ -30,9 +31,10 @@ public void OnBeforeDownload(IBrowser browser, DownloadItem downloadItem, IBefor public void OnDownloadUpdated(IBrowser browser, DownloadItem downloadItem, IDownloadItemCallback callback) { - if (this.OnDownloadUpdatedFired != null) + var handler = OnDownloadUpdatedFired; + if (handler != null) { - this.OnDownloadUpdatedFired.Invoke(this, downloadItem); + handler(this, downloadItem); } } } diff --git a/CefSharp.Wpf.Example/Views/BrowserTabView.xaml b/CefSharp.Wpf.Example/Views/BrowserTabView.xaml index 7b3d95508f..e7cea5f969 100644 --- a/CefSharp.Wpf.Example/Views/BrowserTabView.xaml +++ b/CefSharp.Wpf.Example/Views/BrowserTabView.xaml @@ -14,7 +14,7 @@ - + @@ -44,7 +44,8 @@ GotKeyboardFocus="OnTextBoxGotKeyboardFocus" GotMouseCapture="OnTextBoxGotMouseCapture"> - + diff --git a/CefSharp.Wpf.Example/Views/BrowserTabView.xaml.cs b/CefSharp.Wpf.Example/Views/BrowserTabView.xaml.cs index 911ee8827a..ea99c7dc88 100644 --- a/CefSharp.Wpf.Example/Views/BrowserTabView.xaml.cs +++ b/CefSharp.Wpf.Example/Views/BrowserTabView.xaml.cs @@ -101,7 +101,7 @@ private void OnDownloadUpdatedFired(object sender, DownloadItem e) private void UpdateDownloadAction(string downloadAction, DownloadItem downloadItem) { - this.Dispatcher.Invoke(() => + this.Dispatcher.InvokeAsync(() => { var viewModel = (BrowserTabViewModel)this.DataContext; viewModel.LastDownloadAction = downloadAction;