Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / common / mac / app_mode_chrome_locator.mm
blobac62380cf2fce635039fb9b463e0024a94e2c369
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.
5 #import "chrome/common/mac/app_mode_chrome_locator.h"
7 #import <AppKit/AppKit.h>
8 #include <CoreFoundation/CoreFoundation.h>
10 #include "base/files/file_path.h"
11 #include "base/mac/foundation_util.h"
12 #include "base/strings/sys_string_conversions.h"
13 #include "chrome/common/chrome_constants.h"
15 namespace app_mode {
17 bool FindBundleById(NSString* bundle_id, base::FilePath* out_bundle) {
18   NSWorkspace* ws = [NSWorkspace sharedWorkspace];
19   NSString *bundlePath = [ws absolutePathForAppBundleWithIdentifier:bundle_id];
20   if (!bundlePath)
21     return false;
23   *out_bundle = base::mac::NSStringToFilePath(bundlePath);
24   return true;
27 bool GetChromeBundleInfo(const base::FilePath& chrome_bundle,
28                          base::string16* raw_version_str,
29                          base::FilePath* version_path,
30                          base::FilePath* framework_shlib_path) {
31   using base::mac::ObjCCast;
33   NSString* cr_bundle_path = base::mac::FilePathToNSString(chrome_bundle);
34   NSBundle* cr_bundle = [NSBundle bundleWithPath:cr_bundle_path];
36   if (!cr_bundle)
37     return false;
39   // Read raw version string.
40   NSString* cr_version =
41        ObjCCast<NSString>(
42           [cr_bundle objectForInfoDictionaryKey:@"CFBundleShortVersionString"]);
43   if (!cr_version)
44     return false;
46   // Get versioned directory.
47   NSArray* cr_versioned_path_components =
48       [NSArray arrayWithObjects:cr_bundle_path,
49                                 @"Contents",
50                                 @"Versions",
51                                 cr_version,
52                                 nil];
53   NSString* cr_versioned_path =
54     [NSString pathWithComponents:cr_versioned_path_components];
56   // Get the framework path.
57   NSString* cr_bundle_exe =
58       ObjCCast<NSString>(
59           [cr_bundle objectForInfoDictionaryKey:@"CFBundleExecutable"]);
60   NSString* cr_framework_shlib_path =
61       [cr_versioned_path stringByAppendingPathComponent:
62           base::SysUTF8ToNSString(chrome::kFrameworkName)];
63   // chrome::kFrameworkName looks like "$PRODUCT_STRING Framework.framework".
64   // The library itself is at
65   // "$PRODUCT_STRING Framework.framework/$PRODUCT_STRING Framework", so we cut
66   // off the .framework extension here and append it to the path.
67   // It's important to build the path to the framework this way because
68   // in Canary the framework is still called "Google Chrome Framework".
69   cr_framework_shlib_path =
70       [cr_framework_shlib_path stringByAppendingPathComponent:
71           [base::SysUTF8ToNSString(chrome::kFrameworkName)
72               stringByDeletingPathExtension]];
73   if (!cr_bundle_exe || !cr_framework_shlib_path)
74     return false;
76   // A few more sanity checks.
77   BOOL is_directory;
78   BOOL exists = [[NSFileManager defaultManager]
79                     fileExistsAtPath:cr_framework_shlib_path
80                          isDirectory:&is_directory];
81   if (!exists || is_directory)
82     return false;
84   // Everything OK, copy output parameters.
85   *raw_version_str = base::SysNSStringToUTF16(cr_version);
86   *version_path = base::mac::NSStringToFilePath(cr_versioned_path);
87   *framework_shlib_path =
88       base::mac::NSStringToFilePath(cr_framework_shlib_path);
89   return true;
92 }  // namespace app_mode