1// Copyright (c) 2012 The Chromium Authors. All rights reserved.
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/logging.h"
11#include "base/mac/bundle_locations.h"
12#include "base/mac/foundation_util.h"
13#include "base/strings/sys_string_conversions.h"
14#include "build/build_config.h"
15
16FileVersionInfoMac::FileVersionInfoMac(NSBundle *bundle)
17    : bundle_([bundle retain]) {
18}
19
20FileVersionInfoMac::~FileVersionInfoMac() {}
21
22// static
23std::unique_ptr<FileVersionInfo>
24FileVersionInfo::CreateFileVersionInfoForCurrentModule() {
25  return CreateFileVersionInfo(base::mac::FrameworkBundlePath());
26}
27
28// static
29std::unique_ptr<FileVersionInfo> FileVersionInfo::CreateFileVersionInfo(
30    const base::FilePath& file_path) {
31  NSString* path = base::SysUTF8ToNSString(file_path.value());
32  NSBundle* bundle = [NSBundle bundleWithPath:path];
33  return std::make_unique<FileVersionInfoMac>(bundle);
34}
35
36base::string16 FileVersionInfoMac::company_name() {
37  return base::string16();
38}
39
40base::string16 FileVersionInfoMac::company_short_name() {
41  return base::string16();
42}
43
44base::string16 FileVersionInfoMac::internal_name() {
45  return base::string16();
46}
47
48base::string16 FileVersionInfoMac::product_name() {
49  return GetString16Value(kCFBundleNameKey);
50}
51
52base::string16 FileVersionInfoMac::product_short_name() {
53  return GetString16Value(kCFBundleNameKey);
54}
55
56base::string16 FileVersionInfoMac::product_version() {
57  // On OS X, CFBundleVersion is used by LaunchServices, and must follow
58  // specific formatting rules, so the four-part Chrome version is in
59  // CFBundleShortVersionString. On iOS, both have a policy-enfoced limit
60  // of three version components, so the full version is stored in a custom
61  // key (CrBundleVersion) falling back to CFBundleVersion if not present.
62#if defined(OS_IOS)
63  base::string16 version(GetString16Value(CFSTR("CrBundleVersion")));
64  if (version.length() > 0)
65    return version;
66  return GetString16Value(CFSTR("CFBundleVersion"));
67#else
68  return GetString16Value(CFSTR("CFBundleShortVersionString"));
69#endif  // defined(OS_IOS)
70}
71
72base::string16 FileVersionInfoMac::file_description() {
73  return base::string16();
74}
75
76base::string16 FileVersionInfoMac::file_version() {
77  return product_version();
78}
79
80base::string16 FileVersionInfoMac::original_filename() {
81  return GetString16Value(kCFBundleNameKey);
82}
83
84base::string16 FileVersionInfoMac::special_build() {
85  return base::string16();
86}
87
88base::string16 FileVersionInfoMac::GetString16Value(CFStringRef name) {
89  if (bundle_) {
90    NSString *ns_name = base::mac::CFToNSCast(name);
91    NSString* value = [bundle_ objectForInfoDictionaryKey:ns_name];
92    if (value) {
93      return base::SysNSStringToUTF16(value);
94    }
95  }
96  return base::string16();
97}
98