1 // Copyright 2012 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/file_version_info_apple.h"
7 #import <Foundation/Foundation.h>
9 #include "base/apple/bridging.h"
10 #include "base/apple/bundle_locations.h"
11 #include "base/apple/foundation_util.h"
12 #include "base/files/file_path.h"
13 #include "base/strings/sys_string_conversions.h"
14 #include "build/build_config.h"
16 FileVersionInfoApple::FileVersionInfoApple(NSBundle* bundle)
19 FileVersionInfoApple::~FileVersionInfoApple() = default;
22 std::unique_ptr<FileVersionInfo>
23 FileVersionInfo::CreateFileVersionInfoForCurrentModule() {
24 return CreateFileVersionInfo(base::apple::FrameworkBundlePath());
28 std::unique_ptr<FileVersionInfo> FileVersionInfo::CreateFileVersionInfo(
29 const base::FilePath& file_path) {
30 NSString* path = base::SysUTF8ToNSString(file_path.value());
31 NSBundle* bundle = [NSBundle bundleWithPath:path];
32 return std::make_unique<FileVersionInfoApple>(bundle);
35 std::u16string FileVersionInfoApple::company_name() {
36 return std::u16string();
39 std::u16string FileVersionInfoApple::company_short_name() {
40 return std::u16string();
43 std::u16string FileVersionInfoApple::internal_name() {
44 return std::u16string();
47 std::u16string FileVersionInfoApple::product_name() {
48 return GetString16Value(kCFBundleNameKey);
51 std::u16string FileVersionInfoApple::product_short_name() {
52 return GetString16Value(kCFBundleNameKey);
55 std::u16string FileVersionInfoApple::product_version() {
56 // On macOS, CFBundleVersion is used by LaunchServices, and must follow
57 // specific formatting rules, so the four-part Chrome version is in
58 // CFBundleShortVersionString. On iOS, both have a policy-enforced limit
59 // of three version components, so the full version is stored in a custom
60 // key (CrBundleVersion) falling back to CFBundleVersion if not present.
62 std::u16string version(GetString16Value(CFSTR("CrBundleVersion")));
63 if (version.length() > 0) {
66 return GetString16Value(CFSTR("CFBundleVersion"));
68 return GetString16Value(CFSTR("CFBundleShortVersionString"));
69 #endif // BUILDFLAG(IS_IOS)
72 std::u16string FileVersionInfoApple::file_description() {
73 return std::u16string();
76 std::u16string FileVersionInfoApple::file_version() {
77 return product_version();
80 std::u16string FileVersionInfoApple::original_filename() {
81 return GetString16Value(kCFBundleNameKey);
84 std::u16string FileVersionInfoApple::special_build() {
85 return std::u16string();
88 std::u16string FileVersionInfoApple::GetString16Value(CFStringRef name) {
90 NSString* ns_name = base::apple::CFToNSPtrCast(name);
91 NSString* value = [bundle_ objectForInfoDictionaryKey:ns_name];
93 return base::SysNSStringToUTF16(value);
96 return std::u16string();