FIRFirestoreSettings.mm 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. #import "FIRFirestoreSettings.h"
  17. #include "Firestore/core/src/api/settings.h"
  18. #include "Firestore/core/src/util/exception.h"
  19. #include "Firestore/core/src/util/string_apple.h"
  20. #include "absl/base/attributes.h"
  21. #include "absl/memory/memory.h"
  22. NS_ASSUME_NONNULL_BEGIN
  23. namespace api = firebase::firestore::api;
  24. using api::Settings;
  25. using firebase::firestore::util::MakeString;
  26. using firebase::firestore::util::ThrowInvalidArgument;
  27. // Public constant
  28. ABSL_CONST_INIT extern "C" const int64_t kFIRFirestoreCacheSizeUnlimited =
  29. Settings::CacheSizeUnlimited;
  30. @implementation FIRFirestoreSettings
  31. - (instancetype)init {
  32. if (self = [super init]) {
  33. _host = [NSString stringWithUTF8String:Settings::DefaultHost];
  34. _sslEnabled = Settings::DefaultSslEnabled;
  35. _dispatchQueue = dispatch_get_main_queue();
  36. _persistenceEnabled = Settings::DefaultPersistenceEnabled;
  37. _cacheSizeBytes = Settings::DefaultCacheSizeBytes;
  38. }
  39. return self;
  40. }
  41. - (BOOL)isEqual:(id)other {
  42. if (self == other) {
  43. return YES;
  44. } else if (![other isKindOfClass:[FIRFirestoreSettings class]]) {
  45. return NO;
  46. }
  47. FIRFirestoreSettings *otherSettings = (FIRFirestoreSettings *)other;
  48. return [self.host isEqual:otherSettings.host] &&
  49. self.isSSLEnabled == otherSettings.isSSLEnabled &&
  50. self.dispatchQueue == otherSettings.dispatchQueue &&
  51. self.isPersistenceEnabled == otherSettings.isPersistenceEnabled &&
  52. self.cacheSizeBytes == otherSettings.cacheSizeBytes;
  53. }
  54. - (NSUInteger)hash {
  55. NSUInteger result = [self.host hash];
  56. result = 31 * result + (self.isSSLEnabled ? 1231 : 1237);
  57. // Ignore the dispatchQueue to avoid having to deal with sizeof(dispatch_queue_t).
  58. result = 31 * result + (self.isPersistenceEnabled ? 1231 : 1237);
  59. result = 31 * result + (NSUInteger)self.cacheSizeBytes;
  60. return result;
  61. }
  62. - (id)copyWithZone:(__unused NSZone *_Nullable)zone {
  63. FIRFirestoreSettings *copy = [[FIRFirestoreSettings alloc] init];
  64. copy.host = _host;
  65. copy.sslEnabled = _sslEnabled;
  66. copy.dispatchQueue = _dispatchQueue;
  67. copy.persistenceEnabled = _persistenceEnabled;
  68. copy.cacheSizeBytes = _cacheSizeBytes;
  69. return copy;
  70. }
  71. - (void)setHost:(NSString *)host {
  72. if (!host) {
  73. ThrowInvalidArgument("Host setting may not be nil. You should generally just use the default "
  74. "value (which is %s)",
  75. Settings::DefaultHost);
  76. }
  77. _host = [host mutableCopy];
  78. }
  79. - (void)setDispatchQueue:(dispatch_queue_t)dispatchQueue {
  80. if (!dispatchQueue) {
  81. ThrowInvalidArgument(
  82. "Dispatch queue setting may not be nil. Create a new dispatch queue with "
  83. "dispatch_queue_create(\"com.example.MyQueue\", NULL) or just use the default (which is "
  84. "the main queue, returned from dispatch_get_main_queue())");
  85. }
  86. _dispatchQueue = dispatchQueue;
  87. }
  88. - (void)setCacheSizeBytes:(int64_t)cacheSizeBytes {
  89. if (cacheSizeBytes != kFIRFirestoreCacheSizeUnlimited &&
  90. cacheSizeBytes < Settings::MinimumCacheSizeBytes) {
  91. ThrowInvalidArgument("Cache size must be set to at least %s bytes",
  92. Settings::MinimumCacheSizeBytes);
  93. }
  94. _cacheSizeBytes = cacheSizeBytes;
  95. }
  96. - (BOOL)isUsingDefaultHost {
  97. NSString *defaultHost = [NSString stringWithUTF8String:Settings::DefaultHost];
  98. return [self.host isEqualToString:defaultHost];
  99. }
  100. - (Settings)internalSettings {
  101. Settings settings;
  102. settings.set_host(MakeString(_host));
  103. settings.set_ssl_enabled(_sslEnabled);
  104. settings.set_persistence_enabled(_persistenceEnabled);
  105. settings.set_cache_size_bytes(_cacheSizeBytes);
  106. return settings;
  107. }
  108. @end
  109. NS_ASSUME_NONNULL_END