コード例 #1
0
ファイル: ScriptObject.cs プロジェクト: fmaulanaa/moon
        internal object InvokeInternal(string name, params object [] args)
        {
            CheckName(name);
            CheckHandle();
            int    length = args == null ? 0 : args.Length;
            object result = null;

            Mono.Value    res;
            Mono.Value [] vargs = new Mono.Value [length];

            for (int i = 0; i < length; i++)
            {
                ScriptObjectHelper.ToValue(ref vargs [i], args [i]);
            }

            if (!NativeMethods.html_object_invoke(PluginHost.Handle, Handle, name, vargs, (uint)length, out res))
            {
                throw new InvalidOperationException();
            }

            using (res)
                if (res.k != Mono.Kind.INVALID)
                {
                    result = ScriptObjectHelper.FromValue(res);
                }

            return(result);
        }
コード例 #2
0
ファイル: ScriptObject.cs プロジェクト: fmaulanaa/moon
        internal object InvokeSelfInternal(params object [] args)
        {
            CheckHandle();
            int    length = args == null ? 0 : args.Length;
            object result = null;

            Mono.Value    res;
            Mono.Value [] vargs = new Mono.Value [length];

            for (int i = 0; i < length; i++)
            {
                ScriptObjectHelper.ToValue(ref vargs [i], args [i]);
            }

            try {
                if (!NativeMethods.html_object_invoke_self(PluginHost.Handle, Handle, vargs, (uint)length, out res))
                {
                    throw new InvalidOperationException();
                }

                if (res.k != Mono.Kind.INVALID)
                {
                    result = ScriptObjectHelper.FromValue(res);
                }
            }
            finally {
                res.Dispose();
                foreach (var v in vargs)
                {
                    v.Dispose();
                }
            }
            return(result);
        }
コード例 #3
0
        public static async Task <BrowserInformation> GetBrowserInformation()
        {
            if (browserInfoFunction == null)
            {
                browserInfoFunction = await WebSharp.CreateJavaScriptFunction(
                    @"return function (data, callback) {
                                    try { 
                                        let browserInfo = {}; 
                                        browserInfo['appName'] = navigator.appName;
                                        //browserInfo['appVersion'] = navigator.appVersion;
                                        browserInfo['appVersion'] = process.versions.chrome;
                                        browserInfo['userAgent'] = navigator.userAgent;
                                        browserInfo['platform'] = navigator.platform;
                                        browserInfo['cookieEnabled'] = navigator.cookieEnabled;
                                        browserInfo['appCodeName'] = navigator.appCodeName;
                                        callback(null, browserInfo);
                                    } catch (e) { callback(e, null); }
                                    
                                }");
            }
            var eval = await browserInfoFunction(null);

            if (eval != null)
            {
                var bi = new BrowserInformation();
                ScriptObjectHelper.DictionaryToScriptableType((IDictionary <string, object>)eval, bi);
                return(bi);
            }
            return(default(BrowserInformation));
        }
コード例 #4
0
ファイル: WebSharpHtmlEvent.cs プロジェクト: branux/WebSharp
        internal object Invoke(object evt)
        {
            var eventArgs = new HtmlEventArgs();

            if (evt != null)
            {
                var dict = (IDictionary <string, object>)((object[])evt)[0];
                ScriptObjectHelper.DictionaryToScriptableType(dict, eventArgs);
            }

            EventHandlers?.Invoke(this.Source, eventArgs);

            if (EventHandlers == null)
            {
                try
                {
                    var eventDelegate = (MulticastDelegate)Source.GetType().GetTypeInfo().GetField(EventName, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public).GetValue(Source);
                    eventDelegate.DynamicInvoke(new object[] { Source, EventArgs.Empty });
                }
                catch (Exception exc)
                {
                    // We get an exception if there is nothing to invoke
                    Console.WriteLine($"exception {exc.Message}");
                }
            }
            return(null);
        }
