FIRFirestoreSettings.mm 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * Copyright 2017 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. // TODO(wuandy): Delete this once isPersistenceEnabled and cacheSizeBytes are removed.
  17. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  18. #import "FIRFirestoreSettings.h"
  19. #import <Foundation/NSObject.h>
  20. #import "FIRLocalCacheSettings+Internal.h"
  21. #include "Firestore/Source/Public/FirebaseFirestore/FIRLocalCacheSettings.h"
  22. #include "Firestore/core/src/api/settings.h"
  23. #include "Firestore/core/src/util/exception.h"
  24. #include "Firestore/core/src/util/string_apple.h"
  25. NS_ASSUME_NONNULL_BEGIN
  26. namespace api = firebase::firestore::api;
  27. using api::Settings;
  28. using firebase::firestore::util::MakeString;
  29. using firebase::firestore::util::ThrowInvalidArgument;
  30. // Public constant
  31. extern "C" const int64_t kFIRFirestoreCacheSizeUnlimited = Settings::CacheSizeUnlimited;
  32. @implementation FIRFirestoreSettings
  33. - (instancetype)init {
  34. if (self = [super init]) {
  35. _host = [NSString stringWithUTF8String:Settings::DefaultHost];
  36. _sslEnabled = Settings::DefaultSslEnabled;
  37. _dispatchQueue = dispatch_get_main_queue();
  38. _persistenceEnabled = Settings::DefaultPersistenceEnabled;
  39. _cacheSizeBytes = Settings::DefaultCacheSizeBytes;
  40. }
  41. return self;
  42. }
  43. - (BOOL)isEqual:(id)other {
  44. if (self == other) {
  45. return YES;
  46. } else if (![other isKindOfClass:[FIRFirestoreSettings class]]) {
  47. return NO;
  48. }
  49. FIRFirestoreSettings *otherSettings = (FIRFirestoreSettings *)other;
  50. BOOL equal = [self.host isEqual:otherSettings.host] &&
  51. self.isSSLEnabled == otherSettings.isSSLEnabled &&
  52. self.dispatchQueue == otherSettings.dispatchQueue &&
  53. self.isPersistenceEnabled == otherSettings.isPersistenceEnabled &&
  54. self.cacheSizeBytes == otherSettings.cacheSizeBytes;
  55. if (equal && self.cacheSettings != nil && otherSettings.cacheSettings != nil) {
  56. equal = [self.cacheSettings isEqual:otherSettings];
  57. } else if (equal) {
  58. equal = (self.cacheSettings == otherSettings.cacheSettings);
  59. }
  60. return equal;
  61. }
  62. - (NSUInteger)hash {
  63. NSUInteger result = [self.host hash];
  64. result = 31 * result + (self.isSSLEnabled ? 1231 : 1237);
  65. // Ignore the dispatchQueue to avoid having to deal with sizeof(dispatch_queue_t).
  66. result = 31 * result + (self.isPersistenceEnabled ? 1231 : 1237);
  67. result = 31 * result + (NSUInteger)self.cacheSizeBytes;
  68. if ([_cacheSettings isKindOfClass:[FIRPersistentCacheSettings class]]) {
  69. FIRPersistentCacheSettings *casted = (FIRPersistentCacheSettings *)_cacheSettings;
  70. result = 31 * result + casted.internalSettings.Hash();
  71. } else if ([_cacheSettings isKindOfClass:[FIRMemoryCacheSettings class]]) {
  72. FIRMemoryCacheSettings *casted = (FIRMemoryCacheSettings *)_cacheSettings;
  73. result = 31 * result + casted.internalSettings.Hash();
  74. }
  75. return result;
  76. }
  77. - (id)copyWithZone:(__unused NSZone *_Nullable)zone {
  78. FIRFirestoreSettings *copy = [[FIRFirestoreSettings alloc] init];
  79. copy.host = _host;
  80. copy.sslEnabled = _sslEnabled;
  81. copy.dispatchQueue = _dispatchQueue;
  82. copy.persistenceEnabled = _persistenceEnabled;
  83. copy.cacheSizeBytes = _cacheSizeBytes;
  84. copy.cacheSettings = _cacheSettings;
  85. return copy;
  86. }
  87. - (void)setHost:(NSString *)host {
  88. if (!host) {
  89. ThrowInvalidArgument("Host setting may not be nil. You should generally just use the default "
  90. "value (which is %s)",
  91. Settings::DefaultHost);
  92. }
  93. _host = [host mutableCopy];
  94. }
  95. - (void)setDispatchQueue:(dispatch_queue_t)dispatchQueue {
  96. if (!dispatchQueue) {
  97. ThrowInvalidArgument(
  98. "Dispatch queue setting may not be nil. Create a new dispatch queue with "
  99. "dispatch_queue_create(\"com.example.MyQueue\", NULL) or just use the default (which is "
  100. "the main queue, returned from dispatch_get_main_queue())");
  101. }
  102. _dispatchQueue = dispatchQueue;
  103. }
  104. - (void)setCacheSizeBytes:(int64_t)cacheSizeBytes {
  105. if (cacheSizeBytes != kFIRFirestoreCacheSizeUnlimited &&
  106. cacheSizeBytes < Settings::MinimumCacheSizeBytes) {
  107. ThrowInvalidArgument("Cache size must be set to at least %s bytes",
  108. Settings::MinimumCacheSizeBytes);
  109. }
  110. _cacheSizeBytes = cacheSizeBytes;
  111. }
  112. - (void)setCacheSettings:(id<FIRLocalCacheSettings, NSObject>)cacheSettings {
  113. _cacheSettings = cacheSettings;
  114. }
  115. - (BOOL)isUsingDefaultHost {
  116. NSString *defaultHost = [NSString stringWithUTF8String:Settings::DefaultHost];
  117. return [self.host isEqualToString:defaultHost];
  118. }
  119. - (Settings)internalSettings {
  120. Settings settings;
  121. settings.set_host(MakeString(_host));
  122. settings.set_ssl_enabled(_sslEnabled);
  123. settings.set_persistence_enabled(_persistenceEnabled);
  124. settings.set_cache_size_bytes(_cacheSizeBytes);
  125. if ([_cacheSettings isKindOfClass:[FIRPersistentCacheSettings class]]) {
  126. FIRPersistentCacheSettings *casted = (FIRPersistentCacheSettings *)_cacheSettings;
  127. settings.set_local_cache_settings(casted.internalSettings);
  128. } else if ([_cacheSettings isKindOfClass:[FIRMemoryCacheSettings class]]) {
  129. FIRMemoryCacheSettings *casted = (FIRMemoryCacheSettings *)_cacheSettings;
  130. settings.set_local_cache_settings(casted.internalSettings);
  131. }
  132. return settings;
  133. }
  134. @end
  135. NS_ASSUME_NONNULL_END