• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_mac.h"
6
7#import <Foundation/Foundation.h>
8
9#include "base/files/file_path.h"
10#include "base/mac/bundle_locations.h"
11#include "base/mac/foundation_util.h"
12#include "base/strings/sys_string_conversions.h"
13#include "build/build_config.h"
14
15FileVersionInfoMac::FileVersionInfoMac(NSBundle *bundle)
16    : bundle_([bundle retain]) {
17}
18
19FileVersionInfoMac::~FileVersionInfoMac() {}
20
21// static
22std::unique_ptr<FileVersionInfo>
23FileVersionInfo::CreateFileVersionInfoForCurrentModule() {
24  return CreateFileVersionInfo(base::mac::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<FileVersionInfoMac>(bundle);
33}
34
35std::u16string FileVersionInfoMac::company_name() {
36  return std::u16string();
37}
38
39std::u16string FileVersionInfoMac::company_short_name() {
40  return std::u16string();
41}
42
43std::u16string FileVersionInfoMac::internal_name() {
44  return std::u16string();
45}
46
47std::u16string FileVersionInfoMac::product_name() {
48  return GetString16Value(kCFBundleNameKey);
49}
50
51std::u16string FileVersionInfoMac::product_short_name() {
52  return GetString16Value(kCFBundleNameKey);
53}
54
55std::u16string FileVersionInfoMac::product_version() {
56  // On OS X, 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-enfoced 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  return GetString16Value(CFSTR("CFBundleVersion"));
66#else
67  return GetString16Value(CFSTR("CFBundleShortVersionString"));
68#endif  // BUILDFLAG(IS_IOS)
69}
70
71std::u16string FileVersionInfoMac::file_description() {
72  return std::u16string();
73}
74
75std::u16string FileVersionInfoMac::file_version() {
76  return product_version();
77}
78
79std::u16string FileVersionInfoMac::original_filename() {
80  return GetString16Value(kCFBundleNameKey);
81}
82
83std::u16string FileVersionInfoMac::special_build() {
84  return std::u16string();
85}
86
87std::u16string FileVersionInfoMac::GetString16Value(CFStringRef name) {
88  if (bundle_) {
89    NSString *ns_name = base::mac::CFToNSCast(name);
90    NSString* value = [bundle_ objectForInfoDictionaryKey:ns_name];
91    if (value) {
92      return base::SysNSStringToUTF16(value);
93    }
94  }
95  return std::u16string();
96}
97