added iOS source code
[wl-app.git] / iOS / Pods / Realm / Realm / RLMAnalytics.mm
1 ////////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright 2015 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
19 // Asynchronously submits build information to Realm if running in an iOS
20 // simulator or on OS X if a debugger is attached. Does nothing if running on an
21 // iOS / watchOS device or if a debugger is *not* attached.
22 //
23 // To be clear: this does *not* run when your app is in production or on
24 // your end-user’s devices; it will only run in the simulator or when a debugger
25 // is attached.
26 //
27 // Why are we doing this? In short, because it helps us build a better product
28 // for you. None of the data personally identifies you, your employer or your
29 // app, but it *will* help us understand what language you use, what iOS
30 // versions you target, etc. Having this info will help prioritizing our time,
31 // adding new features and deprecating old features. Collecting an anonymized
32 // bundle & anonymized MAC is the only way for us to count actual usage of the
33 // other metrics accurately. If we don’t have a way to deduplicate the info
34 // reported, it will be useless, as a single developer building their Swift app
35 // 10 times would report 10 times more than a single Objective-C developer that
36 // only builds once, making the data all but useless.
37 // No one likes sharing data unless it’s necessary, we get it, and we’ve
38 // debated adding this for a long long time. Since Realm is a free product
39 // without an email signup, we feel this is a necessary step so we can collect
40 // relevant data to build a better product for you. If you truly, absolutely
41 // feel compelled to not send this data back to Realm, then you can set an env
42 // variable named REALM_DISABLE_ANALYTICS. Since Realm is free we believe
43 // letting these analytics run is a small price to pay for the product & support
44 // we give you.
45 //
46 // Currently the following information is reported:
47 // - What version of Realm is being used, and from which language (obj-c or Swift).
48 // - What version of OS X it's running on (in case Xcode aggressively drops
49 //   support for older versions again, we need to know what we need to support).
50 // - The minimum iOS/OS X version that the application is targeting (again, to
51 //   help us decide what versions we need to support).
52 // - An anonymous MAC address and bundle ID to aggregate the other information on.
53 // - What version of Swift is being used (if applicable).
54
55 #import "RLMAnalytics.hpp"
56
57 #import <Foundation/Foundation.h>
58
59 #if TARGET_IPHONE_SIMULATOR || TARGET_OS_MAC || (TARGET_OS_WATCH && TARGET_OS_SIMULATOR) || (TARGET_OS_TV && TARGET_OS_SIMULATOR)
60 #import "RLMRealm.h"
61 #import "RLMUtil.hpp"
62
63 #import <array>
64 #import <sys/socket.h>
65 #import <sys/sysctl.h>
66 #import <net/if.h>
67 #import <net/if_dl.h>
68
69 #import <CommonCrypto/CommonDigest.h>
70
71 #ifndef REALM_COCOA_VERSION
72 #import "RLMVersion.h"
73 #endif
74
75 #import <realm/sync/version.hpp>
76
77 // Declared for RealmSwiftObjectUtil
78 @interface NSObject (SwiftVersion)
79 + (NSString *)swiftVersion;
80 @end
81
82 // Wrapper for sysctl() that handles the memory management stuff
83 static auto RLMSysCtl(int *mib, u_int mibSize, size_t *bufferSize) {
84     std::unique_ptr<void, decltype(&free)> buffer(nullptr, &free);
85
86     int ret = sysctl(mib, mibSize, nullptr, bufferSize, nullptr, 0);
87     if (ret != 0) {
88         return buffer;
89     }
90
91     buffer.reset(malloc(*bufferSize));
92     if (!buffer) {
93         return buffer;
94     }
95
96     ret = sysctl(mib, mibSize, buffer.get(), bufferSize, nullptr, 0);
97     if (ret != 0) {
98         buffer.reset();
99     }
100
101     return buffer;
102 }
103
104 // Get the version of OS X we're running on (even in the simulator this gives
105 // the OS X version and not the simulated iOS version)
106 static NSString *RLMOSVersion() {
107     std::array<int, 2> mib = {{CTL_KERN, KERN_OSRELEASE}};
108     size_t bufferSize;
109     auto buffer = RLMSysCtl(&mib[0], mib.size(), &bufferSize);
110     if (!buffer) {
111         return nil;
112     }
113
114     return [[NSString alloc] initWithBytesNoCopy:buffer.release()
115                                           length:bufferSize - 1
116                                         encoding:NSUTF8StringEncoding
117                                     freeWhenDone:YES];
118 }
119
120 // Hash the data in the given buffer and convert it to a hex-format string
121 static NSString *RLMHashData(const void *bytes, size_t length) {
122     unsigned char buffer[CC_SHA256_DIGEST_LENGTH];
123     CC_SHA256(bytes, static_cast<CC_LONG>(length), buffer);
124
125     char formatted[CC_SHA256_DIGEST_LENGTH * 2 + 1];
126     for (int i = 0; i < CC_SHA256_DIGEST_LENGTH; ++i) {
127         sprintf(formatted + i * 2, "%02x", buffer[i]);
128     }
129
130     return [[NSString alloc] initWithBytes:formatted
131                                     length:CC_SHA256_DIGEST_LENGTH * 2
132                                   encoding:NSUTF8StringEncoding];
133 }
134
135 // Returns the hash of the MAC address of the first network adaptor since the
136 // vendorIdentifier isn't constant between iOS simulators.
137 static NSString *RLMMACAddress() {
138     int en0 = static_cast<int>(if_nametoindex("en0"));
139     if (!en0) {
140         return nil;
141     }
142
143     std::array<int, 6> mib = {{CTL_NET, PF_ROUTE, 0, AF_LINK, NET_RT_IFLIST, en0}};
144     size_t bufferSize;
145     auto buffer = RLMSysCtl(&mib[0], mib.size(), &bufferSize);
146     if (!buffer) {
147         return nil;
148     }
149
150     // sockaddr_dl struct is immediately after the if_msghdr struct in the buffer
151     auto sockaddr = reinterpret_cast<sockaddr_dl *>(static_cast<if_msghdr *>(buffer.get()) + 1);
152     auto mac = reinterpret_cast<const unsigned char *>(sockaddr->sdl_data + sockaddr->sdl_nlen);
153
154     return RLMHashData(mac, 6);
155 }
156
157 static NSDictionary *RLMAnalyticsPayload() {
158     NSBundle *appBundle = NSBundle.mainBundle;
159     NSString *hashedBundleID = appBundle.bundleIdentifier;
160
161     // Main bundle isn't always the one of interest (e.g. when running tests
162     // it's xctest rather than the app's bundle), so look for one with a bundle ID
163     if (!hashedBundleID) {
164         for (NSBundle *bundle in NSBundle.allBundles) {
165             if ((hashedBundleID = bundle.bundleIdentifier)) {
166                 appBundle = bundle;
167                 break;
168             }
169         }
170     }
171
172     // If we found a bundle ID anywhere, hash it as it could contain sensitive
173     // information (e.g. the name of an unnanounced product)
174     if (hashedBundleID) {
175         NSData *data = [hashedBundleID dataUsingEncoding:NSUTF8StringEncoding];
176         hashedBundleID = RLMHashData(data.bytes, data.length);
177     }
178
179     NSString *osVersionString = [[NSProcessInfo processInfo] operatingSystemVersionString];
180     Class swiftObjectUtilClass = NSClassFromString(@"RealmSwiftObjectUtil");
181     BOOL isSwift = swiftObjectUtilClass != nil;
182     NSString *swiftVersion = isSwift ? [swiftObjectUtilClass swiftVersion] : @"N/A";
183
184     static NSString *kUnknownString = @"unknown";
185     NSString *hashedMACAddress = RLMMACAddress() ?: kUnknownString;
186
187     return @{
188              @"event": @"Run",
189              @"properties": @{
190                      // MixPanel properties
191                      @"token": @"ce0fac19508f6c8f20066d345d360fd0",
192
193                      // Anonymous identifiers to deduplicate events
194                      @"distinct_id": hashedMACAddress,
195                      @"Anonymized MAC Address": hashedMACAddress,
196                      @"Anonymized Bundle ID": hashedBundleID ?: kUnknownString,
197
198                      // Which version of Realm is being used
199                      @"Binding": @"cocoa",
200                      @"Language": isSwift ? @"swift" : @"objc",
201                      @"Realm Version": REALM_COCOA_VERSION,
202                      @"Sync Version": @(REALM_SYNC_VER_STRING),
203 #if TARGET_OS_WATCH
204                      @"Target OS Type": @"watchos",
205 #elif TARGET_OS_TV
206                      @"Target OS Type": @"tvos",
207 #elif TARGET_OS_IPHONE
208                      @"Target OS Type": @"ios",
209 #else
210                      @"Target OS Type": @"osx",
211 #endif
212                      @"Swift Version": swiftVersion,
213                      // Current OS version the app is targetting
214                      @"Target OS Version": osVersionString,
215                      // Minimum OS version the app is targetting
216                      @"Target OS Minimum Version": appBundle.infoDictionary[@"MinimumOSVersion"] ?: kUnknownString,
217
218                      // Host OS version being built on
219                      @"Host OS Type": @"osx",
220                      @"Host OS Version": RLMOSVersion() ?: kUnknownString,
221                  }
222           };
223 }
224
225 void RLMSendAnalytics() {
226     if (getenv("REALM_DISABLE_ANALYTICS") || !RLMIsDebuggerAttached() || RLMIsRunningInPlayground()) {
227         return;
228     }
229
230
231     NSData *payload = [NSJSONSerialization dataWithJSONObject:RLMAnalyticsPayload() options:0 error:nil];
232     NSString *url = [NSString stringWithFormat:@"https://api.mixpanel.com/track/?data=%@&ip=1", [payload base64EncodedStringWithOptions:0]];
233
234     // No error handling or anything because logging errors annoyed people for no
235     // real benefit, and it's not clear what else we could do
236     [[NSURLSession.sharedSession dataTaskWithURL:[NSURL URLWithString:url]] resume];
237 }
238
239 #else
240
241 void RLMSendAnalytics() {}
242
243 #endif