コード例 #5
0
ファイル: ScriptObject.cs プロジェクト: fmaulanaa/moon
        bool SetPropertyFromUnmanaged(string scriptAlias, IntPtr[] uargs, int arg_count, ref Value value)
        {
            if (ManagedObject is IDictionary)
            {
                IDictionary dic = ManagedObject as IDictionary;
                dic[scriptAlias] = ScriptObjectHelper.FromValue(value);
                return(true);
            }
            else if (properties != null && properties.ContainsKey(scriptAlias))
            {
                object[] args = new object[arg_count + 1];
                for (int i = 0; i < arg_count; i++)
                {
                    if (uargs[i] == IntPtr.Zero)
                    {
                        args[i] = null;
                    }
                    else
                    {
                        Value val = (Value)Marshal.PtrToStructure(uargs[i], typeof(Value));
                        args[i] = ScriptObjectHelper.FromValue(val);
                    }
                }

                args[arg_count] = ScriptObjectHelper.FromValue(value);
                SetProperty(scriptAlias, args);

                return(true);
            }
            else if (events != null && events.ContainsKey(scriptAlias))
            {
                if (arg_count != 0)
                {
                    throw new InvalidOperationException("arg count != 0");
                }

                EventInfo einfo = events[scriptAlias];

                if (value.k != Kind.NPOBJ)
                {
                    throw new InvalidOperationException("html bridge only allows function objects as event delegates");
                }

                ScriptObject event_object = ScriptObjectHelper.FromValue(value) as ScriptObject;
                if (event_object == null)
                {
                    RemoveEventHandler(scriptAlias, null);
                }
                else
                {
                    AddEventHandler(scriptAlias, event_object, einfo);
                }

                return(true);
            }
            else
            {
                return(false);
            }
        }
コード例 #6
0
ファイル: ScriptObject.cs プロジェクト: fmaulanaa/moon
        internal void Invoke(string name, object[] args, ref Value ret)
        {
            if (methods != null && methods.ContainsKey(name))
            {
                foreach (Method method in methods[name])
                {
                    if (method.method != null)
                    {
                        if (ValidateArguments(method.method, args))
                        {
                            object rv = Invoke(method.method, method.obj, args);

                            if (method.method.ReturnType != typeof(void))
                            {
                                ScriptObjectHelper.ToValue(ref ret, rv);
                            }

                            return;
                        }
                    }
                }
            }

            Console.WriteLine("ScriptObject.Invoke: NOT IMPLEMENTED: {0}", name.ToLower());
        }
コード例 #7
0
ファイル: ScriptObject.cs プロジェクト: fmaulanaa/moon
        bool GetPropertyFromUnmanaged(string scriptAlias, IntPtr[] uargs, int arg_count, ref Value value)
        {
            object[] args;
            if (ManagedObject is IDictionary && scriptAlias != "item")
            {
                args        = new object[] { scriptAlias };
                scriptAlias = "item";
            }
            else
            {
                args = new object[arg_count];
                for (int i = 0; i < arg_count; i++)
                {
                    if (uargs[i] == IntPtr.Zero)
                    {
                        args[i] = null;
                    }
                    else
                    {
                        Value val = (Value)Marshal.PtrToStructure(uargs[i], typeof(Value));
                        args[i] = ScriptObjectHelper.FromValue(val);
                    }
                }
            }

            object v = GetProperty(scriptAlias, args);

            ScriptObjectHelper.ToValue(ref value, v);

            return(true);
        }
コード例 #8
0
ファイル: ScriptObject.cs プロジェクト: fmaulanaa/moon
        internal T GetProperty <T> (string name)
        {
            object o = GetProperty(name);

            ScriptObjectHelper.TryChangeType(o, typeof(T), null, out o);
            return((T)o);
        }
コード例 #9
0
ファイル: App.cs プロジェクト: branux/WebSharp
        public async Task <LoginItemSettings> GetLoginItemSettings()
        {
            var settings = await Invoke <object>("getLoginItemSettings");

            var lis = new LoginItemSettings();

            ScriptObjectHelper.DictionaryToScriptableType((System.Collections.Generic.IDictionary <string, object>)settings, lis);
            return(lis);
        }
コード例 #10
0
ファイル: ScriptObject.cs プロジェクト: fmaulanaa/moon
 internal static void SetPropertyInternal(IntPtr h, string name, object value)
 {
     CheckName(name);
     Mono.Value dp = new Mono.Value();
     ScriptObjectHelper.ToValue(ref dp, value);
     using (dp) {
         NativeMethods.html_object_set_property(PluginHost.Handle, h, name, ref dp);
     }
 }
