FIRFirestoreSettings.mm 4.6 KB

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