<commit_before>﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace AppShell.Desktop.Views
{
    /// <summary>
    /// Interaction logic for WebBrowserView.xaml
    /// </summary>
    [View(typeof(WebBrowserViewModel))]
    public partial class WebBrowserView : UserControl
    {
        public static readonly DependencyProperty UrlProperty = DependencyProperty.Register("Url", typeof(string), typeof(WebBrowserView), new PropertyMetadata(null, UrlPropertyChanged));
        public static readonly DependencyProperty HtmlProperty = DependencyProperty.Register("Html", typeof(string), typeof(WebBrowserView), new PropertyMetadata(null, HtmlPropertyChanged));

        public string Url { get { return (string)GetValue(UrlProperty); } set { SetValue(UrlProperty, value); } }
        public string Html { get { return (string)GetValue(HtmlProperty); } set { SetValue(HtmlProperty, value); } }

        public static void UrlPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            WebBrowserView webBrowserView  = d as WebBrowserView;

            if (e.NewValue != null)
            {
                string url = (string)e.NewValue;
                webBrowserView.WebBrowser.Navigate(url);
            }
        }

        public static void HtmlPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            WebBrowserView webBrowserView = d as WebBrowserView;

            if (e.NewValue != null)
            {
                string html = (string)e.NewValue;
                webBrowserView.WebBrowser.NavigateToString(html);
            }
        }        

        public WebBrowserView()
        {
            InitializeComponent();

            SetBinding(UrlProperty, new Binding("Url"));
            SetBinding(HtmlProperty, new Binding("Html"));

            WebBrowser.ObjectForScripting = AppShellCore.Container.GetInstance<ScriptInterface>();
            
            string serviceDispatcherScript = null;

            using (StreamReader streamReader = new StreamReader(Assembly.GetExecutingAssembly().GetManifestResourceStream("AppShell.Desktop.ServiceDispatcher.js")))
                serviceDispatcherScript = streamReader.ReadToEnd();

            string services = JsonConvert.SerializeObject(AppShellCore.Container.GetInstance<IServiceDispatcher>().Services);

            WebBrowser.LoadCompleted += (s, e) =>
            {
                WebBrowser.InvokeScript("execScript", new object[] { serviceDispatcherScript, "JavaScript" });
                WebBrowser.InvokeScript("execScript", new object[] { string.Format("serviceDispatcher.initialize({0});", services), "JavaScript" });
            };            
        }
    }

    [ComVisible(true)]
    public class ScriptInterface
    {
        private IServiceDispatcher serviceDispatcher;

        public ScriptInterface(IServiceDispatcher serviceDispatcher)
        {
            this.serviceDispatcher = serviceDispatcher;
        }

        public string Dispatch(string serviceName, string methodName, string arguments)
        {
            object[] parameters = JsonConvert.DeserializeObject<Dictionary<int, object>>(arguments).Select(p => p.Value).ToArray();

            for (int i = 0; i < parameters.Length; i++)
            {
                if (parameters[i] is JArray)
                    parameters[i] = (parameters[i] as JArray).ToObject<object[]>();
                else if (parameters[i] is JObject)
                    parameters[i] = (parameters[i] as JObject).ToObject<Dictionary<string, object>>();
            }

            return JsonConvert.SerializeObject(serviceDispatcher.Dispatch(serviceName, methodName, parameters));
        }
                
        public void SubscribeEvent(string serviceName, string eventName, dynamic callback)
        {
            serviceDispatcher.SubscribeEvent(serviceName, eventName, this, (s, e) =>
            {
                callback(JsonConvert.SerializeObject(e));                
            });
        }

        public void UnsubscribeEvent()
        {
        }
    }
}<commit_msg>Catch UnauthorizedAccessException when dispatching an event<commit_after>﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace AppShell.Desktop.Views
{
    /// <summary>
    /// Interaction logic for WebBrowserView.xaml
    /// </summary>
    [View(typeof(WebBrowserViewModel))]
    public partial class WebBrowserView : UserControl
    {
        public static readonly DependencyProperty UrlProperty = DependencyProperty.Register("Url", typeof(string), typeof(WebBrowserView), new PropertyMetadata(null, UrlPropertyChanged));
        public static readonly DependencyProperty HtmlProperty = DependencyProperty.Register("Html", typeof(string), typeof(WebBrowserView), new PropertyMetadata(null, HtmlPropertyChanged));

        public string Url { get { return (string)GetValue(UrlProperty); } set { SetValue(UrlProperty, value); } }
        public string Html { get { return (string)GetValue(HtmlProperty); } set { SetValue(HtmlProperty, value); } }

        public static void UrlPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            WebBrowserView webBrowserView  = d as WebBrowserView;

            if (e.NewValue != null)
            {
                string url = (string)e.NewValue;
                webBrowserView.WebBrowser.Navigate(url);
            }
        }

        public static void HtmlPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            WebBrowserView webBrowserView = d as WebBrowserView;

            if (e.NewValue != null)
            {
                string html = (string)e.NewValue;
                webBrowserView.WebBrowser.NavigateToString(html);
            }
        }        

        public WebBrowserView()
        {
            InitializeComponent();

            SetBinding(UrlProperty, new Binding("Url"));
            SetBinding(HtmlProperty, new Binding("Html"));

            WebBrowser.ObjectForScripting = AppShellCore.Container.GetInstance<ScriptInterface>();
            
            string serviceDispatcherScript = null;

            using (StreamReader streamReader = new StreamReader(Assembly.GetExecutingAssembly().GetManifestResourceStream("AppShell.Desktop.ServiceDispatcher.js")))
                serviceDispatcherScript = streamReader.ReadToEnd();

            string services = JsonConvert.SerializeObject(AppShellCore.Container.GetInstance<IServiceDispatcher>().Services);

            WebBrowser.LoadCompleted += (s, e) =>
            {
                WebBrowser.InvokeScript("execScript", new object[] { serviceDispatcherScript, "JavaScript" });
                WebBrowser.InvokeScript("execScript", new object[] { string.Format("serviceDispatcher.initialize({0});", services), "JavaScript" });
            };            
        }
    }

    [ComVisible(true)]
    public class ScriptInterface
    {
        private IServiceDispatcher serviceDispatcher;

        public ScriptInterface(IServiceDispatcher serviceDispatcher)
        {
            this.serviceDispatcher = serviceDispatcher;
        }

        public string Dispatch(string serviceName, string methodName, string arguments)
        {
            object[] parameters = JsonConvert.DeserializeObject<Dictionary<int, object>>(arguments).Select(p => p.Value).ToArray();

            for (int i = 0; i < parameters.Length; i++)
            {
                if (parameters[i] is JArray)
                    parameters[i] = (parameters[i] as JArray).ToObject<object[]>();
                else if (parameters[i] is JObject)
                    parameters[i] = (parameters[i] as JObject).ToObject<Dictionary<string, object>>();
            }

            return JsonConvert.SerializeObject(serviceDispatcher.Dispatch(serviceName, methodName, parameters));
        }
                
        public void SubscribeEvent(string serviceName, string eventName, dynamic callback)
        {
            serviceDispatcher.SubscribeEvent(serviceName, eventName, this, (s, e) =>
            {
                try
                {
                    callback(JsonConvert.SerializeObject(e));
                }
                catch(UnauthorizedAccessException)
                {
                }
            });
        }

        public void UnsubscribeEvent()
        {
        }
    }
}
{'fraction_non_alphanumeric': 0.09542189342768308, 'fraction_numerical': 0.0004288624423716093, 'mean_word_length': 2.576687116564417, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 27, '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': '3827094', 'n_tokens_mistral': 2326, 'n_tokens_neox': 2214, 'n_words': 485}