Demonstrate the basic functionality of the File System
[chromium-blink-merge.git] / chrome / common / mac / app_mode_chrome_locator.mm
blob3cd5302cc46cf9b1b8679c84026230719484aa94
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::FilePath* executable_path,
29                          base::string16* raw_version_str,
30                          base::FilePath* version_path,
31                          base::FilePath* framework_shlib_path) {
32   using base::mac::ObjCCast;
34   NSString* cr_bundle_path = base::mac::FilePathToNSString(chrome_bundle);
35   NSBundle* cr_bundle = [NSBundle bundleWithPath:cr_bundle_path];
37   if (!cr_bundle)
38     return false;
40   // Read raw version string.
41   NSString* cr_version =
42        ObjCCast<NSString>(
43           [cr_bundle objectForInfoDictionaryKey:@"CFBundleShortVersionString"]);
44   if (!cr_version)
45     return false;
47   // Get versioned directory.
48   NSArray* cr_versioned_path_components =
49       [NSArray arrayWithObjects:cr_bundle_path,
50                                 @"Contents",
51                                 @"Versions",
52                                 cr_version,
53                                 nil];
54   NSString* cr_versioned_path =
55     [NSString pathWithComponents:cr_versioned_path_components];
57   // Get the framework path.
58   NSString* cr_bundle_exe =
59       ObjCCast<NSString>(
60           [cr_bundle objectForInfoDictionaryKey:@"CFBundleExecutable"]);
61   // Essentially we want chrome::kFrameworkName which looks like
62   // "$PRODUCT_STRING Framework.framework". The library itself is at
63   // "$PRODUCT_STRING Framework.framework/$PRODUCT_STRING Framework". Note that
64   // $PRODUCT_STRING is not |cr_bundle_exe| because in Canary the framework is
65   // still called "Google Chrome Framework".
66   // However, we want the shims to be agnostic to distribution and operate based
67   // on the data in their plist, so encode the framework names here.
68   NSDictionary* framework_for_exe = @{
69     @"Chromium": @"Chromium",
70     @"Google Chrome": @"Google Chrome",
71     @"Google Chrome Canary": @"Google Chrome",
72   };
73   NSString* framework_name = [framework_for_exe objectForKey:cr_bundle_exe];
74   NSString* cr_framework_shlib_path =
75       [cr_versioned_path stringByAppendingPathComponent:
76           [framework_name stringByAppendingString:@" Framework.framework"]];
77   cr_framework_shlib_path =
78       [cr_framework_shlib_path stringByAppendingPathComponent:
79           [framework_name stringByAppendingString:@" Framework"]];
80   if (!cr_bundle_exe || !cr_framework_shlib_path)
81     return false;
83   // A few more sanity checks.
84   BOOL is_directory;
85   BOOL exists = [[NSFileManager defaultManager]
86                     fileExistsAtPath:cr_framework_shlib_path
87                          isDirectory:&is_directory];
88   if (!exists || is_directory)
89     return false;
91   // Everything OK, copy output parameters.
92   *executable_path = base::mac::NSStringToFilePath([cr_bundle executablePath]);
93   *raw_version_str = base::SysNSStringToUTF16(cr_version);
94   *version_path = base::mac::NSStringToFilePath(cr_versioned_path);
95   *framework_shlib_path =
96       base::mac::NSStringToFilePath(cr_framework_shlib_path);
97   return true;
100 }  // namespace app_mode