Updates the mic icon status based on the device's audio state (2nd)
[chromium-blink-merge.git] / chrome / common / mac / app_mode_chrome_locator.mm
blob923e671d804e356e6e42cfd1a17cd96ff6e8081d
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/files/file_util.h"
12 #include "base/mac/foundation_util.h"
13 #include "base/strings/sys_string_conversions.h"
14 #include "chrome/common/chrome_constants.h"
16 namespace app_mode {
18 bool FindBundleById(NSString* bundle_id, base::FilePath* out_bundle) {
19   NSWorkspace* ws = [NSWorkspace sharedWorkspace];
20   NSString *bundlePath = [ws absolutePathForAppBundleWithIdentifier:bundle_id];
21   if (!bundlePath)
22     return false;
24   *out_bundle = base::mac::NSStringToFilePath(bundlePath);
25   return true;
28 NSString* GetVersionedPath(NSString* bundle_path, NSString* version) {
29   return [NSString
30       pathWithComponents:@[ bundle_path, @"Contents", @"Versions", version ]];
33 bool GetChromeBundleInfo(const base::FilePath& chrome_bundle,
34                          const std::string& version_str,
35                          base::FilePath* executable_path,
36                          base::FilePath* version_path,
37                          base::FilePath* framework_shlib_path) {
38   using base::mac::ObjCCast;
40   NSString* cr_bundle_path = base::mac::FilePathToNSString(chrome_bundle);
41   NSBundle* cr_bundle = [NSBundle bundleWithPath:cr_bundle_path];
43   if (!cr_bundle)
44     return false;
46   // Get versioned directory.
47   NSString* cr_versioned_path;
48   if (!version_str.empty()) {
49     cr_versioned_path =
50         GetVersionedPath(cr_bundle_path, base::SysUTF8ToNSString(version_str));
51   }
53   if (version_str.empty() ||
54       !base::PathExists(base::mac::NSStringToFilePath(cr_versioned_path))) {
55     // Read version string.
56     NSString* cr_version = ObjCCast<NSString>(
57         [cr_bundle objectForInfoDictionaryKey:@"CFBundleShortVersionString"]);
58     if (!cr_version)
59       return false;
61     cr_versioned_path = GetVersionedPath(cr_bundle_path, cr_version);
62   }
64   // Get the framework path.
65   NSString* cr_bundle_exe =
66       ObjCCast<NSString>(
67           [cr_bundle objectForInfoDictionaryKey:@"CFBundleExecutable"]);
68   // Essentially we want chrome::kFrameworkName which looks like
69   // "$PRODUCT_STRING Framework.framework". The library itself is at
70   // "$PRODUCT_STRING Framework.framework/$PRODUCT_STRING Framework". Note that
71   // $PRODUCT_STRING is not |cr_bundle_exe| because in Canary the framework is
72   // still called "Google Chrome Framework".
73   // However, we want the shims to be agnostic to distribution and operate based
74   // on the data in their plist, so encode the framework names here.
75   NSDictionary* framework_for_exe = @{
76     @"Chromium": @"Chromium",
77     @"Google Chrome": @"Google Chrome",
78     @"Google Chrome Canary": @"Google Chrome",
79   };
80   NSString* framework_name = [framework_for_exe objectForKey:cr_bundle_exe];
81   NSString* cr_framework_shlib_path =
82       [cr_versioned_path stringByAppendingPathComponent:
83           [framework_name stringByAppendingString:@" Framework.framework"]];
84   cr_framework_shlib_path =
85       [cr_framework_shlib_path stringByAppendingPathComponent:
86           [framework_name stringByAppendingString:@" Framework"]];
87   if (!cr_bundle_exe || !cr_framework_shlib_path)
88     return false;
90   // A few more sanity checks.
91   BOOL is_directory;
92   BOOL exists = [[NSFileManager defaultManager]
93                     fileExistsAtPath:cr_framework_shlib_path
94                          isDirectory:&is_directory];
95   if (!exists || is_directory)
96     return false;
98   // Everything OK, copy output parameters.
99   *executable_path = base::mac::NSStringToFilePath([cr_bundle executablePath]);
100   *version_path = base::mac::NSStringToFilePath(cr_versioned_path);
101   *framework_shlib_path =
102       base::mac::NSStringToFilePath(cr_framework_shlib_path);
103   return true;
106 }  // namespace app_mode