/empty-project/Assets/Plugins/iOS/FirebaseiOSImpl.cs

https://gitlab.com/vectorci/Firebase-Unity · C# · 289 lines · 225 code · 49 blank · 15 comment · 20 complexity · 77499084fa723fe17fe013a092090596 MD5 · raw file

  1. /*
  2. Copyright 2015 Google Inc. All Rights Reserved.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. using UnityEngine;
  14. using System.Collections;
  15. using System.Runtime.InteropServices;
  16. using System;
  17. using System.Collections.Generic;
  18. using AOT;
  19. #if UNITY_IOS
  20. internal class FirebaseiOSImpl : QueryiOSImpl, IFirebase {
  21. static object auth_sync = new object ();
  22. static AuthCallInstanceEntry activeCallEntry;
  23. static OnAuthSuccessHandler authSuccessHandler = new OnAuthSuccessHandler(onAuthSuccess);
  24. static OnAuthCancelHandler authFailureHandler = new OnAuthCancelHandler(onAuthCancel);
  25. public delegate void OnAuthSuccessHandler(int reference, String token, String uid, int expiration);
  26. public delegate void OnAuthCancelHandler(int reference, int code, String message, String details);
  27. public FirebaseiOSImpl (IntPtr nativeReference)
  28. :base(nativeReference)
  29. {
  30. }
  31. public static FirebaseiOSImpl CreateNewFirebaseiOSImpl(IntPtr nativeReference) {
  32. return QueryiOSImpl.GetOrCreateCachedInstance (nativeReference, () => {
  33. return new FirebaseiOSImpl(nativeReference);
  34. });
  35. }
  36. private static IntPtr CreateNativeFirebase (string path)
  37. {
  38. return _FirebaseNew (path);
  39. }
  40. ~FirebaseiOSImpl() {
  41. IntPtr nativeReference = GetiOSObject ();
  42. if (nativeReference != IntPtr.Zero) {
  43. _FirebaseRemoveObservers(nativeReference);
  44. _FirebaseRelease (nativeReference);
  45. }
  46. }
  47. #region Imports
  48. [DllImport ("__Internal")]
  49. private static extern IntPtr _FirebaseNew (string path);
  50. [DllImport ("__Internal")]
  51. private static extern void _FirebaseRelease(IntPtr firebase);
  52. [DllImport("__Internal")]
  53. private static extern void _FirebaseRemoveObservers (IntPtr firebase);
  54. [DllImport ("__Internal")]
  55. private static extern IntPtr _FirebaseChild (IntPtr firebase, string path);
  56. [DllImport ("__Internal")]
  57. private static extern IntPtr _FirebaseParent (IntPtr firebase);
  58. [DllImport ("__Internal")]
  59. private static extern IntPtr _FirebaseRoot (IntPtr firebase);
  60. [DllImport ("__Internal")]
  61. private static extern string _FirebaseGetKey(IntPtr firebase);
  62. [DllImport ("__Internal")]
  63. private static extern IntPtr _FirebasePush (IntPtr firebase);
  64. [DllImport ("__Internal")]
  65. private static extern void _FirebaseSetString (IntPtr firebase, string value);
  66. [DllImport ("__Internal")]
  67. private static extern void _FirebaseSetJson (IntPtr firebase, string json);
  68. [DllImport ("__Internal")]
  69. private static extern void _FirebaseSetFloat (IntPtr firebase, float value);
  70. [DllImport ("__Internal")]
  71. private static extern void _FirebaseSetPriority (IntPtr firebase, string value);
  72. [DllImport ("__Internal")]
  73. private static extern void _FirebaseAuthWithCustomToken (IntPtr firebase, string token,
  74. OnAuthSuccessHandler success, OnAuthCancelHandler cancel,
  75. int callback);
  76. [DllImport ("__Internal")]
  77. private static extern void _FirebaseAuthAnonymously (IntPtr firebase,
  78. OnAuthSuccessHandler success, OnAuthCancelHandler cancel,
  79. int callback);
  80. [DllImport ("__Internal")]
  81. private static extern void _FirebaseAuthWithPassword (IntPtr firebase, string email,
  82. string password,
  83. OnAuthSuccessHandler success, OnAuthCancelHandler cancel,
  84. int callback);
  85. [DllImport ("__Internal")]
  86. private static extern void _FirebaseAuthWithOAuthToken (IntPtr firebase, string provider,
  87. string token,
  88. OnAuthSuccessHandler success, OnAuthCancelHandler cancel,
  89. int callback);
  90. [DllImport ("__Internal")]
  91. private static extern string _FirebaseGetAuthToken(IntPtr firebase);
  92. [DllImport ("__Internal")]
  93. private static extern string _FirebaseGetAuthUid(IntPtr firebase);
  94. [DllImport ("__Internal")]
  95. private static extern int _FirebaseGetAuthExpiration (IntPtr firebase);
  96. [DllImport ("__Internal")]
  97. private static extern void _FirebaseUnAuth( IntPtr firebase);
  98. #endregion
  99. #region IFirebase implementation
  100. public IFirebase Child (string name)
  101. {
  102. return CreateNewFirebaseiOSImpl (_FirebaseChild (GetiOSObject(), name));
  103. }
  104. public IFirebase Parent
  105. {
  106. get {
  107. return CreateNewFirebaseiOSImpl (_FirebaseParent (GetiOSObject ()));
  108. }
  109. }
  110. public IFirebase Root
  111. {
  112. get {
  113. return CreateNewFirebaseiOSImpl (_FirebaseRoot (GetiOSObject ()));
  114. }
  115. }
  116. public string Key
  117. {
  118. get {
  119. return _FirebaseGetKey (GetiOSObject ());
  120. }
  121. }
  122. public IFirebase Push ()
  123. {
  124. return CreateNewFirebaseiOSImpl( _FirebasePush(GetiOSObject ()));
  125. }
  126. public void SetValue (string value)
  127. {
  128. _FirebaseSetString (GetiOSObject (), value);
  129. }
  130. public void Remove ()
  131. {
  132. SetJsonValue ("{}");
  133. }
  134. public void SetJsonValue (string jsonString)
  135. {
  136. _FirebaseSetJson (GetiOSObject (), jsonString);
  137. }
  138. public void SetValue (IDictionary<string, object> value) {
  139. string jsonString = MiniJSON.Json.Serialize (value);
  140. _FirebaseSetJson (GetiOSObject (), jsonString);
  141. }
  142. public void SetValue (float value)
  143. {
  144. _FirebaseSetFloat (GetiOSObject (), value);
  145. }
  146. public void SetPriority (string priority)
  147. {
  148. _FirebaseSetPriority (GetiOSObject (), priority);
  149. }
  150. public void AuthWithCustomToken (string token, Action<AuthData> onSuccess, Action<FirebaseError> onError)
  151. {
  152. lock (auth_sync) {
  153. int newnumber = activeCallEntry != null ? (activeCallEntry.Instance + 1) % int.MaxValue - 1 : 0;
  154. activeCallEntry = new AuthCallInstanceEntry() { Instance = newnumber, OnSuccess = onSuccess, OnError = onError};
  155. _FirebaseAuthWithCustomToken(GetiOSObject(), token, authSuccessHandler, authFailureHandler, activeCallEntry.Instance);
  156. }
  157. }
  158. public void AuthAnonymously (Action<AuthData> onSuccess, Action<FirebaseError> onError)
  159. {
  160. lock (auth_sync) {
  161. int newnumber = activeCallEntry != null ? (activeCallEntry.Instance + 1) % int.MaxValue - 1 : 0;
  162. activeCallEntry = new AuthCallInstanceEntry() { Instance = newnumber, OnSuccess = onSuccess, OnError = onError};
  163. _FirebaseAuthAnonymously(GetiOSObject(), authSuccessHandler, authFailureHandler, activeCallEntry.Instance);
  164. }
  165. }
  166. public void AuthWithPassword (string email, string password, Action<AuthData> onSuccess, Action<FirebaseError> onError)
  167. {
  168. lock (auth_sync) {
  169. int newnumber = activeCallEntry != null ? (activeCallEntry.Instance + 1) % int.MaxValue - 1 : 0;
  170. activeCallEntry = new AuthCallInstanceEntry() { Instance = newnumber, OnSuccess = onSuccess, OnError = onError};
  171. _FirebaseAuthWithPassword(GetiOSObject(), email, password, authSuccessHandler, authFailureHandler, activeCallEntry.Instance);
  172. }
  173. }
  174. public void AuthWithOAuthToken (string provider, string token, Action<AuthData> onSuccess, Action<FirebaseError> onError)
  175. {
  176. lock (auth_sync) {
  177. int newnumber = activeCallEntry != null ? (activeCallEntry.Instance + 1) % int.MaxValue - 1 : 0;
  178. activeCallEntry = new AuthCallInstanceEntry() { Instance = newnumber, OnSuccess = onSuccess, OnError = onError};
  179. _FirebaseAuthWithOAuthToken(GetiOSObject(), provider, token, authSuccessHandler, authFailureHandler, activeCallEntry.Instance);
  180. }
  181. }
  182. public void UnAuth ()
  183. {
  184. _FirebaseUnAuth (GetiOSObject ());
  185. }
  186. public AuthData Auth {
  187. get {
  188. return new AuthData(_FirebaseGetAuthToken(GetiOSObject()), _FirebaseGetAuthUid(GetiOSObject()),
  189. _FirebaseGetAuthExpiration(GetiOSObject()));
  190. }
  191. }
  192. #endregion
  193. [MonoPInvokeCallbackAttribute(typeof(OnAuthSuccessHandler))]
  194. static void onAuthSuccess(int reference, String token, String uid, int expiration) {
  195. Action<AuthData> callback = null;
  196. lock (auth_sync) {
  197. if (activeCallEntry != null && activeCallEntry.Instance == reference) {
  198. callback = activeCallEntry.OnSuccess;
  199. activeCallEntry = null;
  200. }
  201. }
  202. if (callback != null) {
  203. callback(new AuthData(token, uid, expiration));
  204. }
  205. }
  206. [MonoPInvokeCallbackAttribute(typeof(OnAuthCancelHandler))]
  207. static void onAuthCancel(int reference, int code, String message, String details) {
  208. Action<FirebaseError> callback = null;
  209. lock (auth_sync) {
  210. if (activeCallEntry != null && activeCallEntry.Instance == reference) {
  211. callback = activeCallEntry.OnError;
  212. activeCallEntry = null;
  213. }
  214. }
  215. if (callback != null) {
  216. callback(new FirebaseError(code, message, details));
  217. }
  218. }
  219. public class Factory : IFirebaseFactory
  220. {
  221. #region IFirebaseFactory implementation
  222. public IFirebase TryCreate (string path)
  223. {
  224. if (Application.platform == RuntimePlatform.IPhonePlayer) {
  225. return CreateNewFirebaseiOSImpl (CreateNativeFirebase(path));
  226. }
  227. return null;
  228. }
  229. #endregion
  230. }
  231. class AuthCallInstanceEntry {
  232. public int Instance { get; set; }
  233. public Action<AuthData> OnSuccess { get; set; }
  234. public Action<FirebaseError> OnError { get; set;}
  235. }
  236. }
  237. #endif