Skip to content

Commit

Permalink
Added several handlers
Browse files Browse the repository at this point in the history
Added custom context menu items
Added full support for downloads
Added browser settings to saved user preferences
Fixed initalization and deinitalization of CefSharp (no more lingering processes)
Updated Readme to reflect changes
  • Loading branch information
Cryptobyte committed Mar 30, 2018
1 parent f95512a commit ce3b95b
Show file tree
Hide file tree
Showing 19 changed files with 1,843 additions and 136 deletions.
2 changes: 2 additions & 0 deletions Opeity.sln.DotSettings
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:Boolean x:Key="/Default/CodeInspection/CodeAnnotations/NamespacesWithAnnotations/=Opeity_002EAnnotations/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
30 changes: 30 additions & 0 deletions Opeity/App.config
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,36 @@
<setting name="Home" serializeAs="String">
<value>https://start.duckduckgo.com</value>
</setting>
<setting name="WebSecurity" serializeAs="String">
<value>True</value>
</setting>
<setting name="ApplicationCache" serializeAs="String">
<value>False</value>
</setting>
<setting name="Databases" serializeAs="String">
<value>False</value>
</setting>
<setting name="LocalStorage" serializeAs="String">
<value>False</value>
</setting>
<setting name="JavascriptAccessClipboard" serializeAs="String">
<value>False</value>
</setting>
<setting name="JavascriptCloseWindows" serializeAs="String">
<value>False</value>
</setting>
<setting name="FileAccessFromFileUrls" serializeAs="String">
<value>False</value>
</setting>
<setting name="Plugins" serializeAs="String">
<value>False</value>
</setting>
<setting name="LoadImages" serializeAs="String">
<value>True</value>
</setting>
<setting name="WindowlessFrameRate" serializeAs="String">
<value>60</value>
</setting>
</Opeity.Properties.Settings>
</userSettings>
</configuration>
15 changes: 15 additions & 0 deletions Opeity/App.xaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<Application x:Class="Opeity.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mah="http://metro.mahapps.com/winfx/xaml/controls"
Startup="Application_Startup">
<Application.Resources>
<ResourceDictionary>
Expand All @@ -11,6 +12,20 @@
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/Blue.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/BaseLight.xaml" />
</ResourceDictionary.MergedDictionaries>

<Style x:Key="DownloadGridColumnHeader" BasedOn="{StaticResource MetroDataGridColumnHeader}" TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="Foreground" Value="White"/>
<Setter Property="Background" Value="Transparent" />
<Setter Property="BorderBrush" Value="Transparent" />
<Setter Property="BorderThickness" Value="0, 0, 0, 0" />
<Setter Property="SeparatorBrush" Value="Transparent" />
<Setter Property="SeparatorVisibility" Value="Collapsed" />
</Style>

<Style x:Key="DownloadGridRow" BasedOn="{StaticResource MetroDataGridRow}" TargetType="{x:Type DataGridRow}">
<Setter Property="Foreground" Value="White"/>
<Setter Property="Background" Value="Transparent" />
</Style>
</ResourceDictionary>
</Application.Resources>
</Application>
8 changes: 8 additions & 0 deletions Opeity/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using CefSharp;
using CommandLine;

