Core - Add AddPreferenceObserver support. (#5279)

* Core - Add `AddPreferenceObserver` support.
This commit is contained in:
Schuyler Tan
2026-08-29 08:44:25 +08:00
committed by GitHub
parent 16bc6e0711
commit 732afb1f66
12 changed files with 189 additions and 1 deletions
@@ -1,4 +1,4 @@
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// GenAPI Version: 5.0.2.37403
@@ -291,6 +291,7 @@ namespace CefSharp.Core
public virtual System.Threading.Tasks.Task<CefSharp.ResolveCallbackResult> ResolveHostAsync(System.Uri origin) { throw null; }
public virtual void SetContentSetting(string requestingUrl, string topLevelUrl, CefSharp.Enums.ContentSettingTypes contentType, CefSharp.Enums.ContentSettingValues value) { }
public virtual bool SetPreference(string name, object value, out string error) { throw null; }
public virtual CefSharp.IRegistration AddPreferenceObserver(string name, CefSharp.Callback.IPreferenceObserver observer) { throw null; }
public virtual void SetWebsiteSetting(string requestingUrl, string topLevelUrl, CefSharp.Enums.ContentSettingTypes contentType, object value) { }
public virtual CefSharp.IRequestContext UnWrap() { throw null; }
}
@@ -418,6 +418,7 @@
<ClInclude Include="Internals\CefRequestContextHandlerAdapter.h" />
<ClInclude Include="PostData.h" />
<ClInclude Include="PostDataElement.h" />
<ClInclude Include="Internals\PreferenceObserverAdapter.h" />
<ClInclude Include="Request.h" />
<ClInclude Include="resource.h" />
<ClInclude Include="UrlRequest.h" />
@@ -325,6 +325,9 @@
<ClInclude Include="Internals\CefTaskDelegate.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Internals\PreferenceObserverAdapter.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClInclude Include="Internals\CefFrameWrapper.h">
@@ -317,6 +317,7 @@
<ClInclude Include="Internals\CefRequestContextHandlerAdapter.h" />
<ClInclude Include="PostData.h" />
<ClInclude Include="PostDataElement.h" />
<ClInclude Include="Internals\PreferenceObserverAdapter.h" />
<ClInclude Include="Request.h" />
<ClInclude Include="resource.h" />
<ClInclude Include="UrlRequest.h" />
@@ -319,6 +319,9 @@
<ClInclude Include="Internals\CefTaskDelegate.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Internals\PreferenceObserverAdapter.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Internals\CefPermissionPromptCallbackWrapper.h">
<Filter>Header Files</Filter>
</ClInclude>
@@ -0,0 +1,41 @@
// Copyright © 2026 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.
#include "include/cef_preference.h"
#include <gcroot.h>
#include "StringUtils.h"
using namespace CefSharp::Callback;
namespace CefSharp
{
namespace Internals
{
private class PreferenceObserverAdapter : public CefPreferenceObserver
{
private:
gcroot<IPreferenceObserver^> _handler;
public:
PreferenceObserverAdapter(IPreferenceObserver^ handler)
{
_handler = handler;
}
~PreferenceObserverAdapter()
{
delete _handler;
_handler = nullptr;
}
virtual void OnPreferenceChanged(const CefString& name) override
{
_handler->OnPreferenceChanged(StringUtils::ToClr(name));
}
IMPLEMENT_REFCOUNTINGM(PreferenceObserverAdapter);
};
}
}
+14
View File
@@ -11,9 +11,12 @@
#include "Internals\CefSchemeHandlerFactoryAdapter.h"
#include "Internals\CefCompletionCallbackAdapter.h"
#include "Internals\CefResolveCallbackAdapter.h"
#include "Internals\PreferenceObserverAdapter.h"
#include "Internals\TypeConversion.h"
#include "Internals\CefRegistrationWrapper.h"
using namespace System::Runtime::InteropServices;
using namespace CefSharp::Callback;
namespace CefSharp
{
@@ -117,6 +120,17 @@ namespace CefSharp
return success;
}
IRegistration^ RequestContext::AddPreferenceObserver(String^ name, IPreferenceObserver^ observer)
{
ThrowIfDisposed();
ThrowIfExecutedOnNonCefUiThread();
auto registration = _requestContext->AddPreferenceObserver(StringUtils::ToNative(name), new PreferenceObserverAdapter(observer));
return gcnew CefRegistrationWrapper(registration);
}
void RequestContext::ClearCertificateExceptions(ICompletionCallback^ callback)
{
ThrowIfDisposed();
+18
View File
@@ -16,6 +16,7 @@
using namespace System::Runtime::InteropServices;
using namespace System::Threading::Tasks;
using namespace CefSharp::Callback;
namespace CefSharp
{
@@ -254,6 +255,23 @@ namespace CefSharp
/// application thread will be the CEF UI thread.</remarks>
virtual bool CanSetPreference(String^ name);
/// <summary>
/// Add an observer for preference changes. <paramref name="name"/> is the name of the
/// preference to observe. If <paramref name="name"/> is empty then all preferences will
/// be observed. Observing all preferences has performance consequences and
/// is not recommended outside of testing scenarios. The observer will remain
/// registered until the returned Registration object is destroyed. This
/// method must be called on the browser process UI thread.
/// </summary>
/// <param name="name">preference key</param>
/// <param name="observer">preference observer</param>
/// <remarks>Use Cef.UIThreadTaskFactory to execute this method if required,
/// <see cref="IBrowserProcessHandler.OnContextInitialized"/> and ChromiumWebBrowser.IsBrowserInitializedChanged are both
/// executed on the CEF UI thread, so can be called directly.
/// When CefSettings.MultiThreadedMessageLoop == false (the default is true) then the main
/// application thread will be the CEF UI thread.</remarks>
virtual IRegistration^ AddPreferenceObserver(String^ name, IPreferenceObserver^ observer);
/// <summary>
/// Set the value associated with preference name. If value is null the
/// preference will be restored to its default value. If setting the preference
+7
View File
@@ -7,6 +7,7 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using CefSharp.Callback;
using CefSharp.Enums;
namespace CefSharp
@@ -174,6 +175,12 @@ namespace CefSharp
return requestContext.SetPreference(name, value, out error);
}
/// <inheritdoc/>
public IRegistration AddPreferenceObserver(string name, IPreferenceObserver observer)
{
return requestContext.AddPreferenceObserver(name, observer);
}
/// <inheritdoc/>
public void ClearCertificateExceptions(ICompletionCallback callback)
{
@@ -5,6 +5,7 @@
using System;
using System.IO;
using System.Threading.Tasks;
using CefSharp.Callback;
using CefSharp.Enums;
using CefSharp.Example;
using CefSharp.Internals;
@@ -101,5 +102,62 @@ namespace CefSharp.Test.Framework
Assert.Equal(ContentSettingValues.Allow, (ContentSettingValues)actual);
}
[Fact]
public async Task CanObservePreferenceChange()
{
var tcs = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
var ctx = RequestContext.Configure()
.WithCachePath(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "CefSharp\\Tests\\TempCache5"))
.OnInitialize((ctx) =>
{
tcs.SetResult(true);
})
.Create();
await tcs.Task;
const string preferenceName = "autofill.enabled";
object actual = null;
var changeTcs = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
await CefThread.ExecuteOnUiThread(() =>
{
ctx.SetPreference(preferenceName, true, out _);
ctx.AddPreferenceObserver(preferenceName, new TestPreferenceObserver((name) =>
{
if (name == preferenceName)
{
actual = ctx.GetPreference(name);
changeTcs.TrySetResult(true);
}
}));
ctx.SetPreference(preferenceName, false, out _);
});
await changeTcs.Task;
Assert.Equal(false, (bool)actual);
}
private class TestPreferenceObserver : IPreferenceObserver
{
private readonly Action<string> onChanged;
public TestPreferenceObserver(Action<string> _onChanged)
{
onChanged = _onChanged;
}
public void OnPreferenceChanged(string name)
{
onChanged?.Invoke(name);
}
public void Dispose() { }
}
}
}
+23
View File
@@ -0,0 +1,23 @@
// Copyright © 2026 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;
namespace CefSharp.Callback
{
/// <summary>
/// Implemented by the client to observe preference changes and registered via
/// <see cref="IRequestContext.AddPreferenceObserver"/>. The methods of this class will
/// be called on the browser process UI thread.
/// </summary>
public interface IPreferenceObserver : IDisposable
{
/// <summary>
/// Called when a preference has changed. The new value can be retrieved using
/// <see cref="IRequestContext.GetPreference"/>.
/// </summary>
/// <param name="name">preference key</param>
void OnPreferenceChanged(string name);
}
}
+18
View File
@@ -5,6 +5,7 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using CefSharp.Callback;
using CefSharp.Enums;
namespace CefSharp
@@ -158,6 +159,23 @@ namespace CefSharp
/// application thread will be the CEF UI thread.</remarks>
bool SetPreference(string name, object value, out string error);
/// <summary>
/// Add an observer for preference changes. <paramref name="name"/> is the name of the
/// preference to observe. If <paramref name="name"/> is empty then all preferences will
/// be observed. Observing all preferences has performance consequences and
/// is not recommended outside of testing scenarios. The observer will remain
/// registered until the returned Registration object is destroyed. This
/// method must be called on the browser process UI thread.
/// </summary>
/// <param name="name">preference key</param>
/// <param name="observer">preference observer</param>
/// <remarks>Use Cef.UIThreadTaskFactory to execute this method if required,
/// <see cref="IBrowserProcessHandler.OnContextInitialized"/> and ChromiumWebBrowser.IsBrowserInitializedChanged are both
/// executed on the CEF UI thread, so can be called directly.
/// When CefSettings.MultiThreadedMessageLoop == false (the default is true) then the main
/// application thread will be the CEF UI thread.</remarks>
IRegistration AddPreferenceObserver(string name, IPreferenceObserver observer);
/// <summary>
/// Clears all certificate exceptions that were added as part of handling
/// <see cref="IRequestHandler.OnCertificateError"/>. If you call this it is