channel_info_posix.cc 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // Copyright (c) 2011 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 "chrome/common/channel_info.h"
  5. #include <stdlib.h>
  6. #include <string>
  7. #include "base/environment.h"
  8. #include "base/notreached.h"
  9. #include "base/strings/string_piece.h"
  10. #include "base/strings/string_util.h"
  11. #include "build/branding_buildflags.h"
  12. #include "build/build_config.h"
  13. #include "build/chromeos_buildflags.h"
  14. #include "components/version_info/version_info.h"
  15. namespace chrome {
  16. namespace {
  17. struct ChannelState {
  18. version_info::Channel channel;
  19. bool is_extended_stable;
  20. };
  21. // Returns the channel state for the browser based on branding and the
  22. // CHROME_VERSION_EXTRA environment variable. In unbranded (Chromium) builds,
  23. // this function unconditionally returns `channel` = UNKNOWN and
  24. // `is_extended_stable` = false. In branded (Google Chrome) builds, this
  25. // function returns `channel` = UNKNOWN and `is_extended_stable` = false for any
  26. // unexpected $CHROME_VERSION_EXTRA value.
  27. ChannelState GetChannelImpl() {
  28. #if BUILDFLAG(GOOGLE_CHROME_BRANDING)
  29. const char* const env = getenv("CHROME_VERSION_EXTRA");
  30. const base::StringPiece env_str =
  31. env ? base::StringPiece(env) : base::StringPiece();
  32. // Ordered by decreasing expected population size.
  33. if (env_str == "stable")
  34. return {version_info::Channel::STABLE, /*is_extended_stable=*/false};
  35. if (env_str == "extended")
  36. return {version_info::Channel::STABLE, /*is_extended_stable=*/true};
  37. if (env_str == "beta")
  38. return {version_info::Channel::BETA, /*is_extended_stable=*/false};
  39. if (env_str == "unstable") // linux version of "dev"
  40. return {version_info::Channel::DEV, /*is_extended_stable=*/false};
  41. #endif // BUILDFLAG(GOOGLE_CHROME_BRANDING)
  42. return {version_info::Channel::UNKNOWN, /*is_extended_stable=*/false};
  43. }
  44. } // namespace
  45. std::string GetChannelName(WithExtendedStable with_extended_stable) {
  46. #if BUILDFLAG(GOOGLE_CHROME_BRANDING)
  47. const auto channel_state = GetChannelImpl();
  48. switch (channel_state.channel) {
  49. case version_info::Channel::UNKNOWN:
  50. return "unknown";
  51. case version_info::Channel::CANARY:
  52. NOTREACHED();
  53. return "unknown";
  54. case version_info::Channel::DEV:
  55. return "dev";
  56. case version_info::Channel::BETA:
  57. return "beta";
  58. case version_info::Channel::STABLE:
  59. if (with_extended_stable && channel_state.is_extended_stable)
  60. return "extended";
  61. return std::string();
  62. }
  63. #else // BUILDFLAG(GOOGLE_CHROME_BRANDING)
  64. const char* const env = getenv("CHROME_VERSION_EXTRA");
  65. return env ? std::string(base::StringPiece(env)) : std::string();
  66. #endif // BUILDFLAG(GOOGLE_CHROME_BRANDING)
  67. }
  68. std::string GetChannelSuffixForDataDir() {
  69. switch (GetChannel()) {
  70. case version_info::Channel::BETA:
  71. return "-beta";
  72. case version_info::Channel::DEV:
  73. return "-unstable";
  74. default:
  75. // Stable, extended stable, and unknown (e.g. in unbranded builds) don't
  76. // get a suffix.
  77. return std::string();
  78. }
  79. }
  80. // TODO(crbug.com/1052397): Revisit the macro expression once build flag switch
  81. // of lacros-chrome is complete.
  82. #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS_LACROS)
  83. std::string GetDesktopName(base::Environment* env) {
  84. #if BUILDFLAG(GOOGLE_CHROME_BRANDING)
  85. // Google Chrome packaged as a snap is a special case: the application name
  86. // is always "google-chrome", regardless of the channel (channels are built
  87. // in to snapd, switching between them or doing parallel installs does not
  88. // require distinct application names).
  89. std::string snap_name;
  90. if (env->GetVar("SNAP_NAME", &snap_name) && snap_name == "google-chrome")
  91. return "google-chrome.desktop";
  92. version_info::Channel product_channel(GetChannel());
  93. switch (product_channel) {
  94. case version_info::Channel::DEV:
  95. return "google-chrome-unstable.desktop";
  96. case version_info::Channel::BETA:
  97. return "google-chrome-beta.desktop";
  98. default:
  99. // Extended stable is not differentiated from regular stable.
  100. return "google-chrome.desktop";
  101. }
  102. #else // BUILDFLAG(CHROMIUM_BRANDING)
  103. // Allow $CHROME_DESKTOP to override the built-in value, so that development
  104. // versions can set themselves as the default without interfering with
  105. // non-official, packaged versions using the built-in value.
  106. std::string name;
  107. if (env->GetVar("CHROME_DESKTOP", &name) && !name.empty())
  108. return name;
  109. return "chromium-browser.desktop";
  110. #endif
  111. }
  112. #endif // BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS_LACROS)
  113. version_info::Channel GetChannel() {
  114. return GetChannelImpl().channel;
  115. }
  116. bool IsExtendedStableChannel() {
  117. return GetChannelImpl().is_extended_stable;
  118. }
  119. } // namespace chrome