1 // Copyright 2006-2008 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 #include "base/base_paths.h"
6
7 #include "base/environment.h"
8 #include "base/files/file_path.h"
9 #include "base/files/file_util.h"
10 #include "base/logging.h"
11 #include "base/path_service.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "build/build_config.h"
14
15 namespace base {
16
17 // This provider aims at overriding the initial behaviour for all platforms. It
18 // is meant to be run **before** the platform specific provider so that this one
19 // prevails in case the overriding conditions are met. This provider is also
20 // meant to fallback on the platform specific provider, which means it should
21 // not handle the `BasePathKey` for which we do not have overriding behaviours.
EnvOverridePathProvider(int key,FilePath * result)22 bool EnvOverridePathProvider(int key, FilePath* result) {
23 switch (key) {
24 case base::DIR_SRC_TEST_DATA_ROOT: {
25 // Allow passing this in the environment, for more flexibility in build
26 // tree configurations (sub-project builds, gyp --output_dir, etc.)
27 std::unique_ptr<Environment> env(Environment::Create());
28 std::string cr_source_root;
29 FilePath path;
30 if (env->GetVar("CR_SOURCE_ROOT", &cr_source_root)) {
31 #if BUILDFLAG(IS_WIN)
32 path = FilePath(UTF8ToWide(cr_source_root));
33 #else
34 path = FilePath(cr_source_root);
35 #endif
36 if (!path.IsAbsolute()) {
37 FilePath root;
38 if (PathService::Get(DIR_EXE, &root)) {
39 path = root.Append(path);
40 }
41 }
42 if (DirectoryExists(path)) {
43 *result = path;
44 return true;
45 }
46 DLOG(WARNING) << "CR_SOURCE_ROOT is set, but it appears to not "
47 << "point to a directory.";
48 }
49 return false;
50 }
51 default:
52 break;
53 }
54 return false;
55 }
56
PathProvider(int key,FilePath * result)57 bool PathProvider(int key, FilePath* result) {
58 // NOTE: DIR_CURRENT is a special case in PathService::Get
59
60 switch (key) {
61 case DIR_EXE:
62 if (!PathService::Get(FILE_EXE, result))
63 return false;
64 *result = result->DirName();
65 return true;
66 #if !BUILDFLAG(IS_FUCHSIA) && !BUILDFLAG(IS_IOS)
67 case DIR_MODULE:
68 if (!PathService::Get(FILE_MODULE, result))
69 return false;
70 *result = result->DirName();
71 return true;
72 case DIR_ASSETS:
73 return PathService::Get(DIR_MODULE, result);
74 #endif // !BUILDFLAG(IS_FUCHSIA) && !BUILDFLAG(IS_IOS)
75 case DIR_TEMP:
76 return GetTempDir(result);
77 case DIR_HOME:
78 *result = GetHomeDir();
79 return true;
80 case base::DIR_SRC_TEST_DATA_ROOT:
81 // This is only used by tests and overridden by each platform.
82 NOTREACHED();
83 return false;
84 #if !BUILDFLAG(IS_FUCHSIA) && !BUILDFLAG(IS_IOS)
85 case DIR_OUT_TEST_DATA_ROOT:
86 // On most platforms test binaries are run directly from the build-output
87 // directory, so return the directory containing the executable.
88 return PathService::Get(DIR_MODULE, result);
89 #endif // !BUILDFLAG(IS_FUCHSIA) && !BUILDFLAG(IS_IOS)
90 case DIR_GEN_TEST_DATA_ROOT:
91 if (!PathService::Get(DIR_OUT_TEST_DATA_ROOT, result)) {
92 return false;
93 }
94 *result = result->Append(FILE_PATH_LITERAL("gen"));
95 return true;
96 case DIR_TEST_DATA: {
97 FilePath test_data_path;
98 if (!PathService::Get(DIR_SRC_TEST_DATA_ROOT, &test_data_path)) {
99 return false;
100 }
101 test_data_path = test_data_path.Append(FILE_PATH_LITERAL("base"));
102 test_data_path = test_data_path.Append(FILE_PATH_LITERAL("test"));
103 test_data_path = test_data_path.Append(FILE_PATH_LITERAL("data"));
104 if (!PathExists(test_data_path)) // We don't want to create this.
105 return false;
106 *result = test_data_path;
107 return true;
108 }
109 }
110
111 return false;
112 }
113
114 } // namespace base
115