FIRStorageComponent.m 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 "FIRStorageComponent.h"
  15. #import "Public/FIRStorage.h"
  16. #import <FirebaseAuthInterop/FIRAuthInterop.h>
  17. #import <FirebaseCore/FIRAppInternal.h>
  18. #import <FirebaseCore/FIRComponent.h>
  19. #import <FirebaseCore/FIRComponentContainer.h>
  20. #import <FirebaseCore/FIRComponentRegistrant.h>
  21. #import <FirebaseCore/FIRDependency.h>
  22. NS_ASSUME_NONNULL_BEGIN
  23. @interface FIRStorage ()
  24. // Surface the internal initializer to create instances of FIRStorage.
  25. - (instancetype)initWithApp:(FIRApp *)app
  26. bucket:(NSString *)bucket
  27. auth:(nullable id<FIRAuthInterop>)auth;
  28. @end
  29. @interface FIRStorageComponent () <FIRComponentRegistrant>
  30. /// Internal intializer.
  31. - (instancetype)initWithApp:(FIRApp *)app;
  32. @end
  33. @implementation FIRStorageComponent
  34. #pragma mark - Initialization
  35. - (instancetype)initWithApp:(FIRApp *)app {
  36. self = [super init];
  37. if (self) {
  38. _app = app;
  39. }
  40. return self;
  41. }
  42. #pragma mark - Lifecycle
  43. + (void)load {
  44. [FIRComponentContainer registerAsComponentRegistrant:self];
  45. }
  46. #pragma mark - FIRComponentRegistrant
  47. + (nonnull NSArray<FIRComponent *> *)componentsToRegister {
  48. FIRDependency *authDep =
  49. [FIRDependency dependencyWithProtocol:@protocol(FIRAuthInterop) isRequired:NO];
  50. FIRComponentCreationBlock creationBlock =
  51. ^id _Nullable(FIRComponentContainer *container, BOOL *isCacheable) {
  52. return [[FIRStorageComponent alloc] initWithApp:container.app];
  53. };
  54. FIRComponent *storageProvider =
  55. [FIRComponent componentWithProtocol:@protocol(FIRStorageMultiBucketProvider)
  56. instantiationTiming:FIRInstantiationTimingLazy
  57. dependencies:@[ authDep ]
  58. creationBlock:creationBlock];
  59. return @[ storageProvider ];
  60. }
  61. #pragma mark - FIRStorageInstanceProvider Conformance
  62. - (FIRStorage *)storageForBucket:(NSString *)bucket {
  63. // Create an instance of FIRStorage and return it.
  64. id<FIRAuthInterop> auth = FIR_COMPONENT(FIRAuthInterop, self.app.container);
  65. return [[FIRStorage alloc] initWithApp:self.app bucket:bucket auth:auth];
  66. }
  67. @end
  68. NS_ASSUME_NONNULL_END