private void InitializeFieldsAndCefIfRequired() { if (!initialized) { if (!Cef.IsInitialized && !Cef.Initialize(new CefSettings())) { throw new InvalidOperationException("Cef::Initialize() failed"); } Cef.AddDisposable(this); if (FocusHandler == null) { //If the WinForms UI thread and the CEF UI thread are one in the same //then we don't need the FocusHandler, it's only required when using //MultiThreadedMessageLoop (the default) if (!Cef.CurrentlyOnThread(CefThreadIds.TID_UI)) { FocusHandler = new DefaultFocusHandler(); } } if (browserSettings == null) { browserSettings = Core.ObjectFactory.CreateBrowserSettings(autoDispose: true); } managedCefBrowserAdapter = ManagedCefBrowserAdapter.Create(this, false); initialized = true; } }
/// <summary> /// Create a new OffScreen Chromium Browser. If you use <see cref="CefSharp.JavascriptBinding.JavascriptBindingSettings.LegacyBindingEnabled"/> = true then you must /// set <paramref name="automaticallyCreateBrowser"/> to false and call <see cref="CreateBrowser"/> after the objects are registered. /// The underlying Chromium Embedded Framework(CEF) Browser is created asynchronouly, to subscribe to the <see cref="BrowserInitialized"/> event it is recommended /// that you set <paramref name="automaticallyCreateBrowser"/> to false, subscribe to the event and then call <see cref="CreateBrowser(IWindowInfo, IBrowserSettings)"/> /// to ensure you are subscribe to the event before it's fired (Issue https://github.com/cefsharp/CefSharp/issues/3552). /// </summary> /// <param name="address">Initial address (url) to load</param> /// <param name="browserSettings">The browser settings to use. If null, the default settings are used.</param> /// <param name="requestContext">See <see cref="RequestContext" /> for more details. Defaults to null</param> /// <param name="automaticallyCreateBrowser">automatically create the underlying Browser</param> /// <param name="onAfterBrowserCreated"> /// Use as an alternative to the <see cref="BrowserInitialized"/> event. If the underlying Chromium Embedded Framework (CEF) browser is created successfully, /// this action is guranteed to be called after the browser created where as the <see cref="BrowserInitialized"/> event may be called before /// you have a chance to subscribe to the event as the CEF Browser is created async. (Issue https://github.com/cefsharp/CefSharp/issues/3552). /// </param> /// <exception cref="System.InvalidOperationException">Cef::Initialize() failed</exception> public ChromiumWebBrowser(string address = "", IBrowserSettings browserSettings = null, IRequestContext requestContext = null, bool automaticallyCreateBrowser = true, Action <IBrowser> onAfterBrowserCreated = null) { if (!Cef.IsInitialized) { var settings = new CefSettings(); if (!Cef.Initialize(settings)) { throw new InvalidOperationException(CefInitializeFailedErrorMessage); } } RequestContext = requestContext; Cef.AddDisposable(this); Address = address; onAfterBrowserCreatedDelegate = onAfterBrowserCreated; managedCefBrowserAdapter = ManagedCefBrowserAdapter.Create(this, true); if (automaticallyCreateBrowser) { CreateBrowser(null, browserSettings); } RenderHandler = new DefaultRenderHandler(this); }
/// <summary> /// Create a new OffScreen Chromium Browser. If you use <see cref="CefSharp.JavascriptBinding.JavascriptBindingSettings.LegacyBindingEnabled"/> = true then you must /// set <paramref name="automaticallyCreateBrowser"/> to false and call <see cref="CreateBrowser"/> after the objects are registered. /// </summary> /// <param name="address">Initial address (url) to load</param> /// <param name="browserSettings">The browser settings to use. If null, the default settings are used.</param> /// <param name="requestContext">See <see cref="RequestContext" /> for more details. Defaults to null</param> /// <param name="automaticallyCreateBrowser">automatically create the underlying Browser</param> /// <exception cref="System.InvalidOperationException">Cef::Initialize() failed</exception> public ChromiumWebBrowser(string address = "", IBrowserSettings browserSettings = null, IRequestContext requestContext = null, bool automaticallyCreateBrowser = true) { if (!Cef.IsInitialized) { var settings = new CefSettings(); if (!Cef.Initialize(settings)) { throw new InvalidOperationException("Cef::Initialize() failed"); } } RequestContext = requestContext; Cef.AddDisposable(this); Address = address; managedCefBrowserAdapter = ManagedCefBrowserAdapter.Create(this, true); if (automaticallyCreateBrowser) { CreateBrowser(null, browserSettings); } RenderHandler = new DefaultRenderHandler(this); }
/// <summary> /// Required for designer support - this method cannot be inlined as the designer /// will attempt to load libcef.dll and will subsiquently throw an exception. /// TODO: Still not happy with this method name, need something better /// </summary> private void InitializeFieldsAndCefIfRequired() { if (!_initialized) { if (!Cef.IsInitialized && !Cef.Initialize(_settings)) { throw new InvalidOperationException("Cef::Initialize() failed"); } Cef.AddDisposable(this); if (ResourceRequestHandlerFactory == null) { ResourceRequestHandlerFactory = new ResourceRequestHandlerFactory(); } if (_browserSettings == null) { _browserSettings = CefSharpBrowserSettings.Create(true); } _managedCefBrowserAdapter = ManagedCefBrowserAdapter.Create(this, false); _initialized = true; } }
/// <summary> /// Create a new OffScreen Chromium Browser. If you use <see cref="CefSharp.JavascriptBinding.JavascriptBindingSettings.LegacyBindingEnabled"/> = true then you must /// set <paramref name="automaticallyCreateBrowser"/> to false and call <see cref="CreateBrowser"/> after the objects are registered. /// </summary> /// <param name="address">Initial address (url) to load</param> /// <param name="browserSettings">The browser settings to use. If null, the default settings are used.</param> /// <param name="requestContext">See <see cref="RequestContext" /> for more details. Defaults to null</param> /// <param name="automaticallyCreateBrowser">automatically create the underlying Browser</param> /// <exception cref="System.InvalidOperationException">Cef::Initialize() failed</exception> public ChromiumWebBrowser(string address, IBrowserSettings browserSettings, IRequestContext requestContext, bool automaticallyCreateBrowser, IRenderTarget renderer) { if (!Cef.IsInitialized) { throw new InvalidOperationException("Cef IS NOT INITIALIZED"); } RequestContext = requestContext; _renderTarget = renderer; Cef.AddDisposable(this); Address = address; managedCefBrowserAdapter = ManagedCefBrowserAdapter.Create(this, true); if (automaticallyCreateBrowser) { CreateBrowser(null, browserSettings); } }