Show a margin for long ash chromecast tray strings. Also trim them if necessary.
[chromium-blink-merge.git] / base / base_paths_mac.mm
blobaf4bd1a89e3b9dc24db7940409532a958e4215f4
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 // Defines base::PathProviderMac which replaces base::PathProviderPosix for Mac
6 // in base/path_service.cc.
8 #include <dlfcn.h>
9 #import <Foundation/Foundation.h>
10 #include <mach-o/dyld.h>
12 #include "base/base_paths.h"
13 #include "base/compiler_specific.h"
14 #include "base/files/file_path.h"
15 #include "base/files/file_util.h"
16 #include "base/logging.h"
17 #include "base/mac/foundation_util.h"
18 #include "base/path_service.h"
19 #include "base/strings/string_util.h"
20 #include "build/build_config.h"
22 namespace {
24 void GetNSExecutablePath(base::FilePath* path) {
25   DCHECK(path);
26   // Executable path can have relative references ("..") depending on
27   // how the app was launched.
28   uint32_t executable_length = 0;
29   _NSGetExecutablePath(NULL, &executable_length);
30   DCHECK_GT(executable_length, 1u);
31   std::string executable_path;
32   int rv = _NSGetExecutablePath(
33       base::WriteInto(&executable_path, executable_length),
34                       &executable_length);
35   DCHECK_EQ(rv, 0);
37   // _NSGetExecutablePath may return paths containing ./ or ../ which makes
38   // FilePath::DirName() work incorrectly, convert it to absolute path so that
39   // paths such as DIR_SOURCE_ROOT can work, since we expect absolute paths to
40   // be returned here.
41   *path = base::MakeAbsoluteFilePath(base::FilePath(executable_path));
44 // Returns true if the module for |address| is found. |path| will contain
45 // the path to the module. Note that |path| may not be absolute.
46 bool GetModulePathForAddress(base::FilePath* path,
47                              const void* address) WARN_UNUSED_RESULT;
49 bool GetModulePathForAddress(base::FilePath* path, const void* address) {
50   Dl_info info;
51   if (dladdr(address, &info) == 0)
52     return false;
53   *path = base::FilePath(info.dli_fname);
54   return true;
57 }  // namespace
59 namespace base {
61 bool PathProviderMac(int key, base::FilePath* result) {
62   switch (key) {
63     case base::FILE_EXE:
64       GetNSExecutablePath(result);
65       return true;
66     case base::FILE_MODULE:
67       return GetModulePathForAddress(result,
68           reinterpret_cast<const void*>(&base::PathProviderMac));
69     case base::DIR_APP_DATA: {
70       bool success = base::mac::GetUserDirectory(NSApplicationSupportDirectory,
71                                                  result);
72 #if defined(OS_IOS)
73       // On IOS, this directory does not exist unless it is created explicitly.
74       if (success && !base::PathExists(*result))
75         success = base::CreateDirectory(*result);
76 #endif  // defined(OS_IOS)
77       return success;
78     }
79     case base::DIR_SOURCE_ROOT:
80       // Go through PathService to catch overrides.
81       if (!PathService::Get(base::FILE_EXE, result))
82         return false;
84       // Start with the executable's directory.
85       *result = result->DirName();
87 #if !defined(OS_IOS)
88       if (base::mac::AmIBundled()) {
89         // The bundled app executables (Chromium, TestShell, etc) live five
90         // levels down, eg:
91         // src/xcodebuild/{Debug|Release}/Chromium.app/Contents/MacOS/Chromium
92         *result = result->DirName().DirName().DirName().DirName().DirName();
93       } else {
94         // Unit tests execute two levels deep from the source root, eg:
95         // src/xcodebuild/{Debug|Release}/base_unittests
96         *result = result->DirName().DirName();
97       }
98 #endif
99       return true;
100     case base::DIR_USER_DESKTOP:
101 #if defined(OS_IOS)
102       // iOS does not have desktop directories.
103       NOTIMPLEMENTED();
104       return false;
105 #else
106       return base::mac::GetUserDirectory(NSDesktopDirectory, result);
107 #endif
108     case base::DIR_CACHE:
109       return base::mac::GetUserDirectory(NSCachesDirectory, result);
110     default:
111       return false;
112   }
115 }  // namespace base