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