<commit_before>﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;

namespace CefSharp.WinForms
{
    // TODO: Should not implement IRenderWebBrowser straight away, since it means we have to add a bunch of boilerplate properties
    // TODO: which aren't even used. The proper way would be to add another interface, which is only used for WinForms (which the
    // TODO: IRenderWebBrowser should then inherit from, and extend as appropriate).
    public class WebView : Control, IRenderWebBrowser
    {
        private readonly BrowserCore browserCore;
        private ManagedCefBrowserAdapter managedCefBrowserAdapter;
        public BrowserSettings BrowserSettings { get; set; }

        public string Title { get; set; }
        public bool IsLoading { get; set; }
        public string TooltipText { get; set; }

        public string Address
        {
            get
            {
                return browserCore.Address;
            }
            set
            {
                browserCore.Address = value;
            }
        }
        bool IWebBrowser.CanGoForward
        {
            get { return browserCore.CanGoForward; }
        }
        bool IWebBrowser.CanGoBack
        {
            get { return browserCore.CanGoBack; }
        }

        public WebView(string address)
        {
            browserCore = new BrowserCore(address);
            Application.ApplicationExit += OnApplicationExit;
        }

        private void OnApplicationExit(object sender, EventArgs e)
        {
            if (managedCefBrowserAdapter == null)
            {
                return;
            }

            managedCefBrowserAdapter.Close();
            managedCefBrowserAdapter.Dispose();
            managedCefBrowserAdapter = null;
        
            Cef.Shutdown();
        }

        public void LoadHtml(string html, string url)
        {
        }

        public void Load(String url)
        {
            managedCefBrowserAdapter.LoadUrl(url);
        }

        public void RegisterJsObject(string name, object objectToBind)
        {
            throw new NotImplementedException();
        }

        public void ExecuteScriptAsync(string script)
        {
            managedCefBrowserAdapter.ExecuteScriptAsync(script);
        }

        public object EvaluateScript(string script)
        {
            return EvaluateScript(script, timeout: null);
        }

        public object EvaluateScript(string script, TimeSpan? timeout)
        {
            if (timeout == null)
            {
                timeout = TimeSpan.MaxValue;
            }

            return managedCefBrowserAdapter.EvaluateScript(script, timeout.Value);
        }

        public event LoadErrorEventHandler LoadError;
        public event LoadCompletedEventHandler LoadCompleted;
        public event ConsoleMessageEventHandler ConsoleMessage;
        public event PropertyChangedEventHandler PropertyChanged;

        public void OnInitialized()
        {
            browserCore.OnInitialized();
        }

        protected override void OnHandleCreated(EventArgs e)
        {
            base.OnHandleCreated(e);
            managedCefBrowserAdapter = new ManagedCefBrowserAdapter(this);
            managedCefBrowserAdapter.CreateBrowser(BrowserSettings ?? new BrowserSettings(), Handle, browserCore.Address);
        }

        protected override void OnSizeChanged(EventArgs e)
        {
            base.OnSizeChanged(e);
            if (managedCefBrowserAdapter != null)
                managedCefBrowserAdapter.OnSizeChanged(Handle);
        }

        public void SetAddress(string address)
        {
            browserCore.Address = address;
            PropertyChanged(this, new PropertyChangedEventArgs("Address"));
        }

        public void SetIsLoading(bool isLoading)
        {
            IsLoading = isLoading;
        }

        public void SetNavState(bool canGoBack, bool canGoForward, bool canReload)
        {
            browserCore.SetNavState(canGoBack, canGoForward, canReload);
        }

        public void SetTitle(string title)
        {
            Title = title;
        }

        public void SetTooltipText(string tooltipText)<commit_msg>#210 Need to add a null check here.<commit_after>﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;

namespace CefSharp.WinForms
{
    // TODO: Should not implement IRenderWebBrowser straight away, since it means we have to add a bunch of boilerplate properties
    // TODO: which aren't even used. The proper way would be to add another interface, which is only used for WinForms (which the
    // TODO: IRenderWebBrowser should then inherit from, and extend as appropriate).
    public class WebView : Control, IRenderWebBrowser
    {
        private readonly BrowserCore browserCore;
        private ManagedCefBrowserAdapter managedCefBrowserAdapter;
        public BrowserSettings BrowserSettings { get; set; }

        public string Title { get; set; }
        public bool IsLoading { get; set; }
        public string TooltipText { get; set; }

        public string Address
        {
            get
            {
                return browserCore.Address;
            }
            set
            {
                browserCore.Address = value;
            }
        }
        bool IWebBrowser.CanGoForward
        {
            get { return browserCore.CanGoForward; }
        }
        bool IWebBrowser.CanGoBack
        {
            get { return browserCore.CanGoBack; }
        }

        public WebView(string address)
        {
            browserCore = new BrowserCore(address);
            Application.ApplicationExit += OnApplicationExit;
        }

        private void OnApplicationExit(object sender, EventArgs e)
        {
            if (managedCefBrowserAdapter == null)
            {
                return;
            }

            managedCefBrowserAdapter.Close();
            managedCefBrowserAdapter.Dispose();
            managedCefBrowserAdapter = null;
        
            Cef.Shutdown();
        }

        public void LoadHtml(string html, string url)
        {
        }

        public void Load(String url)
        {
            managedCefBrowserAdapter.LoadUrl(url);
        }

        public void RegisterJsObject(string name, object objectToBind)
        {
            throw new NotImplementedException();
        }

        public void ExecuteScriptAsync(string script)
        {
            managedCefBrowserAdapter.ExecuteScriptAsync(script);
        }

        public object EvaluateScript(string script)
        {
            return EvaluateScript(script, timeout: null);
        }

        public object EvaluateScript(string script, TimeSpan? timeout)
        {
            if (timeout == null)
            {
                timeout = TimeSpan.MaxValue;
            }

            return managedCefBrowserAdapter.EvaluateScript(script, timeout.Value);
        }

        public event LoadErrorEventHandler LoadError;
        public event LoadCompletedEventHandler LoadCompleted;
        public event ConsoleMessageEventHandler ConsoleMessage;
        public event PropertyChangedEventHandler PropertyChanged;

        public void OnInitialized()
        {
            browserCore.OnInitialized();
        }

        protected override void OnHandleCreated(EventArgs e)
        {
            base.OnHandleCreated(e);
            managedCefBrowserAdapter = new ManagedCefBrowserAdapter(this);
            managedCefBrowserAdapter.CreateBrowser(BrowserSettings ?? new BrowserSettings(), Handle, browserCore.Address);
        }

        protected override void OnSizeChanged(EventArgs e)
        {
            base.OnSizeChanged(e);
            if (managedCefBrowserAdapter != null)
                managedCefBrowserAdapter.OnSizeChanged(Handle);
        }

        public void SetAddress(string address)
        {
            browserCore.Address = address;

            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs("Address"));
            }
        }

        public void SetIsLoading(bool isLoading)
        {
            IsLoading = isLoading;
        }

        public void SetNavState(bool canGoBack, bool canGoForward, bool canReload)
        {
            browserCore.SetNavState(canGoBack, canGoForward, canReload);
        }

        public void SetTitle(string title)
        {
            Title = title;
        }

        public void SetTooltipText(string tooltipText)
{'fraction_non_alphanumeric': 0.06302325581395349, 'fraction_numerical': 0.0003488372093023256, 'mean_word_length': 1.897911051212938, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2239320', 'n_tokens_mistral': 2054, 'n_tokens_neox': 1992, 'n_words': 533}