2018-09-10 02:41:13 +02:00
// Copyright © 2014 The CefSharp Authors. All rights reserved.
2015-08-03 10:41:29 +10:00
//
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
2018-09-10 02:41:13 +02:00
using System ;
using System.Diagnostics ;
2020-04-29 08:13:08 +10:00
using System.IO ;
2021-04-01 09:27:48 +02:00
using System.Runtime.InteropServices ;
2018-09-10 02:41:13 +02:00
using System.Text ;
2018-09-17 00:46:21 +02:00
using CefSharp.Example.Proxy ;
using CefSharp.SchemeHandler ;
2014-05-29 08:10:44 +10:00
namespace CefSharp.Example
{
public static class CefExample
{
2023-03-24 10:52:58 +10:00
//TODO: Revert after https://github.com/chromiumembedded/cef/issues/2685
2019-06-14 11:44:49 +10:00
//has been fixed.
2020-05-19 21:55:16 +10:00
public const string ExampleDomain = "cefsharp.example" ;
public const string BaseUrl = "https://" + ExampleDomain ;
2017-03-02 09:55:34 +10:00
public const string DefaultUrl = BaseUrl + "/home.html" ;
2020-12-16 11:25:49 +10:00
#if NETCOREAPP
public const string BindingTestUrl = BaseUrl + "/BindingTestNetCore.html" ;
#else
2017-03-02 09:55:34 +10:00
public const string BindingTestUrl = BaseUrl + "/BindingTest.html" ;
2020-12-16 11:25:49 +10:00
#endif
2020-07-24 21:07:29 +10:00
public const string BindingTestNetCoreUrl = BaseUrl + "/BindingTestNetCore.html" ;
2018-03-05 12:18:44 +10:00
public const string BindingTestSingleUrl = BaseUrl + "/BindingTestSingle.html" ;
2019-06-08 17:21:49 +10:00
public const string BindingTestsAsyncTaskUrl = BaseUrl + "/BindingTestsAsyncTask.html" ;
2018-01-22 10:24:25 +10:00
public const string LegacyBindingTestUrl = BaseUrl + "/LegacyBindingTest.html" ;
2019-06-03 21:08:03 +10:00
public const string PostMessageTestUrl = BaseUrl + "/PostMessageTest.html" ;
2017-03-02 09:55:34 +10:00
public const string PopupTestUrl = BaseUrl + "/PopupTest.html" ;
public const string TooltipTestUrl = BaseUrl + "/TooltipTest.html" ;
public const string BasicSchemeTestUrl = BaseUrl + "/SchemeTest.html" ;
public const string ResponseFilterTestUrl = BaseUrl + "/ResponseFilterTest.html" ;
public const string DraggableRegionTestUrl = BaseUrl + "/DraggableRegionTest.html" ;
2019-04-05 02:58:27 +01:00
public const string DragDropCursorsTestUrl = BaseUrl + "/DragDropCursorsTest.html" ;
2017-03-02 09:55:34 +10:00
public const string CssAnimationTestUrl = BaseUrl + "/CssAnimationTest.html" ;
public const string CdmSupportTestUrl = BaseUrl + "/CdmSupportTest.html" ;
2021-04-06 13:12:44 +10:00
public const string HelloWorldUrl = BaseUrl + "/HelloWorld.html" ;
2020-05-19 21:55:16 +10:00
public const string BindingApiCustomObjectNameTestUrl = BaseUrl + "/BindingApiCustomObjectNameTest.html" ;
2014-12-09 12:49:20 +10:00
public const string TestResourceUrl = "http://test/resource/load" ;
2015-08-26 17:26:45 +10:00
public const string RenderProcessCrashedUrl = "http://processcrashed" ;
2014-12-16 10:51:34 +10:00
public const string TestUnicodeResourceUrl = "http://test/resource/loadUnicode" ;
2015-07-28 17:26:34 +10:00
public const string PopupParentUrl = "http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_win_close" ;
2020-09-16 14:32:30 +10:00
public const string ChromeInternalUrls = "chrome://chrome-urls" ;
public const string ChromeNetInternalUrls = "chrome://net-internals" ;
public const string ChromeProcessInternalUrls = "chrome://process-internals" ;
2018-09-10 02:41:13 +02:00
2014-05-29 08:10:44 +10:00
// Use when debugging the actual SubProcess, to make breakpoints etc. inside that project work.
2014-11-05 11:05:41 +10:00
private static readonly bool DebuggingSubProcess = Debugger . IsAttached ;
2014-05-29 08:10:44 +10:00
2020-04-06 14:59:33 +10:00
public static void Init ( CefSettingsBase settings , IBrowserProcessHandler browserProcessHandler )
2014-05-29 08:10:44 +10:00
{
2015-01-28 14:38:19 -08:00
// Set Google API keys, used for Geolocation requests sans GPS. See http://www.chromium.org/developers/how-tos/api-keys
// Environment.SetEnvironmentVariable("GOOGLE_API_KEY", "");
// Environment.SetEnvironmentVariable("GOOGLE_DEFAULT_CLIENT_ID", "");
// Environment.SetEnvironmentVariable("GOOGLE_DEFAULT_CLIENT_SECRET", "");
2015-02-19 15:11:55 +10:00
//Chromium Command Line args
//http://peter.sh/experiments/chromium-command-line-switches/
2015-03-25 11:20:00 +10:00
//NOTE: Not all relevant in relation to `CefSharp`, use for reference purposes only.
2019-02-19 10:07:11 +10:00
//CEF specific command line args
//https://bitbucket.org/chromiumembedded/cef/src/master/libcef/common/cef_switches.cc?fileviewer=file-view-default
2021-03-22 14:14:38 +10:00
//**IMPORTANT**: For enabled/disabled command line arguments like disable-gpu specifying a value of "0" like
2019-09-06 13:04:49 +10:00
//settings.CefCommandLineArgs.Add("disable-gpu", "0"); will have no effect as the second argument is ignored.
2019-06-03 21:08:03 +10:00
2021-03-22 14:14:38 +10:00
//**IMPORTANT**: When using command line arguments the behaviour may change between Chromium versions, if you are experiencing
//issues after upgrading to a newer version, you should remove all custom command line arguments and add test them in
//isolation to determine if they are causing issues.
//The command line arguments shown here are here as a reference only and are not tested to confirm they work with each version
2014-05-29 08:10:44 +10:00
settings . RemoteDebuggingPort = 8088 ;
2015-05-13 09:20:15 +10:00
//The location where cache data will be stored on disk. If empty an in-memory cache will be used for some features and a temporary disk cache for others.
//HTML5 databases such as localStorage will only persist across sessions if a cache path is specified.
2020-05-03 16:59:57 +10:00
settings . RootCachePath = Path . GetFullPath ( "cache" ) ;
//If non-null then CachePath must be equal to or a child of RootCachePath
//We're using a sub folder.
//
settings . CachePath = Path . GetFullPath ( "cache\\global" ) ;
2015-02-27 10:36:06 +10:00
//settings.UserAgent = "CefSharp Browser" + Cef.CefSharpVersion; // Example User Agent
2019-09-06 13:04:49 +10:00
//settings.CefCommandLineArgs.Add("renderer-startup-dialog");
2022-12-06 14:54:19 +10:00
//settings.CefCommandLineArgs.Add("disable-site-isolation-trials");
2019-09-06 13:04:49 +10:00
//settings.CefCommandLineArgs.Add("enable-media-stream"); //Enable WebRTC
//settings.CefCommandLineArgs.Add("no-proxy-server"); //Don't use a proxy server, always make direct connections. Overrides any other proxy server flags that are passed.
2022-03-21 10:01:26 +10:00
//settings.CefCommandLineArgs.Add("allow-running-insecure-content"); //By default, an https page cannot run JavaScript or CSS from http URLs. This provides an override to get the old insecure behavior. Only available in 47 and above.
2019-11-23 06:47:29 +10:00
//https://peter.sh/experiments/chromium-command-line-switches/#disable-site-isolation-trials
//settings.CefCommandLineArgs.Add("disable-site-isolation-trials");
2020-06-19 20:48:25 +10:00
//NOTE: Running the Network Service in Process is not something CEF officially supports
//It may or may not work for newer versions.
//settings.CefCommandLineArgs.Add("enable-features", "CastMediaRouteProvider,NetworkServiceInProcess");
2019-09-06 13:04:49 +10:00
//settings.CefCommandLineArgs.Add("enable-logging"); //Enable Logging for the Renderer process (will open with a cmd prompt and output debug messages - use in conjunction with setting LogSeverity = LogSeverity.Verbose;)
2016-04-11 07:51:29 +10:00
//settings.LogSeverity = LogSeverity.Verbose; // Needed for enable-logging to output messages
2019-09-06 13:04:49 +10:00
//settings.CefCommandLineArgs.Add("disable-extensions"); //Extension support can be disabled
//settings.CefCommandLineArgs.Add("disable-pdf-extension"); //The PDF extension specifically can be disabled
2016-01-29 18:15:11 +10:00
2019-03-20 21:51:18 +10:00
//Audo play example
//settings.CefCommandLineArgs["autoplay-policy"] = "no-user-gesture-required";
2015-08-12 09:10:17 +10:00
//NOTE: For OSR best performance you should run with GPU disabled:
// `--disable-gpu --disable-gpu-compositing --enable-begin-frame-scheduling`
// (you'll loose WebGL support but gain increased FPS and reduced CPU usage).
// http://magpcss.org/ceforum/viewtopic.php?f=6&t=13271#p27075
2015-09-16 07:18:58 +10:00
//https://bitbucket.org/chromiumembedded/cef/commits/e3c1d8632eb43c1c2793d71639f3f5695696a5e8
//NOTE: The following function will set all three params
//settings.SetOffScreenRenderingBestPerformanceArgs();
2019-09-06 13:04:49 +10:00
//settings.CefCommandLineArgs.Add("disable-gpu");
//settings.CefCommandLineArgs.Add("disable-gpu-compositing");
//settings.CefCommandLineArgs.Add("enable-begin-frame-scheduling");
2015-09-16 07:18:58 +10:00
2019-09-06 13:04:49 +10:00
//settings.CefCommandLineArgs.Add("disable-gpu-vsync"); //Disable Vsync
2015-09-16 07:18:58 +10:00
2018-08-16 14:33:02 +05:00
// The following options control accessibility state for all frames.
// These options only take effect if accessibility state is not set by IBrowserHost.SetAccessibilityState call.
// --force-renderer-accessibility enables browser accessibility.
// --disable-renderer-accessibility completely disables browser accessibility.
2019-09-06 13:04:49 +10:00
//settings.CefCommandLineArgs.Add("force-renderer-accessibility");
//settings.CefCommandLineArgs.Add("disable-renderer-accessibility");
2018-08-16 14:33:02 +05:00
2018-03-07 00:21:31 +00:00
//Enables Uncaught exception handler
settings . UncaughtExceptionStackSize = 10 ;
2019-11-23 13:55:13 +10:00
//Disable WebAssembly
//settings.JavascriptFlags = "--noexpose_wasm";
2015-09-04 08:00:04 +10:00
// Off Screen rendering (WPF/Offscreen)
2018-09-10 02:41:13 +02:00
if ( settings . WindowlessRenderingEnabled )
2015-09-04 08:00:04 +10:00
{
2016-08-20 08:24:12 +10:00
//Disable Direct Composition to test https://github.com/cefsharp/CefSharp/issues/1634
2019-09-06 13:04:49 +10:00
//settings.CefCommandLineArgs.Add("disable-direct-composition");
2018-06-14 22:11:56 +10:00
2016-04-19 15:05:33 +10:00
// DevTools doesn't seem to be working when this is enabled
// http://magpcss.org/ceforum/viewtopic.php?f=6&t=14095
2019-09-06 13:04:49 +10:00
//settings.CefCommandLineArgs.Add("enable-begin-frame-scheduling");
2015-09-04 08:00:04 +10:00
}
2015-04-14 13:29:47 +10:00
var proxy = ProxyConfig . GetProxyInformation ( ) ;
switch ( proxy . AccessType )
{
case InternetOpenType . Direct :
{
//Don't use a proxy server, always make direct connections.
2019-09-06 13:04:49 +10:00
settings . CefCommandLineArgs . Add ( "no-proxy-server" ) ;
2015-04-14 13:29:47 +10:00
break ;
}
case InternetOpenType . Proxy :
{
settings . CefCommandLineArgs . Add ( "proxy-server" , proxy . ProxyAddress ) ;
break ;
}
case InternetOpenType . PreConfig :
{
2019-09-06 13:04:49 +10:00
settings . CefCommandLineArgs . Add ( "proxy-auto-detect" ) ;
2015-04-14 13:29:47 +10:00
break ;
}
}
2018-09-10 02:41:13 +02:00
2016-01-29 18:15:11 +10:00
//settings.LogSeverity = LogSeverity.Verbose;
2014-05-29 08:10:44 +10:00
2014-11-05 11:05:41 +10:00
if ( DebuggingSubProcess )
2014-05-29 08:10:44 +10:00
{
2021-04-01 09:27:48 +02:00
var architecture = RuntimeInformation . ProcessArchitecture . ToString ( ) . ToLowerInvariant ( ) ;
2020-07-18 13:30:54 +10:00
#if NETCOREAPP
2025-07-27 02:40:58 +02:00
settings . BrowserSubprocessPath = Path . GetFullPath ( "..\\..\\..\\..\\..\\..\\CefSharp.BrowserSubprocess\\bin.netcore\\" + architecture + "\\Debug\\net6.0-windows\\CefSharp.BrowserSubprocess.exe" ) ;
2020-07-18 13:30:54 +10:00
#else
2020-04-29 08:13:08 +10:00
settings . BrowserSubprocessPath = Path . GetFullPath ( "..\\..\\..\\..\\CefSharp.BrowserSubprocess\\bin\\" + architecture + "\\Debug\\CefSharp.BrowserSubprocess.exe" ) ;
2020-07-23 07:21:14 +02:00
#endif
2020-07-18 13:30:54 +10:00
}
2014-05-29 08:10:44 +10:00
2021-04-13 15:10:38 +10:00
settings . CookieableSchemesList = "custom" ;
2014-05-29 08:10:44 +10:00
settings . RegisterScheme ( new CefCustomScheme
{
SchemeName = CefSharpSchemeHandlerFactory . SchemeName ,
2017-03-13 10:28:44 +10:00
SchemeHandlerFactory = new CefSharpSchemeHandlerFactory ( ) ,
2019-06-14 08:20:06 +10:00
IsSecure = true , //treated with the same security rules as those applied to "https" URLs
} ) ;
settings . RegisterScheme ( new CefCustomScheme
{
SchemeName = "https" ,
SchemeHandlerFactory = new CefSharpSchemeHandlerFactory ( ) ,
2020-05-19 21:55:16 +10:00
DomainName = ExampleDomain
2014-05-29 08:10:44 +10:00
} ) ;
2015-09-16 09:05:52 +10:00
settings . RegisterScheme ( new CefCustomScheme
{
SchemeName = CefSharpSchemeHandlerFactory . SchemeNameTest ,
2017-03-13 10:28:44 +10:00
SchemeHandlerFactory = new CefSharpSchemeHandlerFactory ( ) ,
IsSecure = true //treated with the same security rules as those applied to "https" URLs
2015-09-16 09:05:52 +10:00
} ) ;
2017-03-02 15:09:55 +10:00
//You can use the http/https schemes - best to register for a specific domain
settings . RegisterScheme ( new CefCustomScheme
{
SchemeName = "https" ,
SchemeHandlerFactory = new CefSharpSchemeHandlerFactory ( ) ,
2017-03-13 10:28:44 +10:00
DomainName = "cefsharp.com" ,
IsSecure = true //treated with the same security rules as those applied to "https" URLs
2017-03-02 15:09:55 +10:00
} ) ;
2020-07-23 07:21:14 +02:00
const string cefSharpExampleResourcesFolder =
#if ! NETCOREAPP
@"..\..\..\..\CefSharp.Example\Resources" ;
#else
2021-04-01 09:27:48 +02:00
@"..\..\..\..\..\..\CefSharp.Example\Resources" ;
2020-07-23 07:21:14 +02:00
#endif
2016-06-22 12:23:50 +10:00
settings . RegisterScheme ( new CefCustomScheme
{
SchemeName = "localfolder" ,
2020-07-23 07:21:14 +02:00
SchemeHandlerFactory = new FolderSchemeHandlerFactory ( rootFolder : cefSharpExampleResourcesFolder ,
2016-06-22 12:23:50 +10:00
schemeName : "localfolder" , //Optional param no schemename checking if null
hostName : "cefsharp" , //Optional param no hostname checking if null
defaultPage : "home.html" ) //Optional param will default to index.html
2018-09-10 02:41:13 +02:00
} ) ;
2016-06-22 12:23:50 +10:00
2018-08-02 10:47:12 +10:00
//This must be set before Cef.Initialized is called
CefSharpSettings . FocusedNodeChangedEnabled = true ;
2015-12-07 16:27:17 -05:00
2019-06-08 17:21:49 +10:00
//Async Javascript Binding - methods are queued on TaskScheduler.Default.
//Set this to true to when you have methods that return Task<T>
2018-06-09 21:54:12 +10:00
//CefSharpSettings.ConcurrentTaskExecution = true;
2019-07-03 20:31:21 +10:00
//Legacy Binding Behaviour - Same as Javascript Binding in version 57 and below
2018-06-09 21:54:12 +10:00
//See issue https://github.com/cefsharp/CefSharp/issues/1203 for details
2019-07-05 08:55:53 +10:00
//CefSharpSettings.LegacyJavascriptBindingEnabled = true;
2018-06-09 21:54:12 +10:00
//Exit the subprocess if the parent process happens to close
//This is optional at the moment
//https://github.com/cefsharp/CefSharp/pull/2375/
CefSharpSettings . SubprocessExitIfParentProcessClosed = true ;
2018-03-05 16:18:25 +10:00
//NOTE: Set this before any calls to Cef.Initialize to specify a proxy with username and password
//One set this cannot be changed at runtime. If you need to change the proxy at runtime (dynamically) then
//see https://github.com/cefsharp/CefSharp/wiki/General-Usage#proxy-resolution
//CefSharpSettings.Proxy = new ProxyOptions(ip: "127.0.0.1", port: "8080", username: "cefsharp", password: "123");
2020-07-18 13:30:54 +10:00
bool performDependencyCheck = ! DebuggingSubProcess ;
2020-07-23 07:21:14 +02:00
if ( ! Cef . Initialize ( settings , performDependencyCheck : performDependencyCheck , browserProcessHandler : browserProcessHandler ) )
2014-05-29 08:10:44 +10:00
{
2014-12-22 22:00:00 +10:00
throw new Exception ( "Unable to Initialize Cef" ) ;
2014-05-29 08:10:44 +10:00
}
2017-03-02 15:09:55 +10:00
Cef . AddCrossOriginWhitelistEntry ( BaseUrl , "https" , "cefsharp.com" , false ) ;
2014-05-29 08:10:44 +10:00
}
2014-12-09 12:49:20 +10:00
2022-12-04 19:10:58 +10:00
public static void RegisterTestResources ( IWebBrowser browser )
2014-12-09 12:49:20 +10:00
{
2020-04-08 18:07:01 +10:00
if ( browser . ResourceRequestHandlerFactory = = null )
{
browser . ResourceRequestHandlerFactory = new ResourceRequestHandlerFactory ( ) ;
}
2019-06-24 21:22:20 +10:00
var handler = browser . ResourceRequestHandlerFactory as ResourceRequestHandlerFactory ;
2020-04-08 18:07:01 +10:00
2014-12-09 12:49:20 +10:00
if ( handler ! = null )
{
2015-08-26 17:26:45 +10:00
const string renderProcessCrashedBody = "<html><body><h1>Render Process Crashed</h1><p>Your seeing this message as the render process has crashed</p></body></html>" ;
2018-08-13 12:39:04 +10:00
handler . RegisterHandler ( RenderProcessCrashedUrl , ResourceHandler . GetByteArray ( renderProcessCrashedBody , Encoding . UTF8 ) ) ;
2015-08-26 17:26:45 +10:00
2014-12-09 12:49:20 +10:00
const string responseBody = "<html><body><h1>Success</h1><p>This document is loaded from a System.IO.Stream</p></body></html>" ;
2018-08-13 12:39:04 +10:00
handler . RegisterHandler ( TestResourceUrl , ResourceHandler . GetByteArray ( responseBody , Encoding . UTF8 ) ) ;
2014-12-16 10:51:34 +10:00
const string unicodeResponseBody = "<html><body>整体满意度</body></html>" ;
2018-08-13 12:39:04 +10:00
handler . RegisterHandler ( TestUnicodeResourceUrl , ResourceHandler . GetByteArray ( unicodeResponseBody , Encoding . UTF8 ) ) ;
2014-12-09 12:49:20 +10:00
}
}
2014-05-29 08:10:44 +10:00
}
}