private TiResponse removeEventListener(TiRequestParams data)
        {
            if (!data.ContainsKey("handle"))
            {
                throw new ReflectionException("\"addEventListener\" request missing \"handle\" param");
            }

            string handle   = (string)data["handle"];
            object instance = InstanceRegistry.getInstance(handle);

            if (instance == null)
            {
                throw new ReflectionException("\"addEventListener\" request invalid handle \"" + handle + "\"");
            }

            if (!data.ContainsKey("name"))
            {
                throw new ReflectionException("\"addEventListener\" request missing \"name\" param");
            }

            string   eventName = (string)data["name"];
            var      eventInfo = instance.GetType().GetEvent(eventName);
            Delegate handler   = InstanceRegistry.getDelegate(handle + "." + eventName);

            if (handler != null)
            {
                eventInfo.RemoveEventHandler(instance, handler);
                InstanceRegistry.removeDelegate(handle + "." + eventName);
            }

            return(null);
        }
        public TiResponse process(TiRequestParams data)
        {
            if (data.ContainsKey("message")) {
                string message = (string)data["message"];

                MatchCollection matches = logLevelRegex.Matches(message);
                if (matches.Count > 0) {
                    GroupCollection groups = matches[0].Groups;
                    if (groups.Count > 2) {
                        string level = groups[1].Value.ToUpper();
                        Logger.log(level == "LOG" ? "INFO" : level, groups[2].Value);
                        return null;
                    }
                }

                if (data.ContainsKey("level")) {
                    string level = ((string)data["level"]).ToUpper();
                    Logger.log(level == "LOG" ? "INFO" : level, message);
                    return null;
                }

                Logger.log(message);
            }
            return null;
        }
        private TiResponse staticProperty(TiRequestParams data)
        {
            if (!data.ContainsKey("className"))
            {
                throw new ReflectionException("\"staticProperty\" request missing \"className\" param");
            }

            string className = (string)data["className"];
            var    classType = InstanceRegistry.lookupType(className);

            if (classType == null)
            {
                throw new ReflectionException("\"staticProperty\" request invalid classname \"" + className + "\"");
            }

            if (!data.ContainsKey("property"))
            {
                throw new ReflectionException("\"staticProperty\" request missing \"property\" param");
            }

            PropertyInfo propertyInfo = classType.GetProperty((string)data["property"]);

            if (propertyInfo == null)
            {
                throw new ReflectionException("\"staticProperty\" request invalid property \"" + data["property"] + "\"");
            }

            object val = propertyInfo.GetValue(null);

            return(InstanceRegistry.createReturnType(val));
        }
        public TiResponse process(TiRequestParams data)
        {
            if (data.ContainsKey("message")) {
                JArray message = (JArray)data["message"];

                for (int i = 0; i < message.Count; i++) {
                    string[] lines = ((string)message[i]).Split(new char[] { '\n' });

                    for (int j = 0; j < lines.Length; j++) {
                        string line = lines[j];

                        MatchCollection matches = logLevelRegex.Matches(line);
                        if (matches.Count > 0) {
                            GroupCollection groups = matches[0].Groups;
                            if (groups.Count > 2) {
                                string level = groups[1].Value.ToUpper();
                                Logger.log(level == "LOG" ? "INFO" : level, groups[2].Value);
                                continue;
                            }
                        }

                        if (data.ContainsKey("level")) {
                            string level = ((string)data["level"]).ToUpper();
                            Logger.log(level == "LOG" ? "INFO" : level, line);
                            continue;
                        }

                        Logger.log(line);
                    }
                }
            }
            return null;
        }
