private void eventHandler(Object sender, EventArgs e, String eventName, String handle, WebBrowser browser) { TiResponse response = new TiResponse(); response["_hnd"] = handle; response["type"] = eventName; string senderHandle = ""; bool senderExists = InstanceRegistry.containsInstance(sender); if (senderExists) { senderHandle = InstanceRegistry.getInstanceHandleByValue(sender); } else { senderHandle = InstanceRegistry.createHandle(sender); } response["sender"] = senderHandle; string eventArgsHandle = ""; bool eventArgsExists = InstanceRegistry.containsInstance(e); if (eventArgsExists) { eventArgsHandle = InstanceRegistry.getInstanceHandleByValue(e); } else { eventArgsHandle = InstanceRegistry.createHandle(e); } response["eventArgs"] = eventArgsHandle; response["error"] = null; if (e.GetType() == typeof(ErrorEventArgs)) { response["error"] = ((ErrorEventArgs)e).error; } browser.InvokeScript("execScript", new string[] { "tiwp8.fireEvent(" + JsonConvert.SerializeObject(response, Formatting.None) + ")" }); if (!senderExists) { InstanceRegistry.removeInstance(senderHandle); } if (!eventArgsExists) { InstanceRegistry.removeInstance(eventArgsHandle); } }
public async void run(object instance, MethodInfo methodInfo, object[] fnArguments) { SynchronizationContext ctx = SynchronizationContext.Current; if (!methodInfo.ReturnType.GetInterfaces().Contains(typeof(IAsyncInfo))) { // this should be easy, just run it as if it was synchronous var result = methodInfo.Invoke(instance, fnArguments); InvokeAsyncEventArgs eventArgs = new InvokeAsyncEventArgs(); if (methodInfo.ReturnType == typeof(void)) { eventArgs.primitiveValue = null; } else { Type type = result.GetType(); if (type.IsPrimitive || type == typeof(string) || type == typeof(decimal)) { eventArgs.primitiveValue = result; } else if (InstanceRegistry.containsInstance(result)) { eventArgs.handle = InstanceRegistry.getInstanceHandleByValue(result); } else { string handle = InstanceRegistry.createHandle(result); eventArgs.handle = handle; } } this.OnComplete(eventArgs); return; } MethodInfo castMethod = Type.GetType("TitaniumApp.TiRequestHandlers.AsyncAwaiter").GetMethod("awaitTask"); castMethod = castMethod.MakeGenericMethod(methodInfo.ReturnType.GenericTypeArguments[0]); InvokeAsync _this = this; Task.Run(() => { var comObject = methodInfo.Invoke(instance, fnArguments); Task <object> obj = (Task <object>)castMethod.Invoke(null, new object[] { comObject }); obj.Wait(); var result = obj.Result; InvokeAsyncEventArgs eventArgs = new InvokeAsyncEventArgs(); if (methodInfo.ReturnType == typeof(void)) { eventArgs.primitiveValue = null; } else { Type type = result.GetType(); if (type.IsPrimitive || type == typeof(string) || type == typeof(decimal)) { eventArgs.primitiveValue = result; } else if (InstanceRegistry.containsInstance(result)) { eventArgs.handle = InstanceRegistry.getInstanceHandleByValue(result); } else { string handle = InstanceRegistry.createHandle(result); eventArgs.handle = handle; } } ctx.Post(args => { _this.OnComplete((InvokeAsyncEventArgs)args); }, eventArgs); }); }
private TiResponse invokeStaticAsync(TiRequestParams data) { if (!data.ContainsKey("className")) { throw new ReflectionException("\"invokeStatic\" request missing \"className\" param"); } string className = (string)data["className"]; var classType = InstanceRegistry.lookupType(className); if (classType == null) { throw new ReflectionException("\"invokeStatic\" request invalid classname \"" + className + "\""); } if (!data.ContainsKey("method")) { throw new ReflectionException("\"invokeStatic\" request missing \"method\" param"); } if (!data.ContainsKey("args")) { throw new ReflectionException("\"invokeStatic\" request missing \"args\" param"); } JArray args = (JArray)data["args"]; if (args.Count % 2 != 0) { throw new ReflectionException("\"invokeStatic\" request arguments must contain an even number of type-values"); } // create the argument types array Type[] fnArgumentTypes = new Type[args.Count / 2]; for (int i = 0, j = 0; i < args.Count; i += 2, j++) { fnArgumentTypes[j] = InstanceRegistry.lookupType((string)args[i]); } // get the method info MethodInfo methodInfo = classType.GetMethod((string)data["method"], fnArgumentTypes); // create the argument values array object[] fnArguments = new object[args.Count / 2]; for (int i = 1, j = 0; i < args.Count; i += 2, j++) { JObject arg = (JObject)args[i]; if (arg["valueHnd"] != null) { fnArguments[j] = InstanceRegistry.getInstance((string)arg["valueHnd"]); } else if (fnArgumentTypes[j] == typeof(Uri)) { fnArguments[j] = new Uri((string)arg["valuePrimitive"], UriKind.RelativeOrAbsolute); } else { fnArguments[j] = ((JValue)arg["valuePrimitive"]).Value; } if (fnArguments[j] != null) { IConvertible convertible = fnArguments[j] as IConvertible; if (convertible != null) { fnArguments[j] = Convert.ChangeType(fnArguments[j], fnArgumentTypes[j]); } } } TiResponse response = new TiResponse(); InvokeAsync ia = new InvokeAsync(); response["handle"] = InstanceRegistry.createHandle(ia); ia.run(null, methodInfo, fnArguments); return(response); }
private TiResponse createInstance(TiRequestParams data) { if (!data.ContainsKey("className")) { throw new ReflectionException("\"createInstance\" request missing \"className\" param"); } string className = (string)data["className"]; var classType = InstanceRegistry.lookupType(className); if (classType == null) { throw new ReflectionException("\"createInstance\" request invalid classname \"" + classType + "\""); } if (!data.ContainsKey("args")) { throw new ReflectionException("\"createInstance\" request missing \"args\" param"); } JArray args = (JArray)data["args"]; if (args.Count % 2 != 0) { throw new ReflectionException("\"createInstance\" request arguments must contain an even number of type-values"); } // create the argument types array Type[] ctorArgumentTypes = new Type[args.Count / 2]; for (int i = 0, j = 0; i < args.Count; i += 2, j++) { ctorArgumentTypes[j] = InstanceRegistry.lookupType((string)args[i]); } // create the argument values array object[] ctorArguments = new object[args.Count / 2]; for (int i = 1, j = 0; i < args.Count; i += 2, j++) { JObject arg = (JObject)args[i]; if (arg["valueHnd"] != null) { ctorArguments[j] = InstanceRegistry.getInstance((string)arg["valueHnd"]); } else if (ctorArgumentTypes[j] == typeof(Uri)) { ctorArguments[j] = new Uri((string)arg["valuePrimitive"], UriKind.RelativeOrAbsolute); } else { ctorArguments[j] = ((JValue)arg["valuePrimitive"]).Value; } if (ctorArguments[j] != null) { ctorArguments[j] = Convert.ChangeType(ctorArguments[j], ctorArgumentTypes[j]); } } // Invoke the constructor and return the result var instance = classType.GetConstructor(ctorArgumentTypes).Invoke(ctorArguments); TiResponse response = new TiResponse(); response["handle"] = InstanceRegistry.createHandle(instance); return(response); }
public TiResponse process(TiRequestParams data) { if (!data.ContainsKey("url")) { throw new DownloadException("Request missing 'url' param"); } string url = (string)data["url"]; if (!url.StartsWith("http://") && !url.StartsWith("https://")) { throw new DownloadException("'url' param must start with 'http://' or 'https://'"); } string saveTo = ""; int p; if (data.ContainsKey("saveTo")) { saveTo = (string)data["saveTo"]; } else { // try to determine the filename based on the URL p = url.LastIndexOf('/'); if (p > 8 && p != -1) // make sure the last / is after the :// { saveTo = url.Substring(p + 1); } else { throw new DownloadException("Request missing 'saveTo' param"); } } if (saveTo == null || saveTo == "") { throw new DownloadException("Invalid 'saveTo' param"); } saveTo = saveTo.Replace('\\', '/'); IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication(); p = saveTo.LastIndexOf('\\'); if (p != -1) { string dir = saveTo.Substring(0, p); try { if (!isf.DirectoryExists(dir)) { isf.CreateDirectory(dir); } } catch (IsolatedStorageException) { throw new DownloadException("Unable to create destination directory '" + dir + "' because of insufficient permissions or the isolated storage has been disabled or removed"); } } if (isf.FileExists(saveTo)) { if (data.ContainsKey("overwrite") && (bool)data["overwrite"]) { isf.DeleteFile(saveTo); } else { throw new DownloadException("File '" + saveTo + "' already exists"); } } IsolatedStorageFileStream fileStream = null; try { fileStream = isf.CreateFile(saveTo); } catch (IsolatedStorageException) { throw new DownloadException("Unable to create file '" + saveTo + "' because the isolated storage has been disabled or removed"); } catch (DirectoryNotFoundException) { throw new DownloadException("Unable to create file '" + saveTo + "' because the directory does not exist"); } catch (ObjectDisposedException) { throw new DownloadException("Unable to create file '" + saveTo + "' because the isolated storage has been disposed"); } TiResponse response = new TiResponse(); DownloadFile df = new DownloadFile(SynchronizationContext.Current, new Uri(url), fileStream); response["handle"] = InstanceRegistry.createHandle(df); return(response); }