FIRStorageComponent.m 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // Copyright 2018 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 "FirebaseStorage/Sources/FIRStorageComponent.h"
  15. #import "FirebaseStorage/Sources/Public/FirebaseStorage/FIRStorage.h"
  16. #import "FirebaseAppCheck/Sources/Interop/FIRAppCheckInterop.h"
  17. #import "FirebaseCore/Sources/Private/FirebaseCoreInternal.h"
  18. #import "Interop/Auth/Public/FIRAuthInterop.h"
  19. NS_ASSUME_NONNULL_BEGIN
  20. /** A NSMutableDictionary of FirebaseApp name and bucket names to FIRStorage instance. */
  21. typedef NSMutableDictionary<NSString *, FIRStorage *> FIRStorageDictionary;
  22. @interface FIRStorage ()
  23. // Surface the internal initializer to create instances of FIRStorage.
  24. - (instancetype)initWithApp:(FIRApp *)app
  25. bucket:(NSString *)bucket
  26. auth:(nullable id<FIRAuthInterop>)auth
  27. appCheck:(nullable id<FIRAppCheckInterop>)appCheck;
  28. @end
  29. @interface FIRStorageComponent () <FIRLibrary>
  30. @property(nonatomic) FIRStorageDictionary *instances;
  31. /// Internal initializer.
  32. - (instancetype)initWithApp:(FIRApp *)app;
  33. @end
  34. @implementation FIRStorageComponent
  35. #pragma mark - Initialization
  36. - (instancetype)initWithApp:(FIRApp *)app {
  37. self = [super init];
  38. if (self) {
  39. _app = app;
  40. _instances = [NSMutableDictionary dictionary];
  41. }
  42. return self;
  43. }
  44. #pragma mark - Lifecycle
  45. + (void)load {
  46. [FIRApp registerInternalLibrary:(Class<FIRLibrary>)self withName:@"fire-str"];
  47. }
  48. #pragma mark - FIRComponentRegistrant
  49. + (nonnull NSArray<FIRComponent *> *)componentsToRegister {
  50. FIRDependency *authDep = [FIRDependency dependencyWithProtocol:@protocol(FIRAuthInterop)
  51. isRequired:NO];
  52. FIRComponentCreationBlock creationBlock =
  53. ^id _Nullable(FIRComponentContainer *container, BOOL *isCacheable) {
  54. *isCacheable = YES;
  55. return [[FIRStorageComponent alloc] initWithApp:container.app];
  56. };
  57. FIRComponent *storageProvider =
  58. [FIRComponent componentWithProtocol:@protocol(FIRStorageMultiBucketProvider)
  59. instantiationTiming:FIRInstantiationTimingLazy
  60. dependencies:@[ authDep ]
  61. creationBlock:creationBlock];
  62. return @[ storageProvider ];
  63. }
  64. #pragma mark - FIRStorageInstanceProvider Conformance
  65. - (FIRStorage *)storageForBucket:(NSString *)bucket {
  66. FIRStorageDictionary *instances = [self instances];
  67. @synchronized(instances) {
  68. FIRStorage *instance = instances[bucket];
  69. if (!instance) {
  70. // Create an instance of FIRStorage and return it.
  71. id<FIRAuthInterop> auth = FIR_COMPONENT(FIRAuthInterop, self.app.container);
  72. id<FIRAppCheckInterop> appCheck = FIR_COMPONENT(FIRAppCheckInterop, self.app.container);
  73. instance = [[FIRStorage alloc] initWithApp:self.app
  74. bucket:bucket
  75. auth:auth
  76. appCheck:appCheck];
  77. instances[bucket] = instance;
  78. }
  79. return instance;
  80. }
  81. }
  82. @end
  83. NS_ASSUME_NONNULL_END