2018-09-10 02:41:13 +02:00
|
|
|
// Copyright © 2016 The CefSharp Authors. All rights reserved.
|
2016-07-20 13:08:47 +10:00
|
|
|
//
|
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
|
|
|
|
|
|
2016-09-01 16:51:20 +10:00
|
|
|
using System;
|
2017-02-27 10:03:31 +10:00
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.Threading.Tasks;
|
2018-09-17 00:46:21 +02:00
|
|
|
using CefSharp.SchemeHandler;
|
2016-09-01 16:51:20 +10:00
|
|
|
|
2016-07-20 13:08:47 +10:00
|
|
|
namespace CefSharp.Example.Handlers
|
|
|
|
|
{
|
2021-05-11 20:24:48 +10:00
|
|
|
public class BrowserProcessHandler : CefSharp.Handler.BrowserProcessHandler
|
2016-07-20 13:08:47 +10:00
|
|
|
{
|
2019-06-02 20:34:44 +10:00
|
|
|
/// <summary>
|
|
|
|
|
/// The interval between calls to Cef.DoMessageLoopWork
|
|
|
|
|
/// </summary>
|
|
|
|
|
protected const int SixtyTimesPerSecond = 1000 / 60; // 60fps
|
2016-09-01 16:51:20 +10:00
|
|
|
/// <summary>
|
|
|
|
|
/// The maximum number of milliseconds we're willing to wait between calls to OnScheduleMessagePumpWork().
|
|
|
|
|
/// </summary>
|
2019-06-02 20:34:44 +10:00
|
|
|
protected const int ThirtyTimesPerSecond = 1000 / 30; //30fps
|
2016-09-01 16:51:20 +10:00
|
|
|
|
2021-05-11 20:24:48 +10:00
|
|
|
protected override void OnContextInitialized()
|
2016-07-20 13:08:47 +10:00
|
|
|
{
|
2018-01-29 12:55:12 +10:00
|
|
|
//The Global CookieManager has been initialized, you can now set cookies
|
2016-07-20 13:08:47 +10:00
|
|
|
var cookieManager = Cef.GetGlobalCookieManager();
|
2021-04-13 15:10:38 +10:00
|
|
|
|
2018-09-10 02:41:13 +02:00
|
|
|
if (cookieManager.SetCookie("custom://cefsharp/home.html", new Cookie
|
2017-02-27 10:03:31 +10:00
|
|
|
{
|
|
|
|
|
Name = "CefSharpTestCookie",
|
|
|
|
|
Value = "ILikeCookies",
|
|
|
|
|
Expires = DateTime.Now.AddDays(1)
|
|
|
|
|
}))
|
2018-09-10 02:41:13 +02:00
|
|
|
{
|
2017-02-27 10:03:31 +10:00
|
|
|
cookieManager.VisitUrlCookiesAsync("custom://cefsharp/home.html", false).ContinueWith(previous =>
|
|
|
|
|
{
|
|
|
|
|
if (previous.Status == TaskStatus.RanToCompletion)
|
|
|
|
|
{
|
|
|
|
|
var cookies = previous.Result;
|
2016-07-20 13:08:47 +10:00
|
|
|
|
2017-02-27 10:03:31 +10:00
|
|
|
foreach (var cookie in cookies)
|
2018-09-10 02:41:13 +02:00
|
|
|
{
|
2019-01-10 23:57:28 +01:00
|
|
|
Debug.WriteLine("CookieName: " + cookie.Name);
|
2017-02-27 10:03:31 +10:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Debug.WriteLine("No Cookies found");
|
|
|
|
|
}
|
|
|
|
|
});
|
2017-06-14 10:37:28 +10:00
|
|
|
|
2018-09-10 02:41:13 +02:00
|
|
|
cookieManager.VisitAllCookiesAsync().ContinueWith(t =>
|
2017-06-14 10:37:28 +10:00
|
|
|
{
|
|
|
|
|
if (t.Status == TaskStatus.RanToCompletion)
|
|
|
|
|
{
|
|
|
|
|
var cookies = t.Result;
|
|
|
|
|
|
|
|
|
|
foreach (var cookie in cookies)
|
|
|
|
|
{
|
2019-01-10 23:57:28 +01:00
|
|
|
Debug.WriteLine("CookieName: " + cookie.Name);
|
2017-06-14 10:37:28 +10:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Debug.WriteLine("No Cookies found");
|
|
|
|
|
}
|
|
|
|
|
});
|
2017-02-27 10:03:31 +10:00
|
|
|
}
|
2018-01-29 12:55:12 +10:00
|
|
|
|
|
|
|
|
//The Request Context has been initialized, you can now set preferences, like proxy server settings
|
2016-07-20 13:08:47 +10:00
|
|
|
//Dispose of context when finished - preferable not to keep a reference if possible.
|
|
|
|
|
using (var context = Cef.GetGlobalRequestContext())
|
|
|
|
|
{
|
|
|
|
|
string errorMessage;
|
2022-03-25 10:47:15 +10:00
|
|
|
|
2016-07-20 13:08:47 +10:00
|
|
|
//You can set most preferences using a `.` notation rather than having to create a complex set of dictionaries.
|
2022-03-25 10:47:15 +10:00
|
|
|
//The default is true, you can change to false to disable resizing of text areas
|
|
|
|
|
//This is just one example, there are many many configuration options avaliable via preferences
|
|
|
|
|
var success = context.SetPreference("webkit.webprefs.text_areas_are_resizable", true, out errorMessage);
|
|
|
|
|
|
|
|
|
|
if(!success)
|
|
|
|
|
{
|
|
|
|
|
//Check the errorMessage for more details
|
|
|
|
|
}
|
2018-01-29 12:55:12 +10:00
|
|
|
|
2019-01-18 12:34:25 +10:00
|
|
|
//string error;
|
|
|
|
|
//var dicts = new List<string> { "en-GB", "en-US" };
|
|
|
|
|
//var success = context.SetPreference("spellcheck.dictionaries", dicts, out error);
|
|
|
|
|
|
|
|
|
|
//The no-proxy-server flag is set in CefExample.cs class, you'll have to remove that before you can test
|
|
|
|
|
//this code out
|
|
|
|
|
//var v = new Dictionary<string, string>
|
|
|
|
|
//{
|
|
|
|
|
// ["mode"] = "fixed_servers",
|
|
|
|
|
// ["server"] = "scheme://host:port"
|
|
|
|
|
//};
|
|
|
|
|
//success = context.SetPreference("proxy", v, out errorMessage);
|
|
|
|
|
|
2018-01-29 12:55:12 +10:00
|
|
|
//It's possible to register a scheme handler for the default http and https schemes
|
|
|
|
|
//In this example we register the FolderSchemeHandlerFactory for https://cefsharp.example
|
|
|
|
|
//Best to include the domain name, so only requests for that domain are forwarded to your scheme handler
|
|
|
|
|
//It is possible to intercept all requests for a scheme, including the built in http/https ones, be very careful doing this!
|
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
|
|
|
|
|
var folderSchemeHandlerExample = new FolderSchemeHandlerFactory(rootFolder: cefSharpExampleResourcesFolder,
|
2018-01-29 12:55:12 +10:00
|
|
|
hostName: "cefsharp.example", //Optional param no hostname checking if null
|
|
|
|
|
defaultPage: "home.html"); //Optional param will default to index.html
|
|
|
|
|
|
|
|
|
|
context.RegisterSchemeHandlerFactory("https", "cefsharp.example", folderSchemeHandlerExample);
|
2016-07-20 13:08:47 +10:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|