FIRDatabaseConfig.m 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * Copyright 2017 Google
  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. #import <FirebaseCore/FIRApp.h>
  17. #import "FIRDatabaseConfig.h"
  18. #import "FIRDatabaseConfig_Private.h"
  19. #import "FIRNoopAuthTokenProvider.h"
  20. #import "FAuthTokenProvider.h"
  21. @interface FIRDatabaseConfig (Private)
  22. @property (nonatomic, strong, readwrite) NSString *sessionIdentifier;
  23. @end
  24. @implementation FIRDatabaseConfig
  25. - (id)init {
  26. [NSException raise:NSInvalidArgumentException format:@"Can't create config objects!"];
  27. return nil;
  28. }
  29. - (id)initWithSessionIdentifier:(NSString *)identifier authTokenProvider:(id<FAuthTokenProvider>)authTokenProvider {
  30. self = [super init];
  31. if (self != nil) {
  32. self->_sessionIdentifier = identifier;
  33. self->_callbackQueue = dispatch_get_main_queue();
  34. self->_persistenceCacheSizeBytes = 10*1024*1024; // Default cache size is 10MB
  35. self->_authTokenProvider = authTokenProvider;
  36. }
  37. return self;
  38. }
  39. - (void)assertUnfrozen {
  40. if (self.isFrozen) {
  41. [NSException raise:NSGenericException format:@"Can't modify config objects after they are in use for FIRDatabaseReferences."];
  42. }
  43. }
  44. - (void)setAuthTokenProvider:(id<FAuthTokenProvider>)authTokenProvider {
  45. [self assertUnfrozen];
  46. self->_authTokenProvider = authTokenProvider;
  47. }
  48. - (void)setPersistenceEnabled:(BOOL)persistenceEnabled {
  49. [self assertUnfrozen];
  50. self->_persistenceEnabled = persistenceEnabled;
  51. }
  52. - (void)setPersistenceCacheSizeBytes:(NSUInteger)persistenceCacheSizeBytes {
  53. [self assertUnfrozen];
  54. // Can't be less than 1MB
  55. if (persistenceCacheSizeBytes < 1024*1024) {
  56. [NSException raise:NSInvalidArgumentException format:@"The minimum cache size must be at least 1MB"];
  57. }
  58. if (persistenceCacheSizeBytes > 100*1024*1024) {
  59. [NSException raise:NSInvalidArgumentException format:@"Firebase Database currently doesn't support a cache size larger than 100MB"];
  60. }
  61. self->_persistenceCacheSizeBytes = persistenceCacheSizeBytes;
  62. }
  63. - (void)setCallbackQueue:(dispatch_queue_t)callbackQueue {
  64. [self assertUnfrozen];
  65. self->_callbackQueue = callbackQueue;
  66. }
  67. - (void)freeze {
  68. self->_isFrozen = YES;
  69. }
  70. // TODO: Only used for tests. Migrate to FIRDatabase and remove.
  71. + (FIRDatabaseConfig *)defaultConfig {
  72. static dispatch_once_t onceToken;
  73. static FIRDatabaseConfig *defaultConfig;
  74. dispatch_once(&onceToken, ^{
  75. defaultConfig = [FIRDatabaseConfig configForName:@"default"];
  76. });
  77. return defaultConfig;
  78. }
  79. // TODO: This is only used for tests. We should fix them to go through FIRDatabase and remove
  80. // this method and the sessionsConfigs dictionary (FIRDatabase automatically creates one config per app).
  81. + (FIRDatabaseConfig *)configForName:(NSString *)name {
  82. NSRegularExpression *expression = [NSRegularExpression regularExpressionWithPattern:@"^[a-zA-Z0-9-_]+$" options:0 error:nil];
  83. if ([expression numberOfMatchesInString:name options:0 range:NSMakeRange(0, name.length)] == 0) {
  84. [NSException raise:NSInvalidArgumentException format:@"Name can only contain [a-zA-Z0-9-_]"];
  85. }
  86. static dispatch_once_t onceToken;
  87. static NSMutableDictionary *sessionConfigs;
  88. dispatch_once(&onceToken, ^{
  89. sessionConfigs = [NSMutableDictionary dictionary];
  90. });
  91. @synchronized(sessionConfigs) {
  92. if (!sessionConfigs[name]) {
  93. id<FAuthTokenProvider> authTokenProvider = [FAuthTokenProvider authTokenProviderForApp:[FIRApp defaultApp]];
  94. sessionConfigs[name] = [[FIRDatabaseConfig alloc] initWithSessionIdentifier:name
  95. authTokenProvider:authTokenProvider];
  96. }
  97. return sessionConfigs[name];
  98. }
  99. }
  100. @end