コード例 #11
0
ファイル: ScriptObject.cs プロジェクト: fmaulanaa/moon
        internal bool ValidateArguments(MethodInfo mi, object[] args)
        {
            if (mi.GetParameters().Length != args.Length)
            {
                int  required_paramcount = 0;
                bool optional_param      = false;
                foreach (ParameterInfo p in mi.GetParameters())
                {
                    if (IsOptional(p))
                    {
                        optional_param = true;
                        break;
                    }
                    required_paramcount++;
                }

                // if we don't supply enough required arguments, we fail regardless of optional parameters.
                if (required_paramcount > args.Length)
                {
                    return(false);
                }

                // if we don't have optional parameters, make sure we don't supply more than the required arguments.
                if (!optional_param && args.Length > required_paramcount)
                {
                    return(false);
                }
            }

            // TODO: refactor this, the js binder is doing this work already
            ParameterInfo[] parms = mi.GetParameters();
            for (int i = 0; i < parms.Length; i++)
            {
                if (IsOptional(parms[i]))
                {
                    break;
                }

                if (args[i] == null)
                {
                    continue;
                }

                Type   cstype = parms[i].ParameterType;
                object ret;

                if (ScriptObjectHelper.TryChangeType(args[i], cstype, CultureInfo.CurrentUICulture, out ret))
                {
                    continue;
                }
            }

            return(true);
        }
コード例 #12
0
        public override object Invoke(string name, params object [] args)
        {
            // this should likely call an Invoke overload passing ManagedObject in, instead of "this",
            // but we already do the book keeping in the base class, so do we need this method at all?

            Value v = new Value();

            base.Invoke(name, args, ref v);

            return(ScriptObjectHelper.FromValue(v));
        }
コード例 #13
0
ファイル: HtmlDocument.cs プロジェクト: branux/WebSharp
        public async Task <Location> GetLocation()
        {
            var location = await GetProperty <object>("location");

            if (location != null)
            {
                var loc = new Location();
                ScriptObjectHelper.DictionaryToScriptableType((IDictionary <string, object>)location, loc);
                return(loc);
            }
            return(default(Location));
        }
コード例 #14
0
ファイル: Clipboard.cs プロジェクト: branux/WebSharp
        public async Task <Bookmark> ReadBookmark(ClipboardType type = ClipboardType.None)
        {
            var result = await Invoke <object>("readBookmark", type == ClipboardType.Selection? "selection" : string.Empty);

            var bm = new Bookmark();

            if (result != null)
            {
                ScriptObjectHelper.DictionaryToScriptableType((System.Collections.Generic.IDictionary <string, object>)result, bm);
            }

            return(bm);
        }
コード例 #15
0
    async Task <object> MenuItemClicked(object[] mi)
    {
        try
        {
            var menuItemSelected = (MenuItem)(ScriptObjectHelper.AnonymousObjectToScriptObjectProxy(mi[0]));
            console.Log($"MenuItem: {await menuItemSelected.GetLabel()} was selected and the item is checked {await menuItemSelected.GetChecked()}");
        }
        catch (Exception exc)
        {
            System.Console.WriteLine(exc);
        }

        return(null);
    }
コード例 #16
0
ファイル: ScriptObject.cs プロジェクト: fmaulanaa/moon
        internal static object GetPropertyInternal(IntPtr h, string name)
        {
            object result = null;

            CheckName(name);
            Mono.Value res;
            NativeMethods.html_object_get_property(PluginHost.Handle, h, name, out res);
            using (res)
                if (res.k != Mono.Kind.INVALID)
                {
                    result = ScriptObjectHelper.FromValue(res);
                }
            return(result);
        }
コード例 #17
0
        public async Task <bool> DispatchEvent(Events.Event evt)
        {
            if (evt == null)
            {
                throw new ArgumentNullException(nameof(evt));
            }

            var parm = new
            {
                handle   = Handle,
                eventArg = ScriptObjectHelper.ScriptableTypeToDictionary(evt)
            };

            return(await WebSharp.Bridge.JavaScriptBridge.websharp_dispatchEvent(parm));
        }
コード例 #18
0
        internal object Invoke(object evt)
        {
            var eventArgs = new HtmlEventArgs();

            defaultPrevented = false;
            cancelBubble     = false;
            eventArgs.PreventDefaultAction  = () => { defaultPrevented = true; };
            eventArgs.StopPropagationAction = () => { cancelBubble = true; };

            if (evt != null)
            {
                try
                {
                    var dict = (IDictionary <string, object>)((object[])evt)[0];
                    ScriptObjectHelper.DictionaryToScriptableType(dict, eventArgs);
                    if (eventArgs.DataTransfer != null)
                    {
                        dropEffect = eventArgs.DataTransfer.DropEffectValue;
                        eventArgs.DataTransfer.DropEffectAction = (eff) => { dropEffect = eff; };
                    }
                }
                catch (Exception evtExc)
                {
                    Console.WriteLine($"Error parsing HtmlEventArgs: {evtExc.Message}");
                }
            }

            EventHandlers?.Invoke(this.Source, eventArgs);

            if (EventHandlers == null)
            {
                try
                {
                    var eventDelegate = (MulticastDelegate)Source.GetType().GetTypeInfo().GetField(EventName, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public).GetValue(Source);
                    eventDelegate.DynamicInvoke(new object[] { Source, EventArgs.Empty });
                }
                catch (Exception exc)
                {
                    // We get an exception if there is nothing to invoke
                    Console.WriteLine($"WebSharpHtmlEvent Invoke Exception {exc.Message}");
                }
            }
            return(null);
        }
