command_line_pref_store.cc 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // Copyright 2016 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/prefs/command_line_pref_store.h"
  5. #include <memory>
  6. #include <string>
  7. #include "base/files/file_path.h"
  8. #include "base/logging.h"
  9. #include "base/strings/string_number_conversions.h"
  10. #include "base/strings/string_util.h"
  11. #include "base/values.h"
  12. CommandLinePrefStore::CommandLinePrefStore(
  13. const base::CommandLine* command_line)
  14. : command_line_(command_line) {}
  15. CommandLinePrefStore::~CommandLinePrefStore() {}
  16. void CommandLinePrefStore::ApplyStringSwitches(
  17. const CommandLinePrefStore::SwitchToPreferenceMapEntry string_switch[],
  18. size_t size) {
  19. for (size_t i = 0; i < size; ++i) {
  20. if (command_line_->HasSwitch(string_switch[i].switch_name)) {
  21. SetValue(string_switch[i].preference_path,
  22. std::make_unique<base::Value>(command_line_->GetSwitchValueASCII(
  23. string_switch[i].switch_name)),
  24. WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
  25. }
  26. }
  27. }
  28. void CommandLinePrefStore::ApplyPathSwitches(
  29. const CommandLinePrefStore::SwitchToPreferenceMapEntry path_switch[],
  30. size_t size) {
  31. for (size_t i = 0; i < size; ++i) {
  32. if (command_line_->HasSwitch(path_switch[i].switch_name)) {
  33. SetValue(path_switch[i].preference_path,
  34. std::make_unique<base::Value>(
  35. command_line_->GetSwitchValuePath(path_switch[i].switch_name)
  36. .AsUTF8Unsafe()),
  37. WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
  38. }
  39. }
  40. }
  41. void CommandLinePrefStore::ApplyIntegerSwitches(
  42. const CommandLinePrefStore::SwitchToPreferenceMapEntry integer_switch[],
  43. size_t size) {
  44. for (size_t i = 0; i < size; ++i) {
  45. if (command_line_->HasSwitch(integer_switch[i].switch_name)) {
  46. std::string str_value = command_line_->GetSwitchValueASCII(
  47. integer_switch[i].switch_name);
  48. int int_value = 0;
  49. if (!base::StringToInt(str_value, &int_value)) {
  50. LOG(ERROR) << "The value " << str_value << " of "
  51. << integer_switch[i].switch_name
  52. << " can not be converted to integer, ignoring!";
  53. continue;
  54. }
  55. SetValue(integer_switch[i].preference_path,
  56. std::make_unique<base::Value>(int_value),
  57. WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
  58. }
  59. }
  60. }
  61. void CommandLinePrefStore::ApplyBooleanSwitches(
  62. const CommandLinePrefStore::BooleanSwitchToPreferenceMapEntry
  63. boolean_switch_map[], size_t size) {
  64. for (size_t i = 0; i < size; ++i) {
  65. if (command_line_->HasSwitch(boolean_switch_map[i].switch_name)) {
  66. SetValue(boolean_switch_map[i].preference_path,
  67. std::make_unique<base::Value>(boolean_switch_map[i].set_value),
  68. WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
  69. }
  70. }
  71. }