Skip to content
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

Core - Change Cef.IsInitialized from bool to bool? #4869

Merged
merged 2 commits into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions CefSharp.Core.Runtime/Cef.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ namespace CefSharp
private:
static Object^ _sync;

static bool _initialized = false;
static Nullable<bool> _initialized;
static bool _hasShutdown = false;
static HashSet<IDisposable^>^ _disposables;
static int _initializedThreadId;
Expand Down Expand Up @@ -86,9 +86,9 @@ namespace CefSharp

/// <summary>Gets a value that indicates whether CefSharp is initialized.</summary>
/// <value>true if CefSharp is initialized; otherwise, false.</value>
static property bool IsInitialized
static property Nullable<bool> IsInitialized
{
bool get()
Nullable<bool> get()
{
return _initialized;
}
Expand Down Expand Up @@ -255,7 +255,7 @@ namespace CefSharp
/// <returns>true if successful; otherwise, false.</returns>
static bool Initialize(CefSettingsBase^ cefSettings, bool performDependencyCheck, IApp^ cefApp)
{
if (_initialized)
if (_initialized.HasValue)
{
// NOTE: Can only initialize Cef once, to make this explicitly clear throw exception on subsiquent attempts
throw gcnew Exception("Cef.Initialize can only be called once per process. This is a limitation of the underlying " +
Expand Down Expand Up @@ -555,11 +555,11 @@ namespace CefSharp
/// </summary>
static void Shutdown()
{
if (_initialized)
amaitland marked this conversation as resolved.
Show resolved Hide resolved
if (_initialized.GetValueOrDefault())
{
msclr::lock l(_sync);

if (_initialized)
if (_initialized.GetValueOrDefault())
{
if (_initializedThreadId != Thread::CurrentThread->ManagedThreadId)
{
Expand Down Expand Up @@ -615,11 +615,11 @@ namespace CefSharp
/// </summary>
static void ShutdownWithoutChecks()
{
if (_initialized)
if (_initialized.GetValueOrDefault())
amaitland marked this conversation as resolved.
Show resolved Hide resolved
{
msclr::lock l(_sync);

if (_initialized)
if (_initialized.GetValueOrDefault())
{
CefShutdown();
_initialized = false;
Expand Down Expand Up @@ -821,7 +821,7 @@ namespace CefSharp
return;
}

if (_initialized)
if (_initialized.HasValue)
{
throw gcnew Exception("Must be enabled before Cef.Initialize is called. ");
}
Expand Down
7 changes: 5 additions & 2 deletions CefSharp.Core/Cef.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,11 @@ public static void RemoveDisposable(IDisposable item)
}

/// <summary>Gets a value that indicates whether CefSharp is initialized.</summary>
/// <value>true if CefSharp is initialized; otherwise, false.</value>
public static bool IsInitialized
/// <value>
/// true if CefSharp is initialized; returns false if initialize failed.
/// null if initialize not yet called.
/// </value>
public static bool? IsInitialized
{
get { return Core.Cef.IsInitialized; }
}
Expand Down
10 changes: 1 addition & 9 deletions CefSharp.OffScreen/ChromiumWebBrowser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -224,15 +224,7 @@ public ChromiumWebBrowser(string address = "", IBrowserSettings browserSettings
IRequestContext requestContext = null, bool automaticallyCreateBrowser = true,
Action<IBrowser> onAfterBrowserCreated = null, bool useLegacyRenderHandler = true)
{
if (!Cef.IsInitialized)
{
var settings = new CefSettings();

if (!Cef.Initialize(settings))
{
throw new InvalidOperationException(CefInitializeFailedErrorMessage);
}
}
InitializeCefInternal();

RequestContext = requestContext;

Expand Down
9 changes: 7 additions & 2 deletions CefSharp.Test/CefSharpFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public CefSharpFixture(IMessageSink messageSink)

private void CefInitialize()
{
if (!Cef.IsInitialized)
if (Cef.IsInitialized == null)
{
var isDefault = AppDomain.CurrentDomain.IsDefaultAppDomain();
if (!isDefault)
Expand Down Expand Up @@ -63,11 +63,16 @@ private void CefInitialize()

diagnosticMessageSink.OnMessage(new DiagnosticMessage("Cef Initialized:" + success));
}

if (Cef.IsInitialized == false)
{
throw new InvalidOperationException("Cef.Initialize failed.");
}
}

private void CefShutdown()
{
if (Cef.IsInitialized)
if (Cef.IsInitialized == true)
{
diagnosticMessageSink.OnMessage(new DiagnosticMessage("Before Cef Shutdown"));

Expand Down
5 changes: 1 addition & 4 deletions CefSharp.WinForms/ChromiumWebBrowser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -333,10 +333,7 @@ private void InitializeFieldsAndCefIfRequired()
{
if (!initialized)
{
if (!Cef.IsInitialized && !Cef.Initialize(new CefSettings()))
{
throw new InvalidOperationException(CefInitializeFailedErrorMessage);
}
InitializeCefInternal();

Cef.AddDisposable(this);

Expand Down
11 changes: 1 addition & 10 deletions CefSharp.Wpf/ChromiumWebBrowser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -552,16 +552,7 @@ public ChromiumWebBrowser(string initialAddress)
[MethodImpl(MethodImplOptions.NoInlining)]
private void NoInliningConstructor()
{
//Initialize CEF if it hasn't already been initialized
if (!Cef.IsInitialized)
{
var settings = new CefSettings();

if (!Cef.Initialize(settings))
{
throw new InvalidOperationException(CefInitializeFailedErrorMessage);
}
}
InitializeCefInternal();

//Add this ChromiumWebBrowser instance to a list of IDisposable objects
// that if still alive at the time Cef.Shutdown is called will be disposed of
Expand Down
17 changes: 17 additions & 0 deletions CefSharp/Internals/Partial/ChromiumWebBrowser.Partial.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public partial class ChromiumWebBrowser
"the IsBrowserInitialized property to determine when the browser has been initialized.";

private const string CefInitializeFailedErrorMessage = "Cef.Initialize() failed.Check the log file see https://github.com/cefsharp/CefSharp/wiki/Trouble-Shooting#log-file for details.";
private const string CefIsInitializedFalseErrorMessage = "Cef.IsInitialized was false!.Check the log file for errors!. See https://github.com/cefsharp/CefSharp/wiki/Trouble-Shooting#log-file for details.";

/// <summary>
/// Used as workaround for issue https://github.com/cefsharp/CefSharp/issues/3021
Expand Down Expand Up @@ -506,6 +507,22 @@ private void FreeHandlersExceptLifeSpanAndFocus()
this.FreeDevToolsContext();
}

private static void InitializeCefInternal()
{
if (Cef.IsInitialized == null)
{
if (!Cef.Initialize(new CefSettings()))
{
throw new InvalidOperationException(CefInitializeFailedErrorMessage);
}
}

if (Cef.IsInitialized == false)
{
throw new InvalidOperationException(CefIsInitializedFalseErrorMessage);
}
}

/// <summary>
/// Check is browser is initialized
/// </summary>
Expand Down