FIRFirestoreSettings.mm 4.1 KB

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