/// <summary> /// Create the underlying browser. The instance address, browser settings and request context will be used. /// </summary> /// <param name="windowInfo">Window information used when creating the browser</param> /// <param name="browserSettings">Browser initialization settings</param> /// <exception cref="System.Exception">An instance of the underlying offscreen browser has already been created, this method can only be called once.</exception> public void CreateBrowser(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 = CefSharp.Core.ObjectFactory.CreateBrowserSettings(autoDispose: true); } if (windowInfo == null) { windowInfo = CefSharp.Core.ObjectFactory.CreateWindowInfo(); 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) { browserSettings.Dispose(); } browserSettings = null; }
private void CreateBrowser() { browserCreated = true; if (((IWebBrowserInternal)this).HasParent == false) { if (IsBrowserInitialized == false || browser == null) { var windowInfo = new WindowInfo(); windowInfo.SetAsChild(Handle); managedCefBrowserAdapter.CreateBrowser(windowInfo, (BrowserSettings)browserSettings, (RequestContext)RequestContext, Address); if (browserSettings != null) { browserSettings.Dispose(); browserSettings = null; } } else { //If the browser already exists we'll reparent it to the new Handle var browserHandle = browser.GetHost().GetWindowHandle(); NativeMethodWrapper.SetWindowParent(browserHandle, Handle); } } }
private void InternalDispose(bool disposing) { if (disposing) { Interlocked.Exchange(ref browserInitialized, 0); CanExecuteJavascriptInMainFrame = false; // Don't maintain a reference to event listeners anylonger: AddressChanged = null; ConsoleMessage = null; FrameLoadEnd = null; FrameLoadStart = null; IsBrowserInitializedChanged = null; LoadError = null; LoadingStateChanged = null; StatusMessage = null; TitleChanged = null; JavascriptMessageReceived = null; // Release reference to handlers, except LifeSpanHandler which is done after Disposing // ManagedCefBrowserAdapter otherwise the ILifeSpanHandler.DoClose will not be invoked. // We also leave FocusHandler and override with a NoFocusHandler implementation as // it so we can block taking Focus (we're dispoing afterall). Issue #3715 FreeHandlersExceptLifeSpanAndFocus(); FocusHandler = new NoFocusHandler(); browser = null; if (parentFormMessageInterceptor != null) { parentFormMessageInterceptor.Dispose(); parentFormMessageInterceptor = null; } if (managedCefBrowserAdapter != null) { managedCefBrowserAdapter.Dispose(); managedCefBrowserAdapter = null; } //Dispose of BrowserSettings if we created it, if user created then they're responsible if (browserSettings != null && browserSettings.AutoDispose) { browserSettings.Dispose(); } browserSettings = null; parkingControl?.Dispose(); parkingControl = null; // LifeSpanHandler is set to null after managedCefBrowserAdapter.Dispose so ILifeSpanHandler.DoClose // is called. LifeSpanHandler = null; } Cef.RemoveDisposable(this); }
private void InternalDispose(bool disposing) { if (disposing) { CanExecuteJavascriptInMainFrame = false; IsBrowserInitialized = false; // Don't maintain a reference to event listeners anylonger: AddressChanged = null; ConsoleMessage = null; FrameLoadEnd = null; FrameLoadStart = null; IsBrowserInitializedChanged = null; LoadError = null; LoadingStateChanged = null; StatusMessage = null; TitleChanged = null; JavascriptMessageReceived = null; // Release reference to handlers, except LifeSpanHandler which is done after Disposing // ManagedCefBrowserAdapter otherwise the ILifeSpanHandler.DoClose will not be invoked. this.SetHandlersToNullExceptLifeSpan(); browser = null; if (parentFormMessageInterceptor != null) { parentFormMessageInterceptor.Dispose(); parentFormMessageInterceptor = null; } if (managedCefBrowserAdapter != null) { managedCefBrowserAdapter.Dispose(); managedCefBrowserAdapter = null; } //Dispose of BrowserSettings if we created it, if user created then they're responsible if (browserSettings.FrameworkCreated) { browserSettings.Dispose(); } browserSettings = null; parkingControl?.Dispose(); parkingControl = null; // LifeSpanHandler is set to null after managedCefBrowserAdapter.Dispose so ILifeSpanHandler.DoClose // is called. LifeSpanHandler = null; } Cef.RemoveDisposable(this); }
private void FreeUnmanagedResources() { browser = null; if (parentFormMessageInterceptor != null) { parentFormMessageInterceptor.Dispose(); parentFormMessageInterceptor = null; } if (browserSettings != null) { browserSettings.Dispose(); browserSettings = null; } if (managedCefBrowserAdapter != null) { managedCefBrowserAdapter.Dispose(); managedCefBrowserAdapter = null; } }
/// <summary> /// Create the underlying browser. The instance address, browser settings and request context will be used. /// </summary> /// <param name="windowInfo">Window information used when creating the browser</param> /// <param name="browserSettings">Browser initialization settings</param> /// <exception cref="System.Exception">An instance of the underlying offscreen browser has already been created, this method can only be called once.</exception> public void CreateBrowser(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); } //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) => { 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; }); }
/// <summary> /// Create the underlying CEF browser. The address and request context passed into the constructor /// will be used. If a <see cref="Action{IBrowser}"/> delegate was passed /// into the constructor it will not be called as this method overrides that value internally. /// </summary> /// <param name="windowInfo">Window information used when creating the browser</param> /// <param name="browserSettings">Browser initialization settings</param> /// <exception cref="System.Exception">An instance of the underlying offscreen browser has already been created, this method can only be called once.</exception> /// <returns> /// A <see cref="Task{IBrowser}"/> that represents the creation of the underlying CEF browser (<see cref="IBrowser"/> instance. /// When the task completes then the CEF Browser will have been created and you can start performing basic tasks. /// Note that the control's <see cref="BrowserInitialized"/> event will be invoked after this task completes. /// </returns> public Task <IBrowser> 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 <IBrowser>(); onAfterBrowserCreatedDelegate = new Action <IBrowser>(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) { browserSettings.Dispose(); } browserSettings = null; return(tcs.Task); }