<commit_before>﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using System.Text;
using Xamarin.Forms;
using XLabs.Forms.Controls;
using XLabs.Serialization.JsonNET;

namespace AppShell.Mobile
{
    [View(typeof(WebBrowserViewModel))]
    public class WebBrowserPage : ContentPage
    {
        class DispatchData
        {
            public string ServiceName { get; set; }
            public string MethodName { get; set; }
            public string CallbackId { get; set; }
            public object[] Arguments { get; set; }
        }

        class SubscribeEventData
        {
            public string ServiceName { get; set; }
            public string EventName { get; set; }
            public string CallbackId { get; set; }
        }

        public static readonly BindableProperty UrlProperty = BindableProperty.Create<WebBrowserPage, string>(d => d.Url, null, propertyChanged: UrlPropertyChanged);
        public static readonly BindableProperty HtmlProperty = BindableProperty.Create<WebBrowserPage, string>(d => d.Html, null, propertyChanged: 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(BindableObject d, string oldValue, string newValue)
        {
            WebBrowserPage webBrowserPage = d as WebBrowserPage;

            if (newValue != null)
                webBrowserPage.webView.Source = new UrlWebViewSource() { Url = newValue };
        }

        public static void HtmlPropertyChanged(BindableObject d, string oldValue, string newValue)
        {
            WebBrowserPage webBrowserPage = d as WebBrowserPage;
            
            if (newValue != null)
                webBrowserPage.webView.Source = new HtmlWebViewSource() { Html = newValue };
        }

        private HybridWebView webView;

        public WebBrowserPage(IServiceDispatcher serviceDispatcher)
        {
            webView = new HybridWebView(new XLabs.Serialization.JsonNET.JsonSerializer());
            
            string serviceDispatcherScript = null;

            using (StreamReader streamReader = new StreamReader(typeof(WebBrowserViewModel).GetTypeInfo().Assembly.GetManifestResourceStream("AppShell.ServiceDispatcher.js")))
                serviceDispatcherScript = streamReader.ReadToEnd();

            string services = JsonConvert.SerializeObject(serviceDispatcher.Services);

            webView.LoadFinished += (s, e) =>
            {
                webView.InjectJavaScript(serviceDispatcherScript);
                webView.InjectJavaScript(string.Format("serviceDispatcher.initialize({0});", services));
            };
                        
            webView.RegisterCallback("dispatch", args =>
            {
                DispatchData dispatchData = JsonConvert.DeserializeObject<DispatchData>(args);
                
                string result = JsonConvert.SerializeObject(serviceDispatcher.Dispatch(dispatchData.ServiceName, dispatchData.MethodName, dispatchData.Arguments));

                if (dispatchData.CallbackId != null)
                    webView.InjectJavaScript(string.Format("serviceDispatcher._dispatchCallback('{0}', {1});", dispatchData.CallbackId, result));
            });

            webView.RegisterCallback("subscribeEvent", args =>
            {
                SubscribeEventData subscribeEventData = JsonConvert.DeserializeObject<SubscribeEventData>(args);

                serviceDispatcher.SubscribeEvent(subscribeEventData.ServiceName, subscribeEventData.EventName, this, (s, e) =>
                {
                    if (subscribeEventData.CallbackId != null)
                        webView.InjectJavaScript(string.Format("serviceDispatcher._eventCallback('{0}', '{1}');", subscribeEventData.CallbackId, JsonConvert.SerializeObject(e)));
                });
            });

            Content = webView;       

            SetBinding(UrlProperty, new Binding("Url"));
            SetBinding(HtmlProperty, new Binding("Html"));
        }
    }
}<commit_msg>Execute script calls on ui thread<commit_after>﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using System.Text;
using Xamarin.Forms;
using XLabs.Forms.Controls;
using XLabs.Serialization.JsonNET;

namespace AppShell.Mobile
{
    [View(typeof(WebBrowserViewModel))]
    public class WebBrowserPage : ContentPage
    {
        class DispatchData
        {
            public string ServiceName { get; set; }
            public string MethodName { get; set; }
            public string CallbackId { get; set; }
            public object[] Arguments { get; set; }
        }

        class SubscribeEventData
        {
            public string ServiceName { get; set; }
            public string EventName { get; set; }
            public string CallbackId { get; set; }
        }

        public static readonly BindableProperty UrlProperty = BindableProperty.Create<WebBrowserPage, string>(d => d.Url, null, propertyChanged: UrlPropertyChanged);
        public static readonly BindableProperty HtmlProperty = BindableProperty.Create<WebBrowserPage, string>(d => d.Html, null, propertyChanged: 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(BindableObject d, string oldValue, string newValue)
        {
            WebBrowserPage webBrowserPage = d as WebBrowserPage;

            if (newValue != null)
                webBrowserPage.webView.Source = new UrlWebViewSource() { Url = newValue };
        }

        public static void HtmlPropertyChanged(BindableObject d, string oldValue, string newValue)
        {
            WebBrowserPage webBrowserPage = d as WebBrowserPage;
            
            if (newValue != null)
                webBrowserPage.webView.Source = new HtmlWebViewSource() { Html = newValue };
        }

        private HybridWebView webView;

        public WebBrowserPage(IServiceDispatcher serviceDispatcher, IPlatformProvider platformProvider)
        {
            webView = new HybridWebView(new XLabs.Serialization.JsonNET.JsonSerializer());
            
            string serviceDispatcherScript = null;

            using (StreamReader streamReader = new StreamReader(typeof(WebBrowserViewModel).GetTypeInfo().Assembly.GetManifestResourceStream("AppShell.ServiceDispatcher.js")))
                serviceDispatcherScript = streamReader.ReadToEnd();

            string services = JsonConvert.SerializeObject(serviceDispatcher.Services);

            webView.LoadFinished += (s, e) =>
            {
                webView.InjectJavaScript(serviceDispatcherScript);
                webView.InjectJavaScript(string.Format("serviceDispatcher.initialize({0});", services));
            };
                        
            webView.RegisterCallback("dispatch", args =>
            {
                DispatchData dispatchData = JsonConvert.DeserializeObject<DispatchData>(args);
                
                string result = JsonConvert.SerializeObject(serviceDispatcher.Dispatch(dispatchData.ServiceName, dispatchData.MethodName, dispatchData.Arguments));

                if (dispatchData.CallbackId != null)
                    platformProvider.ExecuteOnUIThread(() => webView.InjectJavaScript(string.Format("serviceDispatcher._dispatchCallback('{0}', {1});", dispatchData.CallbackId, result)));
            });

            webView.RegisterCallback("subscribeEvent", args =>
            {
                SubscribeEventData subscribeEventData = JsonConvert.DeserializeObject<SubscribeEventData>(args);

                serviceDispatcher.SubscribeEvent(subscribeEventData.ServiceName, subscribeEventData.EventName, this, (s, e) =>
                {
                    if (subscribeEventData.CallbackId != null)
                        platformProvider.ExecuteOnUIThread(() => webView.InjectJavaScript(string.Format("serviceDispatcher._eventCallback('{0}', '{1}');", subscribeEventData.CallbackId, JsonConvert.SerializeObject(e))));
                });
            });

            Content = webView;       

            SetBinding(UrlProperty, new Binding("Url"));
            SetBinding(HtmlProperty, new Binding("Html"));
        }
    }
}
{'fraction_non_alphanumeric': 0.09176926584587324, 'fraction_numerical': 0.0011399908800729594, 'mean_word_length': 2.5720684039087947, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 25, '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': '5820744', 'n_tokens_mistral': 2183, 'n_tokens_neox': 2083, 'n_words': 455}