component_updater_command_line_config_policy.cc 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // Copyright 2018 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_command_line_config_policy.h"
  5. #include <string>
  6. #include <vector>
  7. #include "base/command_line.h"
  8. #include "base/containers/contains.h"
  9. #include "base/strings/string_number_conversions.h"
  10. #include "base/strings/string_split.h"
  11. #include "base/strings/sys_string_conversions.h"
  12. #include "build/build_config.h"
  13. #include "components/component_updater/component_updater_switches.h"
  14. namespace component_updater {
  15. namespace {
  16. // Debug values you can pass to --component-updater=value1,value2. Do not
  17. // use these values in production code.
  18. // Speed up the initial component checking.
  19. const char kSwitchFastUpdate[] = "fast-update";
  20. // Disables pings. Pings are the requests sent to the update server that report
  21. // the success or the failure of component install or update attempts.
  22. const char kSwitchDisablePings[] = "disable-pings";
  23. // Sets the URL for updates.
  24. const char kSwitchUrlSource[] = "url-source";
  25. // Disables differential updates.
  26. const char kSwitchDisableDeltaUpdates[] = "disable-delta-updates";
  27. // Configures the initial delay before the first component update check. The
  28. // value is in seconds.
  29. const char kInitialDelay[] = "initial-delay";
  30. #if BUILDFLAG(IS_WIN)
  31. // Disables background downloads.
  32. const char kSwitchDisableBackgroundDownloads[] = "disable-background-downloads";
  33. #endif // BUILDFLAG(IS_WIN)
  34. // If there is an element of |vec| of the form |test|=.*, returns the right-
  35. // hand side of that assignment. Otherwise, returns an empty string.
  36. // The right-hand side may contain additional '=' characters, allowing for
  37. // further nesting of switch arguments.
  38. std::string GetSwitchArgument(const std::vector<std::string>& vec,
  39. const char* test) {
  40. if (vec.empty())
  41. return std::string();
  42. for (auto it = vec.begin(); it != vec.end(); ++it) {
  43. const std::size_t found = it->find("=");
  44. if (found != std::string::npos) {
  45. if (it->substr(0, found) == test) {
  46. return it->substr(found + 1);
  47. }
  48. }
  49. }
  50. return std::string();
  51. }
  52. } // namespace
  53. // Add "testrequest=1" attribute to the update check request.
  54. const char kSwitchTestRequestParam[] = "test-request";
  55. ComponentUpdaterCommandLineConfigPolicy::
  56. ComponentUpdaterCommandLineConfigPolicy(const base::CommandLine* cmdline) {
  57. DCHECK(cmdline);
  58. // Parse comma-delimited debug flags.
  59. std::vector<std::string> switch_values = base::SplitString(
  60. cmdline->GetSwitchValueASCII(switches::kComponentUpdater), ",",
  61. base::KEEP_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
  62. #if BUILDFLAG(IS_WIN)
  63. background_downloads_enabled_ =
  64. !base::Contains(switch_values, kSwitchDisableBackgroundDownloads);
  65. #else
  66. background_downloads_enabled_ = false;
  67. #endif
  68. deltas_enabled_ = !base::Contains(switch_values, kSwitchDisableDeltaUpdates);
  69. fast_update_ = base::Contains(switch_values, kSwitchFastUpdate);
  70. pings_enabled_ = !base::Contains(switch_values, kSwitchDisablePings);
  71. test_request_ = base::Contains(switch_values, kSwitchTestRequestParam);
  72. const std::string switch_url_source =
  73. GetSwitchArgument(switch_values, kSwitchUrlSource);
  74. if (!switch_url_source.empty()) {
  75. url_source_override_ = GURL(switch_url_source);
  76. DCHECK(url_source_override_.is_valid());
  77. }
  78. const std::string initial_delay =
  79. GetSwitchArgument(switch_values, kInitialDelay);
  80. double initial_delay_seconds = 0;
  81. if (base::StringToDouble(initial_delay, &initial_delay_seconds))
  82. initial_delay_ = initial_delay_seconds;
  83. }
  84. bool ComponentUpdaterCommandLineConfigPolicy::BackgroundDownloadsEnabled()
  85. const {
  86. return background_downloads_enabled_;
  87. }
  88. bool ComponentUpdaterCommandLineConfigPolicy::DeltaUpdatesEnabled() const {
  89. return deltas_enabled_;
  90. }
  91. bool ComponentUpdaterCommandLineConfigPolicy::FastUpdate() const {
  92. return fast_update_;
  93. }
  94. bool ComponentUpdaterCommandLineConfigPolicy::PingsEnabled() const {
  95. return pings_enabled_;
  96. }
  97. bool ComponentUpdaterCommandLineConfigPolicy::TestRequest() const {
  98. return test_request_;
  99. }
  100. GURL ComponentUpdaterCommandLineConfigPolicy::UrlSourceOverride() const {
  101. return url_source_override_;
  102. }
  103. double ComponentUpdaterCommandLineConfigPolicy::InitialDelay() const {
  104. return initial_delay_;
  105. }
  106. } // namespace component_updater