namespace Opeity {
Expand All @@ -15,6 +16,13 @@ public partial class App : Application
{
private void Application_Startup(object sender, StartupEventArgs e)
{
Cef.EnableHighDPISupport();
Cef.Initialize(
new CefSettings(),
performDependencyCheck: true,
browserProcessHandler: null
);

new MainWindow().Show();
}
}
Expand Down
22 changes: 22 additions & 0 deletions Opeity/Handlers/ContextHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using CefSharp;

namespace Opeity.Handlers
{
public class RequestContextHandler : IRequestContextHandler
{
private ICookieManager customCookieManager;

public ICookieManager GetCookieManager()
{
if (customCookieManager == null)
customCookieManager = new CookieManager(null, persistSessionCookies: false, callback: null);

return customCookieManager;
}

public bool OnBeforePluginLoad(string mimeType, string url, bool isMainFrame, string topOriginUrl, WebPluginInfo pluginInfo, ref PluginPolicy pluginPolicy)
{
return false;
}
}
}
75 changes: 75 additions & 0 deletions Opeity/Handlers/ContextMenuHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using CefSharp;
using CefSharp.Wpf;
using System.Diagnostics;
using System.Windows;

namespace Opeity.Handlers
{
public class ContextMenuHandler : IContextMenuHandler
{
public void OnBeforeContextMenu(IWebBrowser browserControl, IBrowser browser, IFrame frame, IContextMenuParams parameters, IMenuModel model)
{
if (model.Count > 0)
{
model.AddSeparator();
}

if (!string.IsNullOrEmpty(parameters.UnfilteredLinkUrl))
model.AddItem((CefMenuCommand)26501, "Copy Link Address");

if (!string.IsNullOrEmpty(parameters.LinkUrl))
model.AddItem((CefMenuCommand)26502, "Open Link in New Window");

if (!string.IsNullOrEmpty(parameters.UnfilteredLinkUrl) || !string.IsNullOrEmpty(parameters.LinkUrl))
model.AddSeparator();

var dev = model.AddSubMenu((CefMenuCommand)26503, "Developer Tools");
dev.AddItem((CefMenuCommand)26504, "Inspector");
}

public bool OnContextMenuCommand(IWebBrowser browserControl, IBrowser browser, IFrame frame, IContextMenuParams parameters, CefMenuCommand commandId, CefEventFlags eventFlags)
{
switch (commandId)
{
case (CefMenuCommand)26501:
Clipboard.SetText(parameters.UnfilteredLinkUrl);
return true;

case (CefMenuCommand)26502:
Process.Start(new ProcessStartInfo()
{
FileName = System.Reflection.Assembly.GetExecutingAssembly().Location,
Arguments = $"-o \"{parameters.LinkUrl}\""
});

return true;

case (CefMenuCommand)26504:
if (browser.GetHost().HasDevTools)
browser.GetHost().CloseDevTools();

if (!browser.GetHost().HasDevTools)
browser.GetHost().ShowDevTools();

return true;

default: return false;
}
}

public void OnContextMenuDismissed(IWebBrowser browserControl, IBrowser browser, IFrame frame)
{
var chromiumWebBrowser = (ChromiumWebBrowser)browserControl;

chromiumWebBrowser.Dispatcher.Invoke(() =>
{
chromiumWebBrowser.ContextMenu = null;
});
}

public bool RunContextMenu(IWebBrowser browserControl, IBrowser browser, IFrame frame, IContextMenuParams parameters, IMenuModel model, IRunContextMenuCallback callback)
{
return false;
}
}
}
54 changes: 14 additions & 40 deletions Opeity/Handlers/DownloadHandler.cs
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
// Copyright © 2010-2017 The CefSharp Authors. All rights reserved.
//
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.

using System;
using System.Collections.ObjectModel;
using System.Windows.Shell;
using System.Windows.Threading;
using System;
using CefSharp;

namespace Opeity
namespace Opeity.Handlers
{
public class DownloadHandler : IDownloadHandler
{
public event EventHandler<DownloadItem> OnBeforeDownloadFired;
public event EventHandler<DownloadProgress> OnDownloadItemUpdated;

public class DownloadProgress : EventArgs
{
public DownloadItem Item { get; private set; }

public event EventHandler<DownloadItem> OnDownloadUpdatedFired;
private DownloadProgress() { }

public ObservableCollection<DownloadItem> FilesDownloading = new ObservableCollection<DownloadItem>();
public DownloadProgress(DownloadItem item)
{
Item = item;
}
}

public void OnBeforeDownload(IBrowser browser, DownloadItem downloadItem, IBeforeDownloadCallback callback)
{
OnBeforeDownloadFired?.Invoke(this, downloadItem);

if (callback.IsDisposed)
return;

Expand All @@ -33,32 +32,7 @@ public void OnBeforeDownload(IBrowser browser, DownloadItem downloadItem, IBefor

public void OnDownloadUpdated(IBrowser browser, DownloadItem downloadItem, IDownloadItemCallback callback)
{
if (downloadItem.IsInProgress)
{
if (!FilesDownloading.Contains(downloadItem))
FilesDownloading.Add(downloadItem);

OnDownloadUpdatedFired?.Invoke(this, downloadItem);
return;
}

if (downloadItem.IsCancelled)
{
OnDownloadCanceled(browser, downloadItem);
return;
}

OnDownloadComplete(browser, downloadItem);
}

public void OnDownloadComplete(IBrowser browser, DownloadItem downloadItem)
{
FilesDownloading.Remove(downloadItem);
}

public void OnDownloadCanceled(IBrowser browser, DownloadItem downloadItem)
{
FilesDownloading.Remove(downloadItem);
OnDownloadItemUpdated?.Invoke(this, new DownloadProgress(downloadItem));
}
}
}
10 changes: 2 additions & 8 deletions Opeity/Handlers/LifespanHandler.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
using CefSharp;

namespace Opeity
namespace Opeity.Handlers
{
public class LifespanHandler : ILifeSpanHandler
{
Expand Down
29 changes: 20 additions & 9 deletions Opeity/Handlers/RequestHandler.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using CefSharp;
using System.Diagnostics;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
using CefSharp;
using ControlzEx.Windows.Shell;
using System.Windows;

namespace Opeity.Handlers
{
Expand All @@ -20,6 +14,17 @@ public bool OnBeforeBrowse(IWebBrowser browserControl, IBrowser browser, IFrame

public bool OnOpenUrlFromTab(IWebBrowser browserControl, IBrowser browser, IFrame frame, string targetUrl, WindowOpenDisposition targetDisposition, bool userGesture)
{
if (userGesture)
{
Process.Start(new ProcessStartInfo()
{
FileName = System.Reflection.Assembly.GetExecutingAssembly().Location,
Arguments = $"-o \"{targetUrl}\""
});

return true;
}

return false;
}

Expand Down Expand Up @@ -65,6 +70,12 @@ public void OnResourceRedirect(IWebBrowser browserControl, IBrowser browser, IFr

public bool OnProtocolExecution(IWebBrowser browserControl, IBrowser browser, string url)
{

if (MessageBox.Show($"Do you want to open the application registered to handle this type of url?\n\n{url}", "Open Application?", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
{
Process.Start(url);
}

return false;
}

Expand Down
62 changes: 62 additions & 0 deletions Opeity/Helpers/FileSizeHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Opeity.Helpers
{
public class FileSizeHelper
{
//
// Source: https://stackoverflow.com/a/11124118
//
// Returns the human-readable file size for an arbitrary, 64-bit file size
// The default format is "0.### XB", e.g. "4.2 KB" or "1.434 GB"
public static string GetBytesReadable(long i)
{
var absoluteI = (i < 0 ? -i : i);

string suffix;
double readable;

if (absoluteI >= 0x1000000000000000)
{
suffix = "EBps";
readable = (i >> 50);
}
else if (absoluteI >= 0x4000000000000)
{
suffix = "PBps";
readable = (i >> 40);
}
else if (absoluteI >= 0x10000000000)
{
suffix = "TBps";
readable = (i >> 30);
}
else if (absoluteI >= 0x40000000)
{
suffix = "GBps";
readable = (i >> 20);
}
else if (absoluteI >= 0x100000)
{
suffix = "MBps";
readable = (i >> 10);
}
else if (absoluteI >= 0x400)
{
suffix = "KBps";
readable = i;
}
else
{
return i.ToString("0 Bps");
}

readable = readable / 1024;
return readable.ToString("0.# ") + suffix;
}
}
}
Loading

0 comments on commit ce3b95b

Please sign in to comment.