blob: 284b9102e296f219f4834e4bb5a6447a08cd4420 [file] [log] [blame]
Avi Drissman4e1b7bc32022-09-15 14:03:501// Copyright 2021 The Chromium Authors
John Abd-El-Malek22763052021-06-15 03:35:532// 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 Dorwin2c7fbbf82021-11-06 21:08:587#include "base/base_paths.h"
John Abd-El-Malek22763052021-06-15 03:35:538#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 Wangbd084422022-01-15 18:47:5114#if BUILDFLAG(IS_FUCHSIA)
David Dorwin2c7fbbf82021-11-06 21:08:5815#include "base/fuchsia/file_utils.h"
Xiaohan Wangbd084422022-01-15 18:47:5116#elif BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
John Abd-El-Malek22763052021-06-15 03:35:5317#include "base/nix/xdg_util.h"
John Abd-El-Malek22763052021-06-15 03:35:5318#endif
19
20namespace content {
21
22namespace {
23
24bool GetDefaultUserDataDirectory(base::FilePath* result) {
Xiaohan Wangbd084422022-01-15 18:47:5125#if BUILDFLAG(IS_WIN)
John Abd-El-Malek22763052021-06-15 03:35:5326 CHECK(base::PathService::Get(base::DIR_LOCAL_APP_DATA, result));
27 *result = result->Append(std::wstring(L"content_shell"));
Xiaohan Wangbd084422022-01-15 18:47:5128#elif BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
John Abd-El-Malek22763052021-06-15 03:35:5329 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 Tapuska1f7929c2023-02-03 18:11:5633#elif BUILDFLAG(IS_APPLE)
John Abd-El-Malek22763052021-06-15 03:35:5334 CHECK(base::PathService::Get(base::DIR_APP_DATA, result));
35 *result = result->Append("Chromium Content Shell");
Xiaohan Wangbd084422022-01-15 18:47:5136#elif BUILDFLAG(IS_ANDROID)
John Abd-El-Malek22763052021-06-15 03:35:5337 CHECK(base::PathService::Get(base::DIR_ANDROID_APP_DATA, result));
38 *result = result->Append(FILE_PATH_LITERAL("content_shell"));
Xiaohan Wangbd084422022-01-15 18:47:5139#elif BUILDFLAG(IS_FUCHSIA)
David Dorwin2c7fbbf82021-11-06 21:08:5840 *result = base::FilePath(base::kPersistedDataDirectoryPath)
41 .Append(FILE_PATH_LITERAL("content_shell"));
John Abd-El-Malek22763052021-06-15 03:35:5342#else
43 NOTIMPLEMENTED();
44 return false;
45#endif
46 return true;
47}
48
49} // namespace
50
51class 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
60bool 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
75void RegisterShellPathProvider() {
76 base::PathService::RegisterProvider(ShellPathProvider, SHELL_PATH_START,
77 SHELL_PATH_END);
78}
79
80} // namespace content