-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Core - Add AddPreferenceObserver support.
#5279
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
SLT-World
wants to merge
3
commits into
cefsharp:master
Choose a base branch
from
SLT-World:observable-preferences
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
CefSharp.Core.Runtime/Internals/PreferenceObserverAdapter.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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); | ||
| }; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🗄️ Data Integrity & Integration | 🟡 Minor | ⚡ Quick win
🧩 Analysis chain
🏁 Script executed:
Repository: cefsharp/CefSharp
Length of output: 50374
🏁 Script executed:
Repository: cefsharp/CefSharp
Length of output: 38277
🏁 Script executed:
Repository: cefsharp/CefSharp
Length of output: 50373
🌐 Web query:
CEF 140 CefPreferenceManager AddPreferenceObserver CefRegistration destroyed thread requirement💡 Result:
When using CefPreferenceManager::AddPreferenceObserver in CEF (including version 140), the following requirements apply: 1. Thread Requirement for AddPreferenceObserver: This method must be called on the browser process UI thread [1][2]. 2. Observer Lifetime: The preference observer remains registered only as long as the CefRegistration object returned by AddPreferenceObserver exists [1][2]. Once that CefRegistration object is destroyed, the observer is automatically unregistered [1][2]. 3. Thread Requirement for CefRegistration Destruction: While the official documentation explicitly mandates the UI thread for calling AddPreferenceObserver [1][2], it does not explicitly specify a thread requirement for the destruction of the CefRegistration object itself. However, because CefRegistration objects in CEF are reference-counted (CefRefPtr), their destruction typically happens when the last reference is released. Since many CEF objects have thread-affine requirements, it is considered best practice to manage the lifetime of such objects—including the release of the final reference—on the same thread where they were created (the browser process UI thread) unless explicitly documented otherwise. 4. Observer Execution: Methods implemented by the CefPreferenceObserver will be called on the browser process UI thread [3][4].
Citations:
Retain and dispose the observer registration.
Store the
IRegistrationreturned byAddPreferenceObserveruntilchangeTcscompletes. If the wrapper is finalized early, CEF unregisters the observer and Line 141 can wait indefinitely. Dispose the registration on the CEF UI thread after the callback completes.🤖 Prompt for AI Agents
Source: MCP tools