|
| 1 | +/* |
| 2 | + * Copyright 2023 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +using System; |
| 18 | +using System.Collections.Generic; |
| 19 | +using System.Threading.Tasks; |
| 20 | + |
| 21 | +namespace Firebase.AppCheck { |
| 22 | + |
| 23 | +internal class BuiltInProviderWrapper : IAppCheckProvider { |
| 24 | + |
| 25 | + private static int s_pendingGetTokenKey = 0; |
| 26 | + private static Dictionary<int, TaskCompletionSource<AppCheckToken>> s_pendingGetTokens = |
| 27 | + new Dictionary<int, TaskCompletionSource<AppCheckToken>>(); |
| 28 | + |
| 29 | + // Function for C++ to call when it needs to finish a GetToken call. |
| 30 | + private static AppCheckUtil.CompleteBuiltInGetTokenDelegate completeGetTokenDelegate = |
| 31 | + new AppCheckUtil.CompleteBuiltInGetTokenDelegate(CompleteBuiltInGetTokenMethod); |
| 32 | + |
| 33 | + static BuiltInProviderWrapper() { |
| 34 | + // Register the callback to complete GetTokens |
| 35 | + AppCheckUtil.SetCompleteBuiltInGetTokenCallback(completeGetTokenDelegate); |
| 36 | + } |
| 37 | + |
| 38 | + AppCheckProviderInternal providerInternal; |
| 39 | + |
| 40 | + public BuiltInProviderWrapper(AppCheckProviderInternal providerInternal) { |
| 41 | + this.providerInternal = providerInternal; |
| 42 | + } |
| 43 | + |
| 44 | + private void ThrowIfNull() { |
| 45 | + if (providerInternal == null || |
| 46 | + AppCheckProviderInternal.getCPtr(providerInternal).Handle == System.IntPtr.Zero) { |
| 47 | + throw new System.NullReferenceException(); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + public System.Threading.Tasks.Task<AppCheckToken> GetTokenAsync() { |
| 52 | + ThrowIfNull(); |
| 53 | + |
| 54 | + int key; |
| 55 | + TaskCompletionSource<AppCheckToken> tcs; |
| 56 | + lock (s_pendingGetTokens) { |
| 57 | + key = s_pendingGetTokenKey++; |
| 58 | + tcs = new TaskCompletionSource<AppCheckToken>(); |
| 59 | + s_pendingGetTokens[key] = tcs; |
| 60 | + } |
| 61 | + AppCheckUtil.GetTokenFromBuiltInProvider(providerInternal, key); |
| 62 | + return tcs.Task; |
| 63 | + } |
| 64 | + |
| 65 | + // This is called from the C++ implementation, on the Unity main thread. |
| 66 | + private static void CompleteBuiltInGetTokenMethod(int key, System.IntPtr tokenCPtr, |
| 67 | + int error, string errorMessage) { |
| 68 | + TaskCompletionSource<AppCheckToken> tcs; |
| 69 | + lock (s_pendingGetTokens) { |
| 70 | + if (!s_pendingGetTokens.TryGetValue(key, out tcs)) { |
| 71 | + return; |
| 72 | + } |
| 73 | + s_pendingGetTokens.Remove(key); |
| 74 | + } |
| 75 | + |
| 76 | + if (error == 0) { |
| 77 | + // Create the C# object that wraps the Token's C++ pointer, passing false for ownership |
| 78 | + // to prevent the cleanup of the C# object from deleting the C++ object when done. |
| 79 | + AppCheckTokenInternal tokenInternal = new AppCheckTokenInternal(tokenCPtr, false); |
| 80 | + AppCheckToken token = AppCheckToken.FromAppCheckTokenInternal(tokenInternal); |
| 81 | + tcs.TrySetResult(token); |
| 82 | + } else { |
| 83 | + tcs.TrySetException(new FirebaseException(error, errorMessage)); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | +} |
| 88 | + |
| 89 | +} |
0 commit comments