client_filterable_state.cc 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // Copyright 2017 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/variations/client_filterable_state.h"
  5. #include "base/strings/string_util.h"
  6. #include "base/strings/stringprintf.h"
  7. #include "base/system/sys_info.h"
  8. #include "build/build_config.h"
  9. #include "build/chromeos_buildflags.h"
  10. namespace variations {
  11. // static
  12. Study::Platform ClientFilterableState::GetCurrentPlatform() {
  13. #if BUILDFLAG(IS_WIN)
  14. return Study::PLATFORM_WINDOWS;
  15. #elif BUILDFLAG(IS_IOS)
  16. return Study::PLATFORM_IOS;
  17. #elif BUILDFLAG(IS_MAC)
  18. return Study::PLATFORM_MAC;
  19. #elif BUILDFLAG(IS_CHROMEOS_ASH)
  20. return Study::PLATFORM_CHROMEOS;
  21. #elif BUILDFLAG(IS_CHROMEOS_LACROS)
  22. return Study::PLATFORM_CHROMEOS_LACROS;
  23. #elif BUILDFLAG(IS_ANDROID)
  24. return Study::PLATFORM_ANDROID;
  25. #elif BUILDFLAG(IS_FUCHSIA)
  26. return Study::PLATFORM_FUCHSIA;
  27. #elif BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_BSD) || BUILDFLAG(IS_SOLARIS)
  28. // Default BSD and SOLARIS to Linux to not break those builds, although these
  29. // platforms are not officially supported by Chrome.
  30. return Study::PLATFORM_LINUX;
  31. #else
  32. #error Unknown platform
  33. #endif
  34. }
  35. // TODO(b/957197): Improve how we handle OS versions.
  36. // Add os_version.h and os_version_<platform>.cc that handle retrieving and
  37. // parsing OS versions. Then get rid of all the platform-dependent code here.
  38. //
  39. // static
  40. base::Version ClientFilterableState::GetOSVersion() {
  41. base::Version ret;
  42. #if BUILDFLAG(IS_WIN)
  43. std::string win_version = base::SysInfo::OperatingSystemVersion();
  44. base::ReplaceSubstringsAfterOffset(&win_version, 0, " SP", ".");
  45. ret = base::Version(win_version);
  46. DCHECK(ret.IsValid()) << win_version;
  47. #else
  48. // Every other OS is supported by OperatingSystemVersionNumbers
  49. int major, minor, build;
  50. base::SysInfo::OperatingSystemVersionNumbers(&major, &minor, &build);
  51. ret = base::Version(base::StringPrintf("%d.%d.%d", major, minor, build));
  52. DCHECK(ret.IsValid());
  53. #endif
  54. return ret;
  55. }
  56. ClientFilterableState::ClientFilterableState(
  57. IsEnterpriseFunction is_enterprise_function)
  58. : is_enterprise_function_(std::move(is_enterprise_function)) {
  59. // The callback is only used when processing a study that uses the
  60. // is_enterprise filter. If you're building a client that isn't expecting that
  61. // filter, you should use a callback that always returns false.
  62. DCHECK(is_enterprise_function_);
  63. }
  64. ClientFilterableState::~ClientFilterableState() = default;
  65. bool ClientFilterableState::IsEnterprise() const {
  66. if (!is_enterprise_.has_value())
  67. is_enterprise_ = std::move(is_enterprise_function_).Run();
  68. return is_enterprise_.value();
  69. }
  70. } // namespace variations