From 482b4239ec6d0b07331c0aaec69b31135ba2b623 Mon Sep 17 00:00:00 2001 From: Alex Maitland Date: Tue, 26 Oct 2021 13:56:28 +1000 Subject: [PATCH] OffScreen - Create Browser when Global CefRequestContext initialized. - GlobalContextInitialized wraps a TaskCompletionSource which is resolved when the Global CefRequestContext has been created - Reduce code duplication If successful we can apply the same changes to WinForms/WPF though they're less likely to have issues than the OffScreen implementation as the CreateBrowser doesn't happen until much later Issue #3850 #3634 --- CefSharp.Core.Runtime/Cef.h | 5 ++ CefSharp.Core.Runtime/Internals/CefSharpApp.h | 12 ++-- CefSharp.Core/Cef.cs | 16 ++--- CefSharp.OffScreen/ChromiumWebBrowser.cs | 59 +++++++++---------- .../Internals/GlobalContextInitialized.cs | 46 +++++++++++++++ 5 files changed, 89 insertions(+), 49 deletions(-) create mode 100644 CefSharp/Internals/GlobalContextInitialized.cs diff --git a/CefSharp.Core.Runtime/Cef.h b/CefSharp.Core.Runtime/Cef.h index c58f31a5b..161d7a07b 100644 --- a/CefSharp.Core.Runtime/Cef.h +++ b/CefSharp.Core.Runtime/Cef.h @@ -313,6 +313,11 @@ namespace CefSharp auto success = CefInitialize(main_args, *(cefSettings->_cefSettings), app.get(), nullptr); + if (!success) + { + CefSharp::Internals::GlobalContextInitialized::SetResult(false); + } + _initialized = success; _multiThreadedMessageLoop = cefSettings->MultiThreadedMessageLoop; diff --git a/CefSharp.Core.Runtime/Internals/CefSharpApp.h b/CefSharp.Core.Runtime/Internals/CefSharpApp.h index b3ba15a7c..e6292fee5 100644 --- a/CefSharp.Core.Runtime/Internals/CefSharpApp.h +++ b/CefSharp.Core.Runtime/Internals/CefSharpApp.h @@ -93,11 +93,6 @@ namespace CefSharp virtual void OnContextInitialized() override { - if (!Object::ReferenceEquals(_app, nullptr) && !Object::ReferenceEquals(_app->BrowserProcessHandler, nullptr)) - { - _app->BrowserProcessHandler->OnContextInitialized(); - } - auto customSchemes = (IEnumerable^)_customSchemes; //CefRegisterSchemeHandlerFactory requires access to the Global CefRequestContext @@ -111,6 +106,13 @@ namespace CefSharp CefRegisterSchemeHandlerFactory(StringUtils::ToNative(cefCustomScheme->SchemeName), StringUtils::ToNative(domainName), wrapper); } } + + CefSharp::Internals::GlobalContextInitialized::SetResult(true); + + if (!Object::ReferenceEquals(_app, nullptr) && !Object::ReferenceEquals(_app->BrowserProcessHandler, nullptr)) + { + _app->BrowserProcessHandler->OnContextInitialized(); + } } virtual void OnScheduleMessagePumpWork(int64 delay_ms) override diff --git a/CefSharp.Core/Cef.cs b/CefSharp.Core/Cef.cs index 8aea97f59..a2d65c5d9 100644 --- a/CefSharp.Core/Cef.cs +++ b/CefSharp.Core/Cef.cs @@ -181,28 +181,20 @@ namespace CefSharp /// public static Task InitializeAsync(CefSettingsBase settings, bool performDependencyCheck = true) { - var tcs = new TaskCompletionSource(); - var handler = new InitializeAsyncBrowserProcessHandler(tcs); - using (settings.settings) { try { - var success = Core.Cef.Initialize(settings.settings, performDependencyCheck, handler); - - //Failed, need to check the log file - if (!success) - { - tcs.TrySetResult(false); - } + //Ignore the result, the Task will be set in Core.Cef.Initialze + Core.Cef.Initialize(settings.settings, performDependencyCheck); } catch (Exception ex) { - tcs.TrySetException(ex); + GlobalContextInitialized.SetException(ex); } } - return tcs.Task; + return GlobalContextInitialized.Task; } /// diff --git a/CefSharp.OffScreen/ChromiumWebBrowser.cs b/CefSharp.OffScreen/ChromiumWebBrowser.cs index 961e6e7a2..921938c59 100644 --- a/CefSharp.OffScreen/ChromiumWebBrowser.cs +++ b/CefSharp.OffScreen/ChromiumWebBrowser.cs @@ -323,14 +323,27 @@ namespace CefSharp.OffScreen windowInfo.SetAsWindowless(IntPtr.Zero); } - managedCefBrowserAdapter.CreateBrowser(windowInfo, browserSettings, RequestContext, Address); - - //Dispose of BrowserSettings if we created it, if user created then they're responsible - if (browserSettings.AutoDispose) + //TODO: We need some sort of timeout and + //if we use the same approach for WPF/WinForms then + //we need to move the common code into the partial class + GlobalContextInitialized.ExecuteOrEnqueue((success) => { - browserSettings.Dispose(); - } - browserSettings = null; + if(!success) + { + return; + } + + managedCefBrowserAdapter.CreateBrowser(windowInfo, browserSettings, RequestContext, Address); + + //Dispose of BrowserSettings if we created it, if user created then they're responsible + if (browserSettings.AutoDispose) + { + browserSettings.Dispose(); + } + browserSettings = null; + + }); + } /// @@ -348,39 +361,21 @@ namespace CefSharp.OffScreen /// public Task CreateBrowserAsync(IWindowInfo windowInfo = null, IBrowserSettings browserSettings = null) { - if (browserCreated) - { - throw new Exception("An instance of the underlying offscreen browser has already been created, this method can only be called once."); - } - - browserCreated = true; - - if (browserSettings == null) - { - browserSettings = Core.ObjectFactory.CreateBrowserSettings(autoDispose: true); - } - - if (windowInfo == null) - { - windowInfo = Core.ObjectFactory.CreateWindowInfo(); - windowInfo.SetAsWindowless(IntPtr.Zero); - } - var tcs = new TaskCompletionSource(); - onAfterBrowserCreatedDelegate = new Action(b => + onAfterBrowserCreatedDelegate += new Action(b => { tcs.TrySetResultAsync(b); }); - managedCefBrowserAdapter.CreateBrowser(windowInfo, browserSettings, RequestContext, Address); - - //Dispose of BrowserSettings if we created it, if user created then they're responsible - if (browserSettings.AutoDispose) + try { - browserSettings.Dispose(); + CreateBrowser(windowInfo, browserSettings); + } + catch(Exception ex) + { + tcs.TrySetExceptionAsync(ex); } - browserSettings = null; return tcs.Task; } diff --git a/CefSharp/Internals/GlobalContextInitialized.cs b/CefSharp/Internals/GlobalContextInitialized.cs new file mode 100644 index 000000000..6c50c2ad0 --- /dev/null +++ b/CefSharp/Internals/GlobalContextInitialized.cs @@ -0,0 +1,46 @@ +// Copyright © 2021 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; +using System.Threading.Tasks; + +namespace CefSharp.Internals +{ + public static class GlobalContextInitialized + { + private static TaskCompletionSource TaskCompletionSource = new TaskCompletionSource(); + + public static Task Task + { + get { return TaskCompletionSource.Task; } + } + + public static void SetResult(bool success) + { + TaskCompletionSource.TrySetResult(success); + } + + public static void SetException(Exception ex) + { + TaskCompletionSource.TrySetException(ex); + } + + /// + /// We need to be sure the CEF Global Context has been initialized before + /// we create the browser. If the CefRequestContext has already been initialzed + /// then we'll execute syncroniously. If the CefRequestContext hasn't been + /// initialized then we will continue on the CEF UI Thread. + /// https://github.com/cefsharp/CefSharp/issues/3850 + /// + /// action to invoke + public static void ExecuteOrEnqueue(Action action) + { + TaskCompletionSource.Task.ContinueWith((t) => + { + action(t.Result); + + }, TaskContinuationOptions.ExecuteSynchronously); + } + } +}