channel_info_mac.mm 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. // Copyright (c) 2012 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. #import <Foundation/Foundation.h>
  6. #include <tuple>
  7. #include "base/check.h"
  8. #include "base/mac/bundle_locations.h"
  9. #include "base/no_destructor.h"
  10. #include "base/strings/sys_string_conversions.h"
  11. #include "build/branding_buildflags.h"
  12. #include "components/version_info/version_info.h"
  13. namespace chrome {
  14. namespace {
  15. struct ChannelState {
  16. std::string name;
  17. bool is_extended_stable;
  18. };
  19. #if BUILDFLAG(GOOGLE_CHROME_BRANDING)
  20. // Returns a ChannelState given a KSChannelID value.
  21. ChannelState ParseChannelId(NSString* channel) {
  22. // KSChannelID values:
  23. //
  24. // Intel Arm Universal
  25. // ┌───────────┬────────────────┬────────────────────┐
  26. // Stable │ (not set) │ arm64 │ universal │
  27. // Extended Stable │ extended │ arm64-extended │ universal-extended │
  28. // Beta │ beta │ arm64-beta │ universal-beta │
  29. // Dev │ dev │ arm64-dev │ universal-dev │
  30. // Canary │ canary │ arm64-canary │ universal-canary │
  31. // └───────────┴────────────────┴────────────────────┘
  32. if (!channel || [channel isEqual:@"arm64"] ||
  33. [channel isEqual:@"universal"]) {
  34. return ChannelState{"", false}; // "" means stable channel.
  35. }
  36. if ([channel hasPrefix:@"arm64-"])
  37. channel = [channel substringFromIndex:[@"arm64-" length]];
  38. else if ([channel hasPrefix:@"universal-"])
  39. channel = [channel substringFromIndex:[@"universal-" length]];
  40. if ([channel isEqual:@"extended"])
  41. return ChannelState{"", true}; // "" means stable channel.
  42. if ([channel isEqual:@"beta"] || [channel isEqual:@"dev"] ||
  43. [channel isEqual:@"canary"]) {
  44. return ChannelState{base::SysNSStringToUTF8(channel), false};
  45. }
  46. return ChannelState{"unknown", false};
  47. }
  48. // Returns the ChannelState for this browser based on how it is registered with
  49. // Keystone.
  50. ChannelState DetermineChannelState() {
  51. // Use the main Chrome application bundle and not the framework bundle.
  52. // Keystone keys don't live in the framework.
  53. NSBundle* bundle = base::mac::OuterBundle();
  54. if (![bundle objectForInfoDictionaryKey:@"KSProductID"]) {
  55. // This build is not Keystone-enabled; it can't have a channel.
  56. return ChannelState{"unknown", false};
  57. }
  58. return ParseChannelId([bundle objectForInfoDictionaryKey:@"KSChannelID"]);
  59. }
  60. #endif // BUILDFLAG(GOOGLE_CHROME_BRANDING)
  61. // For a branded build, returns its ChannelState: `name` of "" (for stable or
  62. // extended), "beta", "dev", "canary", or "unknown" (in the case where the
  63. // channel could not be determined or is otherwise inapplicable), and an
  64. // `is_extended_stable` with a value corresponding to whether it is an extended
  65. // stable build or not.
  66. //
  67. // For an unbranded build, always returns a ChannelState with `name` of "" and
  68. // `is_extended_stable` of false.
  69. ChannelState& GetChannelState() {
  70. static base::NoDestructor<ChannelState> channel([] {
  71. #if BUILDFLAG(GOOGLE_CHROME_BRANDING)
  72. return DetermineChannelState();
  73. #else
  74. return ChannelState{"", false};
  75. #endif
  76. }());
  77. return *channel;
  78. }
  79. bool SideBySideCapable() {
  80. #if BUILDFLAG(GOOGLE_CHROME_BRANDING)
  81. static const bool capable = [] {
  82. // Use the main Chrome application bundle and not the framework bundle.
  83. // Keystone keys don't live in the framework.
  84. NSBundle* bundle = base::mac::OuterBundle();
  85. if (![bundle objectForInfoDictionaryKey:@"KSProductID"]) {
  86. // This build is not Keystone-enabled, and without a channel assume it is
  87. // side-by-side capable.
  88. return true;
  89. }
  90. if (GetChannelState().name.empty()) {
  91. // GetChannelState() returns an empty name for the regular and extended
  92. // stable channels. These stable Chromes are what side-by-side capable
  93. // Chromes are running side-by-side *to* and by definition are
  94. // side-by-side capable.
  95. return true;
  96. }
  97. // If there is a CrProductDirName key, then the user data dir of this
  98. // beta/dev/canary Chrome is separate, and it can run side-by-side to the
  99. // stable Chrome.
  100. return [bundle objectForInfoDictionaryKey:@"CrProductDirName"] != nil;
  101. }();
  102. return capable;
  103. #else
  104. return true;
  105. #endif
  106. }
  107. } // namespace
  108. void CacheChannelInfo() {
  109. std::ignore = GetChannelState();
  110. std::ignore = SideBySideCapable();
  111. }
  112. std::string GetChannelName(WithExtendedStable with_extended_stable) {
  113. const auto& channel = GetChannelState();
  114. if (channel.is_extended_stable && with_extended_stable.value())
  115. return "extended";
  116. return channel.name;
  117. }
  118. version_info::Channel GetChannelByName(const std::string& channel) {
  119. #if BUILDFLAG(GOOGLE_CHROME_BRANDING)
  120. if (channel.empty() || channel == "extended")
  121. return version_info::Channel::STABLE;
  122. if (channel == "beta")
  123. return version_info::Channel::BETA;
  124. if (channel == "dev")
  125. return version_info::Channel::DEV;
  126. if (channel == "canary")
  127. return version_info::Channel::CANARY;
  128. #endif
  129. return version_info::Channel::UNKNOWN;
  130. }
  131. bool IsSideBySideCapable() {
  132. return SideBySideCapable();
  133. }
  134. version_info::Channel GetChannel() {
  135. return GetChannelByName(GetChannelState().name);
  136. }
  137. bool IsExtendedStableChannel() {
  138. return GetChannelState().is_extended_stable;
  139. }
  140. #if BUILDFLAG(GOOGLE_CHROME_BRANDING)
  141. namespace {
  142. // True following a call to SetChannelIdForTesting.
  143. bool channel_id_is_overidden_ = false;
  144. } // namespace
  145. void SetChannelIdForTesting(const std::string& channel_id) {
  146. DCHECK(!channel_id_is_overidden_);
  147. GetChannelState() = ParseChannelId(
  148. channel_id.empty() ? nullptr : base::SysUTF8ToNSString(channel_id));
  149. channel_id_is_overidden_ = true;
  150. }
  151. void ClearChannelIdForTesting() {
  152. DCHECK(channel_id_is_overidden_);
  153. channel_id_is_overidden_ = false;
  154. GetChannelState() = DetermineChannelState();
  155. }
  156. #endif // BUILDFLAG(GOOGLE_CHROME_BRANDING)
  157. } // namespace chrome