Exemple #5
0
        public TiResponse process(TiRequestParams data)
        {
            if (data.ContainsKey("message"))
            {
                string message = (string)data["message"];

                MatchCollection matches = logLevelRegex.Matches(message);
                if (matches.Count > 0)
                {
                    GroupCollection groups = matches[0].Groups;
                    if (groups.Count > 2)
                    {
                        string level = groups[1].Value.ToUpper();
                        Logger.log(level == "LOG" ? "INFO" : level, groups[2].Value);
                        return(null);
                    }
                }

                if (data.ContainsKey("level"))
                {
                    string level = ((string)data["level"]).ToUpper();
                    Logger.log(level == "LOG" ? "INFO" : level, message);
                    return(null);
                }

                Logger.log(message);
            }
            return(null);
        }
 public TiResponse process(TiRequestParams data)
 {
     if (data.ContainsKey("message")) {
         Logger.log((string)data["message"]);
     }
     return null;
 }
        private TiResponse addEventListener(TiRequestParams data)
        {
            if (!data.ContainsKey("handle"))
            {
                throw new ReflectionException("\"addEventListener\" request missing \"handle\" param");
            }

            string handle   = (string)data["handle"];
            object instance = InstanceRegistry.getInstance(handle);

            if (instance == null)
            {
                throw new ReflectionException("\"addEventListener\" request invalid handle \"" + handle + "\"");
            }

            if (!data.ContainsKey("name"))
            {
                throw new ReflectionException("\"addEventListener\" request missing \"name\" param");
            }

            string eventName = (string)data["name"];
            var    eventInfo = instance.GetType().GetEvent(eventName);

            // create an array of parameters based on the event info
            var parameters = eventInfo.EventHandlerType.GetMethod("Invoke").GetParameters().
                             Select((p, i) => Expression.Parameter(p.ParameterType, "p" + i)).ToArray();

            // construct a new array of parameters to the method we are actually going to call
            // need to pass in extra information so the Proxy knows which callback to fire
            Expression[] pass = new Expression[5];
            parameters.CopyTo(pass, 0);
            pass[2] = Expression.Constant(eventInfo.Name);
            pass[3] = Expression.Constant(handle);
            pass[4] = Expression.Constant(InstanceRegistry.getInstance("browser"));

            // Get method info of the handler we want to call
            MethodInfo           methodInfo = this.GetType().GetMethod("eventHandler", BindingFlags.NonPublic | BindingFlags.Instance);
            MethodCallExpression methodCall = Expression.Call(Expression.Constant(this), methodInfo, pass);

            // Construct a delegate using a lambda expression
            // (Object, EventArgs) => dummyHandler(Object, EventArgs, String, String, WebBrowser)
            var lambda = Expression.Lambda(
                eventInfo.EventHandlerType,
                methodCall,
                parameters
                ).Compile();

            // Hook the event to the delegate
            eventInfo.AddEventHandler(instance, lambda);

            // Store the delegate to remove it later
            InstanceRegistry.addDelegate(handle + "." + eventName, lambda);

            return(null);
        }
        private TiResponse destroy(TiRequestParams data)
        {
            if (!data.ContainsKey("handle"))
            {
                throw new ReflectionException("\"invokeMethod\" request missing \"handle\" param");
            }

            string handle   = (string)data["handle"];
            object instance = InstanceRegistry.getInstance(handle);

            if (instance == null)
            {
                throw new ReflectionException("\"invokeMethod\" request invalid handle \"" + handle + "\"");
            }

            // remove from parent view
            var propertyInfo = instance.GetType().GetProperty("Parent");

            if (propertyInfo != null)
            {
                var parent = propertyInfo.GetValue(instance);
                if (parent != null)
                {
                    propertyInfo = parent.GetType().GetProperty("Children");
                    if (propertyInfo != null)
                    {
                        var children = propertyInfo.GetValue(parent);
                        var remove   = children.GetType().GetMethod("Remove");
                        remove.Invoke(children, new object[] { instance });
                    }
                }
            }

            // call Dispose method
            var dispose = instance.GetType().GetMethod("Dispose");

            if (dispose != null)
            {
                dispose.Invoke(instance, null);
            }

            // call Finalize method
            var finalize = instance.GetType().GetMethod("Finalize");

            if (finalize != null)
            {
                finalize.Invoke(instance, null);
            }

            // remove global reference
            InstanceRegistry.removeInstance(handle);

            return(null);
        }
        private TiResponse addEventListener(TiRequestParams data)
        {
            if (!data.ContainsKey("handle")) {
                throw new Exception("Reflection Handler Exception: \"addEventListener\" request missing \"handle\" param");
            }

            string handle = (string)data["handle"];

            if (!instances.ContainsKey(handle)) {
                throw new Exception("Reflection Handler Exception: \"addEventListener\" request invalid handle \"" + handle + "\"");
            }

            if (!data.ContainsKey("name")) {
                throw new Exception("Reflection Handler Exception: \"addEventListener\" request missing \"name\" param");
            }

            string eventName = (string)data["name"];
            var instance = instances[handle];
            var eventInfo = instance.GetType().GetEvent(eventName);

            // create an array of parameters based on the event info
            var parameters = eventInfo.EventHandlerType.GetMethod("Invoke").GetParameters().
                    Select((p, i) => Expression.Parameter(p.ParameterType, "p" + i)).ToArray();

            // construct a new array of parameters to the method we are actually going to call
            // need to pass in extra information so the Proxy knows which callback to fire
            Expression[] pass = new Expression[5];
            parameters.CopyTo(pass, 0);
            pass[2] = Expression.Constant(eventInfo.Name);
            pass[3] = Expression.Constant(handle);
            pass[4] = Expression.Constant(instances["browser"]);

            // Get method info of the handler we want to call
            MethodInfo methodInfo = this.GetType().GetMethod("eventHandler", BindingFlags.NonPublic | BindingFlags.Instance);
            MethodCallExpression methodCall = Expression.Call(Expression.Constant(this), methodInfo, pass);

            // Construct a delegate using a lambda expression
            // (Object, EventArgs) => dummyHandler(Object, EventArgs, String, String, WebBrowser)
            var lambda = Expression.Lambda(
                eventInfo.EventHandlerType,
                methodCall,
                parameters
            ).Compile();

            // Hook the event to the delegate
            eventInfo.AddEventHandler(instance, lambda);

            // Store the delegate to remove it later
            delegates[handle + "." + eventName] = lambda;

            return null;
        }
        public TiResponse process(TiRequestParams data)
        {
            if (!data.ContainsKey("file"))
            {
                throw new FileRequestException("Request missing 'file' param");
            }

            string file = collapsePath((string)data["file"]);

            if (file.IndexOf("..") == 0)
            {
                throw new FileRequestException("The requested file must not begin with \"..\"");
            }
            file = "App/" + file;

            if (!File.Exists(file))
            {
                throw new FileRequestException("File \"" + file + "\" does not exist");
            }

            bool isBinary = false;

            if (data.ContainsKey("isBinary"))
            {
                isBinary = (bool)data["isBinary"];
            }

            TiResponse response = new TiResponse();

            response["file"] = file;

            if (isBinary)
            {
                var    filestream = new FileStream(file, FileMode.Open);
                byte[] bytes      = new byte[filestream.Length];
                filestream.Read(bytes, 0, (int)filestream.Length);
                response["encoding"] = "base64";
                response["contents"] = Convert.ToBase64String(bytes);
            }
            else
            {
                response["contents"] = (new StreamReader(file)).ReadToEnd();
            }

            return(response);
        }
        public TiResponse process(TiRequestParams data)
        {
            if (!data.ContainsKey("action")) {
                throw new Exception("Reflection Handler Exception: Request missing 'action' param");
            }

            string action = (string)data["action"];

            switch (action) {
                case "addEventListener":	return addEventListener(data);
                case "createInstance":		return createInstance(data);
                case "destroy":				return destroy(data);
                case "getEnum":				return getEnum(data);
                case "invokeMethod":		return invokeMethod(data);
                case "invokeStatic":		return invokeStatic(data);
                case "property":			return property(data);
                case "removeEventListener":	return removeEventListener(data);
                default:
                    throw new Exception("Reflection Handler Exception: Invalid action \"" + action + "\"");
            }
        }
        private TiResponse getEnum(TiRequestParams data)
        {
            if (!data.ContainsKey("name"))
            {
                throw new ReflectionException("\"getEnum\" request missing \"name\" param");
            }

            if (!data.ContainsKey("value"))
            {
                throw new ReflectionException("\"getEnum\" request missing \"value\" param");
            }

            string name  = (string)data["name"];
            string value = (string)data["value"];

            Type t = InstanceRegistry.lookupType(name);

            if (t == null)
            {
                throw new ReflectionException("\"getEnum\" request failed because \"" + name + "\" is an invalid class name");
            }

            object val = null;

            if (t.IsEnum)
            {
                val = Enum.Parse(t, value);
            }
            else
            {
                var prop = t.GetProperty(value);
                if (prop != null)
                {
                    val = prop.GetValue(null, null);
                }
            }

            return(InstanceRegistry.createReturnType(val));
        }
