weblayer_paths.cc 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // Copyright 2019 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. #include "weblayer/common/weblayer_paths.h"
  5. #include "base/environment.h"
  6. #include "base/files/file_util.h"
  7. #include "base/path_service.h"
  8. #include "base/threading/thread_restrictions.h"
  9. #include "build/build_config.h"
  10. #include "build/chromeos_buildflags.h"
  11. #if BUILDFLAG(IS_ANDROID)
  12. #include "base/android/path_utils.h"
  13. #include "base/base_paths_android.h"
  14. #endif
  15. #if BUILDFLAG(IS_WIN)
  16. #include "base/base_paths_win.h"
  17. #elif BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS_LACROS)
  18. #include "base/nix/xdg_util.h"
  19. #endif
  20. namespace weblayer {
  21. namespace {
  22. bool GetDefaultUserDataDirectory(base::FilePath* result) {
  23. #if BUILDFLAG(IS_ANDROID)
  24. // No need to append "weblayer" here. It's done in java with
  25. // PathUtils.setPrivateDataDirectorySuffix.
  26. return base::PathService::Get(base::DIR_ANDROID_APP_DATA, result);
  27. #elif BUILDFLAG(IS_WIN)
  28. if (!base::PathService::Get(base::DIR_LOCAL_APP_DATA, result))
  29. return false;
  30. *result = result->AppendASCII("weblayer");
  31. return true;
  32. #elif BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS_LACROS)
  33. std::unique_ptr<base::Environment> env(base::Environment::Create());
  34. base::FilePath config_dir(base::nix::GetXDGDirectory(
  35. env.get(), base::nix::kXdgConfigHomeEnvVar, base::nix::kDotConfigDir));
  36. *result = config_dir.AppendASCII("weblayer");
  37. return true;
  38. #else
  39. return false;
  40. #endif
  41. }
  42. } // namespace
  43. class WebLayerPathProvider {
  44. public:
  45. static void CreateDir(const base::FilePath& path) {
  46. base::ScopedAllowBlocking allow_io;
  47. if (!base::PathExists(path))
  48. base::CreateDirectory(path);
  49. }
  50. };
  51. bool PathProvider(int key, base::FilePath* result) {
  52. base::FilePath cur;
  53. switch (key) {
  54. case DIR_USER_DATA: {
  55. bool rv = GetDefaultUserDataDirectory(result);
  56. if (rv)
  57. WebLayerPathProvider::CreateDir(*result);
  58. return rv;
  59. }
  60. #if BUILDFLAG(IS_ANDROID)
  61. case DIR_CRASH_DUMPS:
  62. if (!base::android::GetCacheDirectory(&cur))
  63. return false;
  64. cur = cur.Append(FILE_PATH_LITERAL("Crashpad"));
  65. WebLayerPathProvider::CreateDir(cur);
  66. *result = cur;
  67. return true;
  68. #endif
  69. default:
  70. return false;
  71. }
  72. }
  73. void RegisterPathProvider() {
  74. base::PathService::RegisterProvider(PathProvider, PATH_START, PATH_END);
  75. }
  76. } // namespace weblayer