Skip to content

Core - Add AddPreferenceObserver support. - #5279

Open
SLT-World wants to merge 2 commits into
cefsharp:masterfrom
SLT-World:observable-preferences
Open

Core - Add AddPreferenceObserver support.#5279
SLT-World wants to merge 2 commits into
cefsharp:masterfrom
SLT-World:observable-preferences

Conversation

@SLT-World

@SLT-World SLT-World commented Aug 7, 2026

Copy link
Copy Markdown
Contributor

Fixes: -

Summary:

  • Added support for listening to modifications of preferences exposing AddPreferenceObserver to RequestContext.

Changes:

  • I have modified IRequestContext.cs, RequestContext.cpp, RequestContext.h, CefSharp.Core.Runtime.netcore.cs, created IPreferenceObserver.cs and PreferenceObserverAdapter.h.
    • Added AddPreferenceObserver to RequestContext.
    • Created IPreferenceObserver.

How Has This Been Tested?
Operating System: Windows 11
Environment: Visual Studio 2022
Setup:

await Cef.UIThreadTaskFactory.StartNew(async delegate
{
    IRequestContext GlobalRequestContext = Cef.GetGlobalRequestContext();
    GlobalRequestContext.SetPreference("autofill.enabled", true, out _);
    GlobalRequestContext.AddPreferenceObserver("autofill.enabled", new AutofillPreferenceObserver());
});

public class AutofillPreferenceObserver : IPreferenceObserver
{
    public void Dispose() { }

    public void OnPreferenceChanged(string Name)
    {
        IRequestContext GlobalRequestContext = Cef.GetGlobalRequestContext();
        object Value = GlobalRequestContext.GetPreference(Name);
        MessageBox.Show($"Autofill preference change detected. {Name} = {Value}");
    }
}

Alternated the autofill preference with a button:

bool AutofillState = false;

private async void Button_Click(object sender, RoutedEventArgs e)
{
    bool AutofillStateChange = AutofillState;
    AutofillState = !AutofillState;
    MessageBox.Show($"Changing autofill preference to {AutofillStateChange}.");
    await Cef.UIThreadTaskFactory.StartNew(async delegate
    {
        IRequestContext GlobalRequestContext = Cef.GetGlobalRequestContext();
        GlobalRequestContext.SetPreference("autofill.enabled", AutofillStateChange, out _);
    });
}

The Changing autofill... message box is displayed first, followed by Autofill preference change detected shortly, confirming the functionality.

I'm not clear on why the first line of CefSharp.Core.Runtime.RefAssembly/CefSharp.Core.Runtime.netcore.cs is once more labeled as a change by GitHub.

Screenshots (if appropriate):

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Updated documentation

Checklist:

  • Tested the code(if applicable)
  • Commented my code
  • Changed the documentation(if applicable)
  • New files have a license disclaimer
  • The formatting is consistent with the project (project supports .editorconfig)

Summary by CodeRabbit

  • New Features
    • Added support for monitoring browser preference changes through IRequestContext.
    • Preference observers receive notifications when the specified preference changes.
    • Added registration management so observers can be removed when no longer needed.
    • Added a dedicated observer callback interface for handling preference updates.
    • Documented callback behavior, including browser UI-thread execution and performance considerations.

@coderabbitai

coderabbitai Bot commented Aug 7, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 61563473-9aff-410b-a33c-07dcdd75bc0a

📥 Commits

Reviewing files that changed from the base of the PR and between b1236dd and 9ac6b42.

📒 Files selected for processing (1)
  • CefSharp.Core/RequestContext.cs
🚧 Files skipped from review as they are similar to previous changes (1)
  • CefSharp.Core/RequestContext.cs

📝 Walkthrough

Walkthrough

The PR adds IPreferenceObserver and AddPreferenceObserver to the public API. The C++/CLI runtime adapts managed observers to CEF callbacks, returns registration wrappers, and includes the adapter in both runtime projects.

Changes

Preference Observer Registration

Layer / File(s) Summary
Observer API contract
CefSharp/Callback/IPreferenceObserver.cs, CefSharp/IRequestContext.cs, CefSharp.Core.Runtime/RequestContext.h, CefSharp.Core.Runtime.RefAssembly/CefSharp.Core.Runtime.netcore.cs, CefSharp.Core/RequestContext.cs
Adds the disposable observer contract and the AddPreferenceObserver declarations and wrapper. The API supports one preference or all preferences and returns IRegistration.
Runtime callback registration
CefSharp.Core.Runtime/Internals/PreferenceObserverAdapter.h, CefSharp.Core.Runtime/RequestContext.cpp
Adapts managed callbacks to CEF, forwards preference changes, validates disposal and UI-thread state, and wraps the native registration.
Runtime project inclusion
CefSharp.Core.Runtime/*.vcxproj, CefSharp.Core.Runtime/*.vcxproj.filters
Adds PreferenceObserverAdapter.h to both projects and their header filters.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Sequence Diagram(s)

sequenceDiagram
  participant RequestContext
  participant RuntimeRequestContext
  participant PreferenceObserverAdapter
  participant CefRequestContext
  RequestContext->>RuntimeRequestContext: AddPreferenceObserver(name, observer)
  RuntimeRequestContext->>PreferenceObserverAdapter: Create adapter
  RuntimeRequestContext->>CefRequestContext: Register observer
  CefRequestContext-->>PreferenceObserverAdapter: Notify preference change
  PreferenceObserverAdapter-->>RequestContext: OnPreferenceChanged(name)
  RuntimeRequestContext-->>RequestContext: Return IRegistration
Loading
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 21.43% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely identifies the main change: adding AddPreferenceObserver support to Core.
Description check ✅ Passed The description covers the change, testing environment, test procedure, change types, and checklist; the empty issue reference is non-critical.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@AppVeyorBot

Copy link
Copy Markdown

@AppVeyorBot

Copy link
Copy Markdown

@SLT-World
SLT-World force-pushed the observable-preferences branch from b1236dd to 9ac6b42 Compare August 7, 2026 22:41
@AppVeyorBot

Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants