Skip to content

Commit 57f8cd3

Browse files
authored
Core - Change Cef.IsInitialized from bool to bool? (#4869)
Resolves #4867
1 parent 45f2573 commit 57f8cd3

File tree

7 files changed

+41
-36
lines changed

7 files changed

+41
-36
lines changed

CefSharp.Core.Runtime/Cef.h

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ namespace CefSharp
4646
private:
4747
static Object^ _sync;
4848

49-
static bool _initialized = false;
49+
static Nullable<bool> _initialized;
5050
static bool _hasShutdown = false;
5151
static HashSet<IDisposable^>^ _disposables;
5252
static int _initializedThreadId;
@@ -86,9 +86,9 @@ namespace CefSharp
8686

8787
/// <summary>Gets a value that indicates whether CefSharp is initialized.</summary>
8888
/// <value>true if CefSharp is initialized; otherwise, false.</value>
89-
static property bool IsInitialized
89+
static property Nullable<bool> IsInitialized
9090
{
91-
bool get()
91+
Nullable<bool> get()
9292
{
9393
return _initialized;
9494
}
@@ -255,7 +255,7 @@ namespace CefSharp
255255
/// <returns>true if successful; otherwise, false.</returns>
256256
static bool Initialize(CefSettingsBase^ cefSettings, bool performDependencyCheck, IApp^ cefApp)
257257
{
258-
if (_initialized)
258+
if (_initialized.HasValue)
259259
{
260260
// NOTE: Can only initialize Cef once, to make this explicitly clear throw exception on subsiquent attempts
261261
throw gcnew Exception("Cef.Initialize can only be called once per process. This is a limitation of the underlying " +
@@ -555,11 +555,11 @@ namespace CefSharp
555555
/// </summary>
556556
static void Shutdown()
557557
{
558-
if (_initialized)
558+
if (_initialized.GetValueOrDefault())
559559
{
560560
msclr::lock l(_sync);
561561

562-
if (_initialized)
562+
if (_initialized.GetValueOrDefault())
563563
{
564564
if (_initializedThreadId != Thread::CurrentThread->ManagedThreadId)
565565
{
@@ -615,11 +615,11 @@ namespace CefSharp
615615
/// </summary>
616616
static void ShutdownWithoutChecks()
617617
{
618-
if (_initialized)
618+
if (_initialized.GetValueOrDefault())
619619
{
620620
msclr::lock l(_sync);
621621

622-
if (_initialized)
622+
if (_initialized.GetValueOrDefault())
623623
{
624624
CefShutdown();
625625
_initialized = false;
@@ -821,7 +821,7 @@ namespace CefSharp
821821
return;
822822
}
823823

824-
if (_initialized)
824+
if (_initialized.HasValue)
825825
{
826826
throw gcnew Exception("Must be enabled before Cef.Initialize is called. ");
827827
}

CefSharp.Core/Cef.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,11 @@ public static void RemoveDisposable(IDisposable item)
5050
}
5151

5252
/// <summary>Gets a value that indicates whether CefSharp is initialized.</summary>
53-
/// <value>true if CefSharp is initialized; otherwise, false.</value>
54-
public static bool IsInitialized
53+
/// <value>
54+
/// true if CefSharp is initialized; returns false if initialize failed.
55+
/// null if initialize not yet called.
56+
/// </value>
57+
public static bool? IsInitialized
5558
{
5659
get { return Core.Cef.IsInitialized; }
5760
}

CefSharp.OffScreen/ChromiumWebBrowser.cs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -224,15 +224,7 @@ public ChromiumWebBrowser(string address = "", IBrowserSettings browserSettings
224224
IRequestContext requestContext = null, bool automaticallyCreateBrowser = true,
225225
Action<IBrowser> onAfterBrowserCreated = null, bool useLegacyRenderHandler = true)
226226
{
227-
if (!Cef.IsInitialized)
228-
{
229-
var settings = new CefSettings();
230-
231-
if (!Cef.Initialize(settings))
232-
{
233-
throw new InvalidOperationException(CefInitializeFailedErrorMessage);
234-
}
235-
}
227+
InitializeCefInternal();
236228

237229
RequestContext = requestContext;
238230

CefSharp.Test/CefSharpFixture.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public CefSharpFixture(IMessageSink messageSink)
3131

3232
private void CefInitialize()
3333
{
34-
if (!Cef.IsInitialized)
34+
if (Cef.IsInitialized == null)
3535
{
3636
var isDefault = AppDomain.CurrentDomain.IsDefaultAppDomain();
3737
if (!isDefault)
@@ -63,11 +63,16 @@ private void CefInitialize()
6363

6464
diagnosticMessageSink.OnMessage(new DiagnosticMessage("Cef Initialized:" + success));
6565
}
66+
67+
if (Cef.IsInitialized == false)
68+
{
69+
throw new InvalidOperationException("Cef.Initialize failed.");
70+
}
6671
}
6772

6873
private void CefShutdown()
6974
{
70-
if (Cef.IsInitialized)
75+
if (Cef.IsInitialized == true)
7176
{
7277
diagnosticMessageSink.OnMessage(new DiagnosticMessage("Before Cef Shutdown"));
7378

CefSharp.WinForms/ChromiumWebBrowser.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -333,10 +333,7 @@ private void InitializeFieldsAndCefIfRequired()
333333
{
334334
if (!initialized)
335335
{
336-
if (!Cef.IsInitialized && !Cef.Initialize(new CefSettings()))
337-
{
338-
throw new InvalidOperationException(CefInitializeFailedErrorMessage);
339-
}
336+
InitializeCefInternal();
340337

341338
Cef.AddDisposable(this);
342339

CefSharp.Wpf/ChromiumWebBrowser.cs

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -552,16 +552,7 @@ public ChromiumWebBrowser(string initialAddress)
552552
[MethodImpl(MethodImplOptions.NoInlining)]
553553
private void NoInliningConstructor()
554554
{
555-
//Initialize CEF if it hasn't already been initialized
556-
if (!Cef.IsInitialized)
557-
{
558-
var settings = new CefSettings();
559-
560-
if (!Cef.Initialize(settings))
561-
{
562-
throw new InvalidOperationException(CefInitializeFailedErrorMessage);
563-
}
564-
}
555+
InitializeCefInternal();
565556

566557
//Add this ChromiumWebBrowser instance to a list of IDisposable objects
567558
// that if still alive at the time Cef.Shutdown is called will be disposed of

CefSharp/Internals/Partial/ChromiumWebBrowser.Partial.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public partial class ChromiumWebBrowser
2727
"the IsBrowserInitialized property to determine when the browser has been initialized.";
2828

2929
private const string CefInitializeFailedErrorMessage = "Cef.Initialize() failed.Check the log file see https://github.com/cefsharp/CefSharp/wiki/Trouble-Shooting#log-file for details.";
30+
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.";
3031

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

510+
private static void InitializeCefInternal()
511+
{
512+
if (Cef.IsInitialized == null)
513+
{
514+
if (!Cef.Initialize(new CefSettings()))
515+
{
516+
throw new InvalidOperationException(CefInitializeFailedErrorMessage);
517+
}
518+
}
519+
520+
if (Cef.IsInitialized == false)
521+
{
522+
throw new InvalidOperationException(CefIsInitializedFalseErrorMessage);
523+
}
524+
}
525+
509526
/// <summary>
510527
/// Check is browser is initialized
511528
/// </summary>

0 commit comments

Comments
 (0)