forked from cefsharp/CefSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCefExample.cs
More file actions
136 lines (117 loc) · 6.72 KB
/
CefExample.cs
File metadata and controls
136 lines (117 loc) · 6.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
using System;
using System.Diagnostics;
using System.Text;
using CefSharp.Example.Proxy;
namespace CefSharp.Example
{
public static class CefExample
{
public const string DefaultUrl = "custom://cefsharp/home.html";
public const string BindingTestUrl = "custom://cefsharp/BindingTest.html";
public const string TestResourceUrl = "http://test/resource/load";
public const string TestUnicodeResourceUrl = "http://test/resource/loadUnicode";
// Use when debugging the actual SubProcess, to make breakpoints etc. inside that project work.
private static readonly bool DebuggingSubProcess = Debugger.IsAttached;
public static void Init()
{
// 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", "");
//Chromium Command Line args
//http://peter.sh/experiments/chromium-command-line-switches/
//NOTE: Not all relevant in relation to `CefSharp`, use for reference purposes only.
var settings = new CefSettings();
settings.RemoteDebuggingPort = 8088;
//This will disable WCF (Effectively disabling JavascriptBinding and EvaluateScriptAsync)
//CefSharpSettings.WcfEnabled = false;
//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.
settings.CachePath = "cache";
//settings.UserAgent = "CefSharp Browser" + Cef.CefSharpVersion; // Example User Agent
//settings.CefCommandLineArgs.Add("renderer-process-limit", "1");
//settings.CefCommandLineArgs.Add("renderer-startup-dialog", "1");
//settings.CefCommandLineArgs.Add("disable-gpu", "1");
//settings.CefCommandLineArgs.Add("disable-gpu-vsync", "1");
//settings.CefCommandLineArgs.Add("enable-media-stream", "1"); //Enable WebRTC
//settings.CefCommandLineArgs.Add("no-proxy-server", "1"); //Don't use a proxy server, always make direct connections. Overrides any other proxy server flags that are passed.
//settings.CefCommandLineArgs.Add("debug-plugin-loading", "1"); //Dumps extra logging about plugin loading to the log file.
//settings.CefCommandLineArgs.Add("disable-plugins-discovery", "1"); //Disable discovering third-party plugins. Effectively loading only ones shipped with the browser plus third-party ones as specified by --extra-plugin-dir and --load-plugin switches
//Disables the DirectWrite font rendering system on windows.
//Possibly useful when experiencing blury fonts.
//settings.CefCommandLineArgs.Add("disable-direct-write", "1");
var proxy = ProxyConfig.GetProxyInformation();
switch (proxy.AccessType)
{
case InternetOpenType.Direct:
{
//Don't use a proxy server, always make direct connections.
settings.CefCommandLineArgs.Add("no-proxy-server", "1");
break;
}
case InternetOpenType.Proxy:
{
settings.CefCommandLineArgs.Add("proxy-server", proxy.ProxyAddress);
break;
}
case InternetOpenType.PreConfig:
{
settings.CefCommandLineArgs.Add("proxy-auto-detect", "1");
break;
}
}
settings.LogSeverity = LogSeverity.Verbose;
if (DebuggingSubProcess)
{
var architecture = Environment.Is64BitProcess ? "x64" : "x86";
settings.BrowserSubprocessPath = "..\\..\\..\\..\\CefSharp.BrowserSubprocess\\bin\\" + architecture + "\\Debug\\CefSharp.BrowserSubprocess.exe";
}
settings.RegisterScheme(new CefCustomScheme
{
SchemeName = CefSharpSchemeHandlerFactory.SchemeName,
SchemeHandlerFactory = new CefSharpSchemeHandlerFactory()
});
Cef.OnContextInitialized = delegate
{
Cef.SetCookiePath("cookies", true);
};
if (!Cef.Initialize(settings, shutdownOnProcessExit: true, performDependencyCheck: !DebuggingSubProcess))
{
throw new Exception("Unable to Initialize Cef");
}
}
public static async void RegisterTestResources(IWebBrowser browser)
{
var handler = browser.ResourceHandlerFactory as DefaultResourceHandlerFactory;
if (handler != null)
{
const string responseBody = "<html><body><h1>Success</h1><p>This document is loaded from a System.IO.Stream</p></body></html>";
var response = ResourceHandler.FromString(responseBody);
response.Headers.Add("HeaderTest1", "HeaderTest1Value");
handler.RegisterHandler(TestResourceUrl, response);
const string unicodeResponseBody = "<html><body>整体满意度</body></html>";
handler.RegisterHandler(TestUnicodeResourceUrl, ResourceHandler.FromString(unicodeResponseBody));
var plugins = await Cef.GetPlugins();
var pluginBody = new StringBuilder();
pluginBody.Append("<html><body><h1>Plugins</h1><table>");
pluginBody.Append("<tr>");
pluginBody.Append("<th>Name</th>");
pluginBody.Append("<th>Description</th>");
pluginBody.Append("<th>Version</th>");
pluginBody.Append("<th>Path</th>");
pluginBody.Append("</tr>");
foreach(var plugin in plugins)
{
pluginBody.Append("<tr>");
pluginBody.Append("<td>" + plugin.Name + "</td>");
pluginBody.Append("<td>" + plugin.Description + "</td>");
pluginBody.Append("<td>" + plugin.Version + "</td>");
pluginBody.Append("<td>" + plugin.Path + "</td>");
pluginBody.Append("</tr>");
}
pluginBody.Append("</table></body></html>");
handler.RegisterHandler("custom://cefsharp/plugins", ResourceHandler.FromString(pluginBody.ToString()));
}
}
}
}