No Description

FIRAppAssociationRegistration.m 1.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. // Copyright 2017 Google
  2. //
  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. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #import "Private/FIRAppAssociationRegistration.h"
  15. #import <objc/runtime.h>
  16. @implementation FIRAppAssociationRegistration
  17. + (nullable id)registeredObjectWithHost:(id)host
  18. key:(NSString *)key
  19. creationBlock:(id _Nullable (^)(void))creationBlock {
  20. @synchronized(self) {
  21. SEL dictKey = @selector(registeredObjectWithHost:key:creationBlock:);
  22. NSMutableDictionary<NSString *, id> *objectsByKey = objc_getAssociatedObject(host, dictKey);
  23. if (!objectsByKey) {
  24. objectsByKey = [[NSMutableDictionary alloc] init];
  25. objc_setAssociatedObject(host, dictKey, objectsByKey, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  26. }
  27. id obj = objectsByKey[key];
  28. NSValue *creationBlockBeingCalled = [NSValue valueWithPointer:dictKey];
  29. if (obj) {
  30. if ([creationBlockBeingCalled isEqual:obj]) {
  31. [NSException raise:@"Reentering registeredObjectWithHost:key:creationBlock: not allowed"
  32. format:@"host: %@ key: %@", host, key];
  33. }
  34. return obj;
  35. }
  36. objectsByKey[key] = creationBlockBeingCalled;
  37. obj = creationBlock();
  38. objectsByKey[key] = obj;
  39. return obj;
  40. }
  41. }
  42. @end