aboutsummaryrefslogtreecommitdiffhomepage
path: root/Firebase/Messaging/FIRMessagingUtilities.m
blob: 60f3f28df5a8a0ad67222bbe2768ec3d831d6b68 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
 * Copyright 2017 Google
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#import "FIRMessagingUtilities.h"

#import "Protos/GtalkCore.pbobjc.h"

#import "FIRMessagingLogger.h"

// Convert the macro to a string
#define STR_EXPAND(x) #x
#define STR(x) STR_EXPAND(x)

static const uint64_t kBytesToMegabytesDivisor = 1024 * 1024LL;

#pragma mark - Protocol Buffers

FIRMessagingProtoTag FIRMessagingGetTagForProto(GPBMessage *proto) {
  if ([proto isKindOfClass:[GtalkHeartbeatPing class]]) {
    return kFIRMessagingProtoTagHeartbeatPing;
  } else if ([proto isKindOfClass:[GtalkHeartbeatAck class]]) {
    return kFIRMessagingProtoTagHeartbeatAck;
  } else if ([proto isKindOfClass:[GtalkLoginRequest class]]) {
    return kFIRMessagingProtoTagLoginRequest;
  } else if ([proto isKindOfClass:[GtalkLoginResponse class]]) {
    return kFIRMessagingProtoTagLoginResponse;
  } else if ([proto isKindOfClass:[GtalkClose class]]) {
    return kFIRMessagingProtoTagClose;
  } else if ([proto isKindOfClass:[GtalkIqStanza class]]) {
    return kFIRMessagingProtoTagIqStanza;
  } else if ([proto isKindOfClass:[GtalkDataMessageStanza class]]) {
    return kFIRMessagingProtoTagDataMessageStanza;
  }
  return kFIRMessagingProtoTagInvalid;
}

Class FIRMessagingGetClassForTag(FIRMessagingProtoTag tag) {
  switch (tag) {
    case kFIRMessagingProtoTagHeartbeatPing:
      return GtalkHeartbeatPing.class;
    case kFIRMessagingProtoTagHeartbeatAck:
      return GtalkHeartbeatAck.class;
    case kFIRMessagingProtoTagLoginRequest:
      return GtalkLoginRequest.class;
    case kFIRMessagingProtoTagLoginResponse:
      return GtalkLoginResponse.class;
    case kFIRMessagingProtoTagClose:
      return GtalkClose.class;
    case kFIRMessagingProtoTagIqStanza:
      return GtalkIqStanza.class;
    case kFIRMessagingProtoTagDataMessageStanza:
      return GtalkDataMessageStanza.class;
    case kFIRMessagingProtoTagInvalid:
      return NSNull.class;
  }
  return NSNull.class;
}

#pragma mark - MCS

NSString *FIRMessagingGetRmq2Id(GPBMessage *proto) {
  if ([proto isKindOfClass:[GtalkIqStanza class]]) {
    if (((GtalkIqStanza *)proto).hasPersistentId) {
      return ((GtalkIqStanza *)proto).persistentId;
    }
  } else if ([proto isKindOfClass:[GtalkDataMessageStanza class]]) {
    if (((GtalkDataMessageStanza *)proto).hasPersistentId) {
      return ((GtalkDataMessageStanza *)proto).persistentId;
    }
  }
  return nil;
}

void FIRMessagingSetRmq2Id(GPBMessage *proto, NSString *pID) {
  if ([proto isKindOfClass:[GtalkIqStanza class]]) {
    ((GtalkIqStanza *)proto).persistentId = pID;
  } else if ([proto isKindOfClass:[GtalkDataMessageStanza class]]) {
    ((GtalkDataMessageStanza *)proto).persistentId = pID;
  }
}

int FIRMessagingGetLastStreamId(GPBMessage *proto) {
  if ([proto isKindOfClass:[GtalkIqStanza class]]) {
    if (((GtalkIqStanza *)proto).hasLastStreamIdReceived) {
      return ((GtalkIqStanza *)proto).lastStreamIdReceived;
    }
  } else if ([proto isKindOfClass:[GtalkDataMessageStanza class]]) {
    if (((GtalkDataMessageStanza *)proto).hasLastStreamIdReceived) {
      return ((GtalkDataMessageStanza *)proto).lastStreamIdReceived;
    }
  } else if ([proto isKindOfClass:[GtalkHeartbeatPing class]]) {
    if (((GtalkHeartbeatPing *)proto).hasLastStreamIdReceived) {
      return ((GtalkHeartbeatPing *)proto).lastStreamIdReceived;
    }
  } else if ([proto isKindOfClass:[GtalkHeartbeatAck class]]) {
    if (((GtalkHeartbeatAck *)proto).hasLastStreamIdReceived) {
      return ((GtalkHeartbeatAck *)proto).lastStreamIdReceived;
    }
  }
  return -1;
}

void FIRMessagingSetLastStreamId(GPBMessage *proto, int sid) {
  if ([proto isKindOfClass:[GtalkIqStanza class]]) {
    ((GtalkIqStanza *)proto).lastStreamIdReceived = sid;
  } else if ([proto isKindOfClass:[GtalkDataMessageStanza class]]) {
    ((GtalkDataMessageStanza *)proto).lastStreamIdReceived = sid;
  } else if ([proto isKindOfClass:[GtalkHeartbeatPing class]]) {
    ((GtalkHeartbeatPing *)proto).lastStreamIdReceived = sid;
  } else if ([proto isKindOfClass:[GtalkHeartbeatAck class]]) {
    ((GtalkHeartbeatAck *)proto).lastStreamIdReceived = sid;
  }
}

#pragma mark - Time

int64_t FIRMessagingCurrentTimestampInSeconds(void) {
  return (int64_t)[[NSDate date] timeIntervalSince1970];
}

int64_t FIRMessagingCurrentTimestampInMilliseconds(void) {
  return (int64_t)(FIRMessagingCurrentTimestampInSeconds() * 1000.0);
}

#pragma mark - App Info

NSString *FIRMessagingCurrentAppVersion(void) {
  NSString *version = [[NSBundle mainBundle] infoDictionary][@"CFBundleShortVersionString"];
  if (![version length]) {
    FIRMessagingLoggerError(kFIRMessagingMessageCodeUtilities000,
                            @"Could not find current app version");
    return @"";
  }
  return version;
}

NSString *FIRMessagingAppIdentifier(void) {
  return [[NSBundle mainBundle] bundleIdentifier];
}

uint64_t FIRMessagingGetFreeDiskSpaceInMB(void) {
  NSError *error;
  NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

  NSDictionary *attributesMap =
      [[NSFileManager defaultManager] attributesOfFileSystemForPath:[paths lastObject]
                                                              error:&error];
  if (attributesMap) {
    uint64_t totalSizeInBytes __unused = [attributesMap[NSFileSystemSize] longLongValue];
    uint64_t freeSizeInBytes = [attributesMap[NSFileSystemFreeSize] longLongValue];
    FIRMessagingLoggerDebug(
        kFIRMessagingMessageCodeUtilities001, @"Device has capacity %llu MB with %llu MB free.",
        totalSizeInBytes / kBytesToMegabytesDivisor, freeSizeInBytes / kBytesToMegabytesDivisor);
    return ((double)freeSizeInBytes) / kBytesToMegabytesDivisor;
  } else {
    FIRMessagingLoggerError(kFIRMessagingMessageCodeUtilities002,
                            @"Error in retreiving device's free memory %@", error);
    return 0;
  }
}