|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Reflection; |
| 4 | +using System.Threading; |
| 5 | + |
| 6 | +#if UNITY |
| 7 | +using TypeInfo = System.Type; |
| 8 | +#endif |
| 9 | + |
| 10 | +namespace Parse.Internal { |
| 11 | + internal class ObjectSubclassingController : IObjectSubclassingController { |
| 12 | + private ReaderWriterLockSlim mutex; |
| 13 | + private IDictionary<String, ObjectSubclassInfo> registeredSubclasses; |
| 14 | + |
| 15 | + public ObjectSubclassingController() { |
| 16 | + mutex = new ReaderWriterLockSlim(); |
| 17 | + registeredSubclasses = new Dictionary<String, ObjectSubclassInfo>(); |
| 18 | + } |
| 19 | + |
| 20 | + public String GetClassName(Type type) { |
| 21 | + return ObjectSubclassInfo.GetClassName(type.GetTypeInfo()); |
| 22 | + } |
| 23 | + |
| 24 | + public Type GetType(String className) { |
| 25 | + ObjectSubclassInfo info = null; |
| 26 | + mutex.EnterReadLock(); |
| 27 | + registeredSubclasses.TryGetValue(className, out info); |
| 28 | + mutex.ExitReadLock(); |
| 29 | + |
| 30 | + return info != null |
| 31 | + ? info.TypeInfo.AsType() |
| 32 | + : null; |
| 33 | + } |
| 34 | + |
| 35 | + public bool IsTypeValid(String className, Type type) { |
| 36 | + ObjectSubclassInfo subclassInfo = null; |
| 37 | + |
| 38 | + mutex.EnterReadLock(); |
| 39 | + registeredSubclasses.TryGetValue(className, out subclassInfo); |
| 40 | + mutex.ExitReadLock(); |
| 41 | + |
| 42 | + return subclassInfo == null |
| 43 | + ? type == typeof(ParseObject) |
| 44 | + : subclassInfo.TypeInfo == type.GetTypeInfo(); |
| 45 | + } |
| 46 | + |
| 47 | + public void RegisterSubclass(Type type) { |
| 48 | + TypeInfo typeInfo = type.GetTypeInfo(); |
| 49 | + if (!typeInfo.IsSubclassOf(typeof(ParseObject))) { |
| 50 | + throw new ArgumentException("Cannot register a type that is not a subclass of ParseObject"); |
| 51 | + } |
| 52 | + |
| 53 | + String className = ObjectSubclassInfo.GetClassName(typeInfo); |
| 54 | + |
| 55 | + try { |
| 56 | + // Perform this as a single independent transaction, so we can never get into an |
| 57 | + // intermediate state where we *theoretically* register the wrong class due to a |
| 58 | + // TOCTTOU bug. |
| 59 | + mutex.EnterWriteLock(); |
| 60 | + |
| 61 | + ObjectSubclassInfo previousInfo = null; |
| 62 | + if (registeredSubclasses.TryGetValue(className, out previousInfo)) { |
| 63 | + if (typeInfo.IsAssignableFrom(previousInfo.TypeInfo)) { |
| 64 | + // Previous subclass is more specific or equal to the current type, do nothing. |
| 65 | + return; |
| 66 | + } else if (previousInfo.TypeInfo.IsAssignableFrom(typeInfo)) { |
| 67 | + // Previous subclass is parent of new child, fallthrough and actually register |
| 68 | + // this class. |
| 69 | + /* Do nothing */ |
| 70 | + } else { |
| 71 | + throw new ArgumentException( |
| 72 | + "Tried to register both " + previousInfo.TypeInfo.FullName + " and " + typeInfo.FullName + |
| 73 | + " as the ParseObject subclass of " + className + ". Cannot determine the right class " + |
| 74 | + "to use because neither inherits from the other." |
| 75 | + ); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + ConstructorInfo constructor = type.FindConstructor(); |
| 80 | + if (constructor == null) { |
| 81 | + throw new ArgumentException("Cannot register a type that does not implement the default constructor!"); |
| 82 | + } |
| 83 | + |
| 84 | + registeredSubclasses[className] = new ObjectSubclassInfo(type, constructor); |
| 85 | + } finally { |
| 86 | + mutex.ExitWriteLock(); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + public void UnregisterSubclass(Type type) { |
| 91 | + mutex.EnterWriteLock(); |
| 92 | + registeredSubclasses.Remove(GetClassName(type)); |
| 93 | + mutex.ExitWriteLock(); |
| 94 | + } |
| 95 | + |
| 96 | + public ParseObject Instantiate(String className) { |
| 97 | + ObjectSubclassInfo info = null; |
| 98 | + |
| 99 | + mutex.EnterReadLock(); |
| 100 | + registeredSubclasses.TryGetValue(className, out info); |
| 101 | + mutex.ExitReadLock(); |
| 102 | + |
| 103 | + return info != null |
| 104 | + ? info.Instantiate() |
| 105 | + : new ParseObject(className); |
| 106 | + } |
| 107 | + |
| 108 | + public IDictionary<String, String> GetPropertyMappings(String className) { |
| 109 | + ObjectSubclassInfo info = null; |
| 110 | + mutex.EnterReadLock(); |
| 111 | + registeredSubclasses.TryGetValue(className, out info); |
| 112 | + mutex.ExitReadLock(); |
| 113 | + |
| 114 | + return info != null |
| 115 | + ? info.PropertyMappings |
| 116 | + : null; |
| 117 | + } |
| 118 | + } |
| 119 | +} |
0 commit comments