component_updater_paths.cc 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Copyright 2014 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 "components/component_updater/component_updater_paths.h"
  5. #include "base/files/file_path.h"
  6. #include "base/lazy_instance.h"
  7. #include "base/path_service.h"
  8. namespace component_updater {
  9. namespace {
  10. // This key gives the root directory of all the component installations.
  11. static int g_components_preinstalled_root_key = -1;
  12. static int g_components_preinstalled_root_key_alt = -1;
  13. static int g_components_user_root_key = -1;
  14. } // namespace
  15. bool PathProvider(int key, base::FilePath* result) {
  16. DCHECK_GT(g_components_user_root_key, 0);
  17. DCHECK_GT(g_components_preinstalled_root_key, 0);
  18. // Early exit here to prevent a potential infinite loop when we retrieve
  19. // the path for g_components_*_root_key.
  20. if (key < PATH_START || key > PATH_END)
  21. return false;
  22. switch (key) {
  23. case DIR_COMPONENT_PREINSTALLED:
  24. return base::PathService::Get(g_components_preinstalled_root_key, result);
  25. case DIR_COMPONENT_PREINSTALLED_ALT:
  26. return base::PathService::Get(g_components_preinstalled_root_key_alt,
  27. result);
  28. case DIR_COMPONENT_USER:
  29. return base::PathService::Get(g_components_user_root_key, result);
  30. }
  31. base::FilePath cur;
  32. if (!base::PathService::Get(g_components_user_root_key, &cur))
  33. return false;
  34. switch (key) {
  35. case DIR_COMPONENT_CLD2:
  36. cur = cur.Append(FILE_PATH_LITERAL("CLD"));
  37. break;
  38. case DIR_RECOVERY_BASE:
  39. cur = cur.Append(FILE_PATH_LITERAL("recovery"));
  40. break;
  41. case DIR_SWIFT_SHADER:
  42. cur = cur.Append(FILE_PATH_LITERAL("SwiftShader"));
  43. break;
  44. default:
  45. return false;
  46. }
  47. *result = cur;
  48. return true;
  49. }
  50. // This cannot be done as a static initializer sadly since Visual Studio will
  51. // eliminate this object file if there is no direct entry point into it.
  52. void RegisterPathProvider(int components_preinstalled_root_key,
  53. int components_preinstalled_root_key_alt,
  54. int components_user_root_key) {
  55. DCHECK_EQ(g_components_preinstalled_root_key, -1);
  56. DCHECK_EQ(g_components_preinstalled_root_key_alt, -1);
  57. DCHECK_EQ(g_components_user_root_key, -1);
  58. DCHECK_GT(components_preinstalled_root_key, 0);
  59. DCHECK_GT(components_preinstalled_root_key_alt, 0);
  60. DCHECK_GT(components_user_root_key, 0);
  61. DCHECK(components_preinstalled_root_key < PATH_START ||
  62. components_preinstalled_root_key > PATH_END);
  63. DCHECK(components_preinstalled_root_key_alt < PATH_START ||
  64. components_preinstalled_root_key_alt > PATH_END);
  65. DCHECK(components_user_root_key < PATH_START ||
  66. components_user_root_key > PATH_END);
  67. g_components_preinstalled_root_key = components_preinstalled_root_key;
  68. g_components_preinstalled_root_key_alt = components_preinstalled_root_key_alt;
  69. g_components_user_root_key = components_user_root_key;
  70. base::PathService::RegisterProvider(PathProvider, PATH_START, PATH_END);
  71. }
  72. } // namespace component_updater