Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
[ios, osx] Include app/OS name/version in user agent
Browse files Browse the repository at this point in the history
Fixes #3997.
  • Loading branch information
1ec5 committed Apr 17, 2016
1 parent 943c09f commit f936295
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 2 deletions.
96 changes: 94 additions & 2 deletions platform/darwin/src/http_file_source.mm
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,18 @@
#include <mbgl/util/http_header.hpp>
#include <mbgl/util/async_task.hpp>

#include "version.hpp"

#import <Foundation/Foundation.h>

#include <mutex>

@interface MBGLBundleCanary : NSObject
@end

@implementation MBGLBundleCanary
@end

namespace mbgl {

// Data that is shared between the requesting thread and the thread running the completion handler.
Expand Down Expand Up @@ -82,8 +90,7 @@ void cancel() {

session = [NSURLSession sessionWithConfiguration:sessionConfig];

// Write user agent string
userAgent = @"MapboxGL";
userAgent = getUserAgent();

accountType = [[NSUserDefaults standardUserDefaults] integerForKey:@"MGLMapboxAccountType"];
}
Expand All @@ -92,8 +99,93 @@ void cancel() {
NSURLSession* session = nil;
NSString* userAgent = nil;
NSInteger accountType = 0;

private:
NSString* getUserAgent() const;
NSBundle* getSDKBundle() const;
};

NSString *HTTPFileSource::Impl::getUserAgent() const {
NSMutableArray *userAgentComponents = [NSMutableArray array];

NSBundle *appBundle = [NSBundle mainBundle];
if (appBundle) {
NSString *appName = appBundle.infoDictionary[@"CFBundleName"];
[userAgentComponents addObject:[NSString stringWithFormat:@"%@/%@",
appName.length ? appName : appBundle.infoDictionary[@"CFBundleIdentifier"],
appBundle.infoDictionary[@"CFBundleShortVersionString"]]];
} else {
[userAgentComponents addObject:[NSProcessInfo processInfo].processName];
}

NSBundle *sdkBundle = HTTPFileSource::Impl::getSDKBundle();
if (sdkBundle) {
NSString *versionString = sdkBundle.infoDictionary[@"MGLSemanticVersionString"];
if (!versionString) {
versionString = sdkBundle.infoDictionary[@"CFBundleShortVersionString"];
}
if (versionString) {
[userAgentComponents addObject:[NSString stringWithFormat:@"%@/%@",
sdkBundle.infoDictionary[@"CFBundleName"], versionString]];
}
}

// Avoid %s here because it inserts hidden bidirectional markers on OS X when the system
// language is set to a right-to-left language.
[userAgentComponents addObject:[NSString stringWithFormat:@"MapboxGL/%@ (%@)",
CFSTR(MBGL_VERSION_STRING), CFSTR(MBGL_VERSION_REV)]];

NSString *systemName = @"Darwin";
#if TARGET_OS_IPHONE
systemName = @"iOS";
#elif TARGET_OS_MAC
systemName = @"OS X";
#elif TARGET_OS_WATCH
systemName = @"watchOS";
#elif TARGET_OS_TV
systemName = @"tvOS";
#endif
#if TARGET_OS_SIMULATOR
systemName = [systemName stringByAppendingString:@" Simulator"];
#endif
NSString *systemVersion = nil;
if ([NSProcessInfo instancesRespondToSelector:@selector(operatingSystemVersion)]) {
NSOperatingSystemVersion osVersion = [NSProcessInfo processInfo].operatingSystemVersion;
systemVersion = [NSString stringWithFormat:@"%ld.%ld.%ld",
(long)osVersion.majorVersion, (long)osVersion.minorVersion, (long)osVersion.patchVersion];
}
if (systemVersion) {
[userAgentComponents addObject:[NSString stringWithFormat:@"%@/%@", systemName, systemVersion]];
}

NSString *cpu = nil;
#if TARGET_CPU_X86
cpu = @"x86";
#elif TARGET_CPU_X86_64
cpu = @"x86_64";
#elif TARGET_CPU_ARM
cpu = @"arm";
#elif TARGET_CPU_ARM64
cpu = @"arm64";
#endif
if (cpu) {
[userAgentComponents addObject:[NSString stringWithFormat:@"(%@)", cpu]];
}

return [userAgentComponents componentsJoinedByString:@" "];
}

NSBundle *HTTPFileSource::Impl::getSDKBundle() const {
NSBundle *bundle = [NSBundle bundleForClass:[MBGLBundleCanary class]];
if (bundle && ![bundle.infoDictionary[@"CFBundlePackageType"] isEqualToString:@"FMWK"]) {
// For static frameworks, the class is contained in the application bundle rather than the
// framework bundle.
bundle = [NSBundle bundleWithPath:[bundle.privateFrameworksPath
stringByAppendingPathComponent:@"Mapbox.framework"]];
}
return bundle;
}

HTTPFileSource::HTTPFileSource()
: impl(std::make_unique<Impl>()) {
}
Expand Down
1 change: 1 addition & 0 deletions platform/ios/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Mapbox welcomes participation and contributions from everyone. If you’d like
- Applications linking against the SDK static framework no longer need to add `-ObjC` to the Other Linker Flags (`OTHER_LDFLAGS`) build setting. If you previously added this flag solely for this SDK, removing the flag may potentially reduce the overall size of your application. ([#4641](https://github.com/mapbox/mapbox-gl-native/pull/4641))
- Removed the `armv7s` slice from the SDK to reduce its size. iPhone 5 and iPhone 5c automatically use the `armv7` slice instead. ([#4641](https://github.com/mapbox/mapbox-gl-native/pull/4641))
- User location heading updates now resume properly when an app becomes active again. ([#4674](https://github.com/mapbox/mapbox-gl-native/pull/4674))
- A more specific user agent string is now sent with style and tile requests. ([#4012](https://github.com/mapbox/mapbox-gl-native/pull/4012))
- Removed unused SVG files from the SDK’s resource bundle. ([#4641](https://github.com/mapbox/mapbox-gl-native/pull/4641))

## 3.2.0
Expand Down

0 comments on commit f936295

Please sign in to comment.