summaryrefslogtreecommitdiff
path: root/StoneIsland/platforms/ios/Pods/FirebaseInstanceID/Firebase/InstanceID/FIRInstanceIDUtilities.m
blob: 25d1b97232e58ecf6489442fd3019171d0229d41 (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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*
 * Copyright 2019 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 "FIRInstanceIDUtilities.h"

#if TARGET_OS_IOS || TARGET_OS_TV
#import <UIKit/UIKit.h>
#endif
#import <sys/utsname.h>

#import "FIRInstanceID.h"
#import "FIRInstanceIDConstants.h"
#import "FIRInstanceIDLogger.h"
#import "FirebaseCore/Sources/Private/FirebaseCoreInternal.h"
#import "GoogleUtilities/UserDefaults/Private/GULUserDefaults.h"

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

static NSString *const kFIRInstanceIDAPNSSandboxPrefix = @"s_";
static NSString *const kFIRInstanceIDAPNSProdPrefix = @"p_";

/// FIRMessaging Class that responds to the FIRMessaging SDK version selector.
/// Verify at runtime if the class exists and implements the required method.
NSString *const kFIRInstanceIDFCMSDKClassString = @"FIRMessaging";

/// FIRMessaging selector that returns the current FIRMessaging library version.
static NSString *const kFIRInstanceIDFCMSDKVersionSelectorString = @"FIRMessagingSDKVersion";

/// FIRMessaging selector that returns the current device locale.
static NSString *const kFIRInstanceIDFCMSDKLocaleSelectorString = @"FIRMessagingSDKCurrentLocale";

NSString *const kFIRInstanceIDUserDefaultsKeyLocale =
    @"com.firebase.instanceid.user_defaults.locale";  // locale key stored in GULUserDefaults

/// Static values which will be populated once retrieved using
/// |FIRInstanceIDRetrieveEnvironmentInfoFromFirebaseCore|.
static NSString *operatingSystemVersion;
static NSString *hardwareDeviceModel;

#pragma mark - URL Helpers

NSString *FIRInstanceIDRegisterServer() {
  return @"https://fcmtoken.googleapis.com/register";
}

#pragma mark - Time

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

int64_t FIRInstanceIDCurrentTimestampInMilliseconds() {
  return (int64_t)(FIRInstanceIDCurrentTimestampInSeconds() * 1000.0);
}

#pragma mark - App Info

NSString *FIRInstanceIDCurrentAppVersion() {
  NSString *version = [[NSBundle mainBundle] infoDictionary][@"CFBundleShortVersionString"];
  if (![version length]) {
    return @"";
  }
  return version;
}

NSString *FIRInstanceIDBundleIDByRemovingLastPartFrom(NSString *bundleID) {
  NSString *bundleIDComponentsSeparator = @".";

  NSMutableArray<NSString *> *bundleIDComponents =
      [[bundleID componentsSeparatedByString:bundleIDComponentsSeparator] mutableCopy];
  [bundleIDComponents removeLastObject];

  return [bundleIDComponents componentsJoinedByString:bundleIDComponentsSeparator];
}

NSString *FIRInstanceIDAppIdentifier() {
  NSString *bundleID = [[NSBundle mainBundle] bundleIdentifier];
  if (!bundleID.length) {
    FIRInstanceIDLoggerError(kFIRInstanceIDMessageCodeUtilitiesMissingBundleIdentifier,
                             @"The mainBundle's bundleIdentifier returned '%@'. Bundle identifier "
                             @"expected to be non-empty.",
                             bundleID);
    return @"";
  }
#if TARGET_OS_WATCH
  return FIRInstanceIDBundleIDByRemovingLastPartFrom(bundleID);
#endif
  return bundleID;
}

NSString *FIRInstanceIDFirebaseAppID() {
  return [FIROptions defaultOptions].googleAppID;
}

#pragma mark - Device Info
// Get the device model from Firebase Core's App Environment Util
NSString *FIRInstanceIDDeviceModel() {
  static dispatch_once_t once;
  dispatch_once(&once, ^{
    struct utsname systemInfo;
    if (uname(&systemInfo) == 0) {
      hardwareDeviceModel = [NSString stringWithUTF8String:systemInfo.machine];
    }
  });
  return hardwareDeviceModel;
}

// Get the system version from Firebase Core's App Environment Util
NSString *FIRInstanceIDOperatingSystemVersion() {
#if TARGET_OS_IOS || TARGET_OS_TV
  return [UIDevice currentDevice].systemVersion;
#elif TARGET_OS_OSX || TARGET_OS_WATCH
  return [NSProcessInfo processInfo].operatingSystemVersionString;
#endif
}

BOOL FIRInstanceIDHasLocaleChanged() {
  NSString *lastLocale =
      [[GULUserDefaults standardUserDefaults] stringForKey:kFIRInstanceIDUserDefaultsKeyLocale];
  NSString *currentLocale = FIRInstanceIDCurrentLocale();
  if (lastLocale) {
    if ([currentLocale isEqualToString:lastLocale]) {
      return NO;
    }
  }
  return YES;
}

#pragma mark - Helpers

BOOL FIRInstanceIDIsValidGCMScope(NSString *scope) {
  return [scope compare:kFIRInstanceIDScopeFirebaseMessaging
                options:NSCaseInsensitiveSearch] == NSOrderedSame;
}

NSString *FIRInstanceIDStringForAPNSDeviceToken(NSData *deviceToken) {
  NSMutableString *APNSToken = [NSMutableString string];
  unsigned char *bytes = (unsigned char *)[deviceToken bytes];
  for (int i = 0; i < (int)deviceToken.length; i++) {
    [APNSToken appendFormat:@"%02x", bytes[i]];
  }
  return APNSToken;
}

NSString *FIRInstanceIDAPNSTupleStringForTokenAndServerType(NSData *deviceToken, BOOL isSandbox) {
  if (deviceToken == nil) {
    // A nil deviceToken leads to an invalid tuple string, so return nil.
    return nil;
  }
  NSString *prefix = isSandbox ? kFIRInstanceIDAPNSSandboxPrefix : kFIRInstanceIDAPNSProdPrefix;
  NSString *APNSString = FIRInstanceIDStringForAPNSDeviceToken(deviceToken);
  NSString *APNSTupleString = [NSString stringWithFormat:@"%@%@", prefix, APNSString];

  return APNSTupleString;
}

#pragma mark - GCM Helpers

NSString *FIRInstanceIDCurrentGCMVersion() {
  Class versionClass = NSClassFromString(kFIRInstanceIDFCMSDKClassString);
  SEL versionSelector = NSSelectorFromString(kFIRInstanceIDFCMSDKVersionSelectorString);
  if ([versionClass respondsToSelector:versionSelector]) {
    IMP getVersionIMP = [versionClass methodForSelector:versionSelector];
    NSString *(*getVersion)(id, SEL) = (void *)getVersionIMP;
    return getVersion(versionClass, versionSelector);
  }
  return nil;
}

NSString *FIRInstanceIDCurrentLocale() {
  Class localeClass = NSClassFromString(kFIRInstanceIDFCMSDKClassString);
  SEL localeSelector = NSSelectorFromString(kFIRInstanceIDFCMSDKLocaleSelectorString);

  if ([localeClass respondsToSelector:localeSelector]) {
    IMP getLocaleIMP = [localeClass methodForSelector:localeSelector];
    NSString *(*getLocale)(id, SEL) = (void *)getLocaleIMP;
    NSString *fcmLocale = getLocale(localeClass, localeSelector);
    if (fcmLocale != nil) {
      return fcmLocale;
    }
  }

  NSString *systemLanguage = [[NSLocale preferredLanguages] firstObject];
  if (systemLanguage != nil) {
    return systemLanguage;
  }

  if (@available(macOS 10.12, iOS 10.0, *)) {
    return [NSLocale currentLocale].languageCode;
  } else {
    return nil;
  }
}