1/* 2 * Copyright (C) 2014 Apple Inc. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' 14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS 17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 23 * THE POSSIBILITY OF SUCH DAMAGE. 24 */ 25 26#import "config.h" 27#import "UserAgent.h" 28 29#import "SystemVersion.h" 30 31namespace WebCore { 32 33String systemMarketingVersionForUserAgentString() 34{ 35 // Use underscores instead of dots because when we first added the Mac OS X version to the user agent string 36 // we were concerned about old DHTML libraries interpreting "4." as Netscape 4. That's no longer a concern for us 37 // but we're sticking with the underscores for compatibility with the format used by older versions of Safari. 38 return [systemMarketingVersion() stringByReplacingOccurrencesOfString:@"." withString:@"_"]; 39} 40 41static NSString *userVisibleWebKitBundleVersionFromFullVersion(NSString *fullWebKitVersion) 42{ 43 // If the version is longer than 3 digits then the leading digits represent the version of the OS. Our user agent 44 // string should not include the leading digits, so strip them off and report the rest as the version. <rdar://problem/4997547> 45 NSRange nonDigitRange = [fullWebKitVersion rangeOfCharacterFromSet:[[NSCharacterSet decimalDigitCharacterSet] invertedSet]]; 46 if (nonDigitRange.location == NSNotFound && fullWebKitVersion.length > 3) 47 return [fullWebKitVersion substringFromIndex:fullWebKitVersion.length - 3]; 48 if (nonDigitRange.location != NSNotFound && nonDigitRange.location > 3) 49 return [fullWebKitVersion substringFromIndex:nonDigitRange.location - 3]; 50 return fullWebKitVersion; 51} 52 53String userAgentBundleVersionFromFullVersionString(const String& fullWebKitVersion) 54{ 55 // We include at most three components of the bundle version in the user agent string. 56 NSString *bundleVersion = userVisibleWebKitBundleVersionFromFullVersion(fullWebKitVersion); 57 NSScanner *scanner = [NSScanner scannerWithString:bundleVersion]; 58 NSInteger periodCount = 0; 59 while (true) { 60 if (![scanner scanUpToString:@"." intoString:nullptr] || scanner.isAtEnd) 61 return bundleVersion; 62 63 if (++periodCount == 3) 64 return [bundleVersion substringToIndex:scanner.scanLocation]; 65 66 ++scanner.scanLocation; 67 } 68 69 ASSERT_NOT_REACHED(); 70} 71 72} // namespace WebCore 73