Exemple #13
0
        public TiResponse process(TiRequestParams data)
        {
            if (data.ContainsKey("message"))
            {
                JArray message = (JArray)data["message"];

                for (int i = 0; i < message.Count; i++)
                {
                    string[] lines = ((string)message[i]).Split(new char[] { '\n' });

                    for (int j = 0; j < lines.Length; j++)
                    {
                        string line = lines[j];

                        MatchCollection matches = logLevelRegex.Matches(line);
                        if (matches.Count > 0)
                        {
                            GroupCollection groups = matches[0].Groups;
                            if (groups.Count > 2)
                            {
                                string level = groups[1].Value.ToUpper();
                                Logger.log(level == "LOG" ? "INFO" : level, groups[2].Value);
                                continue;
                            }
                        }

                        if (data.ContainsKey("level"))
                        {
                            string level = ((string)data["level"]).ToUpper();
                            Logger.log(level == "LOG" ? "INFO" : level, line);
                            continue;
                        }

                        Logger.log(line);
                    }
                }
            }
            return(null);
        }
        public TiResponse process(TiRequestParams data)
        {
            if (!data.ContainsKey("action"))
            {
                throw new ReflectionException("Request missing 'action' param");
            }

            string action = (string)data["action"];

            switch (action)
            {
            case "addEventListener":        return(addEventListener(data));

            case "createInstance":          return(createInstance(data));

            case "destroy":                         return(destroy(data));

            case "getEnum":                         return(getEnum(data));

            case "invokeMethod":            return(invokeMethod(data));

            case "invokeMethodAsync":       return(invokeMethodAsync(data));

            case "invokeStatic":            return(invokeStatic(data));

            case "invokeStaticAsync":       return(invokeStaticAsync(data));

            case "property":                        return(property(data));

            case "removeEventListener":     return(removeEventListener(data));

            case "staticProperty":          return(staticProperty(data));

            default:
                throw new ReflectionException("Invalid action \"" + action + "\"");
            }
        }
        public TiResponse process(TiRequestParams data)
        {
            if (!data.ContainsKey("file")) {
                throw new FileRequestException("Request missing 'file' param");
            }

            string file = collapsePath((string)data["file"]);
            if (file.IndexOf("..") == 0) {
                throw new FileRequestException("The requested file must not begin with \"..\"");
            }
            file = "App/" + file;

            if (!File.Exists(file)) {
                throw new FileRequestException("File \"" + file + "\" does not exist");
            }

            bool isBinary = false;
            if (data.ContainsKey("isBinary")) {
                isBinary = (bool)data["isBinary"];
            }

            TiResponse response = new TiResponse();
            response["file"] = file;

            if (isBinary) {
                var filestream = new FileStream(file, FileMode.Open);
                byte[] bytes = new byte[filestream.Length];
                filestream.Read(bytes, 0, (int)filestream.Length);
                response["encoding"] = "base64";
                response["contents"] = Convert.ToBase64String(bytes);
            } else {
                response["contents"] = (new StreamReader(file)).ReadToEnd();
            }

            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 ise) {
					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 ise) {
				throw new DownloadException("Unable to create file '" + saveTo + "' because the isolated storage has been disabled or removed");
			} catch (DirectoryNotFoundException dnfe) {
				throw new DownloadException("Unable to create file '" + saveTo + "' because the directory does not exist");
			} catch (ObjectDisposedException dnfe) {
				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;
		}
        private TiResponse createInstance(TiRequestParams data)
        {
            if (!data.ContainsKey("className")) {
                throw new Exception("Reflection Handler Exception: \"createInstance\" request missing \"className\" param");
            }

            string className = (string)data["className"];
            var classType = lookupType(className);
            if (classType == null) {
                throw new Exception("Reflection Handler Exception: \"createInstance\" request invalid classname \"" + classType + "\"");
            }

            if (!data.ContainsKey("args")) {
                throw new Exception("Reflection Handler Exception: \"createInstance\" request missing \"args\" param");
            }

            JArray args = (JArray)data["args"];
            if (args.Count % 2 != 0) {
                throw new Exception("Reflection Handler Exception: \"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] = 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] = instances[(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]);
                }
            }

            if ((uint)instanceCount > UInt32.MaxValue) {
                throw new Exception("Reflection Handler Exception: Maximum instance count exceeded");
            }

            // Invoke the constructor and return the result
            var instance = classType.GetConstructor(ctorArgumentTypes).Invoke(ctorArguments);

            string handle = instanceCount++.ToString();
            instances[handle] = instance;

            TiResponse response = new TiResponse();
            response["handle"] = handle;
            return response;
        }
        private TiResponse destroy(TiRequestParams data)
        {
            if (!data.ContainsKey("handle")) {
                throw new Exception("Reflection Handler Exception: \"invokeMethod\" request missing \"handle\" param");
            }

            string handle = (string)data["handle"];

            if (!instances.ContainsKey(handle)) {
                throw new Exception("Reflection Handler Exception: \"invokeMethod\" request invalid handle \"" + handle + "\"");
            }

            var instance = instances[handle];

            // remove from parent view
            var propertyInfo = instance.GetType().GetProperty("Parent");
            if (propertyInfo != null) {
                var parent = propertyInfo.GetValue(instance);
                if (parent != null) {
                    propertyInfo = parent.GetType().GetProperty("Children");
                    if (propertyInfo != null) {
                        var children = propertyInfo.GetValue(parent);
                        var remove = children.GetType().GetMethod("Remove");
                        remove.Invoke(children, new object[] { instance });
                    }
                }
            }

            // call Dispose method
            var dispose = instance.GetType().GetMethod("Dispose");
            if (dispose != null) {
                dispose.Invoke(instance, null);
            }

            // call Finalize method
            var finalize = instance.GetType().GetMethod("Finalize");
            if (finalize != null) {
                finalize.Invoke(instance, null);
            }

            // remove global reference
            instances.Remove(handle);

            return null;
        }
        private TiResponse getEnum(TiRequestParams data)
        {
            if (!data.ContainsKey("name")) {
                throw new Exception("Reflection Handler Exception: \"getEnum\" request missing \"name\" param");
            }

            if (!data.ContainsKey("value")) {
                throw new Exception("Reflection Handler Exception: \"getEnum\" request missing \"value\" param");
            }

            string name = (string)data["name"];
            string value = (string)data["value"];

            Type t = lookupType(name);
            if (t == null) {
                throw new Exception("Reflection Handler Exception: \"getEnum\" request failed because \"" + name + "\" is an invalid class name");
            }

            object val = null;

            if (t.IsEnum) {
                val = Enum.Parse(t, value);
            } else {
                var prop = t.GetProperty(value);
                if (prop != null) {
                    val = prop.GetValue(null, null);
                }
            }

            return createReturnType(val.GetType(), val);
        }
        private TiResponse property(TiRequestParams data)
        {
            if (!data.ContainsKey("handle"))
            {
                throw new ReflectionException("\"property\" request missing \"handle\" param");
            }

            string handle   = (string)data["handle"];
            object instance = InstanceRegistry.getInstance(handle);

            if (instance == null)
            {
                throw new ReflectionException("\"property\" request invalid handle \"" + handle + "\"");
            }

            if (!data.ContainsKey("name"))
            {
                throw new ReflectionException("\"property\" request missing \"name\" param");
            }

            var obj = data["name"];

            if (obj == null)
            {
                throw new ReflectionException("\"property\" request \"name\" is null");
            }

            Type instanceType = instance.GetType();
            Type propType     = instanceType.GetType();

            TiResponse response = new TiResponse();

            response["value"] = null;

            switch (obj.GetType().ToString())
            {
            case "Newtonsoft.Json.Linq.JArray":
            {
                // get multiple props
                JArray arr = (JArray)obj;
                Dictionary <string, TiResponse> value = new Dictionary <string, TiResponse>();
                for (var i = 0; i < arr.Count; i++)
                {
                    string propName     = arr[i].ToString();
                    var    propertyInfo = instanceType.GetProperty(propName);
                    if (propertyInfo == null)
                    {
                        throw new ReflectionException("Invalid property \"" + propName + "\"");
                    }
                    object val = propertyInfo.GetValue(instance);
                    value[propName] = InstanceRegistry.createReturnType(val);
                }
                response["value"] = value;
                return(response);
            }

            case "Newtonsoft.Json.Linq.JObject":
            {
                // set multiple props
                JObject props = (JObject)obj;
                foreach (JProperty prop in props.Properties())
                {
                    var propertyInfo = instanceType.GetProperty(prop.Name);
                    if (propertyInfo == null)
                    {
                        throw new ReflectionException("Invalid property \"" + prop.Name + "\"");
                    }
                    JObject value = (JObject)prop.Value;
                    if (value["valueHnd"] != null)
                    {
                        propertyInfo.SetValue(instance, InstanceRegistry.getInstance((string)value["valueHnd"]));
                    }
                    else if (value["valuePrimitive"] != null)
                    {
                        var valuePrimitive = value["valuePrimitive"];
                        if (propertyInfo.PropertyType == typeof(Uri))
                        {
                            propertyInfo.SetValue(instance, Convert.ChangeType(new Uri(valuePrimitive.ToObject <string>(), UriKind.RelativeOrAbsolute), propertyInfo.PropertyType));
                        }
                        else
                        {
                            propertyInfo.SetValue(instance, valuePrimitive.ToObject(propertyInfo.PropertyType));
                        }
                    }
                }

                return(null);
            }

            case "System.String":
            {
                string name         = (string)obj;
                var    propertyInfo = instanceType.GetProperty(name);
                if (propertyInfo == null)
                {
                    throw new ReflectionException("Invalid property \"" + name + "\"");
                }

                // setting a single prop
                if (data.ContainsKey("valueHnd") && data["valueHnd"] != null)
                {
                    propertyInfo.SetValue(instance, InstanceRegistry.getInstance((string)data["valueHnd"]));
                    return(null);
                }
                else if (data.ContainsKey("valuePrimitive"))
                {
                    var valuePrimitive = data["valuePrimitive"];
                    if (propertyInfo.PropertyType == typeof(Uri))
                    {
                        valuePrimitive = new Uri((string)valuePrimitive, UriKind.RelativeOrAbsolute);
                    }
                    propertyInfo.SetValue(instance, Convert.ChangeType(valuePrimitive, propertyInfo.PropertyType));
                    return(null);
                }

                // getting a single prop
                object val = propertyInfo.GetValue(instance);
                return(InstanceRegistry.createReturnType(val));
            }
            }

            return(null);
        }
        private TiResponse removeEventListener(TiRequestParams data)
        {
            if (!data.ContainsKey("handle")) {
                throw new Exception("Reflection Handler Exception: \"addEventListener\" request missing \"handle\" param");
            }

            string handle = (string)data["handle"];

            if (!instances.ContainsKey(handle)) {
                throw new Exception("Reflection Handler Exception: \"addEventListener\" request invalid handle \"" + handle + "\"");
            }

            if (!data.ContainsKey("name")) {
                throw new Exception("Reflection Handler Exception: \"addEventListener\" request missing \"name\" param");
            }

            string eventName = (string)data["name"];
            var instance = instances[handle];
            var eventInfo = instance.GetType().GetEvent(eventName);
            var handler = delegates[handle + "." + eventName];
            eventInfo.RemoveEventHandler(instances[handle], handler);
            delegates.Remove(handle + "." + eventName);

            return null;
        }
        private TiResponse invokeMethod(TiRequestParams data)
        {
            if (!data.ContainsKey("handle"))
            {
                throw new ReflectionException("\"invokeMethod\" request missing \"handle\" param");
            }

            string handle   = (string)data["handle"];
            object instance = InstanceRegistry.getInstance(handle);

            if (instance == null)
            {
                throw new ReflectionException("\"invokeMethod\" request invalid handle \"" + handle + "\"");
            }

            if (!data.ContainsKey("method"))
            {
                throw new ReflectionException("\"invokeMethod\" request missing \"method\" param");
            }

            if (!data.ContainsKey("args"))
            {
                throw new ReflectionException("\"invokeMethod\" request missing \"args\" param");
            }

            JArray args = (JArray)data["args"];

            if (args.Count % 2 != 0)
            {
                throw new ReflectionException("\"invokeMethod\" 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 = instance.GetType().GetMethod((string)data["method"], fnArgumentTypes);

            if (methodInfo.ReturnType.GetInterfaces().Contains(typeof(IAsyncInfo)))
            {
                throw new Exception("Use invokeMethodAsync() to call this method");
            }

            // 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]);
                    }
                }
            }

            // invoke the method
            var result = methodInfo.Invoke(instance, fnArguments);

            if (methodInfo.ReturnType == typeof(void))
            {
                result = null;
            }

            return(InstanceRegistry.createReturnType(result));
        }
        private TiResponse invokeStatic(TiRequestParams data)
        {
            if (!data.ContainsKey("className")) {
                throw new Exception("Reflection Handler Exception: \"invokeStatic\" request missing \"className\" param");
            }

            string className = (string)data["className"];
            var classType = lookupType(className);
            if (classType == null) {
                throw new Exception("Reflection Handler Exception: \"invokeStatic\" request invalid classname \"" + classType + "\"");
            }

            if (!data.ContainsKey("method")) {
                throw new Exception("Reflection Handler Exception: \"invokeStatic\" request missing \"method\" param");
            }

            if (!data.ContainsKey("args")) {
                throw new Exception("Reflection Handler Exception: \"invokeStatic\" request missing \"args\" param");
            }

            JArray args = (JArray)data["args"];
            if (args.Count % 2 != 0) {
                throw new Exception("Reflection Handler Exception: \"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] = lookupType((string)args[i]);
            }

            // 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] = instances[(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) {
                    fnArguments[j] = Convert.ChangeType(fnArguments[j], fnArgumentTypes[j]);
                }
            }

            // get the method info
            MethodInfo methodInfo = classType.GetMethod((string)data["method"], fnArgumentTypes);

            // invoke the method
            var result = methodInfo.Invoke(null, fnArguments);

            if (methodInfo.ReturnType == typeof(void)) {
                result = null;
            }

            return createReturnType(result == null ? null : result.GetType(), result);
        }
        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);
        }
        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);
        }
        private TiResponse property(TiRequestParams data)
        {
            if (!data.ContainsKey("handle")) {
                throw new Exception("Reflection Handler Exception: \"property\" request missing \"handle\" param");
            }

            string handle = (string)data["handle"];

            if (!instances.ContainsKey(handle)) {
                throw new Exception("Reflection Handler Exception: \"property\" request invalid handle \"" + handle + "\"");
            }

            if (!data.ContainsKey("name")) {
                throw new Exception("Reflection Handler Exception: \"property\" request missing \"name\" param");
            }

            var obj = data["name"];
            if (obj == null) {
                throw new Exception("Reflection Handler Exception: \"property\" request \"name\" is null");
            }

            var instance = instances[handle];
            Type instanceType = instance.GetType();
            Type propType = instanceType.GetType();

            TiResponse response = new TiResponse();
            response["value"] = null;

            switch (obj.GetType().ToString()) {
                case "Newtonsoft.Json.Linq.JArray":
                    {
                        // get multiple props
                        JArray arr = (JArray)obj;
                        Dictionary<string, TiResponse> value = new Dictionary<string, TiResponse>();
                        for (var i = 0; i < arr.Count; i++) {
                            string propName = arr[i].ToString();
                            object val = instanceType.GetProperty(propName).GetValue(instance);
                            value[propName] = createReturnType(val.GetType(), val);
                        }
                        response["value"] = value;
                        return response;
                    }

                case "Newtonsoft.Json.Linq.JObject":
                    {
                        // set multiple props
                        JObject props = (JObject)obj;
                        foreach (JProperty prop in props.Properties()) {
                            var propertyInfo = instanceType.GetProperty(prop.Name);
                            JObject value = (JObject)prop.Value;
                            if (value["valueHnd"] != null) {
                                propertyInfo.SetValue(instance, instances[(string)value["valueHnd"]]);
                            } else if (value["valuePrimitive"] != null) {
                                var valuePrimitive = value["valuePrimitive"];
                                if (propertyInfo.PropertyType == typeof(Uri)) {
                                    propertyInfo.SetValue(instance, Convert.ChangeType(new Uri(valuePrimitive.ToObject<string>(), UriKind.RelativeOrAbsolute), propertyInfo.PropertyType));
                                } else {
                                    propertyInfo.SetValue(instance, valuePrimitive.ToObject(propertyInfo.PropertyType));
                                }
                            }
                        }

                        return null;
                    }

                case "System.String":
                    {
                        string name = (string)obj;
                        var propertyInfo = instanceType.GetProperty(name);

                        // setting a single prop
                        if (data.ContainsKey("valueHnd") && data["valueHnd"] != null) {
                            propertyInfo.SetValue(instance, instances[(string)data["valueHnd"]]);
                            return null;
                        } else if (data.ContainsKey("valuePrimitive")) {
                            var valuePrimitive = data["valuePrimitive"];
                            if (propertyInfo.PropertyType == typeof(Uri)) {
                                valuePrimitive = new Uri((string)valuePrimitive, UriKind.RelativeOrAbsolute);
                            }
                            propertyInfo.SetValue(instance, Convert.ChangeType(valuePrimitive, propertyInfo.PropertyType));
                            return null;
                        }

                        // getting a single prop
                        object val = propertyInfo.GetValue(instance);
                        return createReturnType(val.GetType(), val);
                    }
            }

            return null;
        }