Skip to content

Commit

Permalink
Merge 6c7fd7b into efa0c39
Browse files Browse the repository at this point in the history
  • Loading branch information
amaitland committed Jul 6, 2024
2 parents efa0c39 + 6c7fd7b commit 49b0d89
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 36 deletions.
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)
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())
{
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 bool IsBrowserInitialized
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

0 comments on commit 49b0d89

Please sign in to comment.