You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

173 lines
6.3 KiB

6 years ago
  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 "FIRMessagingUtilities.h"
  17. #import "Protos/GtalkCore.pbobjc.h"
  18. #import "FIRMessagingLogger.h"
  19. // Convert the macro to a string
  20. #define STR_EXPAND(x) #x
  21. #define STR(x) STR_EXPAND(x)
  22. static const uint64_t kBytesToMegabytesDivisor = 1024 * 1024LL;
  23. #pragma mark - Protocol Buffers
  24. FIRMessagingProtoTag FIRMessagingGetTagForProto(GPBMessage *proto) {
  25. if ([proto isKindOfClass:[GtalkHeartbeatPing class]]) {
  26. return kFIRMessagingProtoTagHeartbeatPing;
  27. } else if ([proto isKindOfClass:[GtalkHeartbeatAck class]]) {
  28. return kFIRMessagingProtoTagHeartbeatAck;
  29. } else if ([proto isKindOfClass:[GtalkLoginRequest class]]) {
  30. return kFIRMessagingProtoTagLoginRequest;
  31. } else if ([proto isKindOfClass:[GtalkLoginResponse class]]) {
  32. return kFIRMessagingProtoTagLoginResponse;
  33. } else if ([proto isKindOfClass:[GtalkClose class]]) {
  34. return kFIRMessagingProtoTagClose;
  35. } else if ([proto isKindOfClass:[GtalkIqStanza class]]) {
  36. return kFIRMessagingProtoTagIqStanza;
  37. } else if ([proto isKindOfClass:[GtalkDataMessageStanza class]]) {
  38. return kFIRMessagingProtoTagDataMessageStanza;
  39. }
  40. return kFIRMessagingProtoTagInvalid;
  41. }
  42. Class FIRMessagingGetClassForTag(FIRMessagingProtoTag tag) {
  43. switch (tag) {
  44. case kFIRMessagingProtoTagHeartbeatPing:
  45. return GtalkHeartbeatPing.class;
  46. case kFIRMessagingProtoTagHeartbeatAck:
  47. return GtalkHeartbeatAck.class;
  48. case kFIRMessagingProtoTagLoginRequest:
  49. return GtalkLoginRequest.class;
  50. case kFIRMessagingProtoTagLoginResponse:
  51. return GtalkLoginResponse.class;
  52. case kFIRMessagingProtoTagClose:
  53. return GtalkClose.class;
  54. case kFIRMessagingProtoTagIqStanza:
  55. return GtalkIqStanza.class;
  56. case kFIRMessagingProtoTagDataMessageStanza:
  57. return GtalkDataMessageStanza.class;
  58. case kFIRMessagingProtoTagInvalid:
  59. return NSNull.class;
  60. }
  61. return NSNull.class;
  62. }
  63. #pragma mark - MCS
  64. NSString *FIRMessagingGetRmq2Id(GPBMessage *proto) {
  65. if ([proto isKindOfClass:[GtalkIqStanza class]]) {
  66. if (((GtalkIqStanza *)proto).hasPersistentId) {
  67. return ((GtalkIqStanza *)proto).persistentId;
  68. }
  69. } else if ([proto isKindOfClass:[GtalkDataMessageStanza class]]) {
  70. if (((GtalkDataMessageStanza *)proto).hasPersistentId) {
  71. return ((GtalkDataMessageStanza *)proto).persistentId;
  72. }
  73. }
  74. return nil;
  75. }
  76. void FIRMessagingSetRmq2Id(GPBMessage *proto, NSString *pID) {
  77. if ([proto isKindOfClass:[GtalkIqStanza class]]) {
  78. ((GtalkIqStanza *)proto).persistentId = pID;
  79. } else if ([proto isKindOfClass:[GtalkDataMessageStanza class]]) {
  80. ((GtalkDataMessageStanza *)proto).persistentId = pID;
  81. }
  82. }
  83. int FIRMessagingGetLastStreamId(GPBMessage *proto) {
  84. if ([proto isKindOfClass:[GtalkIqStanza class]]) {
  85. if (((GtalkIqStanza *)proto).hasLastStreamIdReceived) {
  86. return ((GtalkIqStanza *)proto).lastStreamIdReceived;
  87. }
  88. } else if ([proto isKindOfClass:[GtalkDataMessageStanza class]]) {
  89. if (((GtalkDataMessageStanza *)proto).hasLastStreamIdReceived) {
  90. return ((GtalkDataMessageStanza *)proto).lastStreamIdReceived;
  91. }
  92. } else if ([proto isKindOfClass:[GtalkHeartbeatPing class]]) {
  93. if (((GtalkHeartbeatPing *)proto).hasLastStreamIdReceived) {
  94. return ((GtalkHeartbeatPing *)proto).lastStreamIdReceived;
  95. }
  96. } else if ([proto isKindOfClass:[GtalkHeartbeatAck class]]) {
  97. if (((GtalkHeartbeatAck *)proto).hasLastStreamIdReceived) {
  98. return ((GtalkHeartbeatAck *)proto).lastStreamIdReceived;
  99. }
  100. }
  101. return -1;
  102. }
  103. void FIRMessagingSetLastStreamId(GPBMessage *proto, int sid) {
  104. if ([proto isKindOfClass:[GtalkIqStanza class]]) {
  105. ((GtalkIqStanza *)proto).lastStreamIdReceived = sid;
  106. } else if ([proto isKindOfClass:[GtalkDataMessageStanza class]]) {
  107. ((GtalkDataMessageStanza *)proto).lastStreamIdReceived = sid;
  108. } else if ([proto isKindOfClass:[GtalkHeartbeatPing class]]) {
  109. ((GtalkHeartbeatPing *)proto).lastStreamIdReceived = sid;
  110. } else if ([proto isKindOfClass:[GtalkHeartbeatAck class]]) {
  111. ((GtalkHeartbeatAck *)proto).lastStreamIdReceived = sid;
  112. }
  113. }
  114. #pragma mark - Time
  115. int64_t FIRMessagingCurrentTimestampInSeconds(void) {
  116. return (int64_t)[[NSDate date] timeIntervalSince1970];
  117. }
  118. int64_t FIRMessagingCurrentTimestampInMilliseconds(void) {
  119. return (int64_t)(FIRMessagingCurrentTimestampInSeconds() * 1000.0);
  120. }
  121. #pragma mark - App Info
  122. NSString *FIRMessagingCurrentAppVersion(void) {
  123. NSString *version = [[NSBundle mainBundle] infoDictionary][@"CFBundleShortVersionString"];
  124. if (![version length]) {
  125. FIRMessagingLoggerError(kFIRMessagingMessageCodeUtilities000,
  126. @"Could not find current app version");
  127. return @"";
  128. }
  129. return version;
  130. }
  131. NSString *FIRMessagingAppIdentifier(void) {
  132. return [[NSBundle mainBundle] bundleIdentifier];
  133. }
  134. uint64_t FIRMessagingGetFreeDiskSpaceInMB(void) {
  135. NSError *error;
  136. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  137. NSDictionary *attributesMap =
  138. [[NSFileManager defaultManager] attributesOfFileSystemForPath:[paths lastObject]
  139. error:&error];
  140. if (attributesMap) {
  141. uint64_t totalSizeInBytes __unused = [attributesMap[NSFileSystemSize] longLongValue];
  142. uint64_t freeSizeInBytes = [attributesMap[NSFileSystemFreeSize] longLongValue];
  143. FIRMessagingLoggerDebug(
  144. kFIRMessagingMessageCodeUtilities001, @"Device has capacity %llu MB with %llu MB free.",
  145. totalSizeInBytes / kBytesToMegabytesDivisor, freeSizeInBytes / kBytesToMegabytesDivisor);
  146. return ((double)freeSizeInBytes) / kBytesToMegabytesDivisor;
  147. } else {
  148. FIRMessagingLoggerError(kFIRMessagingMessageCodeUtilities002,
  149. @"Error in retreiving device's free memory %@", error);
  150. return 0;
  151. }
  152. }