fixup! [M120 Migration] Notify media device state to webbrowser
[platform/framework/web/chromium-efl.git] / base / base_paths_posix.cc
1 // Copyright 2012 The Chromium Authors
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::PathProviderPosix, default path provider on POSIX OSes that
6 // don't have their own base_paths_OS.cc implementation (i.e. all but Mac and
7 // Android).
8
9 #include "base/base_paths.h"
10
11 #include <limits.h>
12 #include <stddef.h>
13
14 #include <memory>
15 #include <ostream>
16 #include <string>
17
18 #include "base/environment.h"
19 #include "base/files/file_path.h"
20 #include "base/files/file_util.h"
21 #include "base/logging.h"
22 #include "base/nix/xdg_util.h"
23 #include "base/notreached.h"
24 #include "base/path_service.h"
25 #include "base/posix/sysctl.h"
26 #include "base/process/process_metrics.h"
27 #include "build/build_config.h"
28
29 #if BUILDFLAG(IS_FREEBSD)
30 #include <sys/param.h>
31 #include <sys/sysctl.h>
32 #elif BUILDFLAG(IS_SOLARIS) || BUILDFLAG(IS_AIX)
33 #include <stdlib.h>
34 #endif
35
36 namespace base {
37
38 bool PathProviderPosix(int key, FilePath* result) {
39   switch (key) {
40     case FILE_EXE:
41     case FILE_MODULE: {  // TODO(evanm): is this correct?
42 #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
43       FilePath bin_dir;
44       if (!ReadSymbolicLink(FilePath(kProcSelfExe), &bin_dir)) {
45         NOTREACHED() << "Unable to resolve " << kProcSelfExe << ".";
46         return false;
47       }
48       *result = bin_dir;
49       return true;
50 #elif BUILDFLAG(IS_FREEBSD)
51       int name[] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 };
52       absl::optional<std::string> bin_dir = StringSysctl(name, std::size(name));
53       if (!bin_dir.has_value() || bin_dir.value().length() <= 1) {
54         NOTREACHED() << "Unable to resolve path.";
55         return false;
56       }
57       *result = FilePath(bin_dir.value());
58       return true;
59 #elif BUILDFLAG(IS_SOLARIS)
60       char bin_dir[PATH_MAX + 1];
61       if (realpath(getexecname(), bin_dir) == NULL) {
62         NOTREACHED() << "Unable to resolve " << getexecname() << ".";
63         return false;
64       }
65       *result = FilePath(bin_dir);
66       return true;
67 #elif BUILDFLAG(IS_OPENBSD) || BUILDFLAG(IS_AIX)
68       // There is currently no way to get the executable path on OpenBSD
69       char* cpath;
70       if ((cpath = getenv("CHROME_EXE_PATH")) != NULL)
71         *result = FilePath(cpath);
72       else
73         *result = FilePath("/usr/local/chrome/chrome");
74       return true;
75 #endif
76     }
77     case DIR_SRC_TEST_DATA_ROOT: {
78       // Allow passing this in the environment, for more flexibility in build
79       // tree configurations (sub-project builds, gyp --output_dir, etc.)
80       std::unique_ptr<Environment> env(Environment::Create());
81       std::string cr_source_root;
82       FilePath path;
83       if (env->GetVar("CR_SOURCE_ROOT", &cr_source_root)) {
84         path = FilePath(cr_source_root);
85         if (PathExists(path)) {
86           *result = path;
87           return true;
88         }
89         DLOG(WARNING) << "CR_SOURCE_ROOT is set, but it appears to not "
90                       << "point to a directory.";
91       }
92       // On POSIX, unit tests execute two levels deep from the source root.
93       // For example:  out/{Debug|Release}/net_unittest
94       if (PathService::Get(DIR_EXE, &path)) {
95         *result = path.DirName().DirName();
96         return true;
97       }
98
99       DLOG(ERROR) << "Couldn't find your source root.  "
100                   << "Try running from your chromium/src directory.";
101       return false;
102     }
103     case DIR_USER_DESKTOP:
104       *result = nix::GetXDGUserDirectory("DESKTOP", "Desktop");
105       return true;
106     case DIR_CACHE: {
107       std::unique_ptr<Environment> env(Environment::Create());
108       FilePath cache_dir(
109           nix::GetXDGDirectory(env.get(), "XDG_CACHE_HOME", ".cache"));
110       *result = cache_dir;
111       return true;
112     }
113   }
114   return false;
115 }
116
117 }  // namespace base