Avi Drissman | 4e1b7bc3 | 2022-09-15 14:03:50 | [diff] [blame] | 1 | // Copyright 2021 The Chromium Authors |
John Abd-El-Malek | 2276305 | 2021-06-15 03:35:53 | [diff] [blame] | 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 "content/shell/browser/shell_paths.h" |
| 6 | |
David Dorwin | 2c7fbbf8 | 2021-11-06 21:08:58 | [diff] [blame] | 7 | #include "base/base_paths.h" |
John Abd-El-Malek | 2276305 | 2021-06-15 03:35:53 | [diff] [blame] | 8 | #include "base/environment.h" |
| 9 | #include "base/files/file_util.h" |
| 10 | #include "base/path_service.h" |
| 11 | #include "base/threading/thread_restrictions.h" |
| 12 | #include "build/build_config.h" |
| 13 | |
Xiaohan Wang | bd08442 | 2022-01-15 18:47:51 | [diff] [blame] | 14 | #if BUILDFLAG(IS_FUCHSIA) |
David Dorwin | 2c7fbbf8 | 2021-11-06 21:08:58 | [diff] [blame] | 15 | #include "base/fuchsia/file_utils.h" |
Xiaohan Wang | bd08442 | 2022-01-15 18:47:51 | [diff] [blame] | 16 | #elif BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) |
John Abd-El-Malek | 2276305 | 2021-06-15 03:35:53 | [diff] [blame] | 17 | #include "base/nix/xdg_util.h" |
John Abd-El-Malek | 2276305 | 2021-06-15 03:35:53 | [diff] [blame] | 18 | #endif |
| 19 | |
| 20 | namespace content { |
| 21 | |
| 22 | namespace { |
| 23 | |
| 24 | bool GetDefaultUserDataDirectory(base::FilePath* result) { |
Xiaohan Wang | bd08442 | 2022-01-15 18:47:51 | [diff] [blame] | 25 | #if BUILDFLAG(IS_WIN) |
John Abd-El-Malek | 2276305 | 2021-06-15 03:35:53 | [diff] [blame] | 26 | CHECK(base::PathService::Get(base::DIR_LOCAL_APP_DATA, result)); |
| 27 | *result = result->Append(std::wstring(L"content_shell")); |
Xiaohan Wang | bd08442 | 2022-01-15 18:47:51 | [diff] [blame] | 28 | #elif BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) |
John Abd-El-Malek | 2276305 | 2021-06-15 03:35:53 | [diff] [blame] | 29 | std::unique_ptr<base::Environment> env(base::Environment::Create()); |
| 30 | base::FilePath config_dir(base::nix::GetXDGDirectory( |
| 31 | env.get(), base::nix::kXdgConfigHomeEnvVar, base::nix::kDotConfigDir)); |
| 32 | *result = config_dir.Append("content_shell"); |
Dave Tapuska | 1f7929c | 2023-02-03 18:11:56 | [diff] [blame] | 33 | #elif BUILDFLAG(IS_APPLE) |
John Abd-El-Malek | 2276305 | 2021-06-15 03:35:53 | [diff] [blame] | 34 | CHECK(base::PathService::Get(base::DIR_APP_DATA, result)); |
| 35 | *result = result->Append("Chromium Content Shell"); |
Xiaohan Wang | bd08442 | 2022-01-15 18:47:51 | [diff] [blame] | 36 | #elif BUILDFLAG(IS_ANDROID) |
John Abd-El-Malek | 2276305 | 2021-06-15 03:35:53 | [diff] [blame] | 37 | CHECK(base::PathService::Get(base::DIR_ANDROID_APP_DATA, result)); |
| 38 | *result = result->Append(FILE_PATH_LITERAL("content_shell")); |
Xiaohan Wang | bd08442 | 2022-01-15 18:47:51 | [diff] [blame] | 39 | #elif BUILDFLAG(IS_FUCHSIA) |
David Dorwin | 2c7fbbf8 | 2021-11-06 21:08:58 | [diff] [blame] | 40 | *result = base::FilePath(base::kPersistedDataDirectoryPath) |
| 41 | .Append(FILE_PATH_LITERAL("content_shell")); |
John Abd-El-Malek | 2276305 | 2021-06-15 03:35:53 | [diff] [blame] | 42 | #else |
| 43 | NOTIMPLEMENTED(); |
| 44 | return false; |
| 45 | #endif |
| 46 | return true; |
| 47 | } |
| 48 | |
| 49 | } // namespace |
| 50 | |
| 51 | class ShellPathProvider { |
| 52 | public: |
| 53 | static void CreateDir(const base::FilePath& path) { |
| 54 | base::ScopedAllowBlocking allow_io; |
| 55 | if (!base::PathExists(path)) |
| 56 | base::CreateDirectory(path); |
| 57 | } |
| 58 | }; |
| 59 | |
| 60 | bool ShellPathProvider(int key, base::FilePath* result) { |
| 61 | base::FilePath cur; |
| 62 | |
| 63 | switch (key) { |
| 64 | case SHELL_DIR_USER_DATA: { |
| 65 | bool rv = GetDefaultUserDataDirectory(result); |
| 66 | if (rv) |
| 67 | ShellPathProvider::CreateDir(*result); |
| 68 | return rv; |
| 69 | } |
| 70 | default: |
| 71 | return false; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | void RegisterShellPathProvider() { |
| 76 | base::PathService::RegisterProvider(ShellPathProvider, SHELL_PATH_START, |
| 77 | SHELL_PATH_END); |
| 78 | } |
| 79 | |
| 80 | } // namespace content |