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