xref: /aosp_15_r20/external/cronet/base/file_version_info_apple.mm (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
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.
4
5#include "base/file_version_info_apple.h"
6
7#import <Foundation/Foundation.h>
8
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"
15
16FileVersionInfoApple::FileVersionInfoApple(NSBundle* bundle)
17    : bundle_(bundle) {}
18
19FileVersionInfoApple::~FileVersionInfoApple() = default;
20
21// static
22std::unique_ptr<FileVersionInfo>
23FileVersionInfo::CreateFileVersionInfoForCurrentModule() {
24  return CreateFileVersionInfo(base::apple::FrameworkBundlePath());
25}
26
27// static
28std::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);
33}
34
35std::u16string FileVersionInfoApple::company_name() {
36  return std::u16string();
37}
38
39std::u16string FileVersionInfoApple::company_short_name() {
40  return std::u16string();
41}
42
43std::u16string FileVersionInfoApple::internal_name() {
44  return std::u16string();
45}
46
47std::u16string FileVersionInfoApple::product_name() {
48  return GetString16Value(kCFBundleNameKey);
49}
50
51std::u16string FileVersionInfoApple::product_short_name() {
52  return GetString16Value(kCFBundleNameKey);
53}
54
55std::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.
61#if BUILDFLAG(IS_IOS)
62  std::u16string version(GetString16Value(CFSTR("CrBundleVersion")));
63  if (version.length() > 0) {
64    return version;
65  }
66  return GetString16Value(CFSTR("CFBundleVersion"));
67#else
68  return GetString16Value(CFSTR("CFBundleShortVersionString"));
69#endif  // BUILDFLAG(IS_IOS)
70}
71
72std::u16string FileVersionInfoApple::file_description() {
73  return std::u16string();
74}
75
76std::u16string FileVersionInfoApple::file_version() {
77  return product_version();
78}
79
80std::u16string FileVersionInfoApple::original_filename() {
81  return GetString16Value(kCFBundleNameKey);
82}
83
84std::u16string FileVersionInfoApple::special_build() {
85  return std::u16string();
86}
87
88std::u16string FileVersionInfoApple::GetString16Value(CFStringRef name) {
89  if (bundle_) {
90    NSString* ns_name = base::apple::CFToNSPtrCast(name);
91    NSString* value = [bundle_ objectForInfoDictionaryKey:ns_name];
92    if (value) {
93      return base::SysNSStringToUTF16(value);
94    }
95  }
96  return std::u16string();
97}
98