RLMSyncManager.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. ////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright 2016 Realm Inc.
  4. //
  5. // Licensed under the Apache License, Version 2.0 (the "License");
  6. // you may not use this file except in compliance with the License.
  7. // You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. //
  17. ////////////////////////////////////////////////////////////////////////////
  18. #import <Foundation/Foundation.h>
  19. #import "RLMSyncUtil.h"
  20. @class RLMSyncSession, RLMSyncTimeoutOptions;
  21. NS_ASSUME_NONNULL_BEGIN
  22. /// An enum representing different levels of sync-related logging that can be configured.
  23. typedef RLM_CLOSED_ENUM(NSUInteger, RLMSyncLogLevel) {
  24. /// Nothing will ever be logged.
  25. RLMSyncLogLevelOff,
  26. /// Only fatal errors will be logged.
  27. RLMSyncLogLevelFatal,
  28. /// Only errors will be logged.
  29. RLMSyncLogLevelError,
  30. /// Warnings and errors will be logged.
  31. RLMSyncLogLevelWarn,
  32. /// Information about sync events will be logged. Fewer events will be logged in order to avoid overhead.
  33. RLMSyncLogLevelInfo,
  34. /// Information about sync events will be logged. More events will be logged than with `RLMSyncLogLevelInfo`.
  35. RLMSyncLogLevelDetail,
  36. /// Log information that can aid in debugging.
  37. ///
  38. /// - warning: Will incur a measurable performance impact.
  39. RLMSyncLogLevelDebug,
  40. /// Log information that can aid in debugging. More events will be logged than with `RLMSyncLogLevelDebug`.
  41. ///
  42. /// - warning: Will incur a measurable performance impact.
  43. RLMSyncLogLevelTrace,
  44. /// Log information that can aid in debugging. More events will be logged than with `RLMSyncLogLevelTrace`.
  45. ///
  46. /// - warning: Will incur a measurable performance impact.
  47. RLMSyncLogLevelAll
  48. };
  49. /// A log callback function which can be set on RLMSyncManager.
  50. ///
  51. /// The log function may be called from multiple threads simultaneously, and is
  52. /// responsible for performing its own synchronization if any is required.
  53. typedef void (*RLMSyncLogFunction)(RLMSyncLogLevel level, NSString *message);
  54. /// A block type representing a block which can be used to report a sync-related error to the application. If the error
  55. /// pertains to a specific session, that session will also be passed into the block.
  56. typedef void(^RLMSyncErrorReportingBlock)(NSError *, RLMSyncSession * _Nullable);
  57. /**
  58. A singleton manager which serves as a central point for sync-related configuration.
  59. */
  60. @interface RLMSyncManager : NSObject
  61. /**
  62. A block which can optionally be set to report sync-related errors to your application.
  63. Any error reported through this block will be of the `RLMSyncError` type, and marked
  64. with the `RLMSyncErrorDomain` domain.
  65. Errors reported through this mechanism are fatal, with several exceptions. Please consult
  66. `RLMSyncError` for information about the types of errors that can be reported through
  67. the block, and for for suggestions on handling recoverable error codes.
  68. @see `RLMSyncError`
  69. */
  70. @property (nullable, nonatomic, copy) RLMSyncErrorReportingBlock errorHandler;
  71. /**
  72. A reverse-DNS string uniquely identifying this application. In most cases this
  73. is automatically set by the SDK, and does not have to be explicitly configured.
  74. */
  75. @property (nonatomic, copy) NSString *appID;
  76. /**
  77. A string identifying this application which is included in the User-Agent
  78. header of sync connections. By default, this will be the application's bundle
  79. identifier.
  80. This property must be set prior to opening a synchronized Realm for the first
  81. time. Any modifications made after opening a Realm will be ignored.
  82. */
  83. @property (nonatomic, copy) NSString *userAgent;
  84. /**
  85. The logging threshold which newly opened synced Realms will use. Defaults to
  86. `RLMSyncLogLevelInfo`.
  87. By default logging strings are output to Apple System Logger. Set `logger` to
  88. perform custom logging logic instead.
  89. @warning This property must be set before any synced Realms are opened. Setting it after
  90. opening any synced Realm will do nothing.
  91. */
  92. @property (nonatomic) RLMSyncLogLevel logLevel;
  93. /**
  94. The function which will be invoked whenever the sync client has a log message.
  95. If nil, log strings are output to Apple System Logger instead.
  96. @warning This property must be set before any synced Realms are opened. Setting
  97. it after opening any synced Realm will do nothing.
  98. */
  99. @property (nonatomic, nullable) RLMSyncLogFunction logger;
  100. /**
  101. The name of the HTTP header to send authorization data in when making requests to a Realm Object Server which has
  102. been configured to expect a custom authorization header.
  103. */
  104. @property (nullable, nonatomic, copy) NSString *authorizationHeaderName;
  105. /**
  106. Extra HTTP headers to append to every request to a Realm Object Server.
  107. Modifying this property while sync sessions are active will result in all
  108. sessions disconnecting and reconnecting using the new headers.
  109. */
  110. @property (nullable, nonatomic, copy) NSDictionary<NSString *, NSString *> *customRequestHeaders;
  111. /**
  112. A map of hostname to file URL for pinned certificates to use for HTTPS requests.
  113. When initiating a HTTPS connection to a server, if this dictionary contains an
  114. entry for the server's hostname, only the certificates stored in the file (or
  115. any certificates signed by it, if the file contains a CA cert) will be accepted
  116. when initiating a connection to a server. This prevents certain certain kinds
  117. of man-in-the-middle (MITM) attacks, and can also be used to trust a self-signed
  118. certificate which would otherwise be untrusted.
  119. On macOS, the certificate files may be in any of the formats supported by
  120. SecItemImport(), including PEM and .cer (see SecExternalFormat for a complete
  121. list of possible formats). On iOS and other platforms, only DER .cer files are
  122. supported.
  123. For example, to pin example.com to a .cer file included in your bundle:
  124. <pre>
  125. RLMSyncManager.sharedManager.pinnedCertificatePaths = @{
  126. @"example.com": [NSBundle.mainBundle pathForResource:@"example.com" ofType:@"cer"]
  127. };
  128. </pre>
  129. */
  130. @property (nullable, nonatomic, copy) NSDictionary<NSString *, NSURL *> *pinnedCertificatePaths;
  131. /**
  132. Options for the assorted types of connection timeouts for sync connections.
  133. If nil default values for all timeouts are used instead.
  134. @warning This property must be set before any synced Realms are opened. Setting
  135. it after opening any synced Realm will do nothing.
  136. */
  137. @property (nullable, nonatomic, copy) RLMSyncTimeoutOptions *timeoutOptions;
  138. /// The sole instance of the singleton.
  139. + (instancetype)sharedManager NS_REFINED_FOR_SWIFT;
  140. /// :nodoc:
  141. - (instancetype)init __attribute__((unavailable("RLMSyncManager cannot be created directly")));
  142. /// :nodoc:
  143. + (instancetype)new __attribute__((unavailable("RLMSyncManager cannot be created directly")));
  144. @end
  145. /**
  146. Options for configuring timeouts and intervals in the sync client.
  147. */
  148. @interface RLMSyncTimeoutOptions : NSObject
  149. /// The maximum number of milliseconds to allow for a connection to
  150. /// become fully established. This includes the time to resolve the
  151. /// network address, the TCP connect operation, the SSL handshake, and
  152. /// the WebSocket handshake.
  153. ///
  154. /// Defaults to 2 minutes.
  155. @property (nonatomic) NSUInteger connectTimeout;
  156. /// The number of milliseconds to keep a connection open after all
  157. /// sessions have been abandoned.
  158. ///
  159. /// After all synchronized Realms have been closed for a given server, the
  160. /// connection is kept open until the linger time has expire to avoid the
  161. /// overhead of reestablishing the connection when Realms are being closed and
  162. /// reopened.
  163. ///
  164. /// Defaults to 30 seconds.
  165. @property (nonatomic) NSUInteger connectionLingerTime;
  166. /// The number of milliseconds between each heartbeat ping message.
  167. ///
  168. /// The client periodically sends ping messages to the server to check if the
  169. /// connection is still alive. Shorter periods make connection state change
  170. /// notifications more responsive at the cost of battery life (as the antenna
  171. /// will have to wake up more often).
  172. ///
  173. /// Defaults to 1 minute.
  174. @property (nonatomic) NSUInteger pingKeepalivePeriod;
  175. /// How long in milliseconds to wait for a reponse to a heartbeat ping before
  176. /// concluding that the connection has dropped.
  177. ///
  178. /// Shorter values will make connection state change notifications more
  179. /// responsive as it will only change to `disconnected` after this much time has
  180. /// elapsed, but overly short values may result in spurious disconnection
  181. /// notifications when the server is simply taking a long time to respond.
  182. ///
  183. /// Defaults to 2 minutes.
  184. @property (nonatomic) NSUInteger pongKeepaliveTimeout;
  185. /// The maximum amount of time, in milliseconds, since the loss of a
  186. /// prior connection, for a new connection to be considered a "fast
  187. /// reconnect".
  188. ///
  189. /// When a client first connects to the server, it defers uploading any local
  190. /// changes until it has downloaded all changesets from the server. This
  191. /// typically reduces the total amount of merging that has to be done, and is
  192. /// particularly beneficial the first time that a specific client ever connects
  193. /// to the server.
  194. ///
  195. /// When an existing client disconnects and then reconnects within the "fact
  196. /// reconnect" time this is skipped and any local changes are uploaded
  197. /// immediately without waiting for downloads, just as if the client was online
  198. /// the whole time.
  199. ///
  200. /// Defaults to 1 minute.
  201. @property (nonatomic) NSUInteger fastReconnectLimit;
  202. @end
  203. NS_ASSUME_NONNULL_END