1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97

base / file_version_info_apple.mm [blame]

// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/file_version_info_apple.h"

#import <Foundation/Foundation.h>

#include "base/apple/bridging.h"
#include "base/apple/bundle_locations.h"
#include "base/apple/foundation_util.h"
#include "base/files/file_path.h"
#include "base/strings/sys_string_conversions.h"
#include "build/build_config.h"

FileVersionInfoApple::FileVersionInfoApple(NSBundle* bundle)
    : bundle_(bundle) {}

FileVersionInfoApple::~FileVersionInfoApple() = default;

// static
std::unique_ptr<FileVersionInfo>
FileVersionInfo::CreateFileVersionInfoForCurrentModule() {
  return CreateFileVersionInfo(base::apple::FrameworkBundlePath());
}

// static
std::unique_ptr<FileVersionInfo> FileVersionInfo::CreateFileVersionInfo(
    const base::FilePath& file_path) {
  NSString* path = base::SysUTF8ToNSString(file_path.value());
  NSBundle* bundle = [NSBundle bundleWithPath:path];
  return std::make_unique<FileVersionInfoApple>(bundle);
}

std::u16string FileVersionInfoApple::company_name() {
  return std::u16string();
}

std::u16string FileVersionInfoApple::company_short_name() {
  return std::u16string();
}

std::u16string FileVersionInfoApple::internal_name() {
  return std::u16string();
}

std::u16string FileVersionInfoApple::product_name() {
  return GetString16Value(kCFBundleNameKey);
}

std::u16string FileVersionInfoApple::product_short_name() {
  return GetString16Value(kCFBundleNameKey);
}

std::u16string FileVersionInfoApple::product_version() {
  // On macOS, CFBundleVersion is used by LaunchServices, and must follow
  // specific formatting rules, so the four-part Chrome version is in
  // CFBundleShortVersionString. On iOS, both have a policy-enforced limit
  // of three version components, so the full version is stored in a custom
  // key (CrBundleVersion) falling back to CFBundleVersion if not present.
#if BUILDFLAG(IS_IOS)
  std::u16string version(GetString16Value(CFSTR("CrBundleVersion")));
  if (version.length() > 0) {
    return version;
  }
  return GetString16Value(CFSTR("CFBundleVersion"));
#else
  return GetString16Value(CFSTR("CFBundleShortVersionString"));
#endif  // BUILDFLAG(IS_IOS)
}

std::u16string FileVersionInfoApple::file_description() {
  return std::u16string();
}

std::u16string FileVersionInfoApple::file_version() {
  return product_version();
}

std::u16string FileVersionInfoApple::original_filename() {
  return GetString16Value(kCFBundleNameKey);
}

std::u16string FileVersionInfoApple::special_build() {
  return std::u16string();
}

std::u16string FileVersionInfoApple::GetString16Value(CFStringRef name) {
  if (bundle_) {
    NSString* ns_name = base::apple::CFToNSPtrCast(name);
    NSString* value = [bundle_ objectForInfoDictionaryKey:ns_name];
    if (value) {
      return base::SysNSStringToUTF16(value);
    }
  }
  return std::u16string();
}