コード例 #19
0
ファイル: ScriptObject.cs プロジェクト: fmaulanaa/moon
        bool InvokeFromUnmanaged(string name, IntPtr[] uargs, int arg_count, ref Value return_value)
        {
            object[] args = new object[arg_count];
            for (int i = 0; i < arg_count; i++)
            {
                if (uargs[i] == IntPtr.Zero)
                {
                    args[i] = null;
                }
                else
                {
                    Value v = (Value)Marshal.PtrToStructure(uargs[i], typeof(Value));
                    args[i] = ScriptObjectHelper.FromValue(v);
                }
            }

            Invoke(name, args, ref return_value);

            return(true);
        }
コード例 #20
0
ファイル: HtmlWindow.cs プロジェクト: ynkbt/moon
        public object Eval(string code)
        {
            if (code == null)
            {
                throw new ArgumentNullException("code");
            }
            if (code.Length == 0)
            {
                throw new ArgumentException("code");
            }

            IntPtr result;

            result = Mono.NativeMethods.plugin_instance_evaluate(PluginHost.Handle, code);

            if (result != IntPtr.Zero)
            {
                Value v = (Value)Marshal.PtrToStructure(result, typeof(Value));
                return(ScriptObjectHelper.FromValue(v));
            }
            return(null);
        }
コード例 #21
0
ファイル: ConvertUI.cs プロジェクト: hefujie1988/WebSharp
    /// <summary>
    /// Default entry into managed code.
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public async Task <object> Invoke(object input)
    {
        if (console == null)
        {
            console = await WebSharpJs.NodeJS.Console.Instance();
        }

        try
        {
            ipcRenderer = await IpcRenderer.Create();

            document = await HtmlPage.GetDocument();

            var form = await document.QuerySelector("form");

            // Input fields
            source = await form.QuerySelector("input[name=\"source\"]");

            destination = await form.QuerySelector("input[name=\"destination\"]");

            name = await form.QuerySelector("input[name=\"name\"]");

            // Buttons
            btnSource = await document.GetElementById("chooseSource");

            btnDestination = await document.GetElementById("chooseDestination");

            btnSubmit = await form.QuerySelector("button[type=\"submit\"]");

            // Handle input fields
            await source.AttachEvent(HtmlEventNames.Input, updateUI);

            await source.AttachEvent(HtmlEventNames.Change, updateUI);

            await destination.AttachEvent(HtmlEventNames.Input, updateUI);

            await destination.AttachEvent(HtmlEventNames.Change, updateUI);

            await name.AttachEvent(HtmlEventNames.Input, updateUI);

            // Handle buttons
            await btnSource.AttachEvent(HtmlEventNames.Click, handleSource);

            await btnDestination.AttachEvent(HtmlEventNames.Click, handleDestination);

            // Handle form Submit
            await form.AttachEvent(HtmlEventNames.Submit,
                                   async (s, e) => {
                e.PreventDefault();

                await ToggleUI(true);

                // Send off the convert request
                await ipcRenderer.Send("submitform",
                                       await source.GetProperty <string>("value"),
                                       await destination.GetProperty <string>("value"),
                                       await name.GetProperty <string>("value")
                                       );
            }
                                   );

            await ipcRenderer.AddListener("gotMetaData",
                                          new IpcRendererEventListener(
                                              async(result) =>
            {
                var state = result.CallbackState as object[];
                var ipcMainEvent = (IpcRendererEvent)state[0];
                var parms = state[1] as object[];

                var audio = ScriptObjectHelper.AnonymousObjectToScriptableType <Audio>(parms[0]);
                var video = ScriptObjectHelper.AnonymousObjectToScriptableType <Video>(parms[1]);
                var duration = TimeSpan.Parse(parms[2].ToString());

                await updateInfo(audio, video, duration);
            }

                                              )

                                          );

            await ipcRenderer.AddListener("completed",
                                          new IpcRendererEventListener(
                                              async(result) =>
            {
                await ToggleUI(false);
            }

                                              )

                                          );

            await console.Log($"Hello:  {input}");
        }
        catch (Exception exc) { await console.Log($"extension exception:  {exc.Message}"); }

        return(null);
    }